easier to handle comments for the compiler

This commit is contained in:
Daniella / Tove 2022-02-23 18:07:04 +01:00
parent f0577d27de
commit ed8ee68c5d
2 changed files with 98 additions and 54 deletions

View file

@ -1,16 +1,62 @@
def insns def insns
func main { # entry point returning int func main { # "entry point returning int" pop
def insns filename def insns filename
=args # put arg array into args var =args # "put arg array into args var" pop
args 0 aget =filename args 0 aget =filename
# Start compile # "Start compile" pop
filename _file compile filename _file compile
0 0
} }
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
}
func compile { func compile {
def file def file
def content def content
@ -20,25 +66,25 @@ func compile {
def c def c
=file =file
# read & split content # "read & split content" pop
0 _char (file flength) anew "\n" strsplit =content 0 _char ( file flength ) anew "\n" strsplit =content
1 neg =i 1 neg =i
# preprocess string # "preprocess string" pop
while (i 1 + =i) (i content alen lt) { while { ( i 1 + =i ) ( i content alen lt ) } {
content i aget =ln content i aget =ln
# remove tabs/spaces # "remove tabs/spaces" pop
while (ln 0 aget dup) eq ' ' eq '\t' or { while { ( ln 0 aget dup ) eq ' ' eq '\t' or } {
# remove first character # "remove first character" pop
ln 1 ln alen strsub =ln ln 1 ln alen strsub =ln
} }
# remove comments # "remove comments" pop
1 neg =j 1 neg =j
while (j 1 + =j) (j ln alen lt) { while { ( j 1 + =j ) ( j ln alen lt ) } {
(ln j aget dup =c dup) ('\"' eq '\'' eq or) if { ( ln j aget dup =c dup ) ( '\"' eq '\'' eq or ) if {
j 1 + =j j 1 + =j
while (ln j aget dup =c dup) ('\"' eq '\'' eq or) not { while { ( ln j aget dup =c dup ) ( '\"' eq '\'' eq or ) not } {
j (c '\\' eq if { 2 } else { 1 }) + =j j ( c '\\' eq if { 2 } else { 1 } ) + =j
} }
} }
c '#' eq if { c '#' eq if {
@ -48,7 +94,7 @@ func compile {
} }
} }
# join content # "join content" pop
content " " strjoin " " strsplit =content content " " strjoin " " strsplit =content
content 1 mkcode content 1 mkcode
@ -69,12 +115,12 @@ func mkcode {
dup =definefunc dup =definefunc
dup =level dup =level
pop pop
while (i 1 + =i) (i content alen lt) { while { ( i 1 + =i ) ( i content alen lt ) } {
content i aget =keyword content i aget =keyword
keyword "" eq not if { keyword "" eq not if {
allowfunc if { allowfunc if {
funcname 0 eq not keyword "{" eq and if { funcname 0 eq not keyword "{" eq and if {
i (content i 1 + content alen strsub 0 mkcode) + =i i ( content i 1 + content alen strsub 0 mkcode ) + =i
content i aget =keyword content i aget =keyword
2 stop 2 stop
} }
@ -96,15 +142,5 @@ func mkcode {
} }
func append { func append {
=str cmp swap "\n" strconcat strconcat =cmp
insns str strconcat "\n" strconcat =insns
}
func pushElement {
# bind args
=x
=type
"PUSH " type _string strconcat append
"PUSH " x strconcat append
} }

View file

@ -42,14 +42,15 @@ native flength
native write native write
native type native type
native mktype native mktype
# call a dynamically computed function # "call a dynamically computed function" pop
native call native call
native typename native typename
native istype native istype
native settype native settype
# try and catch are keywords, not functions # "try and catch are keywords, not functions" pop
native throw native throw
# storelength native exit
# "storelength" pop
native stlen native stlen
native eq native eq
@ -70,6 +71,14 @@ native dup
native pop native pop
native swap native swap
func ++ { 1 + }
func -- { 1 - }
# "this looks so wrong" pop
func ( { }
func ) { }
func # { }
"char" mktype =TYPE_CHAR "char" mktype =TYPE_CHAR
"int" mktype =TYPE_INT "int" mktype =TYPE_INT
"file" mktype =TYPE_FILE "file" mktype =TYPE_FILE
@ -123,7 +132,7 @@ func dtos {
ntos ntos
} }
# Number to string # "Number to string" pop
func ntos { func ntos {
"" =s "" =s
=n =n
@ -131,24 +140,23 @@ func ntos {
n 0 lt dup if { n neg =n 1 =negative } =negative n 0 lt dup if { n neg =n 1 =negative } =negative
n type =thetype n type =thetype
while n 1 lt not { while { n 1 lt not } {
n (n 10 % dup =x) - =n n ( n 10 % dup =x ) - =n
s (n '0' + _char) strconcat =s s ( n '0' + _char ) strconcat =s
} }
1 =steps 1 =steps
while n 0 eq not { while { n 0 eq not } {
n (n (1 (steps dup 10 * =steps) /) % dup =x) - =n n ( n ( 1 ( steps dup 10 * =steps ) / ) % dup =x ) - =n
s (x '0' + _char) strconcat =s s ( x '0' + _char ) strconcat =s
} }
negative if { "-" s strconcat =s }
s s
} }
# Copy array # "Copy array" pop
func acopy { func acopy {
# bind args # "bind args" pop
=len =len
=idx2 =idx2
=idx1 =idx1
@ -156,13 +164,13 @@ func acopy {
=arr1 =arr1
1 neg =i 1 neg =i
while (i 1 + =i) (i len lt) { while { ( i 1 + =i ) ( i len lt ) } {
arr2 i idx2 + (arr1 i idx1 + aget) aput arr2 i idx2 + ( arr1 i idx1 + aget ) aput
} }
} }
func strconcat { func strconcat {
# bind args # "bind args" pop
_string =str2 _string =str2
_string =str1 _string =str1
@ -175,22 +183,22 @@ func strconcat {
} }
func strsplit { func strsplit {
# bind args # "bind args" pop
_string =splitter _string =splitter
_string =str _string =str
1 neg =i 1 neg =i
"" 0 anew =sequences "" 0 anew =sequences
"" =sequence "" =sequence
while (i 1 + =i) (i str alen lt) { while { ( i 1 + =i ) ( i str alen lt ) } {
str i aget =chr str i aget =chr
splitter 0 aget =spl splitter 0 aget =spl
sequence chr strconcat =sequence sequence chr strconcat =sequence
1 neg =j 1 neg =j
while (j 1 + =j) (chr spl eq) { while { ( j 1 + =j ) ( chr spl eq ) } {
j splitter alen eq if { j splitter alen eq if {
sequences =oldsq sequences =oldsq
"" (sequences alen dup =seq 1 +) anew =sequences "" ( sequences alen dup =seq 1 + ) anew =sequences
oldsq sequences 0 0 oldsq alen acopy oldsq sequences 0 0 oldsq alen acopy
sequences seq sequence aput sequences seq sequence aput
"" =sequence "" =sequence
@ -205,13 +213,13 @@ func strsplit {
} }
func strjoin { func strjoin {
# bind args # "bind args" pop
_string =joiner _string =joiner
_array =arr _array =arr
1 neg =i 1 neg =i
"" =s "" =s
while (i 1 + =i) (i arr alen lt) { while { ( i 1 + =i ) ( i arr alen lt ) } {
s arr i aget joiner strconcat strconcat =s s arr i aget joiner strconcat strconcat =s
} }
@ -219,20 +227,20 @@ func strjoin {
} }
func strsub { func strsub {
# bind args # "bind args" pop
_int =pos2 _int =pos2
_int =pos1 _int =pos1
_string =string _string =string
pos2 pos1 - =length pos2 pos1 - =length
(o _char length anew dup) string swap pos1 0 length acopy ( o _char length anew dup ) string swap pos1 0 length acopy
} }
func puts { func puts {
_string =str _string =str
1 neg =i 1 neg =i
while (i 1 + =i) (i str alen lt) { while { ( i 1 + =i ) ( i str alen lt ) } {
str i aget putchar str i aget putchar
} }
} }