remove nil/null: absence is an error, empty object is the only no-value
- 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
This commit is contained in:
1 parent
e0bf102f2a
commit
d29b0706c9
36 files changed
+257
-244
No files matched your search
+19
-19
@@ -3,7 +3,7 @@
|
||||
-- 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="empty" } { 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 }
|
||||
@@ -11,19 +11,19 @@
|
||||
-- { 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="table", fields={ {k=..., v=...}, ... } } -- positional k is empty
|
||||
-- { 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="if", conds={ {cond=..., body={...}}, ... }, els={...}|empty }
|
||||
-- { t="while", cond=E, body={...} }
|
||||
-- { t="repeat", body={...}, cond=E }
|
||||
-- { t="for", var=..., from=E, to=E, step=E|nil, body={...} }
|
||||
-- { t="for", var=..., from=E, to=E, step=E|empty, body={...} }
|
||||
-- { t="funcdef", name=..., func=F }
|
||||
-- { t="return", e=E|nil }
|
||||
-- { t="return", e=E|empty }
|
||||
-- { t="expr", e=E }
|
||||
--
|
||||
-- All parser state lives in globals prefixed PAR_ (closures do not
|
||||
@@ -62,7 +62,7 @@ end
|
||||
|
||||
function par_fail(msg)
|
||||
PAR_ERR = "line " .. tostring(par_cur().line) .. ": " .. msg
|
||||
return nil
|
||||
return empty
|
||||
end
|
||||
|
||||
function par_expect(kind, what)
|
||||
@@ -70,19 +70,19 @@ function par_expect(kind, what)
|
||||
return par_advance()
|
||||
end
|
||||
par_fail("expected " .. what .. ", got " .. par_cur().kind)
|
||||
return nil
|
||||
return empty
|
||||
end
|
||||
|
||||
-- forward decls (filled below; catscript resolves globals at call time)
|
||||
function par_block() return {} end
|
||||
function par_expr() return { t = "nil" } end
|
||||
function par_expr() return { t = "empty" } 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 == "empty" then par_advance() return { t = "empty" } 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
|
||||
@@ -103,7 +103,7 @@ function par_primary()
|
||||
return par_table()
|
||||
end
|
||||
par_fail("unexpected token " .. k)
|
||||
return { t = "nil" }
|
||||
return { t = "empty" }
|
||||
end
|
||||
|
||||
function par_table()
|
||||
@@ -111,8 +111,8 @@ function par_table()
|
||||
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
|
||||
local fkey = empty
|
||||
if par_at("NAME") and PAR_TOKS[PAR_I + 1] ~= empty and PAR_TOKS[PAR_I + 1].kind == "=" then
|
||||
fkey = par_advance().text
|
||||
par_advance() -- =
|
||||
end
|
||||
@@ -249,7 +249,7 @@ end
|
||||
function par_stat()
|
||||
local t = par_cur()
|
||||
local k = t.kind
|
||||
if k == ";" then par_advance() return nil end
|
||||
if k == ";" then par_advance() return empty end
|
||||
|
||||
if k == "local" then
|
||||
par_advance()
|
||||
@@ -264,7 +264,7 @@ function par_stat()
|
||||
par_advance()
|
||||
par_advance()
|
||||
end
|
||||
local init = nil
|
||||
local init = empty
|
||||
if par_at("=") then
|
||||
par_advance()
|
||||
init = par_expr()
|
||||
@@ -294,7 +294,7 @@ function par_stat()
|
||||
nc = nc + 1
|
||||
conds[nc] = { cond = c2, body = par_block() }
|
||||
end
|
||||
local els = nil
|
||||
local els = empty
|
||||
if par_at("else") then
|
||||
par_advance()
|
||||
els = par_block()
|
||||
@@ -331,7 +331,7 @@ function par_stat()
|
||||
local from = par_expr()
|
||||
par_expect(",", "','")
|
||||
local to = par_expr()
|
||||
local step = nil
|
||||
local step = empty
|
||||
if par_at(",") then
|
||||
par_advance()
|
||||
step = par_expr()
|
||||
@@ -344,7 +344,7 @@ function par_stat()
|
||||
|
||||
if k == "return" then
|
||||
par_advance()
|
||||
local e = nil
|
||||
local e = empty
|
||||
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()
|
||||
@@ -356,7 +356,7 @@ function par_stat()
|
||||
-- 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
|
||||
if nxt ~= empty 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
|
||||
@@ -403,7 +403,7 @@ function par_block()
|
||||
or par_at("elseif") or par_at("until")) do
|
||||
local before = PAR_I
|
||||
local s = par_stat()
|
||||
if s ~= nil then
|
||||
if s ~= empty then
|
||||
n = n + 1
|
||||
stats[n] = s
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user