From dbece888613119c14471ddff46065cba30b35afd Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Tue, 27 Jul 2021 01:02:16 -0400 Subject: [PATCH] Added loop if/else br_if --- comp_wasm.kp | 24 ++++++++++++++++++++++++ k_prime.krak | 6 +++++- wasm.kp | 34 ++++++++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/comp_wasm.kp b/comp_wasm.kp index e72e296..51ca179 100644 --- a/comp_wasm.kp +++ b/comp_wasm.kp @@ -36,6 +36,30 @@ (i32.const 4) ;; opened fd out ptr ) drop + (block $a + (block $b + (br $a) + (br_if $b (i32.const 3)) + (loop $l + (br $a) + (br $l) + ) + (_if $myif (i32.const 1) + (then + (i32.const 1) + drop + (br $b) + ) + (else + (br $myif) + ) + ) + (_if $another (i32.const 1) (br $b)) + (i32.const 1) + (_if $third (br $b)) + (_if $fourth (br $fourth)) + ) + ) (call $fd_read (i32.const 0) ;; file descriptor diff --git a/k_prime.krak b/k_prime.krak index 41f22dd..5f790c8 100644 --- a/k_prime.krak +++ b/k_prime.krak @@ -825,7 +825,11 @@ fun main(argc: int, argv: **char): int { return make_pair(dynamic_env, KPResult::Ok(params[i+1])) } } - return make_pair(null(), KPResult::Err(kpString(str("None of cond branches were true")))) + var it = str() + for (var i = 0; i < params.size; i+=1;) { + it += pr_str(params[i], true) + " " + } + return make_pair(null(), KPResult::Err(kpString(str("None of cond branches were true: ") + it))) })); env->set(str("symbol?"), make_builtin_combiner(str("symbol?"), 1, false, fun(params: vec, dynamic_env: *KPEnv): pair<*KPEnv, KPResult> { diff --git a/wasm.kp b/wasm.kp index 708a731..3af8725 100644 --- a/wasm.kp +++ b/wasm.kp @@ -112,7 +112,11 @@ ) (cond (= op 'unreachable) [0x00] (= op 'nop) [0x01] (= op 'block) (concat [0x02] (encode_blocktype (idx ins 1)) (flat_map recurse (idx ins 2)) [0x0B]) + (= op 'loop) (concat [0x03] (encode_blocktype (idx ins 1)) (flat_map recurse (idx ins 2)) [0x0B]) + (= op 'if) (concat [0x04] (encode_blocktype (idx ins 1)) (flat_map recurse (idx ins 2)) (if (!= 3 (len ins)) (concat [0x05] (flat_map recurse (idx ins 3))) + []) [0x0B]) (= op 'br) (concat [0x0C] (encode_u_LEB128 (idx ins 1))) + (= op 'br_if) (concat [0x0D] (encode_u_LEB128 (idx ins 1))) ;... (= op 'return) [0x0F] (= op 'call) (concat [0x10] (encode_u_LEB128 (idx ins 1))) @@ -262,13 +266,35 @@ i64.load (lambda (& flatten) (concat (flat_map (lambda (x) x) flatten) [['i64.load 3 0]])) i32.store (lambda (& flatten) (concat (flat_map (lambda (x) x) flatten) [['i32.store 2 0]])) i64.store (lambda (& flatten) (concat (flat_map (lambda (x) x) flatten) [['i64.store 3 0]])) - block (vau de (name & inner) (let ( + flat_eval_ins (lambda (instructions de) (flat_map (lambda (x) (let (ins (eval x de)) (cond (array? ins) ins + true (ins)))) instructions)) + block_like_body (lambda (name de inner) (let ( new_depth (+ 1 (eval 'depth de)) inner_env (add-dict-to-env de [[ name [new_depth] ] [ 'depth new_depth ]]) - ) [['block [] (flat_map (lambda (x) (let (ins (eval x inner_env)) (cond (array? ins) ins - true (ins)))) inner)]])) + ) (flat_eval_ins inner inner_env))) + block (vau de (name & inner) [['block [] (block_like_body name de inner)]]) + loop (vau de (name & inner) [['loop [] (block_like_body name de inner)]]) + _if (vau de (name & inner) (let ( + (end_idx else_section) (if (= 'else (idx (idx inner -1) 0)) [ -2 (slice (idx inner -1) 1 -1) ] + [ -1 nil ]) + (end_idx then_section) (if (= 'then (idx (idx inner end_idx) 0)) [ (- end_idx 1) (slice (idx inner end_idx) 1 -1) ] + [ (- end_idx 1) [ (idx inner end_idx) ] ]) + flattened (flat_eval_ins (slice inner 0 end_idx) de) + _ (println "flattened " flattened " then_section " then_section " else_section " else_section) + then_block (block_like_body name de then_section) + else_block (if (!= nil else_section) [(block_like_body name de else_section)] + []) + final_if (concat ['if [] then_block] else_block) + _ (println "final if is " final_if) + ) (concat flattened [final_if ]))) + br (vau de (b) (let (block (eval b de)) (if (int? block) [['br block]] [['br (eval [- 'depth (idx block 0)] de)]]))) + br_if (vau de (b & flatten) (let (block (eval b de) + block_val (if (int? block) block + (eval [- 'depth (idx block 0)] de)) + rest (flat_eval_ins flatten de) + ) (concat rest [['br_if block_val]]))) call (lambda (f & flatten) (concat (flat_map (lambda (x) x) flatten) [['call f]])) import (vau de (mod_name name t_idx_typ) (lambda (name_dict type import function memory export code data) (let ( _ (if (!= 'func (idx t_idx_typ 0)) (error "only supporting importing functions rn")) @@ -291,5 +317,5 @@ drop i32.const i64.const local.get i32.add i32.load i64.load i32.store i64.store - block br call) + block loop _if br br_if call) ))