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

This commit is contained in:
Daniella / Tove 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
=args # put arg array into args var
args 0 aget =file
args 0 aget =filename
# Start compile
filename _file compile
@ -11,6 +11,79 @@ func main { # entry point returning int
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 {
=str
insns str strconcat "\n" strconcat =insns
@ -21,6 +94,6 @@ func pushElement {
=x
=type
"PUSH " type _string_string strconcat append
"PUSH " type _string strconcat append
"PUSH " x strconcat append
}

View file

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