Files
catscript/tests/run_tests.sh
T
Minus 77ea433215 native x86-64 backend (pluggable, per-opcode lowering)
A second backend that lowers each bytecode instruction to a machine-code
call into a runtime helper, with control flow as real branches.

- cat/backend/x86_64_glue.c: one helper per opcode, mirroring cat/vm.c
  exactly (frame lifecycle, arith, cmp, attr/idx, call/callmethod, is,
  next/fornext, try/raise).  Helpers use the SysV convention (f in rdi,
  a/b/c in rsi/rdx/rcx) and return 0/1/-1.
- cat/backend/x86_64.c: emits AT&T assembly (one labelled block per
  instruction, one call per helper, real jumps for JUMP/JUMPIF/JUMPIFNOT/
  FORNEXT/RETURN, an .Lonerror block that resumes try handlers), plus a
  small generated _main.c that rebuilds the bytecode and boots the VM.
  Builds with as + cc linked against libcatscript.a.
- generators are not lowered: their prototype carries no nat_entry, so
  the VM runs them (their bytecode is rebuilt by the generated main), and
  the emitted code simply falls back to the interpreter.
- cat_vm_exec_closure dispatches to a prototype's nat_entry when present.
- tests: native-vs-interpreter equivalence check.  Suite 155 -> 156.

Verified: hello, control, classes, generators, arith, errors, text, oop,
slots, reflect, toml, files, ffi all match the interpreter natively.
2026-11-18 20:19:54 +00:00

363 lines
16 KiB
Bash

