Skip to content

Commit fbee4ec

Browse files
loredanacirsteakanaka
authored andcommitted
wasm: add seq
1 parent 43ae30f commit fbee4ec

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

impls/wasm/core.wam

+63-2
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,67 @@
333333
(i32.eq ($TYPE ($MEM_VAL1_ptr $args))
334334
(global.get $VECTOR_T)))))
335335

336+
(func $seq (param $args i32) (result i32)
337+
(LET $mv ($MEM_VAL1_ptr $args)
338+
$type ($TYPE $mv)
339+
$res 0
340+
$ret 0
341+
$empty 0
342+
$current 0
343+
$i 0
344+
$char 0)
345+
346+
(if (i32.eq $type (global.get $NIL_T))
347+
(then (return (global.get $NIL))))
348+
349+
(if (AND (i32.ne $type (global.get $LIST_T))
350+
(i32.ne $type (global.get $VECTOR_T))
351+
(i32.ne $type (global.get $STRING_T)))
352+
(then (return (global.get $NIL))))
353+
354+
(if (i32.eqz ($VAL0 $mv)) (then (return (global.get $NIL))))
355+
356+
(if (i32.eq $type (global.get $LIST_T))
357+
(then (return ($FORCE_SEQ_TYPE (global.get $LIST_T) $mv))))
358+
359+
(if (i32.eq $type (global.get $VECTOR_T))
360+
(then (return ($FORCE_SEQ_TYPE (global.get $LIST_T) $mv))))
361+
362+
(if (i32.eq $type (global.get $STRING_T))
363+
(then
364+
(local.set $mv ($to_String $mv))
365+
(if (i32.eqz ($strlen $mv)) (then (return (global.get $NIL))))
366+
(local.set $res ($MAP_LOOP_START (global.get $LIST_T)))
367+
(local.set $empty $res)
368+
(local.set $current $res)
369+
(local.set $ret $res)
370+
(local.set $i 0)
371+
(block $done
372+
(loop $loop
373+
(local.set $char (i32.load8_u (i32.add $mv $i)))
374+
(br_if $done (i32.eq $char 0))
375+
376+
(i32.store8 (global.get $token_buf) $char)
377+
(i32.store8 (i32.add (global.get $token_buf) 1) 0)
378+
379+
(local.set $res
380+
($MAP_LOOP_UPDATE (global.get $LIST_T)
381+
$empty $current ($STRING (global.get $STRING_T) (global.get $token_buf)) 0))
382+
(if (i32.le_u $current (global.get $EMPTY_LIST))
383+
;; if first element, set return to new element
384+
(local.set $ret $res))
385+
386+
(local.set $i (i32.add $i 1))
387+
(local.set $current $res)
388+
(br $loop)
389+
)
390+
)
391+
(return $ret)
392+
)
393+
)
394+
(global.get $NIL)
395+
)
396+
336397
(func $cons (param $args i32) (result i32)
337398
($LIST ($MEM_VAL1_ptr ($MEM_VAL0_ptr $args)) ($MEM_VAL1_ptr $args)))
338399

@@ -468,7 +529,7 @@
468529
(LET $f ($MEM_VAL1_ptr $args)
469530
$rest_args ($MEM_VAL1_ptr ($MEM_VAL0_ptr $args))
470531
$f_args 0
471-
$res ($MAP_LOOP_START (global.get $LIST_T))
532+
$res ($MAP_LOOP_START (global.get $LIST_T))
472533
;; push MAP_LOOP stack
473534
$ret $res
474535
$current $res
@@ -641,7 +702,7 @@
641702
$apply
642703
$map
643704
$nop ;; $conj
644-
$nop ;; $seq
705+
$seq
645706

646707
;; 54
647708
$with_meta

0 commit comments

Comments
 (0)