From 14ea14619ac7b97e463cf665b8e1a791d382e796 Mon Sep 17 00:00:00 2001 From: Willyboar Date: Fri, 19 May 2023 00:53:37 +0300 Subject: [PATCH] Make some progress --- src/glove.gleam | 60 +++++++++++++++++++++++++++++++++---------- test/glove_test.gleam | 31 +++++++++++++++++++++- 2 files changed, 77 insertions(+), 14 deletions(-) diff --git a/src/glove.gleam b/src/glove.gleam index 2d41703..b3216c4 100644 --- a/src/glove.gleam +++ b/src/glove.gleam @@ -1,5 +1,5 @@ import gleam/int -import gleam/option.{Option} +import gleam/option.{None, Option, Some} // QBE Comparison Operators pub type Comp { @@ -68,8 +68,29 @@ pub type Inst { } // Display function for Instructions -pub fn display_inst() -> String { - todo +// UNFINISHED +pub fn display_inst(inst: Inst) -> String { + case inst { + Add(a, b) -> "add " <> display_value(a) <> ", " <> display_value(b) + Sub(a, b) -> "sub" <> display_value(a) <> ", " <> display_value(b) + Mul(a, b) -> "mul" <> display_value(a) <> ", " <> display_value(b) + 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 " + Copy(val) -> "copy " <> display_value(val) + Ret(val) -> "val" + Jnz(val, label1, label2) -> "jnz" + Jmp(str) -> "jmp" + Call(val1, #(typ, val2)) -> "call" + Alloc4(int) -> int.to_string(int) + Alloc8(int) -> int.to_string(int) + Alloc16(int) -> int.to_string(int) + Store(typ, val1, val2) -> "store" + Load(typ, val) -> "load" + Blit(val1, val2, int) -> "blit" + } } // QBE Value for instructions @@ -103,8 +124,7 @@ pub type Type { Halfword } -// Agreegate type with a specified name -// Agreegate(TypeDef) +//Agreegate(TypeDef) // Display Type function pub fn display_type(ty: Type) -> String { @@ -155,14 +175,14 @@ pub type TypeDef { } // Display function for TypeDef -pub fn display_type_def() -> Nil { +pub fn display_type_def(def: TypeDef) -> String { todo } // QBE Data definition item pub type DataItem { // Symbol and offset - Symbol(String, Int) + Symbol(String, Option(Int)) // String Str(String) // @@ -170,8 +190,17 @@ pub type DataItem { } // Display function for DataItem -pub fn display_data_item() -> String { - todo +pub fn display_data_item(item: DataItem) -> String { + case item { + Symbol(name, offset) -> { + case offset { + Some(off) -> "$" <> name <> " +" <> int.to_string(off) + None -> "$" <> name + } + } + Str(string) -> "\"" <> string <> "\"" + Constant(val) -> int.to_string(val) + } } // IR Statement @@ -180,9 +209,14 @@ pub type Statement { Volatile(Inst) } -// Display function for Statement -pub fn display_statement() -> String { - todo +// 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" + } } // Function block with a label @@ -191,7 +225,7 @@ pub type Block { } // Display function for block -pub fn display_block() -> String { +pub fn display_block(block: Block) -> String { todo } diff --git a/test/glove_test.gleam b/test/glove_test.gleam index be4796d..0725e48 100644 --- a/test/glove_test.gleam +++ b/test/glove_test.gleam @@ -1,3 +1,4 @@ +import gleam/option.{None, Some} import gleeunit import gleeunit/should import glove @@ -8,7 +9,7 @@ pub fn main() { // gleeunit test functions end in `_test` -// Tests for QBE.Value +// Tests for QBE.Value Display // Test QBE.Value.Temporary pub fn qbe_value_temp_test() { glove.Temporary("temp") @@ -29,3 +30,31 @@ pub fn qbe_value_const_test() { |> glove.display_value() |> should.equal("1") } + +// Tests for QBE.DataItem Display +pub fn display_data_item_test() { + let item1 = glove.Symbol("symbol1", Some(10)) + let item2 = glove.Symbol("symbol2", None) + let item3 = glove.Str("string value") + let item4 = glove.Constant(42) + + // Symbol with offset + item1 + |> glove.display_data_item + |> should.equal("$symbol1 +10") + + // Symbol without offset + item2 + |> glove.display_data_item + |> should.equal("$symbol2") + + // String value + item3 + |> glove.display_data_item + |> should.equal("\"string value\"") + + // Constant value + item4 + |> glove.display_data_item + |> should.equal("42") +}