Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroflag committed Dec 27, 2024
1 parent d0b9ea5 commit 5d56a03
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 205 deletions.
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
LUA = lua5.4
TEST_FILES = $(wildcard test_*.lua)
TEST_LUA_FILES = $(wildcard test_*.lua)
TEST_EQX_FILES = $(wildcard test_*.eqx)
EQUINOX = equinox.lua
REPL = repl.lua

ifeq ($(shell command -v $(LUA) 2>/dev/null),)
Expand All @@ -11,10 +13,17 @@ all: test

test:
@echo "Running Lua tests..."
@for file in $(TEST_FILES); do \
@for file in $(TEST_LUA_FILES); do \
echo "Running $$file..."; \
$(LUA) $$file || exit 1; \
done

@echo "Running Equinox tests..."
@for file in $(TEST_EQX_FILES); do \
echo "Running $$file..."; \
$(LUA) $(EQUINOX) $$file || exit 1; \
done

@echo "All tests passed!"

repl:
Expand Down
1 change: 1 addition & 0 deletions dict.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dict.defword("or", "ops._or", false)
dict.defword("..", "ops.concat", false)
dict.defword(">a", "ops.to_aux", false)
dict.defword("a>", "ops.from_aux", false)
dict.defword("assert", "ops.assert", false)
dict.defword("if", "macros._if", true)
dict.defword("then", "macros._then", true)
dict.defword("else", "macros._else", true)
Expand Down
11 changes: 11 additions & 0 deletions equinox.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
compiler = require("compiler")

if #arg < 1 then
print("Usage: lua equinox.lua <script.eqx>")
os.exit(1)
end

local filename = arg[1]

compiler:eval_file("lib.eqx")
compiler:eval_file(filename)
29 changes: 28 additions & 1 deletion macros.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,34 @@ end

function macros.colon(compiler)
local alias = compiler:word()
local name = "w_" .. string.gsub(alias, "-", "_minus_")
local name = "w_" ..
alias:gsub("-", "_mi_")
:gsub("%+", "_pu_")
:gsub("%%", "_pe_")
:gsub("/", "_fs_")
:gsub("\\", "_bs_")
:gsub("~", "_ti_")
:gsub("#", "_hs_")
:gsub("%*", "_sr_")
:gsub(";", "_sc_")
:gsub("&", "_an_")
:gsub("|", "_or_")
:gsub("@", "_at_")
:gsub("`", "_bt_")
:gsub("=", "_eq_")
:gsub("'", "_sq_")
:gsub('"', "_dq_")
:gsub("?", "_qe_")
:gsub("!", "_ex_")
:gsub(",", "_ca_")
:gsub(":", "_cm_")
:gsub("%.", "_dt_")
:gsub("%{", "_c1_")
:gsub("%}", "_c2_")
:gsub("%[", "_b1_")
:gsub("%]", "_b2_")
:gsub("%(", "_p1_")
:gsub("%(", "_p2_")
compiler:defword(alias, name, false)
compiler:emit_line("function " .. name .. "()")
end
Expand Down
203 changes: 1 addition & 202 deletions test_compiler.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local compiler = require("compiler")

compiler.trace = true
compiler:eval_file("lib.eqx")

function assert_tos(result, code)
Expand All @@ -13,207 +12,7 @@ function assert_tos(result, code)
stack:pop()
end

-- arithmetics - add
assert_tos(3, "1 2 +")
assert_tos(33, "0 33 +")
assert_tos(-6, "-2 -4 +")
assert_tos(-17, "-21 4 +")
assert_tos(140, "145 -5 +")
assert_tos(5.8, "3.3 2.5 +")
-- arithmetics - sub
assert_tos(1, "2 1 -")
assert_tos(-33, "0 33 -")
assert_tos(2, "-2 -4 -")
assert_tos(-25, "-21 4 -")
assert_tos(150, "145 -5 -")
assert_tos(10, "11.5 1.5 -")
-- arithmetics - mul
assert_tos(6, "2 3 *")
assert_tos(0, "0 33 *")
assert_tos(8, "-2 -4 *")
assert_tos(-300, "100 -3 *")
assert_tos(10, "2.5 4 *")
assert_tos(6.25, "2.5 2.5 *")
-- arithmetics - div
assert_tos(5, "10 2 /")
assert_tos(0.2, "2 10 /")
assert_tos(2, "-4 -2 /")
assert_tos(-2, "6 -3 /")
assert_tos(2.5, "10 4 /")
assert_tos(1, "2.5 2.5 /")
-- arithmetics - eq
assert_tos(true, "5 5 =")
assert_tos(false, "6 5 =")
assert_tos(true, "-6 -6 =")
assert_tos(false, "6 -6 =")
-- arithmetics - jeq
assert_tos(false, "5 5 !=")
assert_tos(true, "6 5 !=")
assert_tos(false, "-6 -6 !=")
assert_tos(true, "6 -6 !=")
-- arithmetics - lt
assert_tos(false, "5 5 <")
assert_tos(false, "6 5 <")
assert_tos(false, "-6 -6 <")
assert_tos(false, "6 -6 <")
assert_tos(true, "3 5 <")
assert_tos(true, "-1 5 <")
assert_tos(true, "-6 -2 <")
-- booleans - not
assert_tos(true, "false not")
assert_tos(false, "true not")
assert_tos(true, "true not not")
-- booleans - and
assert_tos(true, "true true and")
assert_tos(false, "true false and")
assert_tos(false, "false true and")
assert_tos(false, "false false and")
-- booleans - or
assert_tos(true, "true true or")
assert_tos(true, "true false or")
assert_tos(true, "false true or")
assert_tos(false, "false false or")
-- arithmetics - gt
assert_tos(false, "5 5 >")
assert_tos(true, "6 5 >")
assert_tos(false, "-6 -6 >")
assert_tos(true, "6 -6 >")
assert_tos(false, "3 5 >")
assert_tos(false, "-1 5 >")
assert_tos(false, "-6 -2 >")
-- arithmetics - lte
assert_tos(true, "5 5 <=")
assert_tos(false, "6 5 <=")
assert_tos(true, "-6 -6 <=")
assert_tos(false, "6 -6 <=")
assert_tos(true, "3 5 <=")
assert_tos(true, "-1 5 <=")
assert_tos(true, "-6 -2 <=")
-- arithmetics - gte
assert_tos(true, "5 5 >=")
assert_tos(true, "6 5 >=")
assert_tos(true, "-6 -6 >=")
assert_tos(true, "6 -6 >=")
assert_tos(false, "3 5 >=")
assert_tos(false, "-1 5 >=")
assert_tos(false, "-6 -2 >=")

