added support for raw l2 statementrs inside 'fn' decls

This commit is contained in:
IgorCielniak
2025-12-14 17:02:09 +01:00
parent 566a438242
commit 1dd1ba377b
7 changed files with 93 additions and 25 deletions

BIN
a.out

Binary file not shown.

View File

@@ -362,7 +362,34 @@ word_rpick:
sub r12, 8 sub r12, 8
mov [r12], rax mov [r12], rax
ret ret
word_foo:
call word__3er
call word__3er
; push 1
sub r12, 8
mov qword [r12], 1
call word_puts
; push 0
sub r12, 8
mov qword [r12], 0
call word_rpick
; push 1
sub r12, 8
mov qword [r12], 1
call word_rpick
call word__2b
call word_rdrop
call word_rdrop
ret
word_main: word_main:
; push 3
sub r12, 8
mov qword [r12], 3
; push 2
sub r12, 8
mov qword [r12], 2
call word_foo
call word_puts
; push 0 ; push 0
sub r12, 8 sub r12, 8
mov qword [r12], 0 mov qword [r12], 0

BIN
build/t.o

Binary file not shown.

View File

@@ -594,18 +594,18 @@ word_test_2dif:
mov rax, [r12] mov rax, [r12]
add r12, 8 add r12, 8
test rax, rax test rax, rax
jz L_if_false_34 jz L_if_false_40
; push 111 ; push 111
sub r12, 8 sub r12, 8
mov qword [r12], 111 mov qword [r12], 111
call word_puts call word_puts
jmp L_if_end_35 jmp L_if_end_41
L_if_false_34: L_if_false_40:
; push 222 ; push 222
sub r12, 8 sub r12, 8
mov qword [r12], 222 mov qword [r12], 222
call word_puts call word_puts
L_if_end_35: L_if_end_41:
ret ret
word_test_2delse_2dif: word_test_2delse_2dif:
; push 2 ; push 2
@@ -619,13 +619,13 @@ word_test_2delse_2dif:
mov rax, [r12] mov rax, [r12]
add r12, 8 add r12, 8
test rax, rax test rax, rax
jz L_if_false_36 jz L_if_false_42
; push 50 ; push 50
sub r12, 8 sub r12, 8
mov qword [r12], 50 mov qword [r12], 50
call word_puts call word_puts
jmp L_if_end_37 jmp L_if_end_43
L_if_false_36: L_if_false_42:
call word_dup call word_dup
; push 2 ; push 2
sub r12, 8 sub r12, 8
@@ -634,19 +634,19 @@ L_if_false_36:
mov rax, [r12] mov rax, [r12]
add r12, 8 add r12, 8
test rax, rax test rax, rax
jz L_if_false_38 jz L_if_false_44
; push 60 ; push 60
sub r12, 8 sub r12, 8
mov qword [r12], 60 mov qword [r12], 60
call word_puts call word_puts
jmp L_if_end_39 jmp L_if_end_45
L_if_false_38: L_if_false_44:
; push 70 ; push 70
sub r12, 8 sub r12, 8
mov qword [r12], 70 mov qword [r12], 70
call word_puts call word_puts
L_if_end_39: L_if_end_45:
L_if_end_37: L_if_end_43:
call word_drop call word_drop
ret ret
word_test_2dfor: word_test_2dfor:
@@ -659,10 +659,10 @@ word_test_2dfor:
mov rax, [r12] mov rax, [r12]
add r12, 8 add r12, 8
cmp rax, 0 cmp rax, 0
jle L_for_end_41 jle L_for_end_47
sub r13, 8 sub r13, 8
mov [r13], rax mov [r13], rax
L_for_loop_40: L_for_loop_46:
; push 1 ; push 1
sub r12, 8 sub r12, 8
mov qword [r12], 1 mov qword [r12], 1
@@ -670,9 +670,9 @@ L_for_loop_40:
mov rax, [r13] mov rax, [r13]
dec rax dec rax
mov [r13], rax mov [r13], rax
jg L_for_loop_40 jg L_for_loop_46
add r13, 8 add r13, 8
L_for_end_41: L_for_end_47:
call word_puts call word_puts
ret ret
word_test_2dfor_2dzero: word_test_2dfor_2dzero:
@@ -685,17 +685,17 @@ word_test_2dfor_2dzero:
mov rax, [r12] mov rax, [r12]
add r12, 8 add r12, 8
cmp rax, 0 cmp rax, 0
jle L_for_end_43 jle L_for_end_49
sub r13, 8 sub r13, 8
mov [r13], rax mov [r13], rax
L_for_loop_42: L_for_loop_48:
call word_drop call word_drop
mov rax, [r13] mov rax, [r13]
dec rax dec rax
mov [r13], rax mov [r13], rax
jg L_for_loop_42 jg L_for_loop_48
add r13, 8 add r13, 8
L_for_end_43: L_for_end_49:
call word_puts call word_puts
ret ret
word_test_2dstruct: word_test_2dstruct:

Binary file not shown.

43
fn.sl
View File

@@ -128,6 +128,46 @@ compile-only
; ;
compile-only compile-only
: fn-filter-raw-body # bodyLexemes -- tokens
list-new swap # out body
begin
dup list-empty? if
drop # out
exit
then
list-pop-front # out body' tok
swap >r # out tok (r: body')
dup "return" string= if
drop
r>
continue
then
dup ";" string= if
drop
r>
continue
then
list-append # out'
r> # out' body'
continue
again
;
compile-only
: fn-body->tokens # bodyLexemes -- tokens
dup list-length 0 == if "empty function body" parse-error then
dup 0 list-get token-lexeme "return" string= if
fn-validate-body # expr
shunt # postfix
exit
then
fn-filter-raw-body
dup list-length 0 == if "empty function body" parse-error then
;
compile-only
: fn-emit-prologue # params out -- params out : fn-emit-prologue # params out -- params out
over list-length # params out n over list-length # params out n
begin begin
@@ -322,8 +362,7 @@ compile-only
fn-collect-body # params bodyTokens fn-collect-body # params bodyTokens
swap >r # bodyTokens (r: params) swap >r # bodyTokens (r: params)
fn-lexemes-from-tokens # lexemes fn-lexemes-from-tokens # lexemes
fn-validate-body # expr fn-body->tokens # tokens
shunt # postfix
r> # postfix params r> # postfix params
fn-build-body # body fn-build-body # body
r> drop # drop name string r> drop # drop name string

8
t.sl
View File

@@ -1,13 +1,15 @@
import stdlib.sl import stdlib.sl
import fn.sl import fn.sl
fn foo(int a){ fn foo(int a, int b){
return a + 1; 1
puts
return a b +;
} }
: main : main
extend-syntax extend-syntax
foo(1) foo(3, 2)
puts puts
0 0
; ;