#include #include #include #include "com.h" #include "mem.h" #include "chunk.h" #include "ast.h" #include "read.h" #define BYTECODE(C) (C->ch->bc) static size_t placeholder(Compiler *C) { size_t old_ix = BYTECODE(C).len; chunk_wbc(C, 0x00); chunk_wbc(C, 0x00); return old_ix; } static void patch(Compiler *C, size_t addr, uint16_t val) { BYTECODE(C).d[addr] = val & 0xff; BYTECODE(C).d[addr+1] = (val & 0xff00) >> 8; } Chunk chunk_new(State *S) { return (Chunk){ 0 }; } Compiler compiler_new(Compiler *outer, Chunk *ch) { return (Compiler){ .S = outer->S, .ch = ch, }; } size_t chunk_wbc(Compiler *C, uint8_t byte) { Chunk *ch = C->ch; if (ch->bc.len == ch->bc.cap) { size_t newsz = (ch->bc.cap == 0 ? 8 : ch->bc.cap * 2); ch->bc.d = RENEW_ARR(C->S, ch->bc.d, uint8_t, ch->bc.cap, newsz); ch->bc.cap = newsz; } size_t ix = ch->bc.len; ch->bc.d[ix] = byte; ch->bc.len ++; return ix; } size_t chunk_wconst(Compiler *C, Val v) { Chunk *ch = C->ch; for (int i = 0; i < ch->consts.len; i ++) if (val_equal(v, ch->consts.d[i])) return i; if (ch->consts.len == ch->consts.cap) { size_t newsz = (ch->consts.cap == 0 ? 8 : ch->consts.cap *2); ch->consts.d = RENEW_ARR(C->S, ch->consts.d, Val, ch->consts.cap, newsz); ch->consts.cap = newsz; } size_t ix = ch->consts.len; ch->consts.d[ix] = v; ch->consts.len ++; return ix; } static void compile_node(Compiler *C, AstNode a); typedef void (*form_compiler)(Compiler *C, AstVec l, Op op); typedef struct { char *name; int min_params; bool ellipsis; form_compiler action; Op op; } BuiltinForm; void single_form(Compiler *C, AstVec l, Op op) { compile_node(C, l.vals[1]); chunk_wbc(C, op); } void set_form(Compiler *C, AstVec l, Op _) { AstNode ident = l.vals[1]; if (ident.ty != AST_IDENT) { fprintf(stderr, "set's first argument must be identifier"); exit(1); } ObjString *o = objstring_copy_cstr(C->S, ident.as.str); compile_node(C, l.vals[2]); chunk_wbc(C, OP_SETGLOBAL); chunk_wbc(C, chunk_wconst(C, VAL_OBJ(o))); } void do_form(Compiler *C, AstVec l, Op _) { for (int i = 1; i < l.len - 1; i++) { compile_node(C, l.vals[i]); chunk_wbc(C, OP_DROP); } compile_node(C, l.vals[l.len - 1]); } void if_form(Compiler *C, AstVec l, Op _) { // (if cond if-true if-false) // cond // 0branch ->A // if-true // skip ->B // A: if-false // B: compile_node(C, l.vals[1]); chunk_wbc(C, OP_0BRANCH); size_t ph_a = placeholder(C); compile_node(C, l.vals[2]); chunk_wbc(C, OP_SKIP); size_t ph_b = placeholder(C); size_t dest_a = BYTECODE(C).len; compile_node(C, l.vals[3]); size_t dest_b = BYTECODE(C).len; patch(C, ph_a, dest_a - ph_a - 2); patch(C, ph_b, dest_b - ph_b - 2); } void while_form(Compiler *C, AstVec l, Op _) { // (while cond body ...) // A: // cond // 0branch ->B // body .... // redo ->A // B: // nil (while loop always returns nil) size_t dest_a = BYTECODE(C).len; compile_node(C, l.vals[1]); chunk_wbc(C, OP_0BRANCH); size_t ph_b = placeholder(C); for (int i = 2; i < l.len; i++) { compile_node(C, l.vals[i]); chunk_wbc(C, OP_DROP); } chunk_wbc(C, OP_REDO); size_t ph_a = placeholder(C); size_t dest_b = BYTECODE(C).len; chunk_wbc(C, OP_NIL); patch(C, ph_a, ph_a - dest_a + 2); patch(C, ph_b, dest_b - ph_b - 2); } void arith_form(Compiler *C, AstVec l, Op op) { compile_node(C, l.vals[1]); compile_node(C, l.vals[2]); chunk_wbc(C, op); } void fn_form(Compiler *C, AstVec l, Op op) { // (fn (arg arg arg) body ...) if (l.vals[1].ty != AST_LIST) { fprintf(stderr, "fn's first argument must be list"); exit(1); } AstVec arglist = l.vals[1].as.list; ObjFunc *func = objfunc_new(C->S); Compiler subcompiler = compiler_new(C, &func->ch); for (int i = 2; i < l.len - 1; i++) { compile_node(&subcompiler, l.vals[i]); chunk_wbc(&subcompiler, OP_DROP); } compile_node(&subcompiler, l.vals[l.len-1]); chunk_wbc(&subcompiler, OP_RET); chunk_wbc(C, OP_LOADK); chunk_wbc(C, chunk_wconst(C, VAL_OBJ(func))); } static BuiltinForm builtin_forms[] = { { "puts", 1, false, single_form, OP_PUTS }, { "print", 1, false, single_form, OP_PRINT }, { "set", 2, false, set_form, 0 }, { "do", 1, true, do_form, 0 }, { "if", 3, false, if_form, 0 }, { "while", 2, true, while_form, 0 }, { "fn", 2, true, fn_form, 0 }, #define ARITH_OP(str, op) \ { str, 2, false, arith_form, op }, ARITH_OP("+", OP_ADD) ARITH_OP("-", OP_SUB) ARITH_OP("*", OP_MUL) ARITH_OP("/", OP_DIV) ARITH_OP("=", OP_EQU) ARITH_OP("<", OP_CMP) ARITH_OP("%", OP_MOD) #undef ARITH_OP { 0 }, }; typedef struct { char *name; Op op; } BuiltinIdent; static BuiltinIdent builtin_idents[] = { { "true", OP_TRUE }, { "false", OP_FALSE }, { "nil", OP_NIL }, { 0 }, }; static void compile_node(Compiler *C, AstNode a) { switch (a.ty) { case AST_IDENT:; char *ident = a.as.str; bool found_builtin = false; for (BuiltinIdent *b = builtin_idents; b->name != NULL; b++) { if (0 == strcmp(b->name, ident)) { chunk_wbc(C, b->op); found_builtin = true; break; } } if (!found_builtin) { // read global variable ObjString *o = objstring_copy_cstr(C->S, a.as.str); chunk_wbc(C, OP_GETGLOBAL); chunk_wbc(C, chunk_wconst(C, VAL_OBJ(o))); } break; case AST_NUM: chunk_wbc(C, OP_LOADK); chunk_wbc(C, chunk_wconst(C, VAL_NUM(a.as.num))); break; case AST_STRING: { ObjString *o = objstring_copy_cstr(C->S, a.as.str); chunk_wbc(C, OP_LOADK); chunk_wbc(C, chunk_wconst(C, VAL_OBJ(o))); break; } case AST_LIST: { AstVec l = a.as.list; #define CK(cond, msg) if (!(cond)) { fputs(msg "\n", stderr); exit(1); } CK(l.len > 0, "can't handle empty list"); #undef CK BuiltinForm *form = NULL; 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; } } } if (form != NULL) { if (form->ellipsis && l.len < form->min_params + 1) { fprintf(stderr, "%s requires at least %d parameters\n", form->name, form->min_params); exit(1); } else if (!form->ellipsis && l.len != form->min_params + 1) { fprintf(stderr, "%s requires exactly %d parameters\n", form->name, form->min_params); exit(1); } form->action(C, l, form->op); } else { // 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; } } } int main(int argc, char **argv) { State st = state_new(); State *S = &st; 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"; char n3[] = "baz"; ObjString *o1 = objstring_copy_cstr(S, n1); ObjString *o2 = objstring_copy_cstr(S, n2); ObjString *o3 = objstring_copy_cstr(S, n3); ht_put(S, &st.globals, o1, VAL_NUM(69)); ht_put(S, &st.globals, o2, VAL_NUM(2)); ht_put(S, &st.globals, o3, VAL_NUM(3)); AstNode an = read(); Compiler com = (Compiler){ .S = S, .ch = &ch, }; Compiler *C = &com; compile_node(C, an); chunk_wbc(C, OP_PUTS); chunk_wbc(C, OP_HALT); Thread th = thread_new(S); th.ch = &ch; S->th = &th; return runvm(S); }