- delete the nil type; catscript has no nil and no null - add the empty object (Object with no members), exposed as the global 'empty'; it is a shared immutable singleton - 'nil' is no longer a keyword: it is an undefined global, which raises - undefined global -> error (consistent with a missing attribute) - only 'false' is falsy (numbers/objects are truthy); booleans kept - OP_NIL -> OP_EMPTY; compiler, VM, stdlib, toml, ffi, generator, file, list, selector, value all migrated - rename the table type to Object - std/ front-end and examples/tests migrated to 'empty' - 84/84 tests
133 lines
2.8 KiB
Plaintext
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 == empty 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 == empty 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
|