Skip to content

Commit

Permalink
add custom struct printer to fmt test
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmuth committed Sep 27, 2023
1 parent 7a60a50 commit 3909c21
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
29 changes: 29 additions & 0 deletions FrontEnd/Lib/fmt_test.cw
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,36 @@
(entry green 3)
(entry red 4))


(fun @polymorphic fmt::SysRender [
(param v color)
(param out (slice @mut u8))
(param options (ptr @mut fmt::SysFormatOptions))] uint :
(return (@polymorphic fmt::SysRender [(as v s32) out options])))


(defrec @pub ic32 :
(field real s32)
(field imag s32))

(fun @polymorphic fmt::SysRender [
(param v ic32)
(param s (slice @mut u8))
(param opt (ptr @mut fmt::SysFormatOptions))] uint :
(let f auto (front @mut s))
(let l auto (len s))
(let @mut n uint 0)
(= n (@polymorphic fmt::SysRender [
(. v real) s opt]))
(+= n (@polymorphic fmt::SysRender [
"+" (slice_val (incp f n) (- l n)) opt]))
(+= n (@polymorphic fmt::SysRender [
(. v imag) (slice_val (incp f n) (- l n)) opt]))
(+= n (@polymorphic fmt::SysRender [
"i" (slice_val (incp f n) (- l n)) opt]))
(return n))


(fun @cdecl main [(param argc s32) (param argv (ptr (ptr u8)))] s32 :
(let @mut @ref opt auto (rec_val fmt::SysFormatOptions []))
(let @mut buffer auto (array_val fmt::FORMATED_STRING_MAX_LEN u8))
Expand Down Expand Up @@ -55,6 +79,11 @@
s
(& @mut opt)]))
(test::AssertSliceEq! (slice_val (front s) n) "2")
(= n (@polymorphic fmt::SysRender [
(rec_val ic32 [(field_val 111) (field_val 222)])
s
(& @mut opt)]))
(test::AssertSliceEq! (slice_val (front s) n) "111+222i")
@doc "test end"
(test::Success!)
(return 0))
Expand Down
3 changes: 2 additions & 1 deletion FrontEnd/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ def ReadModsFromStream(fp, fn="stdin") -> List[cwast.DefMod]:
cwast.CompilerError(
stream.srcloc(), f"expect start of new node, got '{t}']")
mod = ReadSExpr(stream, None, attr)
assert isinstance(mod, cwast.DefMod)
if not isinstance(mod, cwast.DefMod):
cwast.CompilerError(stream.srcloc(), f"expected end of module but for {mod}")
asts.append(mod)
failure = False
except StopIteration:
Expand Down

0 comments on commit 3909c21

Please sign in to comment.