From e271feed245256363c12895696b03e0e7c4eb58e Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Tue, 20 Jul 2021 00:37:27 -0400 Subject: [PATCH] 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. --- collections.kp | 23 +++++++++++++++++++++++ comp_wasm.kp | 13 ++++++++----- k_prime.krak | 6 +++--- prelude.kp | 3 ++- wasm.kp | 44 +++++++++++++++++++++++++++++++++----------- 5 files changed, 69 insertions(+), 20 deletions(-) create mode 100644 collections.kp diff --git a/collections.kp b/collections.kp new file mode 100644 index 0000000..2eb79a8 --- /dev/null +++ b/collections.kp @@ -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) +) + diff --git a/comp_wasm.kp b/comp_wasm.kp index 99a0c24..ebf5432 100644 --- a/comp_wasm.kp +++ b/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)) ) diff --git a/k_prime.krak b/k_prime.krak index 1c9f8f5..73f8f9c 100644 --- a/k_prime.krak +++ b/k_prime.krak @@ -919,7 +919,7 @@ fun main(argc: int, argv: **char): int { if !params[0].is_array() && !params[0].is_string() { return make_pair(null(), KPResult::Err(kpString(str("first param to slice is not string or array")))); } if !params[1].is_int() { return make_pair(null(), 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(), KPResult::Err(kpString(str("third param to slice is not int")))); } + if !params[2].is_int() { return make_pair(null(), 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(), 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(), KPResult::Err(kpString(str("called < with not an int")))); } + if !params[i+1].is_int() { return make_pair(null(), 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(), KPResult::Ok(kpBool(false))) } @@ -1092,7 +1092,7 @@ fun main(argc: int, argv: **char): int { } if !params[0].is_int() { return make_pair(null(), 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(), KPResult::Err(kpString(str("called > with not an int")))); } + if !params[i+1].is_int() { return make_pair(null(), 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(), KPResult::Ok(kpBool(false))) } diff --git a/prelude.kp b/prelude.kp index 9f5ba7d..ac6bea3 100644 --- a/prelude.kp +++ b/prelude.kp @@ -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 diff --git a/wasm.kp b/wasm.kp index cb54c7a..c27b714 100644 --- a/wasm.kp +++ b/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) -) +))