diff options
author | ubq323 <ubq323@ubq323.website> | 2024-06-22 18:22:24 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-06-22 18:22:24 +0100 |
commit | 8c3037662ba572a6935170dbd4cb8cc8a3636417 (patch) | |
tree | 1d585a5f0eab8eed96d0741421656f11c99c7c33 /com.c | |
parent | 1d496f5f7f01b20fb34e63d1c85a33d8decd1894 (diff) |
compilation of functions, and some parts of interpreting them
Diffstat (limited to 'com.c')
-rw-r--r-- | com.c | 43 |
1 files changed, 26 insertions, 17 deletions
@@ -36,7 +36,6 @@ Compiler compiler_new(Compiler *outer, Chunk *ch) { size_t chunk_wbc(Compiler *C, uint8_t byte) { - printf("\t%p %hd\n",C, byte); Chunk *ch = C->ch; if (ch->bc.len == ch->bc.cap) { size_t newsz = (ch->bc.cap == 0 ? 8 : ch->bc.cap * 2); @@ -168,7 +167,7 @@ void fn_form(Compiler *C, AstVec l, Op op) { AstVec arglist = l.vals[1].as.list; ObjFunc *func = objfunc_new(C->S); - Compiler subcompiler = compiler_new(C, &func->chunk); + Compiler subcompiler = compiler_new(C, &func->ch); for (int i = 2; i < l.len - 1; i++) { compile_node(&subcompiler, l.vals[i]); @@ -219,16 +218,15 @@ static void compile_node(Compiler *C, AstNode a) { switch (a.ty) { case AST_IDENT:; char *ident = a.as.str; - BuiltinIdent *found_builtin = NULL; + bool found_builtin = false; for (BuiltinIdent *b = builtin_idents; b->name != NULL; b++) { if (0 == strcmp(b->name, ident)) { - found_builtin = b; + chunk_wbc(C, b->op); + found_builtin = true; break; } } - if (found_builtin != NULL) { - chunk_wbc(C, found_builtin->op); - } else { + if (!found_builtin) { // read global variable ObjString *o = objstring_copy_cstr(C->S, a.as.str); chunk_wbc(C, OP_GETGLOBAL); @@ -250,16 +248,17 @@ static void compile_node(Compiler *C, AstNode a) { #define CK(cond, msg) if (!(cond)) { fputs(msg "\n", stderr); exit(1); } CK(l.len > 0, "can't handle empty list"); - CK(l.vals[0].ty == AST_IDENT, "can only call ops"); #undef CK - char *head = l.vals[0].as.str; - BuiltinForm *form = NULL; - for (BuiltinForm *b = builtin_forms; b->name != NULL; b++) { - if (0 == strcmp(b->name, head)) { - form = b; - break; + + if (l.vals[0].ty == AST_IDENT) { + char *head = l.vals[0].as.str; + for (BuiltinForm *b = builtin_forms; b->name != NULL; b++) { + if (0 == strcmp(b->name, head)) { + form = b; + break; + } } } @@ -276,8 +275,17 @@ static void compile_node(Compiler *C, AstNode a) { form->action(C, l, form->op); } else { - fprintf(stderr, "unknown form %s\n", head); - exit(1); + // function call + // (f a b c ) + if (l.len > 255) { + fprintf(stderr, "can't have more than 255 args in a function call\n"); + exit(1); + } + for (int i = 0; i < l.len; i++) { + compile_node(C, l.vals[i]); + } + chunk_wbc(C, OP_CALL); + chunk_wbc(C, l.len); } break; @@ -291,6 +299,7 @@ int main(int argc, char **argv) { Chunk ch = chunk_new(S); S->do_disasm = (argc > 1 && 0 == strcmp(argv[1], "-l")); + S->do_trace = false; char n1[] = "foo"; char n2[] = "bar"; @@ -314,7 +323,7 @@ int main(int argc, char **argv) { chunk_wbc(C, OP_PUTS); - chunk_wbc(C, OP_RET); + chunk_wbc(C, OP_HALT); Thread th = thread_new(S); th.ch = &ch; |