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:
1 parent
3c20496c78
commit
ef305e7824
19 files changed
+370
-9
No files matched your search
@@ -90,6 +90,22 @@ check "slot-path" "path: /tmp/catscript_slots_demo.txt" "`echo "$out" | sed -n 1
|
||||
check "slot-mode" "mode: 2" "`echo "$out" | sed -n 2p`"
|
||||
check "slot-closed" "closed: false" "`echo "$out" | sed -n 3p`"
|
||||
check "slot-closed-2" "closed: true" "`echo "$out" | sed -n 4p`"
|
||||
|
||||
# ---- class / extends / use (import) ---------------------------------
|
||||
|
||||
out=`$BIN examples/classes.cat`
|
||||
check "class-method" "cat makes a sound" "`echo "$out" | sed -n 1p`"
|
||||
check "class-attr" "cat" "`echo "$out" | sed -n 2p`"
|
||||
check "class-override" "Rex barks" "`echo "$out" | sed -n 3p`"
|
||||
check "class-inherit" "Rex" "`echo "$out" | sed -n 4p`"
|
||||
check "class-use" "double 21 = 42" "`echo "$out" | sed -n 5p`"
|
||||
|
||||
# ---- std/ tree modules (use "std/...") -------------------------------
|
||||
|
||||
out=`$BIN tests/std.cat`
|
||||
check "std-math" "math double: 42" "`echo "$out" | sed -n 1p`"
|
||||
check "std-io" "io join: a-b-c" "`echo "$out" | sed -n 4p`"
|
||||
check "std-fs" "fs copy: true" "`echo "$out" | sed -n 6p`"
|
||||
# ---- bytecode dump ---------------------------------------------------
|
||||
|
||||
out=`$BIN --dump examples/hello.cat`
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
-- This proves the front-end is self-hosted: given catscript source text,
|
||||
-- the catscript programs compile it to bytecode and execute it.
|
||||
|
||||
dofile("src/catscript/lexer.cat")
|
||||
dofile("src/catscript/parser.cat")
|
||||
dofile("src/catscript/checker.cat")
|
||||
dofile("src/catscript/compiler.cat")
|
||||
dofile("std/lang/lexer.cat")
|
||||
dofile("std/lang/parser.cat")
|
||||
dofile("std/lang/checker.cat")
|
||||
dofile("std/lang/compiler.cat")
|
||||
|
||||
function selfhost_compile(src)
|
||||
local toks = lexer_tokenize(src)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
-- tests/selfhost_lexer.cat -- drives the catscript lexer (written in
|
||||
-- catscript) and prints the token stream for a sample program.
|
||||
|
||||
dofile("src/catscript/lexer.cat")
|
||||
dofile("std/lang/lexer.cat")
|
||||
|
||||
local src = "local x : int = 42 -- note\nfunction f(a, b)\n return a + b * 2.5\nend\nprint(f(1, 2) .. \"!\")"
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
-- tests/selfhost_parser.cat -- drives the catscript parser (written in
|
||||
-- catscript), printing a compact dump of the AST.
|
||||
|
||||
dofile("src/catscript/lexer.cat")
|
||||
dofile("src/catscript/parser.cat")
|
||||
dofile("std/lang/lexer.cat")
|
||||
dofile("std/lang/parser.cat")
|
||||
|
||||
local src = "local x : int = 42\nfunction add(a, b)\n return a + b * 2\nend\nprint(add(x, 1) .. \"!\")"
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
-- tests/std.cat -- exercises the std/ tree (use/import + modules).
|
||||
|
||||
use "std/math/math.cat"
|
||||
use "std/io/io.cat"
|
||||
use "std/fs/fs.cat"
|
||||
|
||||
print("math double:", double(21))
|
||||
print("math square:", square(9))
|
||||
print("math abs:", abs(-7))
|
||||
|
||||
print("io join:", join({ "a", "b", "c" }, "-"))
|
||||
|
||||
local path = "/tmp/catscript_std_test.txt"
|
||||
fs_write(path, "one")
|
||||
fs_append(path, "two")
|
||||
print("fs read:", fs_read(path))
|
||||
print("fs copy:", fs_copy(path, "/tmp/catscript_std_copy.txt"))
|
||||
print("fs read2:", fs_read("/tmp/catscript_std_copy.txt"))
|
||||
Reference in new issue
Block a user