Files
catscript/std/lang/checker.cat
T
Minus 7cdf388bf4 object, not table: fix user-facing terminology
The language is OOP with a single compound value, the object (an
anonymous `{...}` or a class instance); it is not Lua and has no
"tables".  The visible type name was already `object`; this fixes the
remaining user-facing wording:

- error messages: "object has no member 'x'", "object is not callable".
- README: drop the "Lua-like" phrasing; describe member objects, not
  tables.
- self-hosted front-end AST node `t="object"` (was "table"), updated
  across parser/compiler/checker.
- test comments/expected output aligned ("object:").

The internal C symbols keep the cat_types_table_* naming convention.
Suite stays 130 green.
2026-11-17 23:14:17 +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 == 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 == "object" 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