Super basic and a tiny bit hardcoded WAT-like syntax for generating the array of arrays of arrays of arrays wasm implementation in wasm.kp, allowing us to write psudo-WAT in kraken and output binary wasm with 1 function call
This commit is contained in:
16
comp_wasm.kp
16
comp_wasm.kp
@@ -1,11 +1,19 @@
|
|||||||
(with_import "./wasm.kp"
|
(with_import "./wasm.kp"
|
||||||
(let (
|
(let (
|
||||||
_ (println "args" *ARGV*)
|
_ (println "args" *ARGV*)
|
||||||
(_ _ in out) (cond (!= (len *ARGV*) 4) (error "wrong number of params")
|
(_ _ out) (cond (!= (len *ARGV*) 3) (error "wrong number of params")
|
||||||
true *ARGV*)
|
true *ARGV*)
|
||||||
_ (println "in" in "out" out)
|
_ (println "out" out)
|
||||||
wasm_code [
|
wasm_code (module
|
||||||
]
|
(func (param i32) (result i32)
|
||||||
|
(i32.const 34)
|
||||||
|
(local.get 0)
|
||||||
|
(i32.add)
|
||||||
|
)
|
||||||
|
(export "add" (func 0))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
_ (write_file out (wasm_to_binary wasm_code))
|
_ (write_file out (wasm_to_binary wasm_code))
|
||||||
return_code 0
|
return_code 0
|
||||||
) return_code ))
|
) return_code ))
|
||||||
|
|||||||
12
k_prime.krak
12
k_prime.krak
@@ -892,8 +892,18 @@ fun main(argc: int, argv: **char): int {
|
|||||||
if !params[1].is_int() { return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("Param 2 to idx is not int ") + pr_str(params[1], true)))); }
|
if !params[1].is_int() { return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("Param 2 to idx is not int ") + pr_str(params[1], true)))); }
|
||||||
|
|
||||||
var index = params[1].get_int()
|
var index = params[1].get_int()
|
||||||
|
var size = 0
|
||||||
|
if params[0].is_array() {
|
||||||
|
size = params[0].get_array_rc().get().size
|
||||||
|
} else {
|
||||||
|
size = params[0].get_string().length()
|
||||||
|
}
|
||||||
if index < 0 {
|
if index < 0 {
|
||||||
index += params[0].get_array_rc().get().size
|
index += size
|
||||||
|
}
|
||||||
|
|
||||||
|
if index < 0 || index >= size {
|
||||||
|
return make_pair(null<KPEnv>(), KPResult::Err(kpString(str("idx out of bounds, tried to get index ") + index + " in " + pr_str(params[0], true))))
|
||||||
}
|
}
|
||||||
|
|
||||||
if params[0].is_array() {
|
if params[0].is_array() {
|
||||||
|
|||||||
55
wasm.kp
55
wasm.kp
@@ -42,9 +42,6 @@
|
|||||||
(encode_result_type (idx x 1)))
|
(encode_result_type (idx x 1)))
|
||||||
)
|
)
|
||||||
|
|
||||||
; Instructions
|
|
||||||
; TODO
|
|
||||||
|
|
||||||
; Modules
|
; Modules
|
||||||
encode_type_section (lambda (x)
|
encode_type_section (lambda (x)
|
||||||
(let (
|
(let (
|
||||||
@@ -119,21 +116,49 @@
|
|||||||
|
|
||||||
wasm_to_binary (lambda (wasm_code)
|
wasm_to_binary (lambda (wasm_code)
|
||||||
(let (
|
(let (
|
||||||
|
(type_section function_section memory_section export_section code_section) wasm_code
|
||||||
|
_ (println "type_section" type_section "function_section" function_section "memory_section" memory_section "export_section" export_section "code_section" code_section)
|
||||||
magic [ 0x00 0x61 0x73 0x6D ]
|
magic [ 0x00 0x61 0x73 0x6D ]
|
||||||
version [ 0x01 0x00 0x00 0x00 ]
|
version [ 0x01 0x00 0x00 0x00 ]
|
||||||
type (encode_type_section [ [['i32] ['i32]] ])
|
type (encode_type_section type_section)
|
||||||
function (encode_function_section [ 0 ])
|
function (encode_function_section function_section)
|
||||||
memory (encode_memory_section [ [0x20 0x30] ])
|
memory (encode_memory_section memory_section)
|
||||||
export (encode_export_section [ ["add" 'func 0] ])
|
export (encode_export_section export_section)
|
||||||
code (encode_code_section [
|
code (encode_code_section code_section)
|
||||||
[ [] [
|
|
||||||
['i32.const 1337]
|
|
||||||
['local.get 0]
|
|
||||||
['i32.add]
|
|
||||||
] ]
|
|
||||||
])
|
|
||||||
) (concat magic version type function memory export code))
|
) (concat magic version type function memory export code))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
module (lambda (& args) (let (
|
||||||
|
helper (rec-lambda recurse (entries i 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 [] [] [] [] [])))
|
||||||
|
|
||||||
|
func (vau de (p_type r_type & body) (lambda (type function memory export code)
|
||||||
|
(let (
|
||||||
|
our_type [ [ (idx p_type 1) ] [ (idx r_type 1) ] ]
|
||||||
|
our_code (map (lambda (x) (eval x de)) body)
|
||||||
|
) [
|
||||||
|
; type
|
||||||
|
(concat type [ our_type ])
|
||||||
|
; function
|
||||||
|
(concat function [ (len function) ])
|
||||||
|
; memory
|
||||||
|
memory
|
||||||
|
; export
|
||||||
|
export
|
||||||
|
; code
|
||||||
|
(concat code [ [ [] our_code ] ])
|
||||||
|
])
|
||||||
|
))
|
||||||
|
i32.const (lambda (const) ['i32.const const])
|
||||||
|
local.get (lambda (const) ['local.get const])
|
||||||
|
i32.add (lambda () ['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 ]
|
||||||
|
))
|
||||||
)
|
)
|
||||||
(provide wasm_to_binary)
|
(provide wasm_to_binary module func i32.const local.get i32.add export)
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user