Files
l2/tests/integration_core.sl

192 lines
2.3 KiB
Plaintext
Raw Normal View History

2026-01-08 13:15:27 +01:00
import ../stdlib/stdlib.sl
import ../stdlib/io.sl
import ../fn.sl
2025-12-06 16:30:58 +01:00
2025-12-06 17:24:30 +01:00
:asm mem-slot {
lea rax, [rel print_buf]
sub r12, 8
mov [r12], rax
}
;
macro square
dup *
;
macro defconst 2
word $1
$2
end
;
macro defadder 3
word $1
$2 $3 +
end
;
defconst MAGIC 99
defadder add13 5 8
struct: Point
field x 8
field y 8
;struct
2025-12-08 18:14:52 +01:00
extend-syntax
2025-12-11 20:07:10 +01:00
fn fancy_add(int a, int b){
return (a + b) * b;
2025-12-08 18:14:52 +01:00
}
word test-add
2025-12-19 20:23:26 +01:00
5 7 + puti cr
end
2025-12-06 16:30:58 +01:00
word test-sub
2025-12-19 20:23:26 +01:00
10 3 - puti cr
end
2025-12-06 16:30:58 +01:00
word test-mul
2025-12-19 20:23:26 +01:00
6 7 * puti cr
end
2025-12-06 16:30:58 +01:00
word test-div
2025-12-19 20:23:26 +01:00
84 7 / puti cr
end
2025-12-06 16:30:58 +01:00
word test-mod
2025-12-19 20:23:26 +01:00
85 7 % puti cr
end
2025-12-06 16:30:58 +01:00
word test-drop
2025-12-19 20:23:26 +01:00
10 20 drop puti cr
end
2025-12-06 16:30:58 +01:00
word test-dup
2025-12-19 20:23:26 +01:00
11 dup + puti cr
end
2025-12-06 16:30:58 +01:00
word test-swap
2025-12-19 20:23:26 +01:00
2 5 swap - puti cr
end
2025-12-06 16:30:58 +01:00
word test-store
2025-12-06 17:24:30 +01:00
mem-slot dup
123 swap !
2025-12-19 20:23:26 +01:00
@ puti cr
end
2025-12-06 17:24:30 +01:00
word test-mmap
2025-12-06 17:24:30 +01:00
0 # addr hint (NULL)
4096 # length (page)
3 # prot (PROT_READ | PROT_WRITE)
34 # flags (MAP_PRIVATE | MAP_ANON)
-1 # fd (ignored for MAP_ANON)
0 # offset
mmap
dup
1337 swap !
dup
2025-12-19 20:23:26 +01:00
@ puti cr
2025-12-06 17:24:30 +01:00
4096 munmap drop
end
2025-12-06 17:24:30 +01:00
word test-macro
2025-12-19 20:23:26 +01:00
9 square puti cr
MAGIC puti cr
add13 puti cr
end
word test-if
5 5 == if
2025-12-19 20:23:26 +01:00
111 puti cr
else
2025-12-19 20:23:26 +01:00
222 puti cr
2026-01-03 09:04:50 +00:00
end
end
word test-else-if
2
dup 1 == if
2025-12-19 20:23:26 +01:00
50 puti cr
else
dup 2 == if
2025-12-19 20:23:26 +01:00
60 puti cr
else
2025-12-19 20:23:26 +01:00
70 puti cr
2026-01-03 09:04:50 +00:00
end
end
drop
end
word test-for
0
5 for
1 +
2026-01-03 09:04:50 +00:00
end
2025-12-19 20:23:26 +01:00
puti cr
end
word test-for-zero
123
0 for
drop
2026-01-03 09:04:50 +00:00
end
2025-12-19 20:23:26 +01:00
puti cr
end
word test-struct
mem-slot
dup 111 swap Point.x!
dup 222 swap Point.y!
2025-12-19 20:23:26 +01:00
dup Point.x@ puti cr
Point.y@ puti cr
Point.size puti cr
end
word test-cmp
2025-12-19 20:23:26 +01:00
5 5 == puti cr
5 4 == puti cr
5 4 != puti cr
4 4 != puti cr
3 5 < puti cr
5 3 < puti cr
5 3 > puti cr
3 5 > puti cr
5 5 <= puti cr
6 5 <= puti cr
5 5 >= puti cr
4 5 >= puti cr
end
word test-c-fn
2025-12-08 18:14:52 +01:00
3
7
fancy_add()
2025-12-19 20:23:26 +01:00
puti cr
end
2025-12-08 18:14:52 +01:00
word main
2025-12-06 16:30:58 +01:00
test-add
test-sub
test-mul
test-div
test-mod
test-drop
test-dup
test-swap
2025-12-06 17:24:30 +01:00
test-store
test-mmap
test-macro
test-if
test-else-if
test-for
test-for-zero
test-cmp
test-struct
2025-12-08 18:14:52 +01:00
test-c-fn
2025-12-06 16:30:58 +01:00
0
end