Skip to content

Commit

Permalink
Make some progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Willyboar committed May 18, 2023
1 parent 0f1294a commit 14ea146
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 14 deletions.
60 changes: 47 additions & 13 deletions src/glove.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gleam/int
import gleam/option.{Option}
import gleam/option.{None, Option, Some}

// QBE Comparison Operators
pub type Comp {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -155,23 +175,32 @@ 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)
//
Constant(Int)
}

// 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
Expand All @@ -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
Expand All @@ -191,7 +225,7 @@ pub type Block {
}

// Display function for block
pub fn display_block() -> String {
pub fn display_block(block: Block) -> String {
todo
}

Expand Down
31 changes: 30 additions & 1 deletion test/glove_test.gleam
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gleam/option.{None, Some}
import gleeunit
import gleeunit/should
import glove
Expand All @@ -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")
Expand All @@ -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")
}

0 comments on commit 14ea146

Please sign in to comment.