unified the block endings
This commit is contained in:
80
fn.sl
80
fn.sl
@@ -1,14 +1,14 @@
|
||||
: call-syntax-rewrite # ( fnameToken -- handled )
|
||||
dup token-lexeme identifier? 0 == if drop 0 exit then
|
||||
peek-token dup nil? if drop drop 0 exit then
|
||||
dup token-lexeme "(" string= 0 == if drop drop 0 exit then
|
||||
dup token-lexeme identifier? 0 == if drop 0 exit end
|
||||
peek-token dup nil? if drop drop 0 exit end
|
||||
dup token-lexeme "(" string= 0 == if drop drop 0 exit end
|
||||
swap >r # stash fnameTok
|
||||
drop # discard peeked '('
|
||||
next-token drop # consume '('
|
||||
list-new # out
|
||||
list-new # out cur
|
||||
begin
|
||||
next-token dup nil? if "unterminated call expression" parse-error then
|
||||
next-token dup nil? if "unterminated call expression" parse-error end
|
||||
dup token-lexeme ")" string= if
|
||||
drop
|
||||
# flush current arg
|
||||
@@ -16,13 +16,13 @@ begin
|
||||
r> list-append # out''
|
||||
inject-tokens
|
||||
1 exit
|
||||
then
|
||||
end
|
||||
dup token-lexeme "," string= if
|
||||
drop
|
||||
list-extend # out'
|
||||
list-new # out' cur
|
||||
continue
|
||||
then
|
||||
end
|
||||
# default: append tok to cur
|
||||
list-append
|
||||
again
|
||||
@@ -39,11 +39,11 @@ compile-only
|
||||
|
||||
|
||||
: fn-op-prec
|
||||
dup "+" string= if drop 1 exit then
|
||||
dup "-" string= if drop 1 exit then
|
||||
dup "*" string= if drop 2 exit then
|
||||
dup "/" string= if drop 2 exit then
|
||||
dup "%" string= if drop 2 exit then
|
||||
dup "+" string= if drop 1 exit end
|
||||
dup "-" string= if drop 1 exit end
|
||||
dup "*" string= if drop 2 exit end
|
||||
dup "/" string= if drop 2 exit end
|
||||
dup "%" string= if drop 2 exit end
|
||||
drop 0
|
||||
;
|
||||
compile-only
|
||||
@@ -59,10 +59,10 @@ compile-only
|
||||
begin
|
||||
over list-length swap >= if # params flag
|
||||
r> exit
|
||||
then
|
||||
end
|
||||
dup >r # params idx (r: idx name)
|
||||
over swap list-get # params elem
|
||||
1 rpick string= if "duplicate parameter names in fn definition" parse-error then
|
||||
1 rpick string= if "duplicate parameter names in fn definition" parse-error end
|
||||
drop # drop comparison flag when no error
|
||||
r> 1 + # params idx+1
|
||||
again
|
||||
@@ -76,18 +76,18 @@ compile-only
|
||||
begin
|
||||
0 rpick lexer-pop token-lexeme # params lex
|
||||
swap drop # params lex (drop returned lexer)
|
||||
dup ")" string= if drop r> exit then
|
||||
dup "int" string= 0 == if "only 'int' parameters are supported in fn definitions" parse-error then
|
||||
dup ")" string= if drop r> exit end
|
||||
dup "int" string= 0 == if "only 'int' parameters are supported in fn definitions" parse-error end
|
||||
drop # params
|
||||
0 rpick lexer-pop token-lexeme # params lexer pname
|
||||
swap drop # params pname
|
||||
dup identifier? 0 == if "invalid parameter name in fn definition" parse-error then
|
||||
dup identifier? 0 == if "invalid parameter name in fn definition" parse-error end
|
||||
fn-check-dup # params pname
|
||||
list-append # params
|
||||
0 rpick lexer-pop token-lexeme # params lexer sep
|
||||
swap drop # params sep
|
||||
dup "," string= if drop continue then
|
||||
dup ")" string= if drop r> exit then
|
||||
dup "," string= if drop continue end
|
||||
dup ")" string= if drop r> exit end
|
||||
"expected ',' or ')' in parameter list" parse-error
|
||||
again
|
||||
;
|
||||
@@ -106,7 +106,7 @@ compile-only
|
||||
begin
|
||||
0 rpick list-empty? if
|
||||
rdrop exit
|
||||
then
|
||||
end
|
||||
0 rpick list-pop-front # acc tokens' first
|
||||
rdrop # acc tokens'
|
||||
swap # acc first tokens'
|
||||
@@ -118,13 +118,13 @@ again
|
||||
compile-only
|
||||
|
||||
: fn-validate-body
|
||||
dup list-length 0 == if "empty function body" parse-error then
|
||||
dup 0 list-get token-lexeme "return" string= 0 == if "function body must start with 'return'" parse-error then
|
||||
dup list-last ";" string= 0 == if "function body must terminate with ';'" parse-error then
|
||||
dup list-length 0 == if "empty function body" parse-error end
|
||||
dup 0 list-get token-lexeme "return" string= 0 == if "function body must start with 'return'" parse-error end
|
||||
dup list-last ";" string= 0 == if "function body must terminate with ';'" parse-error end
|
||||
list-clone # body body'
|
||||
list-pop drop # body expr' (trim trailing ';')
|
||||
list-pop-front drop # body expr (trim leading 'return')
|
||||
dup list-length 0 == if "missing return expression" parse-error then
|
||||
dup list-length 0 == if "missing return expression" parse-error end
|
||||
;
|
||||
compile-only
|
||||
|
||||
@@ -135,19 +135,19 @@ begin
|
||||
dup list-empty? if
|
||||
drop # out
|
||||
exit
|
||||
then
|
||||
end
|
||||
list-pop-front # out body' tok
|
||||
swap >r # out tok (r: body')
|
||||
dup "return" string= if
|
||||
drop
|
||||
r>
|
||||
continue
|
||||
then
|
||||
end
|
||||
dup ";" string= if
|
||||
drop
|
||||
r>
|
||||
continue
|
||||
then
|
||||
end
|
||||
list-append # out'
|
||||
r> # out' body'
|
||||
continue
|
||||
@@ -157,14 +157,14 @@ compile-only
|
||||
|
||||
|
||||
: fn-body->tokens # bodyLexemes -- tokens
|
||||
dup list-length 0 == if "empty function body" parse-error then
|
||||
dup list-length 0 == if "empty function body" parse-error end
|
||||
dup 0 list-get token-lexeme "return" string= if
|
||||
fn-validate-body # expr
|
||||
shunt # postfix
|
||||
exit
|
||||
then
|
||||
end
|
||||
fn-filter-raw-body
|
||||
dup list-length 0 == if "empty function body" parse-error then
|
||||
dup list-length 0 == if "empty function body" parse-error end
|
||||
;
|
||||
compile-only
|
||||
|
||||
@@ -177,7 +177,7 @@ begin
|
||||
">r" list-append # params out'
|
||||
r> # params out' n-1
|
||||
continue
|
||||
then
|
||||
end
|
||||
drop # params out
|
||||
exit
|
||||
again
|
||||
@@ -191,7 +191,7 @@ begin
|
||||
1 - >r
|
||||
"rdrop" list-append
|
||||
continue
|
||||
then
|
||||
end
|
||||
drop # drop counter
|
||||
swap drop # out
|
||||
exit
|
||||
@@ -204,7 +204,7 @@ compile-only
|
||||
1 -
|
||||
0 rpick ">r" list-append drop
|
||||
fn-translate-prologue-loop
|
||||
then
|
||||
end
|
||||
drop
|
||||
;
|
||||
compile-only
|
||||
@@ -214,7 +214,7 @@ compile-only
|
||||
1 -
|
||||
0 rpick "rdrop" list-append drop
|
||||
fn-translate-epilogue-loop
|
||||
then
|
||||
end
|
||||
drop
|
||||
;
|
||||
compile-only
|
||||
@@ -228,13 +228,13 @@ begin
|
||||
drop # params
|
||||
r> drop # drop name
|
||||
-1 0 exit # params -1 0
|
||||
then # params idx
|
||||
end # params idx
|
||||
over over list-get # params idx elem
|
||||
0 rpick string= # params idx flag
|
||||
if
|
||||
r> drop # drop name
|
||||
1 exit # params idx 1
|
||||
then
|
||||
end
|
||||
drop # params idx
|
||||
1 + # params idx+1
|
||||
again
|
||||
@@ -250,7 +250,7 @@ compile-only
|
||||
over swap >= if # params map idx flag
|
||||
drop # params map
|
||||
exit
|
||||
then # params map idx
|
||||
end # params map idx
|
||||
2 pick over list-get # params map idx name
|
||||
swap # params map name idx
|
||||
dup >r # params map name idx (r: idx)
|
||||
@@ -274,7 +274,7 @@ compile-only
|
||||
list-append # out'
|
||||
r> # out' map
|
||||
exit
|
||||
then
|
||||
end
|
||||
drop # out map tok
|
||||
|
||||
# param?
|
||||
@@ -292,7 +292,7 @@ compile-only
|
||||
# drop saved tok
|
||||
r> drop # out'' map
|
||||
exit
|
||||
then
|
||||
end
|
||||
# not a param: drop idx|nil, append original tok
|
||||
drop # out map
|
||||
r> # out map tok
|
||||
@@ -308,7 +308,7 @@ compile-only
|
||||
dup list-empty? if
|
||||
drop
|
||||
exit
|
||||
then
|
||||
end
|
||||
list-pop-front # map out postfix' tok
|
||||
swap >r # map out tok (r: postfix')
|
||||
>r swap r> # out map tok (r: postfix')
|
||||
@@ -354,7 +354,7 @@ compile-only
|
||||
dup lexer-pop # lexer nameTok
|
||||
dup >r # save nameTok
|
||||
token-lexeme # lexer name
|
||||
dup identifier? 0 == if "invalid function name for 'fn'" parse-error then
|
||||
dup identifier? 0 == if "invalid function name for 'fn'" parse-error end
|
||||
>r # save name string
|
||||
drop # leave lexer only for params
|
||||
"(" lexer-expect drop # consume '(' keep lexer
|
||||
|
||||
Reference in New Issue
Block a user