Skip to content

Commit

Permalink
Implement display blocks and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Willyboar committed May 19, 2023
1 parent 38be37f commit f80f57a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/glove.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import gleam/int
import gleam/string
import gleam/option.{None, Option, Some}
import gleam/list

// QBE Comparison Operators
pub type Comp {
Expand Down Expand Up @@ -185,7 +187,12 @@ pub type TypeDef {

// Display function for TypeDef
pub fn display_type_def(def: TypeDef) -> String {
todo
let align_str = case def.align {
Some(align) -> "align " <> int.to_string(align) <> " "
None -> ""
}

"type :" <> def.name <> " = " <> align_str <> "{ " <> " }"
}

// QBE Data definition item
Expand Down Expand Up @@ -219,8 +226,8 @@ pub type Statement {
}

// Display function for Statement
pub fn display_statement(state: Statement) -> String {
case state {
pub fn display_statement(stmt: Statement) -> String {
case stmt {
Assign(val, typ, inst) ->
display_value(val) <> " =" <> display_type(typ) <> " " <> display_inst(
inst,
Expand All @@ -231,12 +238,18 @@ pub fn display_statement(state: Statement) -> String {

// Function block with a label
pub type Block {
Block(label: String, statements: #(Statement))
Block(label: String, statements: List(Statement))
}

// Display function for block
pub fn display_block(block: Block) -> String {
todo
let label = block.label
let statements =
block.statements
|> list.map(display_statement)
|> string.join("\n")

label <> ":\n" <> statements
}

/// Adds a new instruction to the block
Expand Down Expand Up @@ -312,7 +325,7 @@ pub fn private_with_section() -> Nil {
todo
}

/// A complete IL file
// A complete IL file
pub type Module {
Module(functions: #(Function), types: #(TypeDef), data: #(DataDef))
}
Expand Down
25 changes: 25 additions & 0 deletions test/glove_test.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gleam/option.{None, Some}
import gleam/list
import gleeunit
import gleeunit/should
import glove
Expand Down Expand Up @@ -83,3 +84,27 @@ pub fn display_statement_test() {
|> glove.display_statement
|> should.equal("ret")
}

// Tests for QBE.Block Display
pub fn display_block_test() {
// Test empty block
let empty_block = glove.Block("label", [])
empty_block
|> glove.display_block()
|> should.equal("label:\n")

// Test block with statements
let statements = [
glove.Assign(
glove.Temporary("temp1"),
glove.Word,
glove.Add(glove.Temporary("a"), glove.Temporary("b")),
),
glove.Volatile(glove.Ret(Some(glove.Temporary("temp1")))),
]

let block_with_statements = glove.Block("label", statements)
block_with_statements
|> glove.display_block
|> should.equal("label:\n%temp1 =w add %a, %b\nret %temp1")
}

0 comments on commit f80f57a

Please sign in to comment.