From a53b2943928e5d1e3e954eb95d8e665266ae3f64 Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Mon, 25 Mar 2024 16:58:23 +0100 Subject: [PATCH] Fix bug --- backend_wat.go | 13 +++++++++++-- example/add.lang | 1 + example/test.lang | 3 +++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 example/test.lang diff --git a/backend_wat.go b/backend_wat.go index 699d165..40f04ba 100644 --- a/backend_wat.go +++ b/backend_wat.go @@ -134,6 +134,8 @@ func castPrimitiveWAT(from PrimitiveType, to PrimitiveType) (string, error) { if getBits(to) < getBits(from) { return getTypeCast(to), nil } + + return "", nil } if getBits(from) < 64 && getBits(to) == 64 { @@ -334,7 +336,6 @@ func compileStatementWAT(stmt Statement, block *Block) (string, error) { } // TODO: upcast to return type for non-primitive types - return wat + "return\n", nil case Statement_DeclareLocalVariable: dlv := stmt.Value.(DeclareLocalVariableStatement) @@ -347,7 +348,15 @@ func compileStatementWAT(stmt Statement, block *Block) (string, error) { return "", err } - // TODO: make sure to upcast to target type + if dlv.VariableType.Type == Type_Primitive { + castWAT, err := castPrimitiveWAT(dlv.Initializer.ValueType.Value.(PrimitiveType), dlv.VariableType.Value.(PrimitiveType)) + if err != nil { + return "", err + } + + wat += castWAT + } + return wat + "local.set $" + strconv.Itoa(block.Locals[dlv.Variable].Index) + "\n", nil case Statement_If: ifS := stmt.Value.(IfStatement) diff --git a/example/add.lang b/example/add.lang index f324eed..47be3bd 100644 --- a/example/add.lang +++ b/example/add.lang @@ -8,6 +8,7 @@ void a() { (u8, u8) doNothing(u8 a, u8 b) { a = b; + u16 c = 1u8; return a, b; } diff --git a/example/test.lang b/example/test.lang new file mode 100644 index 0000000..8b96e03 --- /dev/null +++ b/example/test.lang @@ -0,0 +1,3 @@ +void doNothing() { + u64 sus = 1u8; +} \ No newline at end of file