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
+3
-3
@@ -6,7 +6,7 @@
|
||||
-- Read a whole file, returning its contents ("" when missing).
|
||||
function fs_read(path)
|
||||
local f = File::open(path, "r")
|
||||
if f == nil then
|
||||
if f == empty then
|
||||
return ""
|
||||
end
|
||||
local data = f:read()
|
||||
@@ -17,7 +17,7 @@ end
|
||||
-- Write text to a file, replacing its contents.
|
||||
function fs_write(path, text)
|
||||
local f = File::open(path, "w")
|
||||
if f == nil then
|
||||
if f == empty then
|
||||
return false
|
||||
end
|
||||
f:write(text)
|
||||
@@ -28,7 +28,7 @@ end
|
||||
-- Append text to a file.
|
||||
function fs_append(path, text)
|
||||
local f = File::open(path, "a")
|
||||
if f == nil then
|
||||
if f == empty then
|
||||
return false
|
||||
end
|
||||
f:write(text)
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ end
|
||||
-- Read a whole file as text ("" when missing).
|
||||
function read_text(path)
|
||||
local s = read_file(path)
|
||||
if s == nil then
|
||||
if s == empty then
|
||||
return ""
|
||||
end
|
||||
return s
|
||||
|
||||
@@ -28,7 +28,7 @@ function chk_diag(msg)
|
||||
end
|
||||
|
||||
function chk_expr(e)
|
||||
if e == nil then
|
||||
if e == empty then
|
||||
return
|
||||
end
|
||||
local t = e.t
|
||||
@@ -72,7 +72,7 @@ function chk_expr(e)
|
||||
end
|
||||
|
||||
function chk_block(stats)
|
||||
if stats == nil then
|
||||
if stats == empty then
|
||||
return
|
||||
end
|
||||
local i = 1
|
||||
|
||||
+12
-12
@@ -14,7 +14,7 @@
|
||||
--
|
||||
-- All state lives in globals prefixed CMP_ (closures capture no locals).
|
||||
|
||||
CMP_CHUNK = nil
|
||||
CMP_CHUNK = empty
|
||||
CMP_CONSTS = {}
|
||||
CMP_NC = 0
|
||||
CMP_PROTOS = {}
|
||||
@@ -80,7 +80,7 @@ function cmp_restore(s)
|
||||
end
|
||||
|
||||
function cmp_newchunk(name)
|
||||
CMP_CHUNK = nil
|
||||
CMP_CHUNK = empty
|
||||
CMP_CONSTS = {}
|
||||
CMP_NC = 0
|
||||
CMP_PROTOS = {}
|
||||
@@ -99,7 +99,7 @@ end
|
||||
|
||||
function cmp_expr(e)
|
||||
local t = e.t
|
||||
if t == "nil" then cmp_emit(2, 0, 0, 0)
|
||||
if t == "empty" then cmp_emit(2, 0, 0, 0)
|
||||
elseif t == "true" then cmp_emit(3, 0, 0, 0)
|
||||
elseif t == "false" then cmp_emit(4, 0, 0, 0)
|
||||
elseif t == "int" then cmp_emit(1, cmp_addconst(e.v), 0, 0)
|
||||
@@ -151,7 +151,7 @@ function cmp_expr(e)
|
||||
local i = 1
|
||||
while i <= len(e.fields) do
|
||||
local f = e.fields[i]
|
||||
if f.k ~= nil then
|
||||
if f.k ~= empty then
|
||||
cmp_emit(37, 0, 0, 0) -- DUP obj
|
||||
cmp_expr(f.v)
|
||||
cmp_emit(23, cmp_addstr(f.k), 0, 0)
|
||||
@@ -207,7 +207,7 @@ end
|
||||
-- ---- statements ------------------------------------------------------
|
||||
|
||||
function cmp_block(stats)
|
||||
if stats == nil then
|
||||
if stats == empty then
|
||||
return
|
||||
end
|
||||
local i = 1
|
||||
@@ -220,7 +220,7 @@ end
|
||||
function cmp_stat(s)
|
||||
local k = s.t
|
||||
if k == "local" then
|
||||
if s.init ~= nil then
|
||||
if s.init ~= empty then
|
||||
cmp_expr(s.init)
|
||||
else
|
||||
cmp_emit(2, 0, 0, 0)
|
||||
@@ -267,7 +267,7 @@ function cmp_stat(s)
|
||||
elseif k == "for" then
|
||||
cmp_for(s)
|
||||
elseif k == "return" then
|
||||
if s.e ~= nil then
|
||||
if s.e ~= empty then
|
||||
cmp_expr(s.e)
|
||||
cmp_emit(30, 0, 0, 0)
|
||||
else
|
||||
@@ -295,7 +295,7 @@ function cmp_if(s)
|
||||
cmp_block(s.conds[i].body)
|
||||
i = i + 1
|
||||
end
|
||||
if s.els ~= nil then
|
||||
if s.els ~= empty then
|
||||
nends = nends + 1
|
||||
ends[nends] = cmp_emit(27, 0, 0, 0)
|
||||
cmp_patch(jf, CMP_NI)
|
||||
@@ -317,7 +317,7 @@ function cmp_while(s)
|
||||
CMP_LOOPS[len(CMP_LOOPS) + 1] = { breaks = {} }
|
||||
cmp_block(s.body)
|
||||
local loop = CMP_LOOPS[len(CMP_LOOPS)]
|
||||
CMP_LOOPS[len(CMP_LOOPS)] = nil
|
||||
CMP_LOOPS[len(CMP_LOOPS)] = empty
|
||||
cmp_emit(27, jcond, 0, 0)
|
||||
cmp_patch(jend, CMP_NI)
|
||||
local i = 1
|
||||
@@ -332,7 +332,7 @@ function cmp_repeat(s)
|
||||
CMP_LOOPS[len(CMP_LOOPS) + 1] = { breaks = {} }
|
||||
cmp_block(s.body)
|
||||
local loop = CMP_LOOPS[len(CMP_LOOPS)]
|
||||
CMP_LOOPS[len(CMP_LOOPS)] = nil
|
||||
CMP_LOOPS[len(CMP_LOOPS)] = empty
|
||||
cmp_expr(s.cond)
|
||||
cmp_emit(29, jtop, 0, 0)
|
||||
local i = 1
|
||||
@@ -344,7 +344,7 @@ end
|
||||
|
||||
function cmp_for(s)
|
||||
cmp_expr(s.from)
|
||||
if s.step ~= nil then
|
||||
if s.step ~= empty then
|
||||
cmp_expr(s.step)
|
||||
else
|
||||
cmp_emit(1, cmp_addconst(1), 0, 0)
|
||||
@@ -365,7 +365,7 @@ function cmp_for(s)
|
||||
CMP_LOOPS[len(CMP_LOOPS) + 1] = { breaks = {} }
|
||||
cmp_block(s.body)
|
||||
local loop = CMP_LOOPS[len(CMP_LOOPS)]
|
||||
CMP_LOOPS[len(CMP_LOOPS)] = nil
|
||||
CMP_LOOPS[len(CMP_LOOPS)] = empty
|
||||
cmp_emit(5, slot, 0, 0)
|
||||
cmp_emit(5, slot + 2, 0, 0)
|
||||
cmp_emit(9, 0, 0, 0)
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ LEX_LINE = 1
|
||||
|
||||
LEX_KEYWORDS = {
|
||||
"and", "break", "do", "else", "elseif", "end", "false", "for",
|
||||
"function", "if", "in", "local", "nil", "not", "or", "repeat",
|
||||
"function", "if", "in", "local", "not", "or", "repeat",
|
||||
"return", "then", "true", "until", "while"
|
||||
}
|
||||
|
||||
|
||||
+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