improvements to stdlib, add stuff to compiler, create incomplete bootstrapper file

This commit is contained in:
Daniella 2022-02-06 02:12:39 +01:00
parent 2f4964d9b9
commit 0dfe8dc93f
3 changed files with 116 additions and 6 deletions

View file

@ -0,0 +1,6 @@
#!/bin/node
const fs = require('fs')
const process = require('process')

View file

@ -3,7 +3,7 @@
func main { # entry point returning int func main { # entry point returning int
=args # put arg array into args var =args # put arg array into args var
args 0 aget =file args 0 aget =filename
# Start compile # Start compile
filename _file compile filename _file compile
@ -11,6 +11,79 @@ func main { # entry point returning int
0 0
} }
func compile {
=file
# read & split content
0 _char (file flength) anew "\n" strsplit =content
1 neg =i
# preprocess string
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
ln 1 ln alen strsub =ln
}
# remove comments
1 neg =j
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
}
}
c '#' eq if {
ln 0 j strsub
1 stop
}
}
}
# join content
content " " strjoin " " strsplit =content
content 1 mkcode
}
func mkcode {
=allowfunc
=content
1 neg =i
0 dup =isnative
dup =funcname
dup =definefunc
dup =level
pop
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
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
}
func append { func append {
=str =str
insns str strconcat "\n" strconcat =insns insns str strconcat "\n" strconcat =insns
@ -21,6 +94,6 @@ func pushElement {
=x =x
=type =type
"PUSH " type _string_string strconcat append "PUSH " type _string strconcat append
"PUSH " x strconcat append "PUSH " x strconcat append
} }

View file

@ -39,11 +39,15 @@ native func stop
native func include native func include
native func putchar native func putchar
native func read native func read
native func flength
native func write
native func type native func type
native func mktype native func mktype
# call a dynamically computed function # call a dynamically computed function
native func call native func call
native func typename native func typename
native func istype
native func settype
native func throw native func throw
# try and catch are keywords, not functions # try and catch are keywords, not functions
# storelength # storelength
@ -53,6 +57,8 @@ native func eq
native func gt native func gt
native func lt native func lt
native func not native func not
native func or
native func and
native func + native func +
native func - native func -
native func / native func /
@ -161,7 +167,7 @@ func strconcat {
_string =str2 _string =str2
_string =str1 _string =str1
0 _char str1 alen str2 alen anew = str 0 _char str1 alen str2 alen + anew =str
str1 str 0 0 str1 alen acopy str1 str 0 0 str1 alen acopy
str2 str 0 str1 alen str2 alen acopy str2 str 0 str1 alen str2 alen acopy
@ -171,13 +177,13 @@ func strconcat {
func strsplit { func strsplit {
# bind args # bind args
_string =str
_string =splitter _string =splitter
_string =str
1 neg =i 1 neg =i
"" 0 anew =sequences "" 0 anew =sequences
"" =sequence "" =sequence
while (i 1 + =1) (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
@ -189,7 +195,7 @@ func strsplit {
oldsq sequences 0 0 oldsq alen acopy oldsq sequences 0 0 oldsq alen acopy
sequences seq sequence aput sequences seq sequence aput
"" =sequence "" =sequence
stop 2 stop
} }
str i j + aget dup =chr str i j + aget dup =chr
splitter j aget dup =spl splitter j aget dup =spl
@ -199,6 +205,31 @@ func strsplit {
sequences sequences
} }
func strjoin {
# bind args
_string =joiner
_array =arr
1 neg =i
"" =s
while (i 1 + =i) (i arr alen lt) {
s arr i aget joiner strconcat strconcat =s
}
s
}
func strsub {
# bind args
_int =pos2
_int =pos1
_string =string
pos2 pos1 - =length
(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