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

View file

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