#include"value.h" #include"table.h" #include"parse.h" #include"vm.h" #include"lexer.h" #include"str.h" #include"dump.h" static size_t native_print(LVM *lvm, void *ud, size_t argn, LValue *values) { if(lvalue_tag(values[0]) == LTAG_STRING) { LString *lstr = (void*) (values[0].u & ~LTAG_MASK); printf("%.*s\n", (int) lstr->length, lstr->data); } else if(lvalue_tag(values[0]) == LTAG_I32) { printf("%i\n", lvalue_to_int32(values[0])); } else if(values[0].u == LTAG_NIL) { printf("nil\n"); } return 0; } int main() { LTable *env = ltable_new(128); LString *key = lstring_newz("print"); LFunc *func = lvm_func_from_native(native_print, NULL); ltable_set(env, lvalue_from_string(key), lvalue_from_func(func)); const char *bufs = "for i = 1, 1000000 do print(i) if i % 3 == 0 then print(\"Fizz\") end if i % 5 == 0 then print(\"Buzz\") end end"; //const char *bufs = "local t = {a = 9} print(t.a)"; //const char *bufs = "z = 5 print(z)"; //const char *bufs = "local i = 0 while i ~= 1500000 do print(i) i = i + 1 end"; vec_Token toks = ltokenize(bufs, strlen(bufs)); LUnit *unit = lparse(toks.size, toks.data, env); dump(unit->funcs[0].lua_instrs); LValue regs[256]; lvm_reset_regs(regs); LVM lvm = {}; lvm_run(&lvm, &unit->funcs[0], 0, regs); }