changed the way that functions are defined
This commit is contained in:
@@ -2,22 +2,22 @@ import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
import ../stdlib/mem.sl
|
||||
|
||||
: test-mem-alloc
|
||||
word test-mem-alloc
|
||||
4096 alloc dup 1337 swap ! # allocate 4096 bytes, store 1337 at start
|
||||
dup @ puti cr # print value at start
|
||||
4096 free # free the memory
|
||||
;
|
||||
end
|
||||
|
||||
struct: Point
|
||||
field x 8
|
||||
field y 8
|
||||
;struct
|
||||
|
||||
: main
|
||||
word main
|
||||
32 alloc # allocate 32 bytes (enough for a Point struct)
|
||||
dup 111 swap Point.x!
|
||||
dup 222 swap Point.y!
|
||||
dup Point.x@ puti cr
|
||||
Point.y@ puti cr
|
||||
32 free # free the memory
|
||||
;
|
||||
end
|
||||
@@ -2,14 +2,14 @@ import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
import ../fn.sl
|
||||
|
||||
: main
|
||||
word main
|
||||
2 40 +
|
||||
puti cr
|
||||
extend-syntax
|
||||
foo(1, 2)
|
||||
puti cr
|
||||
0
|
||||
;
|
||||
end
|
||||
|
||||
fn foo(int a, int b){
|
||||
return a + b;
|
||||
|
||||
@@ -2,7 +2,7 @@ import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
import ../stdlib/debug.sl
|
||||
|
||||
: main
|
||||
word main
|
||||
1 1 2dup 2dup puti cr puti cr
|
||||
+
|
||||
dup puti cr
|
||||
@@ -15,12 +15,12 @@ import ../stdlib/debug.sl
|
||||
r> 3 + puti
|
||||
" numbers printed from the fibonaci sequence" puts
|
||||
0
|
||||
;
|
||||
end
|
||||
|
||||
: main2
|
||||
word main2
|
||||
1 2 while over 100 < do
|
||||
over puti cr
|
||||
swap over +
|
||||
end
|
||||
;
|
||||
end
|
||||
|
||||
|
||||
@@ -9,21 +9,21 @@ import ../fn.sl
|
||||
}
|
||||
;
|
||||
|
||||
macro: square
|
||||
macro square
|
||||
dup *
|
||||
;macro
|
||||
;
|
||||
|
||||
macro: defconst 2
|
||||
: $1
|
||||
macro defconst 2
|
||||
word $1
|
||||
$2
|
||||
;
|
||||
;macro
|
||||
end
|
||||
;
|
||||
|
||||
macro: defadder 3
|
||||
: $1
|
||||
macro defadder 3
|
||||
word $1
|
||||
$2 $3 +
|
||||
;
|
||||
;macro
|
||||
end
|
||||
;
|
||||
|
||||
defconst MAGIC 99
|
||||
defadder add13 5 8
|
||||
@@ -39,46 +39,46 @@ fn fancy_add(int a, int b){
|
||||
return (a + b) * b;
|
||||
}
|
||||
|
||||
: test-add
|
||||
word test-add
|
||||
5 7 + puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: test-sub
|
||||
word test-sub
|
||||
10 3 - puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: test-mul
|
||||
word test-mul
|
||||
6 7 * puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: test-div
|
||||
word test-div
|
||||
84 7 / puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: test-mod
|
||||
word test-mod
|
||||
85 7 % puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: test-drop
|
||||
word test-drop
|
||||
10 20 drop puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: test-dup
|
||||
word test-dup
|
||||
11 dup + puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: test-swap
|
||||
word test-swap
|
||||
2 5 swap - puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: test-store
|
||||
word test-store
|
||||
mem-slot dup
|
||||
123 swap !
|
||||
@ puti cr
|
||||
;
|
||||
end
|
||||
|
||||
|
||||
: test-mmap
|
||||
word test-mmap
|
||||
0 # addr hint (NULL)
|
||||
4096 # length (page)
|
||||
3 # prot (PROT_READ | PROT_WRITE)
|
||||
@@ -91,23 +91,23 @@ fn fancy_add(int a, int b){
|
||||
dup
|
||||
@ puti cr
|
||||
4096 munmap drop
|
||||
;
|
||||
end
|
||||
|
||||
: test-macro
|
||||
word test-macro
|
||||
9 square puti cr
|
||||
MAGIC puti cr
|
||||
add13 puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: test-if
|
||||
word test-if
|
||||
5 5 == if
|
||||
111 puti cr
|
||||
else
|
||||
222 puti cr
|
||||
end
|
||||
;
|
||||
end
|
||||
|
||||
: test-else-if
|
||||
word test-else-if
|
||||
2
|
||||
dup 1 == if
|
||||
50 puti cr
|
||||
@@ -119,34 +119,34 @@ fn fancy_add(int a, int b){
|
||||
end
|
||||
end
|
||||
drop
|
||||
;
|
||||
end
|
||||
|
||||
: test-for
|
||||
word test-for
|
||||
0
|
||||
5 for
|
||||
1 +
|
||||
end
|
||||
puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: test-for-zero
|
||||
word test-for-zero
|
||||
123
|
||||
0 for
|
||||
drop
|
||||
end
|
||||
puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: test-struct
|
||||
word test-struct
|
||||
mem-slot
|
||||
dup 111 swap Point.x!
|
||||
dup 222 swap Point.y!
|
||||
dup Point.x@ puti cr
|
||||
Point.y@ puti cr
|
||||
Point.size puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: test-cmp
|
||||
word test-cmp
|
||||
5 5 == puti cr
|
||||
5 4 == puti cr
|
||||
5 4 != puti cr
|
||||
@@ -159,16 +159,16 @@ fn fancy_add(int a, int b){
|
||||
6 5 <= puti cr
|
||||
5 5 >= puti cr
|
||||
4 5 >= puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: test-c-fn
|
||||
word test-c-fn
|
||||
3
|
||||
7
|
||||
fancy_add()
|
||||
puti cr
|
||||
;
|
||||
end
|
||||
|
||||
: main
|
||||
word main
|
||||
test-add
|
||||
test-sub
|
||||
test-mul
|
||||
@@ -188,4 +188,4 @@ fn fancy_add(int a, int b){
|
||||
test-struct
|
||||
test-c-fn
|
||||
0
|
||||
;
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
|
||||
: main
|
||||
word main
|
||||
"/tmp/l2_read_file_test.txt"
|
||||
"read_file works\n"
|
||||
write_file drop
|
||||
@@ -33,4 +33,4 @@ import ../stdlib/io.sl
|
||||
"unknown read_file failure" puts
|
||||
dup # file_len file_len file_addr
|
||||
exit # Exit with returned file_len as the program exit code (debug)
|
||||
;
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
|
||||
: main
|
||||
word main
|
||||
1024
|
||||
read_stdin # returns (addr len)
|
||||
dup 0 > if
|
||||
@@ -10,4 +10,4 @@ import ../stdlib/io.sl
|
||||
end
|
||||
"read_stdin failed" puts
|
||||
exit
|
||||
;
|
||||
end
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
|
||||
: main
|
||||
word main
|
||||
"hello from write_buf test\n"
|
||||
write_buf
|
||||
0
|
||||
;
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
|
||||
: main
|
||||
word main
|
||||
"/tmp/l2_write_file_test.txt" # path
|
||||
"hello from write_file test\n" # buffer
|
||||
write_file
|
||||
@@ -14,4 +14,4 @@ import ../stdlib/io.sl
|
||||
"write failed errno=" puts
|
||||
puti cr
|
||||
exit
|
||||
;
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
|
||||
: main
|
||||
word main
|
||||
10
|
||||
while
|
||||
dup 0 >
|
||||
@@ -10,4 +10,4 @@ import ../stdlib/io.sl
|
||||
1 -
|
||||
end
|
||||
drop
|
||||
;
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
|
||||
: main
|
||||
word main
|
||||
0
|
||||
5 for
|
||||
1 +
|
||||
@@ -10,4 +10,4 @@ import ../stdlib/io.sl
|
||||
5 5 == puti cr
|
||||
5 4 == puti cr
|
||||
0
|
||||
;
|
||||
end
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
|
||||
: main
|
||||
word main
|
||||
mem 5 swap !
|
||||
mem 8 + 6 swap !
|
||||
mem @ puti cr
|
||||
mem 8 + @ puti cr
|
||||
;
|
||||
end
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
|
||||
: dup
|
||||
word dup
|
||||
6
|
||||
;
|
||||
end
|
||||
compile-only
|
||||
|
||||
: emit-overridden
|
||||
word emit-overridden
|
||||
"dup" use-l2-ct
|
||||
42
|
||||
dup
|
||||
@@ -17,12 +17,12 @@ compile-only
|
||||
swap
|
||||
list-append
|
||||
inject-tokens
|
||||
;
|
||||
end
|
||||
immediate
|
||||
compile-only
|
||||
|
||||
: main
|
||||
word main
|
||||
emit-overridden
|
||||
puti cr
|
||||
0
|
||||
;
|
||||
end
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
|
||||
: main
|
||||
word main
|
||||
"hello world" puts
|
||||
"line1\nline2" puts
|
||||
"" puts
|
||||
0
|
||||
;
|
||||
end
|
||||
|
||||
1
tests/word_syntax.expected
Normal file
1
tests/word_syntax.expected
Normal file
@@ -0,0 +1 @@
|
||||
7
|
||||
12
tests/word_syntax.sl
Normal file
12
tests/word_syntax.sl
Normal file
@@ -0,0 +1,12 @@
|
||||
import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
|
||||
word add-two
|
||||
+
|
||||
end
|
||||
|
||||
word main
|
||||
3 4 add-two
|
||||
puti cr
|
||||
0
|
||||
end
|
||||
1
tests/word_syntax.test
Normal file
1
tests/word_syntax.test
Normal file
@@ -0,0 +1 @@
|
||||
python main.py tests/word_syntax.sl -o /tmp/word_syntax > /dev/null && /tmp/word_syntax
|
||||
Reference in New Issue
Block a user