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
This commit is contained in:
Minus committed 2026-11-16 01:53:53 +00:00
1 parent 3c20496c78
commit ef305e7824
19 files changed
+370 -9

No files matched your search

+425
View File
@@ -0,0 +1,425 @@
-- src/catscript/parser.cat -- the catscript parser, written in catscript.
--
-- Turns the token array from lexer.cat into an AST. AST nodes are
-- ordinary tables with a `t` (tag) field:
--
-- { t="nil" } { t="true" } { t="false" }
-- { t="int", v=42 } { t="float", v=2.5 } { t="str", v="s" }
-- { t="name", v="x" }
-- { t="bin", op="+", l=L, r=R } { t="un", op="-", e=E }
-- { t="call", f=Fn, args={...} }
-- { t="method", obj=O, name="m", args={...} }
-- { t="index", obj=O, idx=I }
-- { t="attr", obj=O, name="a" }
-- { t="table", fields={ {k=..., v=...}, ... } } -- positional k is nil
-- { t="func", name=..., params={...}, body={stats} }
--
-- Statements:
-- { t="local", name=..., init=E }
-- { t="assign", name=..., value=E } { t="setidx", name, idx, value }
-- { t="setattr", name, attr, value }
-- { t="if", conds={ {cond=..., body={...}}, ... }, els={...}|nil }
-- { t="while", cond=E, body={...} }
-- { t="repeat", body={...}, cond=E }
-- { t="for", var=..., from=E, to=E, step=E|nil, body={...} }
-- { t="funcdef", name=..., func=F }
-- { t="return", e=E|nil }
-- { t="expr", e=E }
--
-- All parser state lives in globals prefixed PAR_ (closures do not
-- capture outer locals in catscript).
PAR_TOKS = {}
PAR_I = 0
PAR_N = 0
PAR_ERR = ""
function par_init(toks)
PAR_TOKS = toks
PAR_I = 1
PAR_N = len(toks)
PAR_ERR = ""
end
function par_cur()
if PAR_I <= PAR_N then
return PAR_TOKS[PAR_I]
end
return { kind = "EOF", text = "", line = 0 }
end
function par_at(kind)
return par_cur().kind == kind
end
function par_advance()
local t = par_cur()
if PAR_I <= PAR_N then
PAR_I = PAR_I + 1
end
return t
end
function par_fail(msg)
PAR_ERR = "line " .. tostring(par_cur().line) .. ": " .. msg
return nil
end
function par_expect(kind, what)
if par_at(kind) then
return par_advance()
end
par_fail("expected " .. what .. ", got " .. par_cur().kind)
return nil
end
-- forward decls (filled below; catscript resolves globals at call time)
function par_block() return {} end
function par_expr() return { t = "nil" } end
-- ---- expressions -----------------------------------------------------
function par_primary()
local t = par_cur()
local k = t.kind
if k == "nil" then par_advance() return { t = "nil" } end
if k == "true" then par_advance() return { t = "true" } end
if k == "false" then par_advance() return { t = "false" } end
if k == "INT" then par_advance() return { t = "int", v = tonumber(t.text) } end
if k == "FLOAT" then par_advance() return { t = "float", v = tonumber(t.text) } end
if k == "STRING" then par_advance() return { t = "str", v = t.text } end
if k == "NAME" then par_advance() return { t = "name", v = t.text } end
if k == "(" then
par_advance()
local e = par_expr()
par_expect(")", "')'")
return e
end
if k == "function" then
par_advance()
return par_funcbody("anon")
end
if k == "{" then
return par_table()
end
par_fail("unexpected token " .. k)
return { t = "nil" }
end
function par_table()
par_advance() -- {
local fields = {}
local n = 0
while not par_at("}") and not par_at("EOF") do
local fkey = nil
if par_at("NAME") and PAR_TOKS[PAR_I + 1] ~= nil and PAR_TOKS[PAR_I + 1].kind == "=" then
fkey = par_advance().text
par_advance() -- =
end
local v = par_expr()
n = n + 1
fields[n] = { k = fkey, v = v }
if not (par_at(",") or par_at(";")) then
break
end
par_advance()
end
par_expect("}", "'}'")
return { t = "table", fields = fields }
end
function par_args()
par_advance() -- (
local args = {}
local n = 0
while not par_at(")") and not par_at("EOF") do
local e = par_expr()
n = n + 1
args[n] = e
if not par_at(",") then
break
end
par_advance()
end
par_expect(")", "')'")
return args
end
function par_suffixes(e)
local go = true
while go do
if par_at(".") then
par_advance()
local nm = par_expect("NAME", "attribute name")
e = { t = "attr", obj = e, name = nm.text }
elseif par_at("[") then
par_advance()
local idx = par_expr()
par_expect("]", "']'")
e = { t = "index", obj = e, idx = idx }
elseif par_at(":") then
par_advance()
local nm = par_expect("NAME", "method name")
local args = par_args()
e = { t = "method", obj = e, name = nm.text, args = args }
elseif par_at("(") then
local args = par_args()
e = { t = "call", f = e, args = args }
else
go = false
end
end
return e
end
function par_unary()
if par_at("-") then
par_advance()
return { t = "un", op = "-", e = par_unary() }
end
if par_at("not") then
par_advance()
return { t = "un", op = "not", e = par_unary() }
end
if par_at("#") then
par_advance()
return { t = "un", op = "#", e = par_unary() }
end
return par_suffixes(par_primary())
end
function par_bp(k)
if k == "or" then return 1 end
if k == "and" then return 2 end
if k == "==" or k == "~=" or k == "<" or k == "<=" or k == ">" or k == ">=" then return 3 end
if k == ".." then return 4 end
if k == "+" or k == "-" then return 5 end
if k == "*" or k == "/" or k == "%" then return 6 end
return 0
end
function par_expr_bp(minbp)
local l = par_unary()
while true do
local k = par_cur().kind
local bp = par_bp(k)
if bp == 0 or bp < minbp then
break
end
par_advance()
local r = par_expr_bp(bp + 1)
l = { t = "bin", op = k, l = l, r = r }
end
return l
end
function par_expr()
return par_expr_bp(1)
end
-- ---- statements ------------------------------------------------------
function par_funcbody(name)
par_expect("(", "'('")
local params = {}
local np = 0
while not par_at(")") and not par_at("EOF") do
local nm = par_expect("NAME", "parameter name")
np = np + 1
params[np] = nm.text
if par_at(":") then
par_advance()
par_advance() -- type hint name (advisory)
end
if not par_at(",") then
break
end
par_advance()
end
par_expect(")", "')'")
if par_at("->") then
par_advance()
par_advance() -- return type hint (advisory)
end
local body = par_block()
par_expect("end", "'end'")
return { t = "func", name = name, params = params, body = body }
end
function par_stat()
local t = par_cur()
local k = t.kind
if k == ";" then par_advance() return nil end
if k == "local" then
par_advance()
if par_at("function") then
par_advance()
local nm = par_expect("NAME", "function name")
local f = par_funcbody(nm.text)
return { t = "local", name = nm.text, init = f }
end
local nm = par_expect("NAME", "local name")
if par_at(":") then
par_advance()
par_advance()
end
local init = nil
if par_at("=") then
par_advance()
init = par_expr()
end
return { t = "local", name = nm.text, init = init }
end
if k == "function" then
par_advance()
local nm = par_expect("NAME", "function name")
local f = par_funcbody(nm.text)
return { t = "funcdef", name = nm.text, func = f }
end
if k == "if" then
par_advance()
local conds = {}
local nc = 0
local cond = par_expr()
par_expect("then", "'then'")
nc = nc + 1
conds[nc] = { cond = cond, body = par_block() }
while par_at("elseif") do
par_advance()
local c2 = par_expr()
par_expect("then", "'then'")
nc = nc + 1
conds[nc] = { cond = c2, body = par_block() }
end
local els = nil
if par_at("else") then
par_advance()
els = par_block()
end
par_expect("end", "'end'")
return { t = "if", conds = conds, els = els }
end
if k == "while" then
par_advance()
local cond = par_expr()
par_expect("do", "'do'")
local body = par_block()
par_expect("end", "'end'")
return { t = "while", cond = cond, body = body }
end
if k == "repeat" then
par_advance()
local body = par_block()
par_expect("until", "'until'")
local cond = par_expr()
return { t = "repeat", body = body, cond = cond }
end
if k == "for" then
par_advance()
local nm = par_expect("NAME", "loop variable")
if par_at(":") then
par_advance()
par_advance()
end
par_expect("=", "'='")
local from = par_expr()
par_expect(",", "','")
local to = par_expr()
local step = nil
if par_at(",") then
par_advance()
step = par_expr()
end
par_expect("do", "'do'")
local body = par_block()
par_expect("end", "'end'")
return { t = "for", var = nm.text, from = from, to = to, step = step, body = body }
end
if k == "return" then
par_advance()
local e = nil
if not (par_at("end") or par_at("EOF") or par_at(";") or par_at("else")
or par_at("elseif") or par_at("until")) then
e = par_expr()
end
if par_at(";") then par_advance() end
return { t = "return", e = e }
end
-- assignment or expression statement
if k == "NAME" then
local nxt = PAR_TOKS[PAR_I + 1]
if nxt ~= nil and (nxt.kind == "=" or nxt.kind == "[" or nxt.kind == ".") then
-- could still be a call/index expression; disambiguate
local nm = par_advance().text
if par_at("=") then
par_advance()
local v = par_expr()
return { t = "assign", name = nm, value = v }
end
if par_at("[") then
par_advance()
local idx = par_expr()
par_expect("]", "']'")
if par_at("=") then
par_advance()
local v = par_expr()
return { t = "setidx", name = nm, idx = idx, value = v }
end
-- not an assignment: rebuild as expression
local base = par_suffixes({ t = "name", v = nm })
-- base already consumed [idx]; wrap
return { t = "expr", e = base }
end
if par_at(".") then
par_advance()
local attr = par_expect("NAME", "attribute")
if par_at("=") then
par_advance()
local v = par_expr()
return { t = "setattr", name = nm, attr = attr.text, value = v }
end
local base = par_suffixes({ t = "attr", obj = { t = "name", v = nm }, name = attr.text })
return { t = "expr", e = base }
end
end
end
local e = par_expr()
return { t = "expr", e = e }
end
function par_block()
local stats = {}
local n = 0
while not (par_at("end") or par_at("EOF") or par_at("else")
or par_at("elseif") or par_at("until")) do
local before = PAR_I
local s = par_stat()
if s ~= nil then
n = n + 1
stats[n] = s
end
if PAR_I == before then
par_fail("parse error near " .. par_cur().kind)
break
end
end
return stats
end
function par_parse(toks)
par_init(toks)
local body = par_block()
if PAR_ERR ~= "" then
return { ok = false, err = PAR_ERR }
end
return { ok = true, body = body }
end