spl/test.spl
2023-02-19 02:03:06 +01:00

122 lines
2.3 KiB
Text

def null
func println { |
print "\n" print
}
{ int | array-get } "get" "array" dyn-def-method
{ int | array-len } "len" "array" dyn-def-method
{ any | array-set } "set" "array" dyn-def-method
{ | with this ;
def len this:len =len
def i 0 =i
while { i len lt } {
i this:get
i 1 + =i
}
} "to-stack" "array" dyn-def-method
{ any | with type ;
null clone type settype "construct" dyn-objcall
} "new" "str" dyn-def-method
{ | with callable this ;
def i 0 =i
while { i this:len lt } { i this:get callable call i ++ =i }
} "foreach" "array" dyn-def-method
construct ChangingArray {
array
;
construct { this | with array this ;
array this:=array
this
}
push { | with item this ;
[ this:array:to-stack item ] this:=array
}
}
construct ShrinkingArray_trait {
;
pop { any | with this ;
[ this:array:to-stack pop ] this:=array
}
}
include ShrinkingArray_trait in ChangingArray
"ChangingArray now has push and pop.";
construct shadow { }
"Copy array";
func acopy { array | with arr1 arr2 idx1 idx2 len ;
def i 0 =i
while { i len lt } {
(( i idx1 + ) arr1:get) (i idx2 +) arr2:set;
i 1 + =i
}
arr2
}
func aadd { array | with arr1 arr2 ;
def newarr arr1:len arr2:len + anew =newarr
arr1 newarr 0 0 arr1:len acopy =newarr
arr2 newarr 0 arr1:len arr2:len acopy =newarr
newarr
}
func [ { shadow |
"array" "shadow" settype
}
func ] { array |
"create an array containing everything on stack until the arrayshadow";
def array 0 anew =array
def array2
while { dup [ eq not } {
1 anew =array2
0 array2:set;
array2 array aadd =array
}
pop array
}
func ++ { mega |
1 +
}
{ | with type ;
"registering on " print type println
{ self | } "unwrap" type dyn-def-method
} dyn-all-types:foreach
{ | with this ;
"ERR: null cannot be unwrapped." println
1 _int exit
} "unwrap" "null" dyn-def-method
def thing
1 anew =thing
"hi" 0 thing:unwrap:set;
def thing2 thing:unwrap ChangingArray:new =thing2
"world" thing2:unwrap:push
def thing3 thing2:unwrap:array =thing3
0 thing3:unwrap:get println
1 thing3:unwrap:get println
"\"heya1\" println" dyn-read call
{ | "heya2" println } call