- std/ layout: std/lang/{lexer,parser,checker,compiler}.cat moved from src/catscript
- std/io/io.cat, std/fs/fs.cat, std/math/math.cat modules
- use "path.cat": import another unit (compile+run in the same VM)
- class NAME [extends BASE] ... end: methods, inheritance via __index,
default new constructor (runs optional init), tables are callable
- examples/classes.cat, tests/std.cat; 48/48 tests
38 lines
1.1 KiB
Plaintext
38 lines
1.1 KiB
Plaintext
-- 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
|