#!/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`"
# ---- generators / iterators (yield, next, for..in) ------------------
out=`$BIN examples/generators.cat`
check "gen-type" "type: generator" "`echo "$out" | sed -n 1p`"
check "gen-next-1" "next: 1" "`echo "$out" | sed -n 2p`"
check "gen-next-nil" "next: empty" "`echo "$out" | sed -n 5p`"
check "gen-forin-sum" "sum 1..5 = 15" "`echo "$out" | sed -n 6p`"
check "gen-break-lazy" "taken < 3: 3" "`echo "$out" | sed -n 7p`"
# ---- errors (raise, try, except) ------------------------------------
out=`$BIN examples/errors.cat`
check "err-ok" "ok: 42" "`echo "$out" | sed -n 1p`"
check "err-catch" "caught: boom" "`echo "$out" | sed -n 2p`"
check "err-struct-code" "code: 42" "`echo "$out" | sed -n 3p`"
check "err-struct-what" "what: custom" "`echo "$out" | sed -n 4p`"
check "err-propagate" "propagated: negative not allowed" "`echo "$out" | sed -n 5p`"
check "err-survive" "still running" "`echo "$out" | sed -n 6p`"
# ---- slots: C-known object layout -----------------------------------
out=`$BIN examples/slots.cat`
check "slot-path" "path: /tmp/catscript_slots_demo.txt" "`echo "$out" | sed -n 1p`"
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`"
# ---- ffi: call native code (ctypes/CDLL-like) -----------------------
out=`$BIN examples/ffi.cat`
check "ffi-pow" "pow(2,10) = 1024" "`echo "$out" | sed -n 1p`"
check "ffi-sqrt" "sqrt(144) = 12" "`echo "$out" | sed -n 2p`"
check "ffi-getpid" "getpid > 0: true" "`echo "$out" | sed -n 3p`"
check "ffi-strlen" "strlen: 9" "`echo "$out" | sed -n 4p`"
check "ffi-abs" "abs(-42) = 42" "`echo "$out" | sed -n 5p`"
# ---- ffi edge cases (deterministic) ---------------------------------
out=`$BIN tests/ffi.cat`
check "ffi-badload-empty" "bad load empty: true" "`echo "$out" | sed -n 1p`"
check "ffi-badload-type" "bad load type: empty" "`echo "$out" | sed -n 2p`"
check "ffi-bound" "pow bound: true" "`echo "$out" | sed -n 3p`"
check "ffi-missing-sym" "missing sym empty: true" "`echo "$out" | sed -n 4p`"
check "ffi-badsig" "bad sig empty: true" "`echo "$out" | sed -n 5p`"
check "ffi-badret" "bad rettype empty: true" "`echo "$out" | sed -n 6p`"
check "ffi-pow-2-10" "pow 2,10: 1024" "`echo "$out" | sed -n 7p`"
check "ffi-pow-3-4" "pow 3,4: 81" "`echo "$out" | sed -n 8p`"
check "ffi-floor" "floor 3.7: 3" "`echo "$out" | sed -n 9p`"
check "ffi-sqrt-144" "sqrt 144: 12" "`echo "$out" | sed -n 10p`"
check "ffi-abs-neg" "abs -42: 42" "`echo "$out" | sed -n 11p`"
check "ffi-strlen-abc" "strlen abc: 3" "`echo "$out" | sed -n 12p`"
check "ffi-strlen-empty" "strlen empty: 0" "`echo "$out" | sed -n 13p`"
check "ffi-void-empty" "void ret empty: true" "`echo "$out" | sed -n 14p`"
check "ffi-strchr-bound" "strchr bound: true" "`echo "$out" | sed -n 15p`"
check "ffi-lib-callable" "cos 0: 1" "`echo "$out" | sed -n 16p`"
# ---- reflection: attributes() / help() ------------------------------
out=`$BIN examples/reflect.cat`
check "attr-count" "point attrs: 4" "`echo "$out" | sed -n 1p`"
check "attr-x" " - x" "`echo "$out" | sed -n 2p`"
check "attr-y" " - y" "`echo "$out" | sed -n 3p`"
check "attr-name" " - name" "`echo "$out" | sed -n 4p`"
check "attr-doc" " - _doc_" "`echo "$out" | sed -n 5p`"
check "attr-method" "animal has speak: true" "`echo "$out" | sed -n 6p`"
check "help-doc" "A 2D point with x, y coordinates." "`echo "$out" | sed -n 8p`"
check "help-native" "print(a, ...) -- escreve os argumentos em stdout." "`echo "$out" | sed -n 10p`"
# ---- functions are objects too (member table, _doc_) ----------------
out=`$BIN tests/functions_as_objects.cat`
check "fn-call" "call: 5" "`echo "$out" | sed -n 1p`"
check "fn-name" "name: add" "`echo "$out" | sed -n 2p`"
check "fn-doc" "doc: add(a, b) -- soma dois numeros." "`echo "$out" | sed -n 3p`"
check "fn-attr" "attr: _doc_" "`echo "$out" | sed -n 4p`"
check "fn-method-doc" "method doc: add(a, b) on Calc." "`echo "$out" | sed -n 5p`"
check "fn-method-call" "method call: 9" "`echo "$out" | sed -n 6p`"
check "fn-help" "add(a, b) -- soma dois numeros." "`echo "$out" | sed -n 7p`"
# ---- _repr_ protocol -------------------------------------------------
out=`$BIN tests/repr.cat`
check "repr-value" "object: a custom point" "`echo "$out" | sed -n 1p`"
check "repr-method" "point: Point(3, 4)" "`echo "$out" | sed -n 2p`"
check "repr-int" "int: 42" "`echo "$out" | sed -n 3p`"
check "repr-str" "str: hi" "`echo "$out" | sed -n 4p`"
check "repr-bool" "bool: true" "`echo "$out" | sed -n 5p`"
check "repr-list" "list: [a, b, _repr_]" "`echo "$out" | sed -n 6p`"
# ---- REPL: persistent state + echo ----------------------------------
out=`printf 'x = 50\nx\nx + 5\n' | $BIN | tr '>' '\n' | grep -oE '[0-9]+'`
check "repl-echo" "50" "`echo "$out" | sed -n 1p`"
check "repl-state" "55" "`echo "$out" | sed -n 2p`"
# ---- selectors: a.b (with self) / a::b (no self) ----------------------
out=`$BIN tests/selectors.cat`
check "sel-self" "with self: 7" "`echo "$out" | sed -n 1p`"
check "sel-field" "field x: 10" "`echo "$out" | sed -n 2p`"
check "sel-static" "static: 42" "`echo "$out" | sed -n 3p`"
check "sel-method" "self=obj" "`echo "$out" | sed -n 4p`"
check "sel-noself" "static no self: 5" "`echo "$out" | sed -n 5p`"
# ---- GPU bindings (GLFW + Vulkan) ------------------------------------
if command -v xvfb-run >/dev/null 2>&1 && [ -f vendor/lib/libglfw.so.3 ]; then
out=`LD_LIBRARY_PATH=vendor/lib xvfb-run -a $BIN tests/glfw.cat 2>/dev/null`
check "glfw-version" "glfw: 3 4 0" "`echo "$out" | sed -n 1p`"
check "glfw-init" "init: 1" "`echo "$out" | sed -n 2p`"
check "glfw-window" "window: true" "`echo "$out" | sed -n 3p`"
check "glfw-fb" "framebuffer: 320 240" "`echo "$out" | sed -n 4p`"
else
echo "skip - glfw tests (needs xvfb-run + vendor/lib/libglfw.so.3)"
fi
out=`$BIN tests/vulkan.cat 2>/dev/null`
check "vk-instance" "create_instance: true" "`echo "$out" | sed -n 1p`"
check "vk-devices" "devices > 0: true" "`echo "$out" | sed -n 2p`"
# ---- memory buffer ----------------------------------------------------
out=`$BIN tests/memory.cat`
check "mem-u32" "u32: 1234" "`echo "$out" | sed -n 1p`"
check "mem-u64" "u64: 9999999999" "`echo "$out" | sed -n 2p`"
check "mem-text" "text: hi" "`echo "$out" | sed -n 3p`"
check "mem-size" "size: 16" "`echo "$out" | sed -n 4p`"
check "mem-f32" "f32: 3.5" "`echo "$out" | sed -n 5p`"
check "mem-zero" "zero: 0" "`echo "$out" | sed -n 7p`"
check "mem-oob" "caught oob" "`echo "$out" | sed -n 10p`"
out=`printf 'print(0xAB)\nprint(0b1010)\nprint(0o17)\n' | $BIN | grep -oE '[0-9]+'`
check "hex" "171" "`echo "$out" | sed -n 1p`"
check "bin" "10" "`echo "$out" | sed -n 2p`"
check "oct" "15" "`echo "$out" | sed -n 3p`"
# ---- imports: `use name` ---------------------------------------------
out=`$BIN tests/import.cat`
check "import-name" "double 21 = 42" "`echo "$out" | sed -n 1p`"
check "import-sq" "square 7 = 49" "`echo "$out" | sed -n 2p`"
check "import-io" "join: a-b-c" "`echo "$out" | sed -n 3p`"
check "import-path" "abs(-5) = 5" "`echo "$out" | sed -n 4p`"
# ---- the `module` global ---------------------------------------------
out=`$BIN tests/module.cat`
check "module-name" "name: module.cat" "`echo "$out" | sed -n 1p`"
check "module-dir" "dir: tests" "`echo "$out" | sed -n 2p`"
check "module-mut" "extra: tagged" "`echo "$out" | sed -n 3p`"
check "module-imp" "lib name: module_lib.cat" "`echo "$out" | sed -n 4p`"
check "module-impdir" "lib dir: tests" "`echo "$out" | sed -n 5p`"
check "module-back" "back: module.cat" "`echo "$out" | sed -n 6p`"
check "module-kept" "extra kept: tagged" "`echo "$out" | sed -n 7p`"
# ---- bytecode injection (fc_fn) --------------------------------------
out=`$BIN tests/bc.cat`
check "bc-type" "type: function" "`echo "$out" | sed -n 1p`"
check "bc-name" "name: add" "`echo "$out" | sed -n 2p`"
check "bc-call" "call: 42" "`echo "$out" | sed -n 3p`"
check "bc-call2" "call2: 3" "`echo "$out" | sed -n 4p`"
check "bc-doc" "doc: soma dois numeros" "`echo "$out" | sed -n 5p`"
check "bc-hof" "hof: 101" "`echo "$out" | sed -n 6p`"
# ---- 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/traits.cat`
check "trait-default" "hello, Marcel" "`echo "$out" | sed -n 1p`"
check "trait-second" "HELLO, Marcel" "`echo "$out" | sed -n 2p`"
check "trait-is" "person is Greeter: true" "`echo "$out" | sed -n 3p`"
check "trait-override" "beep R2" "`echo "$out" | sed -n 4p`"
check "trait-robot" "robot is Greeter: true" "`echo "$out" | sed -n 6p`"
check "trait-sized" "sized: true" "`echo "$out" | sed -n 7p`"
check "trait-sized-fn" "implements Sized: true" "`echo "$out" | sed -n 8p`"
out=`$BIN tests/generics.cat`
check "generic-box" "box: hi T" "`echo "$out" | sed -n 1p`"
check "generic-iface" "vec: 3 true" "`echo "$out" | sed -n 2p`"
check "generic-pair" "pair: x 9 K,V" "`echo "$out" | sed -n 3p`"
out=`$BIN tests/aot.cat`
exp=`$BIN tests/aot.cat`
$BIN --build tests/aot.cat build/tests/aot_native >/dev/null 2>&1
if [ -x build/tests/aot_native ]; then
got=`build/tests/aot_native`
if [ "$exp" = "$got" ]; then
check "aot-native" "interpreter == native" "interpreter == native"
else
check "aot-native" "interpreter == native" "diverged: $got"
fi
else
check "aot-native" "native binary built" "missing"
fi
out=`$BIN tests/comptime.cat`
check "comptime-expr" "ct-expr: 42" "`echo "$out" | sed -n 1p`"
check "comptime-text" "ct-text: hi there" "`echo "$out" | sed -n 2p`"
check "comptime-stmt" "after-ct" "`echo "$out" | sed -n 3p`"
out=`$BIN tests/types.cat`
check "types-ok" "ok: 42 3.5 hi true" "`echo "$out" | sed -n 1p`"
check "types-widen" "widen: 7" "`echo "$out" | sed -n 2p`"
check "types-opaque" "opaque: 42 empty" "`echo "$out" | sed -n 3p`"
check "types-ret" "ret: 5" "`echo "$out" | sed -n 4p`"
# type hints are CHECKED where both sides are concrete
printf 'local x : int = "bad"\n' > /tmp/catscript_badtype.cat
if $BIN /tmp/catscript_badtype.cat >/dev/null 2>&1
then
check "types-reject" "reject" "accepted"
else
check "types-reject" "reject" "reject"
fi
rm -f /tmp/catscript_badtype.cat
out=`$BIN tests/interface.cat`
check "iface-is-sub" "circle is Drawable: true" "`echo "$out" | sed -n 1p`"
check "iface-is-inherited" "circle is Shape: true" "`echo "$out" | sed -n 2p`"
check "iface-method" "draw circle" "`echo "$out" | sed -n 3p`"
check "iface-duck" "blob is Shape: true" "`echo "$out" | sed -n 6p`"
check "iface-missing" "line is Shape: false" "`echo "$out" | sed -n 8p`"
check "iface-fn" "implements(c, Shape): true" "`echo "$out" | sed -n 9p`"
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
# the native x86_64 backend must AGREE with the interpreter
cat > /tmp/cat_nat_eq.cat <<'EOCAT'
local i = 0
local acc = 0
while i < 5 do
acc = acc + i * 2
i = i + 1
end
print("acc", acc)
print("cmp", 3 < 4, "a" == "a")
local t = {}
t.x = 7
t["y"] = 8
print("t", t.x + t.y)
print("hi " .. "there")
EOCAT
want=`$BIN /tmp/cat_nat_eq.cat 2>&1`
if $BIN --backend x86_64 --build /tmp/cat_nat_eq.cat /tmp/cat_nat_eq_bin >/dev/null 2>&1 && [ -x /tmp/cat_nat_eq_bin ]
then
got=`/tmp/cat_nat_eq_bin 2>&1`
if [ "$got" = "$want" ]; then
check "nat-x86_64-eq" "match" "match"
else
check "nat-x86_64-eq" "match" "differs"
fi
else
check "nat-x86_64-eq" "skip" "skip"
fi
rm -f /tmp/cat_nat_eq.cat /tmp/cat_nat_eq_bin
echo ""
echo "$pass passed, $fail failed"
[ "$fail" -eq 0 ]