catscript: núcleo C, stdlib (TOML), família de arquivos e front-end self-hosted

- Modelo de objetos Cat_Value + vtable (interfaces abstratas)
- VM de bytecode, sem GIL (VM por-thread, nativas como valores)
- Stdlib com TOML embutido (toml_parse/toml_encode)
- Família de arquivos: AbstractFile (abstrata), File::open, StringIO, BytesIO
- Front-end (lexer/parser/checker/compiler) implementado em catscript
- cpk (cat package manager) como primeiro programa; manifest package.toml
- Type hints advisory, inclusive DynSized() e sentinel_sized<...>
- 25/25 testes passando (make test)
This commit is contained in:
Minus committed 2026-11-16 01:27:44 +00:00
commit 52d118211f
69 files changed
+6938

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
#!/bin/sh
# tests/run_tests.sh -- automated catscript test suite.
#
# Runs each example and asserts expected stdout, then runs the
# self-hosted front-end tests (lexer/parser/compiler written in
# catscript itself). Exits non-zero on the first failure.
BIN="./build/catscript/catscript"
pass=0
fail=0
check() {
name="$1"; expected="$2"; got="$3"
if [ "$expected" = "$got" ]; then
echo "ok - $name"
pass=$((pass + 1))
else
echo "FAIL - $name"
echo " expected: [$expected]"
echo " got: [$got]"
fail=$((fail + 1))
fi
}
# ---- examples (core language) ----------------------------------------
out=`$BIN examples/hello.cat`
check "hello-1" "hello from catscript" "`echo "$out" | sed -n 1p`"
check "hello-2" "2 + 3 = 5" "`echo "$out" | sed -n 2p`"
out=`$BIN examples/arith.cat`
check "arith-add" "a + b = 13" "`echo "$out" | sed -n 1p`"
check "arith-div" "a / b = 3.33333" "`echo "$out" | sed -n 4p`"
out=`$BIN examples/text.cat`
check "text-concat" "hello, cat" "`echo "$out" | sed -n 1p`"
out=`$BIN examples/control.cat`
check "classify-neg" "negative" "`echo "$out" | sed -n 1p`"
check "classify-zero" "zero" "`echo "$out" | sed -n 2p`"
check "classify-pos" "positive" "`echo "$out" | sed -n 3p`"
check "while-sum" "sum 1..5 = 15" "`echo "$out" | sed -n 4p`"
check "for-sum" "sum 1..4 = 10" "`echo "$out" | sed -n 5p`"
out=`$BIN examples/oop.cat`
check "oop-method" "counter: 3" "`echo "$out" | sed -n 1p`"
out=`$BIN examples/toml.cat`
check "toml-title" "title: catscript" "`echo "$out" | sed -n 1p`"
check "toml-version" "version: 1" "`echo "$out" | sed -n 2p`"
check "toml-pi" "pi: 3.14" "`echo "$out" | sed -n 3p`"
# ---- cpk: the cat package manager (first real catscript program) -----
out=`$BIN examples/cpk.cat`
check "cpk-name" "package: hello-world" "`echo "$out" | sed -n 1p`"
check "cpk-version" "version: 0.1.0" "`echo "$out" | sed -n 2p`"
# ---- file family: AbstractFile / File / StringIO / BytesIO ----------
out=`$BIN examples/files.cat`
check "file-stringio" "stringio: hello, catscript" "`echo "$out" | sed -n 1p`"
check "file-bytesio" "bytesio: bytes io" "`echo "$out" | sed -n 2p`"
check "file-open" "file: line one" "`echo "$out" | sed -n 3p`"
check "file-hints" "n=3 buf=abc" "`echo "$out" | sed -n 5p`"
# ---- bytecode dump ---------------------------------------------------
out=`$BIN --dump examples/hello.cat`
case "$out" in
*"CONST"*) check "bytecode-dump" "yes" "yes" ;;
*) check "bytecode-dump" "CONST present" "missing" ;;
esac
# ---- self-hosted front-end (written in catscript) -------------------
out=`$BIN tests/selfhost_lexer.cat`
case "$out" in
*"NAME [x]"*) check "selfhost-lexer" "yes" "yes" ;;
*) check "selfhost-lexer" "NAME token" "missing" ;;
esac
out=`$BIN tests/selfhost_parser.cat`
check "selfhost-parser" "parsed 3 statements" "`echo "$out" | sed -n 1p`"
out=`$BIN tests/selfhost_compile.cat`
check "selfhost-compile-1" "compiled ok (21 instrs)" "`echo "$out" | sed -n 1p`"
case "$out" in
*"sum = 42"*) check "selfhost-compile-2" "yes" "yes" ;;
*) check "selfhost-compile-2" "sum = 42" "missing" ;;
esac
echo ""
echo "$pass passed, $fail failed"
[ "$fail" -eq 0 ]
+37
View File
@@ -0,0 +1,37 @@
-- tests/selfhost_compile.cat -- drives the FULL self-hosted front-end:
-- lexer -> parser -> checker -> compiler (all written in catscript),
-- then runs the resulting bytecode via the C runtime's bc_run.
--
-- 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")
function selfhost_compile(src)
local toks = lexer_tokenize(src)
local pr = par_parse(toks)
if not pr.ok then
return { ok = false, err = pr.err }
end
local chk = chk_check(pr)
local chunk = cmp_compile(pr)
return { ok = true, chunk = chunk, diags = chk.diags }
end
local src = "local x = 40\nlocal y = 2\nprint(\"sum =\", x + y)\nfunction sq(n)\n return n * n\nend\nprint(\"sq =\", sq(7))\n"
local r = selfhost_compile(src)
if not r.ok then
print("compile error: " .. r.err)
else
print("compiled ok (" .. tostring(len(r.chunk.code)) .. " instrs)")
local i = 1
while i <= len(r.diags) do
print("diag: " .. r.diags[i])
i = i + 1
end
bc_run(r.chunk)
end
+14
View File
@@ -0,0 +1,14 @@
-- 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")
local src = "local x : int = 42 -- note\nfunction f(a, b)\n return a + b * 2.5\nend\nprint(f(1, 2) .. \"!\")"
local toks = lexer_tokenize(src)
local i = 1
while i <= len(toks) do
local t = toks[i]
print(i, t.kind, "[" .. t.text .. "]", t.line)
i = i + 1
end
+21
View File
@@ -0,0 +1,21 @@
-- 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")
local src = "local x : int = 42\nfunction add(a, b)\n return a + b * 2\nend\nprint(add(x, 1) .. \"!\")"
local toks = lexer_tokenize(src)
local r = par_parse(toks)
if not r.ok then
print("parse error: " .. r.err)
else
print("parsed " .. tostring(len(r.body)) .. " statements")
local i = 1
while i <= len(r.body) do
local s = r.body[i]
print(tostring(i) .. ": " .. s.t)
i = i + 1
end
end