Implemented $ references for functions and parameters, which are also parsed for real now, along with the result. Setup for locals added, but not it's backend. Added pretty interesting add-dict-to-env function to collections.kp, which has also been added to put collections stuff in one place.
This commit is contained in:
23
collections.kp
Normal file
23
collections.kp
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
(let (
|
||||
|
||||
foldr (let (helper (rec-lambda recurse (f z v i) (if (= i (len v)) z
|
||||
(f (idx v i) (recurse f z v (+ i 1))))))
|
||||
(lambda (f z v) (helper f z v 0)))
|
||||
|
||||
empty_dict []
|
||||
put (lambda (m k v) (cons [k v] m))
|
||||
get-value-helper (rec-lambda recurse (dict key i) (if (>= i (len dict))
|
||||
(error (str key " not found in " dict))
|
||||
(if (= key (idx (idx dict i) 0))
|
||||
(idx (idx dict i) 1)
|
||||
(recurse dict key (+ i 1)))))
|
||||
get-value (lambda (dict key) (get-value-helper dict key 0))
|
||||
add-dict-to-env (let (helper (rec-lambda recurse (env dict i)
|
||||
(if (= i (len dict)) env
|
||||
(recurse (eval [ [vau '_ [(idx (idx dict i) 0)] [ [vau 'inner [] 'inner] ] ] (idx (idx dict i) 1) ] env) dict (+ i 1)))))
|
||||
(lambda (env dict) (helper env dict 0)))
|
||||
)
|
||||
(provide foldr empty_dict put get-value add-dict-to-env)
|
||||
)
|
||||
|
||||
13
comp_wasm.kp
13
comp_wasm.kp
@@ -5,13 +5,16 @@
|
||||
true *ARGV*)
|
||||
_ (println "out" out)
|
||||
wasm_code (module
|
||||
(func (param i32) (result i32)
|
||||
(i32.const 4)
|
||||
(local.get 0)
|
||||
(func $add (param $num i32) (result i32)
|
||||
(local $tmp1 i32)
|
||||
(local $tmp2 i32)
|
||||
(i32.const 11)
|
||||
(local.get $num)
|
||||
i32.add
|
||||
(i32.add (local.get 0))
|
||||
(i32.add (local.get $num))
|
||||
)
|
||||
(export "add" (func 0))
|
||||
; nothing
|
||||
(export "add" (func $add))
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -919,7 +919,7 @@ fun main(argc: int, argv: **char): int {
|
||||
if !params[0].is_array() && !params[0].is_string() { return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("first param to slice is not string or array")))); }
|
||||
if !params[1].is_int() { return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("second param to slice is not int")))); }
|
||||
var start = params[1].get_int();
|
||||
if !params[2].is_int() { return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("third param to slice is not int")))); }
|
||||
if !params[2].is_int() { return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("third param to slice is not int ") + pr_str(params[2], true)))); }
|
||||
var end = params[2].get_int();
|
||||
|
||||
if params[0].is_array() {
|
||||
@@ -1066,7 +1066,7 @@ fun main(argc: int, argv: **char): int {
|
||||
}
|
||||
if !params[0].is_int() { return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("called < with first not an int")))); }
|
||||
for (var i = 0; i < params.size - 1; i++;) {
|
||||
if !params[i+1].is_int() { return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("called < with not an int")))); }
|
||||
if !params[i+1].is_int() { return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("called < with param ") + (i+1) + " not an int " + pr_str(params[i+1], true)))); }
|
||||
if !(params[i].get_int() < params[i+1].get_int()) {
|
||||
return make_pair(null<KPEnv>(), KPResult::Ok(kpBool(false)))
|
||||
}
|
||||
@@ -1092,7 +1092,7 @@ fun main(argc: int, argv: **char): int {
|
||||
}
|
||||
if !params[0].is_int() { return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("called > with first not an int")))); }
|
||||
for (var i = 0; i < params.size - 1; i++;) {
|
||||
if !params[i+1].is_int() { return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("called > with not an int")))); }
|
||||
if !params[i+1].is_int() { return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("called > with param ") + (i+1) + " not an int " + pr_str(params[i+1], true)))); }
|
||||
if !(params[i].get_int() > params[i+1].get_int()) {
|
||||
return make_pair(null<KPEnv>(), KPResult::Ok(kpBool(false)))
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@
|
||||
(array (quote number) (array "(0(x|X)([0-9]|[a-f]|[A-F])+)|(-?[0-9]+)") (lambda (x) (string-to-int x)))
|
||||
(array (quote string) (array "\"([#-[]| |[]-~]|(\\\\\\\\)|(\\\\n)|(\\\\t)|(\\*)|(\\\\0)|
|
||||
|[ -!]|(\\\\\"))*\"") (lambda (x) (unescape-str x)))
|
||||
(array (quote bool_nil_symbol) (array "-|(([a-z]|[A-Z]|_|\\*|/|\\?|\\+|!|=|&|\\||<|>|%)([a-z]|[A-Z]|_|[0-9]|\\*|\\?|\\+|-|!|=|&|\\||<|>|%|\\.)*)") (lambda (x) (cond (= "true" x) true
|
||||
(array (quote bool_nil_symbol) (array "-|(([a-z]|[A-Z]|_|\\*|/|\\?|\\+|!|=|&|\\||<|>|%|$)([a-z]|[A-Z]|_|[0-9]|\\*|\\?|\\+|-|!|=|&|\\||<|>|%|$|\\.)*)") (lambda (x) (cond (= "true" x) true
|
||||
(= "false" x) false
|
||||
(= "nil" x) nil
|
||||
true (str-to-symbol x))))
|
||||
@@ -192,6 +192,7 @@
|
||||
(flat_map (lambda (item) (array item (array quote (eval item de)))) items)))
|
||||
scope_let_sans_import_gram (provide
|
||||
root_env
|
||||
current-env
|
||||
lambda
|
||||
rec-lambda
|
||||
let
|
||||
|
||||
44
wasm.kp
44
wasm.kp
@@ -1,5 +1,6 @@
|
||||
|
||||
(with_import "./collections.kp"
|
||||
(let (
|
||||
|
||||
; Vectors and Values
|
||||
; Bytes encode themselves
|
||||
encode_u_LEB128 (rec-lambda recurse (x)
|
||||
@@ -129,22 +130,43 @@
|
||||
)
|
||||
|
||||
module (lambda (& args) (let (
|
||||
helper (rec-lambda recurse (entries i type function memory export code)
|
||||
helper (rec-lambda recurse (entries i name_dict type function memory export code)
|
||||
(if (= i (len entries)) [ type function memory export code ]
|
||||
(let (
|
||||
(t f m e c) ((idx entries i) type function memory export code)
|
||||
) (recurse entries (+ i 1) t f m e c))))
|
||||
) (helper args 0 [] [] [] [] [])))
|
||||
(n_d t f m e c) ((idx entries i) name_dict type function memory export code)
|
||||
) (recurse entries (+ i 1) n_d t f m e c))))
|
||||
) (helper args 0 empty_dict [] [] [] [] [])))
|
||||
|
||||
func (vau de (p_type r_type & body) (lambda (type function memory export code)
|
||||
func (vau de (name & inside) (lambda (name_dict type function memory export code)
|
||||
(let (
|
||||
our_type [ [ (idx p_type 1) ] [ (idx r_type 1) ] ]
|
||||
our_code (flat_map (lambda (x) (let (ins (eval x de))
|
||||
(params result locals body) ((rec-lambda recurse (i pe re)
|
||||
(cond (and (= nil pe) (< i (len inside)) (array? (idx inside i)) (< 0 (len (idx inside i))) (= 'param (idx (idx inside i) 0)))
|
||||
(recurse (+ i 1) pe re)
|
||||
(and (= nil pe) (= nil re) (< i (len inside)) (array? (idx inside i)) (< 0 (len (idx inside i))) (= 'result (idx (idx inside i) 0)))
|
||||
; only one result possible
|
||||
(recurse (+ i 1) i (+ i 1))
|
||||
(= nil pe) (recurse (+ i 1) i i)
|
||||
(and (< i (len inside)) (array? (idx inside i)) (< 0 (len (idx inside i))) (= 'local (idx (idx inside i) 0)))
|
||||
(recurse (+ i 1) pe re)
|
||||
true [ (slice inside 0 (or pe 0)) (slice inside (or pe 0) (or re pe 0)) (slice inside (or re pe 0) i) (slice inside i -1) ]
|
||||
)
|
||||
) 0 nil nil)
|
||||
result (if (!= 0 (len result)) (idx result 0)
|
||||
result)
|
||||
_ (if (!= 0 (len locals)) (error "We don't support locals yet!"))
|
||||
_ (println "params " params " result " result " locals " locals " body " body)
|
||||
outer_name_dict (put name_dict name (len function))
|
||||
(num_params inner_name_dict) (foldr (lambda (x a) [(+ (idx a 0) 1) (put (idx a 1) (idx x 1) (idx a 0))]) [ 0 outer_name_dict ] params)
|
||||
_ (println "inner name dict" inner_name_dict)
|
||||
inner_env (add-dict-to-env de inner_name_dict)
|
||||
our_type [ (map (lambda (x) (idx x 2)) params) (slice result 1 -1) ]
|
||||
our_code (flat_map (lambda (x) (let (ins (eval x inner_env))
|
||||
(cond (array? ins) ins
|
||||
true (ins) ; un-evaled function, bare WAT
|
||||
)))
|
||||
body)
|
||||
) [
|
||||
outer_name_dict
|
||||
; type
|
||||
(concat type [ our_type ])
|
||||
; function
|
||||
@@ -160,9 +182,9 @@
|
||||
i32.const (lambda (const) [['i32.const const]])
|
||||
local.get (lambda (const) [['local.get const]])
|
||||
i32.add (lambda (& flatten) (concat (map (lambda (x) (idx x 0)) flatten) [['i32.add]]))
|
||||
export (vau de (name t_v) (lambda (type function memory export code)
|
||||
[ type function memory (concat export [ [ name (idx t_v 0) (idx t_v 1) ] ]) code ]
|
||||
export (vau de (name t_v) (lambda (name_dict type function memory export code)
|
||||
[ name_dict type function memory (concat export [ [ name (idx t_v 0) (get-value name_dict (idx t_v 1)) ] ]) code ]
|
||||
))
|
||||
)
|
||||
(provide wasm_to_binary module func i32.const local.get i32.add export)
|
||||
)
|
||||
))
|
||||
|
||||
Reference in New Issue
Block a user