From 2fe7a8e65f07f0433b09fbe2eee80ef66503b189 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 16 Aug 2025 14:46:47 +0200 Subject: [PATCH] Implemented memory allocation of basic types --- lib/io/io_windows.q | 5 +++-- lib/io/tmp_windows.q | 10 ++++++---- src/core/decompose.go | 2 +- src/core/evaluateArray.go | 15 +++++++++++---- src/core/evaluateCall.go | 8 +++----- src/types/Is.go | 5 +++++ tests/examples_test.go | 1 + 7 files changed, 30 insertions(+), 16 deletions(-) diff --git a/lib/io/io_windows.q b/lib/io/io_windows.q index fc82e32..a830689 100644 --- a/lib/io/io_windows.q +++ b/lib/io/io_windows.q @@ -1,7 +1,8 @@ write(buffer string) -> (written int) { stdout := kernel32.GetStdHandle(-11) - kernel32.WriteFile(stdout, buffer.ptr, buffer.len, 0, 0) - return buffer.len + count := new(uint32) + kernel32.WriteFile(stdout, buffer.ptr, buffer.len, count, 0) + return [count] } extern { diff --git a/lib/io/tmp_windows.q b/lib/io/tmp_windows.q index 1f4bb09..234d89e 100644 --- a/lib/io/tmp_windows.q +++ b/lib/io/tmp_windows.q @@ -1,11 +1,13 @@ read2(buffer *byte, length int) -> int { stdin := kernel32.GetStdHandle(-10) - kernel32.ReadFile(stdin, buffer, length, 0, 0) - return length + count := new(uint32) + kernel32.ReadFile(stdin, buffer, length, count, 0) + return [count] } write2(buffer *byte, length int) -> (written int) { stdout := kernel32.GetStdHandle(-11) - kernel32.WriteFile(stdout, buffer, length, 0, 0) - return length + count := new(uint32) + kernel32.WriteFile(stdout, buffer, length, count, 0) + return [count] } \ No newline at end of file diff --git a/src/core/decompose.go b/src/core/decompose.go index 06cc71d..67e034b 100644 --- a/src/core/decompose.go +++ b/src/core/decompose.go @@ -23,7 +23,7 @@ func (f *Function) decompose(nodes []*expression.Expression, typeCheck []*ssa.Pa Encountered: value.Type().Name(), Expected: typeCheck[i].Typ.Name(), ParameterName: typeCheck[i].Name, - }, f.File, value.(ssa.HasSource).Start()) + }, f.File, node.Source().StartPos) } structure, isStruct := value.(*ssa.Struct) diff --git a/src/core/evaluateArray.go b/src/core/evaluateArray.go index 85a8967..17f53d0 100644 --- a/src/core/evaluateArray.go +++ b/src/core/evaluateArray.go @@ -8,18 +8,25 @@ import ( // evaluateArray converts a array indexing expression to an SSA value. func (f *Function) evaluateArray(expr *expression.Expression) (ssa.Value, error) { + address := expr.Children[0] - index := expr.Children[1] addressValue, err := f.evaluate(address) if err != nil { return nil, err } - indexValue, err := f.evaluate(index) + var indexValue ssa.Value - if err != nil { - return nil, err + if len(expr.Children) > 1 { + index := expr.Children[1] + indexValue, err = f.evaluate(index) + + if err != nil { + return nil, err + } + } else { + indexValue = f.Append(&ssa.Int{Int: 0}) } v := f.Append(&ssa.Load{ diff --git a/src/core/evaluateCall.go b/src/core/evaluateCall.go index 29d971d..3086458 100644 --- a/src/core/evaluateCall.go +++ b/src/core/evaluateCall.go @@ -15,14 +15,12 @@ func (f *Function) evaluateCall(expr *expression.Expression) (ssa.Value, error) if identifier.Token.Kind == token.Identifier { switch identifier.String(f.File.Bytes) { case "new": - structName := expr.Children[1].String(f.File.Bytes) - pkg := f.Env.Packages[f.Package] - structure := pkg.Structs[structName] + typ := ParseType([]token.Token{expr.Children[1].Token}, f.File.Bytes, f.Env) malloc := f.Env.Function("mem", "alloc") - returnType := &types.Pointer{To: structure} + returnType := &types.Pointer{To: typ} size := f.Append(&ssa.Int{ - Int: structure.Size(), + Int: typ.Size(), }) call := f.Append(&ssa.Call{ diff --git a/src/types/Is.go b/src/types/Is.go index a136519..6465681 100644 --- a/src/types/Is.go +++ b/src/types/Is.go @@ -53,5 +53,10 @@ func Is(a Type, b Type) bool { return true } + // TODO: Remove temporary hack to allow uint32 -> int64 conversion + if (a == UInt32 || a == UInt64) && b == Int64 { + return true + } + return false } \ No newline at end of file diff --git a/tests/examples_test.go b/tests/examples_test.go index a88635e..f91711d 100644 --- a/tests/examples_test.go +++ b/tests/examples_test.go @@ -18,6 +18,7 @@ var examples = []run{ {"collatz", "", "12 6 3 10 5 16 8 4 2 1", 0}, {"prime", "", "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97", 0}, {"point", "", "Point: 1, 2", 0}, + {"echo", "Echo", "Echo", 0}, } func TestExamples(t *testing.T) { -- 2.51.2