10. Compile
Compiles a string into bytecode.
<<funcdefs>>=
int bitlang_compile(bitlang_state *st, const char *code);
<<funcs>>=
static int match(const char *str1, int sz1,
const char *str2, int sz2)
{
int n;
if (sz1 != sz2) return 0;
for (n = 0; n < sz1; n++)
if (str1[n] != str2[n]) return 0;
return 1;
}
static int isnum(const char *str) {
char c;
c = str[0] - '0';
if (c >= 1 && c <= 9) return 1;
return 0;
}
static int mknum(const char *str, int len) {
int x;
int i;
x = 0;
for (i = 0; i < len; i++) {
int c;
c = str[i] - '0';
if (c >= 0 && c <= 9) {
x *= 10;
x += c;
}
}
return x;
}
static int tokenize(bitlang_state *st,
const char *code,
int b, int e)
{
int len;
const char *str;
len = (e - b) + 1;
str = &code[b];
if (isnum(str)) {
return bitlang_num(st, mknum(str, len));
}
<<search>>
return 1;
}
int bitlang_compile(bitlang_state *st, const char *code)
{
int b, e;
int sz;
int n;
int mode;
sz = strlen(code);
b = 0;
e = 0;
mode = 0;
n = 0;
while (n < sz) {
char c;
c = code[n];
if (mode == 0) {
if (c == ' ') {
n++;
} else {
b = n;
mode = 1;
n++;
}
} else if (mode == 1) {
if (c == ' ') {
e = n - 1;
mode = 0;
tokenize(st, code, b, e);
}
n++;
} else {
n++;
}
}
if (b < sz) {
e = sz - 1;
tokenize(st, code, b, e);
}
return 0;
}
prev | home | next