2022-02-08 20:23:05 +01:00
|
|
|
def insns
|
2022-02-05 21:20:51 +01:00
|
|
|
|
2022-02-23 18:07:04 +01:00
|
|
|
func main { # "entry point returning int" pop
|
2022-02-08 20:23:05 +01:00
|
|
|
def insns filename
|
2022-02-23 18:07:04 +01:00
|
|
|
=args # "put arg array into args var" pop
|
2022-02-06 02:12:39 +01:00
|
|
|
args 0 aget =filename
|
2022-02-05 21:20:51 +01:00
|
|
|
|
2022-02-23 18:07:04 +01:00
|
|
|
# "Start compile" pop
|
2022-02-05 21:20:51 +01:00
|
|
|
filename _file compile
|
|
|
|
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2022-02-23 18:07:04 +01:00
|
|
|
def ptr 0 =ptr
|
|
|
|
|
|
|
|
# "function to fail the compiler due to an oversight by isbpl devs" pop
|
|
|
|
func internal_error {
|
|
|
|
"internal_error\n" puts
|
|
|
|
# "arg is on stack, no need for variables etc" pop
|
|
|
|
"\n" strconcat puts
|
|
|
|
2 exit
|
|
|
|
}
|
|
|
|
|
|
|
|
func is_space {
|
|
|
|
# "each line: dup-d or-d -> or-d dup-d" pop
|
|
|
|
dup " " eq
|
|
|
|
swap dup "\n" eq or
|
|
|
|
swap dup "\r" eq or
|
|
|
|
swap dup "\t" eq or
|
|
|
|
swap dup "" eq or
|
|
|
|
# "dup-d or-d -> or-d dup-d -> or-d" pop
|
|
|
|
swap pop swap
|
|
|
|
}
|
|
|
|
|
|
|
|
func read_string {
|
|
|
|
def s "" =s
|
|
|
|
def insn
|
|
|
|
|
|
|
|
while { 1 } {
|
|
|
|
insns ptr aget =insn
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# "function to read a block of insns" pop
|
|
|
|
func read_block {
|
|
|
|
insns ptr aget dup # "dup the insn for later use" pop ( "{" eq not if {
|
|
|
|
"ERROR: read_block called with invalid ptr" internal_error
|
|
|
|
} )
|
|
|
|
def insn =insn # "insn was dup'd" pop
|
|
|
|
|
|
|
|
insn is_space not if {
|
|
|
|
# "if the insn is anything useful" pop
|
|
|
|
insn 0 aget '"' eq if { read_string }
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr dup ++ =ptr
|
|
|
|
}
|
|
|
|
|
2022-02-06 02:12:39 +01:00
|
|
|
func compile {
|
2022-02-08 20:23:05 +01:00
|
|
|
def file
|
|
|
|
def content
|
|
|
|
def i
|
|
|
|
def ln
|
|
|
|
def j
|
|
|
|
def c
|
2022-02-06 02:12:39 +01:00
|
|
|
=file
|
|
|
|
|
2022-02-23 18:07:04 +01:00
|
|
|
# "read & split content" pop
|
|
|
|
0 _char ( file flength ) anew "\n" strsplit =content
|
2022-02-06 02:12:39 +01:00
|
|
|
|
|
|
|
1 neg =i
|
2022-02-23 18:07:04 +01:00
|
|
|
# "preprocess string" pop
|
|
|
|
while { ( i 1 + =i ) ( i content alen lt ) } {
|
2022-02-06 02:12:39 +01:00
|
|
|
content i aget =ln
|
2022-02-23 18:07:04 +01:00
|
|
|
# "remove tabs/spaces" pop
|
|
|
|
while { ( ln 0 aget dup ) eq ' ' eq '\t' or } {
|
|
|
|
# "remove first character" pop
|
2022-02-06 02:12:39 +01:00
|
|
|
ln 1 ln alen strsub =ln
|
|
|
|
}
|
2022-02-23 18:07:04 +01:00
|
|
|
# "remove comments" pop
|
2022-02-06 02:12:39 +01:00
|
|
|
1 neg =j
|
2022-02-23 18:07:04 +01:00
|
|
|
while { ( j 1 + =j ) ( j ln alen lt ) } {
|
|
|
|
( ln j aget dup =c dup ) ( '\"' eq '\'' eq or ) if {
|
2022-02-06 02:12:39 +01:00
|
|
|
j 1 + =j
|
2022-02-23 18:07:04 +01:00
|
|
|
while { ( ln j aget dup =c dup ) ( '\"' eq '\'' eq or ) not } {
|
|
|
|
j ( c '\\' eq if { 2 } else { 1 } ) + =j
|
2022-02-06 02:12:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c '#' eq if {
|
|
|
|
ln 0 j strsub
|
|
|
|
1 stop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 18:07:04 +01:00
|
|
|
# "join content" pop
|
2022-02-06 02:12:39 +01:00
|
|
|
content " " strjoin " " strsplit =content
|
|
|
|
|
|
|
|
content 1 mkcode
|
|
|
|
}
|
|
|
|
|
|
|
|
func mkcode {
|
2022-02-08 20:23:05 +01:00
|
|
|
def allowfunc
|
|
|
|
def content
|
|
|
|
def definefunc
|
|
|
|
def level
|
|
|
|
def i
|
2022-02-06 02:12:39 +01:00
|
|
|
=allowfunc
|
|
|
|
=content
|
|
|
|
|
|
|
|
1 neg =i
|
|
|
|
0 dup =isnative
|
|
|
|
dup =funcname
|
|
|
|
dup =definefunc
|
|
|
|
dup =level
|
|
|
|
pop
|
2022-02-23 18:07:04 +01:00
|
|
|
while { ( i 1 + =i ) ( i content alen lt ) } {
|
2022-02-06 02:12:39 +01:00
|
|
|
content i aget =keyword
|
|
|
|
keyword "" eq not if {
|
|
|
|
allowfunc if {
|
|
|
|
funcname 0 eq not keyword "{" eq and if {
|
2022-02-23 18:07:04 +01:00
|
|
|
i ( content i 1 + content alen strsub 0 mkcode ) + =i
|
2022-02-06 02:12:39 +01:00
|
|
|
content i aget =keyword
|
|
|
|
2 stop
|
|
|
|
}
|
|
|
|
definefunc if {
|
|
|
|
keyword =funcname
|
|
|
|
}
|
|
|
|
keyword "func" eq =definefunc
|
|
|
|
keyword "native" eq =native
|
|
|
|
}
|
|
|
|
keyword '{' if { level 1 + =level }
|
|
|
|
keyword '}' if { level 1 - =level }
|
|
|
|
keyword 'if' if {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
level 1 neg eq if { 1 stop }
|
|
|
|
}
|
|
|
|
|
|
|
|
i
|
|
|
|
}
|
|
|
|
|
2022-02-05 21:20:51 +01:00
|
|
|
func append {
|
2022-02-23 18:07:04 +01:00
|
|
|
cmp swap "\n" strconcat strconcat =cmp
|
2022-02-05 21:20:51 +01:00
|
|
|
}
|