Browse Source

backup

master
zapashcanon 2 months ago
parent
commit
1b78a28f91
Signed by: zapashcanon GPG Key ID: 8981C3C62D1D28F1
  1. 11
      src/canvas.wast
  2. 105
      src/list.wast

11
src/canvas.wast

@ -74,13 +74,18 @@
))
(func $canvas_get_absolute_origin_aux (param $canvas (ref $canvas)) (param $abs_orig (ref $point)) (result (ref $point))
(local $parent (ref null $canvas))
(local.set $parent (struct.get $canvas $parent (local.get $canvas)))
;; if there's no parent, we're the `root` canvas
(if (result (ref $point)) (ref.is_null (struct.get $canvas $parent (local.get $canvas))) (then
(if (result (ref $point)) (ref.is_null (local.get $parent)) (then
local.get $abs_orig
)
;; otherwise, we have a parent, and our absolute origin is the sum of the current absolute origin and the one of our parent, hence the recursive call
;; otherwise, we have a parent, and our absolute origin is the sum of the current absolute origin, our absolute origin and the one of our parent, hence the recursive call
(else
(call $canvas_get_absolute_origin_aux (struct.get $canvas $parent (local.get $canvas)) (call $point_add (local.get $abs_orig) (struct.get $canvas $origin (local.get $canvas))))
(call $canvas_get_absolute_origin_aux
(ref.cast (local.get $parent) (rtt.canon $canvas))
(call $point_add (local.get $abs_orig) (struct.get $canvas $origin (local.get $canvas)))
)
))
return
)

105
src/list.wast

@ -0,0 +1,105 @@
(module $list
(type $list (struct
(field $head i32)
(field $tail (ref null $list))
))
(func $nil (result (ref null $list))
(return (ref.null $list))
)
(func $cons (param $head i32) (param $tail (ref null $list)) (result (ref $list))
(struct.new $list (local.get $head) (local.get $tail) (rtt.canon $list))
return
)
(func $is_nil (param $list (ref null $list)) (result i32)
(block $l (result i32)
i32.const 1
(br_on_null $l (local.get $list))
drop
drop
i32.const 0
)
return
)
(func $head (param $list (ref $list)) (result i32)
(struct.get $list $head (local.get $list))
return
)
(func $tail (param $list (ref $list)) (result (ref null $list))
(struct.get $list $tail (local.get $list))
return
)
(func $length_aux (param $acc i32) (param $list (ref null $list)) (result i32)
(if (result i32) (call $is_nil (local.get $list))
(then
(return (local.get $acc))
) (else
(return
(call $length_aux
(i32.add (local.get $acc) (i32.const 1))
(call $tail (ref.as_non_null (local.get $list)))
)
)
)
)
)
(func $length (param $list (ref null $list)) (result i32)
(return (call $length_aux (i32.const 0) (local.get $list)))
)
(func $nth_aux (param $list (ref null $list)) (param $n i32) (result i32)
(if (call $is_nil (local.get $list))
(then unreachable)
)
(if (result i32) (i32.eq (i32.const 0) (local.get $n))
(then (call $head (ref.as_non_null (local.get $list))))
(else
(call $nth_aux
(call $tail (ref.as_non_null (local.get $list)))
(i32.sub (local.get $n) (i32.const 1))
)
))
return
)
(func $nth (param $list (ref null $list)) (param $n i32) (result i32)
(if (i32.lt_s (local.get $n) (i32.const 0))
(then unreachable)
)
(return (call $nth_aux (local.get $list) (local.get $n)))
)
(type $mapf (func (param i32) (result i32)))
(func $map (param $f (ref $mapf)) (param $list (ref null $list)) (result (ref null $list))
(if (result (ref null $list)) (call $is_nil (local.get $list))
(then (call $nil))
(else
(call $cons
(call_ref (call $head (ref.as_non_null (local.get $list))) (local.get $f))
(call $map (local.get $f) (call $tail (ref.as_non_null (local.get $list))))
)
))
return
)
(func (export "test_should_be_nil") (result i32)
(return (call $is_nil (call $nil)))
)
(func (export "test_should_not_be_nil") (result i32)
(return
(call $is_nil
(call $cons (i32.const 42) (call $nil))))
)
)
(assert_return (invoke "test_should_be_nil") (i32.const 1))
(assert_return (invoke "test_should_not_be_nil") (i32.const 0))
Loading…
Cancel
Save