-- tests/selfhost_compile.cat -- drives the FULL self-hosted front-end: -- lexer -> parser -> checker -> compiler (all written in catscript), -- then runs the resulting bytecode via the C runtime's bc_run. -- -- This proves the front-end is self-hosted: given catscript source text, -- the catscript programs compile it to bytecode and execute it. dofile("std/lang/lexer.cat") dofile("std/lang/parser.cat") dofile("std/lang/checker.cat") dofile("std/lang/compiler.cat") function selfhost_compile(src) local toks = lexer_tokenize(src) local pr = par_parse(toks) if not pr.ok then return { ok = false, err = pr.err } end local chk = chk_check(pr) local chunk = cmp_compile(pr) return { ok = true, chunk = chunk, diags = chk.diags } end local src = "local x = 40\nlocal y = 2\nprint(\"sum =\", x + y)\nfunction sq(n)\n return n * n\nend\nprint(\"sq =\", sq(7))\n" local r = selfhost_compile(src) if not r.ok then print("compile error: " .. r.err) else print("compiled ok (" .. tostring(len(r.chunk.code)) .. " instrs)") local i = 1 while i <= len(r.diags) do print("diag: " .. r.diags[i]) i = i + 1 end bc_run(r.chunk) end