- 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
42 lines
813 B
Plaintext
42 lines
813 B
Plaintext
-- std/io/io.cat -- input/output helpers (standard library module).
|
|
--
|
|
-- Thin, catscript-level conveniences over the C-provided natives.
|
|
-- Use: use "std/io/io.cat"
|
|
|
|
-- Print each argument on its own line.
|
|
function print_lines(a)
|
|
local i = 1
|
|
while i <= len(a) do
|
|
print(a[i])
|
|
i = i + 1
|
|
end
|
|
end
|
|
|
|
-- Join a list's items with a separator, returning one string.
|
|
function join(a, sep)
|
|
local out = ""
|
|
local i = 1
|
|
while i <= len(a) do
|
|
if i > 1 then
|
|
out = out .. sep
|
|
end
|
|
out = out .. tostring(a[i])
|
|
i = i + 1
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- Read a whole file as text ("" when missing).
|
|
function read_text(path)
|
|
local s = read_file(path)
|
|
if s == empty then
|
|
return ""
|
|
end
|
|
return s
|
|
end
|
|
|
|
-- Write text to a file, returning bytes written.
|
|
function write_text(path, text)
|
|
return write_file(path, text)
|
|
end
|