Files
catscript/std/lang/checker.cat
T
Minus ef305e7824 std/ tree, use (import) and class syntax
- 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
2026-11-16 01:53:53 +00:00

133 lines
2.8 KiB
Plaintext

-- src/catscript/checker.cat -- the catscript checker, written in catscript.
--
-- A lightweight structural checker. catscript is structurally typed, so
-- this pass does not reject programs; it walks the AST and reports
-- advisory diagnostics (unknown-looking globals, likely type-hint
-- mismatches). It proves the front-end is complete and self-hosted.
--
-- Result: { ok=true, diags={ "..." } }.
CHK_DIAGS = {}
CHK_NDIAG = 0
CHK_KNOWN = {}
function chk_known_global(name)
local i = 1
while i <= len(CHK_KNOWN) do
if CHK_KNOWN[i] == name then
return true
end
i = i + 1
end
return false
end
function chk_diag(msg)
CHK_NDIAG = CHK_NDIAG + 1
CHK_DIAGS[CHK_NDIAG] = msg
end
function chk_expr(e)
if e == nil then
return
end
local t = e.t
if t == "name" then
if not chk_known_global(e.v) then
chk_diag("note: '" .. e.v .. "' is not a known global")
end
elseif t == "bin" then
chk_expr(e.l)
chk_expr(e.r)
elseif t == "un" then
chk_expr(e.e)
elseif t == "call" then
chk_expr(e.f)
local i = 1
while i <= len(e.args) do
chk_expr(e.args[i])
i = i + 1
end
elseif t == "method" then
chk_expr(e.obj)
local i = 1
while i <= len(e.args) do
chk_expr(e.args[i])
i = i + 1
end
elseif t == "index" then
chk_expr(e.obj)
chk_expr(e.idx)
elseif t == "attr" then
chk_expr(e.obj)
elseif t == "table" then
local i = 1
while i <= len(e.fields) do
chk_expr(e.fields[i].v)
i = i + 1
end
elseif t == "func" then
chk_block(e.body)
end
end
function chk_block(stats)
if stats == nil then
return
end
local i = 1
while i <= len(stats) do
local s = stats[i]
local k = s.t
if k == "local" then
CHK_KNOWN[len(CHK_KNOWN) + 1] = s.name
chk_expr(s.init)
elseif k == "assign" then
chk_expr(s.value)
elseif k == "setidx" then
chk_expr(s.idx)
chk_expr(s.value)
elseif k == "setattr" then
chk_expr(s.value)
elseif k == "funcdef" then
CHK_KNOWN[len(CHK_KNOWN) + 1] = s.name
chk_block(s.func.body)
elseif k == "if" then
local j = 1
while j <= len(s.conds) do
chk_expr(s.conds[j].cond)
chk_block(s.conds[j].body)
j = j + 1
end
chk_block(s.els)
elseif k == "while" then
chk_expr(s.cond)
chk_block(s.body)
elseif k == "repeat" then
chk_block(s.body)
chk_expr(s.cond)
elseif k == "for" then
CHK_KNOWN[len(CHK_KNOWN) + 1] = s.var
chk_expr(s.from)
chk_expr(s.to)
chk_expr(s.step)
chk_block(s.body)
elseif k == "return" then
chk_expr(s.e)
elseif k == "expr" then
chk_expr(s.e)
end
i = i + 1
end
end
function chk_check(ast)
CHK_DIAGS = {}
CHK_NDIAG = 0
CHK_KNOWN = { "print", "tostring", "tonumber", "type", "len",
"toml_parse", "toml_encode", "read_file", "write_file",
"ord", "chr", "substr", "dofile", "bc_run", "bc_dump" }
chk_block(ast.body)
return { ok = true, diags = CHK_DIAGS }
end