Skip to content

Commit

Permalink
fix: rework if to support nested expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpie123 committed May 12, 2022
1 parent 66e3821 commit 4541dd1
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion wat/printer.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,13 @@
[(cons (Call f) is) (string-append (tabs ntabs) "(call $" (symbol->string f) ")\n"
(parse-instruction-list is ntabs))]
[(cons (WatIf p t f) is) (string-append
(parse-instruction-list (seq p) ntabs)
(tabs ntabs) "(if (result i64)\n"
(parse-instruction-list (seq p t f) (add1 ntabs))
(tabs (add1 ntabs)) "(then\n" (parse-instruction-list (seq t) (+ 2 ntabs))
(tabs (add1 ntabs)) ")\n"
(tabs (add1 ntabs)) "(else\n"
(parse-instruction-list (seq f) (+ 2 ntabs))
(tabs (add1 ntabs)) ")\n"
(tabs ntabs) ")\n"
(parse-instruction-list is ntabs))]
[(cons (ConstT t n) is) (string-append
Expand Down Expand Up @@ -161,9 +166,20 @@
(tabs ntabs) ")\n"
(parse-instruction-list is ntabs)
)]
[(cons (Loop e n) is) (string-append (tabs ntabs) "(loop $" (symbol->string n) "\n"
(parse-instruction-list (seq e) (add1 ntabs))
(tabs ntabs) ")\n"
(parse-instruction-list is ntabs)
)]
[(cons (BranchIf p n) is) (string-append (tabs ntabs) "(br_if\n"
(parse-instruction-list (seq p) (add1 ntabs))
(tabs ntabs) "$" (symbol->string n) ")\n"
(parse-instruction-list is ntabs)
)]
[(cons (Drop) is) (string-append (tabs ntabs) "(drop)\n"
(parse-instruction-list is ntabs)
)]
[(cons (Load) is) (parse-instruction-list is ntabs)]
[(cons x _) (parse-error "Instruction not recognized:" x)]
[x (parse-error "Expected instruction list, got:" x)]))

Expand Down

0 comments on commit 4541dd1

Please sign in to comment.