-- stack - dup
assert_tos(10, "5 5 +")
-- stack - swap
assert_tos(3, "7 10 swap -")
-- stack - over
assert_tos(2, "1 2 over - +")
-- stack - rot
assert_tos(1, "1 2 3 rot nip nip")
assert_tos(3, "1 2 3 rot drop nip")
assert_tos(2, "1 2 3 rot drop drop")
-- stack - -rot
assert_tos(2, "1 2 3 -rot nip nip")
assert_tos(1, "1 2 3 -rot drop nip")
assert_tos(3, "1 2 3 -rot drop drop")
-- stack - tuck
assert_tos(2, "1 2 tuck nip nip")
assert_tos(1, "1 2 tuck drop nip")
assert_tos(2, "1 2 tuck drop drop")

-- control if
assert_tos(8, "1 2 < if 8 then")
assert_tos(4, "1 2 > if 8 else 4 then")

-- control begin until
assert_tos(2048, "2 10 begin 1 - swap 2 * swap dup 0 = until drop")

-- def :
assert_tos(42, ": tst 42 ; tst")
assert_tos(6, ": dbl dup + ; 3 dbl")

compiler:eval(": min ( n n -- n ) 2dup < if drop else nip then ;")
assert_tos(4, "4 6 min")
assert_tos(2, "5 2 min")

-- max
compiler:eval(": max ( n n -- n ) 2dup < if nip else drop then ;")
assert_tos(6, "4 6 max")
assert_tos(5, "5 2 max")

assert_tos(5, [[
\ 1 2 +
\ 1 1 *
\ 3 4 +
3 2 +
\ this is a comment
]])

assert_tos(5, [[
( 1 2 +
1 1 *
3 4 + )
3 2 +
( this is a comment)
]])

-- var local
assert_tos(22, [[
var v1
var v2
10 -> v1
12 -> v2
v1 v2 +
]])

-- var local
assert_tos(-3, [[
var v1
var v2
10 -> v1 v1 -> v2
3 v2 + -> v2 ( 13 = v2 )
v1 v2 - ( 10 13 - )
]])

-- var global
assert_tos(12, [[
3 -> v1
4 -> v2
v1 v2 *
]])

assert_tos(3, [[:xyz string.len/1]])

assert_tos(4, [["asdf" string.len/1]])
assert_tos(9, [["asdf jkle" string.len/1]])
assert_tos(10, [["asdf jkle " string.len/1]])
assert_tos(10, [[" asdf jkle" string.len/1]])
assert_tos(11, [[" asdf jkle " string.len/1]])
assert_tos(0, [["" string.len/1]])
assert_tos(1, [[" " string.len/1]])
assert_tos(2, [[" " string.len/1]])
assert_tos(14, [[" asdf jkle " string.len/1]])

assert_tos(" abc xyz ", [[
" abc "
" xyz "
..
]])

assert_tos(256, [[
8 2 math.pow/2
]])

assert_tos(502, [[
502 1002 math.min/2
]])
assert_tos(1002, [[
502 1002 math.max/2
]])

assert_tos(502, [[
502 1002 math.min/2
]])

assert_tos(0, [[
1 2 >a 3 a> - -
]])
assert_tos(2, "1 2 math.max/2")

local status, result = pcall(
function()
Expand Down
Loading

0 comments on commit 5d56a03

Please sign in to comment.