Files
tyche/test/code-tests.lua
2026-05-17 10:03:02 -05:00

132 lines
4.3 KiB
Lua

return {
{
name = "VM: basic",
code = [[
.func 0
pushi 2
pushi 3
sum
ret
]],
expected_stack_size = 1,
expected_stack_top = 5,
},
{
name = "VM: integer expressions",
template = [[
.func 0
pushi %d
pushi %d
%s
ret
]],
scenarios = {
{ parameters = { 2, 5, 'sum' }, name = "Sum", expected_stack_top = 7 },
{ parameters = { 2, 5, 'sub' }, name = "Subtraction", expected_stack_top = -3 },
{ parameters = { 2, 5, 'mul' }, name = "Multiplication", expected_stack_top = 10 },
{ parameters = { 20, 3, 'idiv' }, name = "Integer division", expected_stack_top = 6 },
{ parameters = { 5, 5, 'eq' }, name = "Equality", expected_stack_top = 1 },
{ parameters = { 5, 5, 'neq' }, name = "Inequality", expected_stack_top = 0 },
{ parameters = { 4, 5, 'lt' }, name = "Less than", expected_stack_top = 1 },
{ parameters = { 5, 5, 'lt' }, name = "Less than", expected_stack_top = 0 },
{ parameters = { 4, 5, 'lte' }, name = "Less than or equal", expected_stack_top = 1 },
{ parameters = { 5, 5, 'lte' }, name = "Less than or equal", expected_stack_top = 1 },
{ parameters = { 5, 5, 'gt' }, name = "Greater than", expected_stack_top = 0 },
{ parameters = { 5, 5, 'gte' }, name = "Greater than or equal", expected_stack_top = 1 },
{ parameters = { 20, 5, 'and' }, name = "Logical AND", expected_stack_top = 4 },
{ parameters = { 20, 5, 'or' }, name = "Logical OR", expected_stack_top = 21 },
{ parameters = { 20, 5, 'xor' }, name = "Logical XOR", expected_stack_top = 17 },
{ parameters = { 2, 5, 'pow' }, name = "Power", expected_stack_top = 32 },
{ parameters = { 2, 5, 'shl' }, name = "Shift left", expected_stack_top = 64 },
{ parameters = { 20, 3, 'shr' }, name = "Shift right", expected_stack_top = 2},
{ parameters = { 20, 3, 'mod' }, name = "Modulo", expected_stack_top = 2 },
},
},
{
name = "VM: local variables",
code = [[
.func 0
pushv 2 ; local a, b
pushi 3 ; a = 3
set 0
pushi 4 ; b = 4
set 1
dupv 0 ; return a
ret
]],
expected_stack_size = 1,
expected_stack_top = 3,
},
{
name = "VM: functions",
code = [[
.func 0
pushf 1
pushi 2
pushi 3
call 2
ret
.func 1
dupv 0
dupv 1
sub
ret
]],
expected_stack_size = 1,
expected_stack_top = -1,
},
{
name = "VM: jumps (jmp + bnz)",
code = [[
.func 0
jmp @x1 ; 0
pushi 5 ; 3
@x1:
pushi 1 ; 5
bnz @x2 ; 7
pushi 1 ; 10
bz @x3 ; 12
@x2:
pushi 6 ; 15
ret ; 17
@x3:
pushi 7 ; 18
ret ; 20
]],
--debug_bytecode = true,
--decompile = true,
--debug = true,
expected_stack_top = 6,
},
{
name = "VM: jumps (bz)",
code = [[
.func 0
jmp @x1
pushi 5
@x1:
pushi 0
bnz @x2
pushi 0
bz @x3
@x2:
pushi 6
ret
@x3:
pushi 7
ret
]],
expected_stack_top = 7,
},
{
name = "VM: string from const",
code = [[
.const
0: "Hello"
.func 0
pushc 0
ret
]],
expected_stack_top = "Hello"
}
}