From 38be37f97d3936e1406f9887f3e79d36cc2efddd Mon Sep 17 00:00:00 2001 From: Willyboar Date: Fri, 19 May 2023 14:20:46 +0300 Subject: [PATCH] implement display statements and tests --- src/glove.gleam | 34 ++++++++++++++++++++++------------ test/glove_test.gleam | 25 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/glove.gleam b/src/glove.gleam index b3216c4..e2e30d3 100644 --- a/src/glove.gleam +++ b/src/glove.gleam @@ -77,19 +77,28 @@ pub fn display_inst(inst: Inst) -> String { Div(a, b) -> "div" <> display_value(a) <> ", " <> display_value(b) Rem(a, b) -> "rem" <> display_value(a) <> ", " <> display_value(b) Comp(ty, cmp, a, b) -> "cmp" - And(a, b) -> "and " - Or(a, b) -> "or " + And(a, b) -> "and " <> display_value(a) <> ", " <> display_value(b) + Or(a, b) -> "or " <> display_value(a) <> ", " <> display_value(b) Copy(val) -> "copy " <> display_value(val) - Ret(val) -> "val" - Jnz(val, label1, label2) -> "jnz" - Jmp(str) -> "jmp" + Ret(val) -> { + case val { + Some(val) -> "ret " <> display_value(val) + None -> "ret" + } + } + Jnz(val, if_nonzero, if_zero) -> + "jnz " <> display_value(val) <> ", @" <> if_nonzero <> ", @" <> if_zero + Jmp(str) -> "jmp @" <> str Call(val1, #(typ, val2)) -> "call" - Alloc4(int) -> int.to_string(int) - Alloc8(int) -> int.to_string(int) - Alloc16(int) -> int.to_string(int) + Alloc4(int) -> "alloc4 " <> int.to_string(int) + Alloc8(int) -> "alloc8 " <> int.to_string(int) + Alloc16(int) -> "alloc16 " <> int.to_string(int) Store(typ, val1, val2) -> "store" Load(typ, val) -> "load" - Blit(val1, val2, int) -> "blit" + Blit(src, dest, n) -> + "blit " <> display_value(src) <> ", " <> display_value(dest) <> ", " <> int.to_string( + n, + ) } } @@ -210,12 +219,13 @@ pub type Statement { } // Display function for Statement -// TODO: FINISH AFTER DISPLAY INST pub fn display_statement(state: Statement) -> String { case state { Assign(val, typ, inst) -> - display_value(val) <> " =" <> display_type(typ) <> " " <> "inst" - Volatile(inst) -> "inst" + display_value(val) <> " =" <> display_type(typ) <> " " <> display_inst( + inst, + ) + Volatile(inst) -> display_inst(inst) } } diff --git a/test/glove_test.gleam b/test/glove_test.gleam index 0725e48..3286882 100644 --- a/test/glove_test.gleam +++ b/test/glove_test.gleam @@ -58,3 +58,28 @@ pub fn display_data_item_test() { |> glove.display_data_item |> should.equal("42") } + +// Tests for QBE.Statement Display +pub fn display_statement_test() { + // Test Assign statement + let assign = + glove.Assign( + glove.Temporary("temp"), + glove.Word, + glove.Add(glove.Temporary("a"), glove.Temporary("b")), + ) + assign + |> glove.display_statement + |> should.equal("%temp =w add %a, %b") + // Test Volatile statement + + let volatile = glove.Volatile(glove.Ret(Some(glove.Const(0)))) + volatile + |> glove.display_statement + |> should.equal("ret 0") + + let empty_volatile = glove.Volatile(glove.Ret(None)) + empty_volatile + |> glove.display_statement + |> should.equal("ret") +}