From 5e539c3f8151ef135481cae0a266102e66ffc2df Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 24 Jun 2026 16:24:14 +0200 Subject: [PATCH] Fixed global variable assignment operators --- src/core/define.go | 44 +++++++++++++++++++++++--------------------- tests/global-math.q | 16 ++++++++++++++++ tests/global.q | 6 +++--- tests/tests_test.go | 1 + 4 files changed, 43 insertions(+), 24 deletions(-) create mode 100644 tests/global-math.q diff --git a/src/core/define.go b/src/core/define.go index 5082c2f..c58451d 100644 --- a/src/core/define.go +++ b/src/core/define.go @@ -28,24 +28,6 @@ func (f *Function) define(left *expression.Expression, right *expression.Express return err } - global, isGlobal := leftValue.(*ssa.Global) - - if isGlobal { - zero := f.Append(&ssa.Int{Int: 0}) - - f.Append(&ssa.Store{ - Memory: &ssa.Memory{ - Typ: global.Typ, - Address: global, - Index: zero, - Source: global.Source, - }, - Value: rightValue, - }) - - return nil - } - call, isCall := rightValue.(*ssa.Call) if isCall && len(call.Func.Typ.Output) != 1 { @@ -71,15 +53,35 @@ func (f *Function) define(left *expression.Expression, right *expression.Express if isAssign && root.Token.Kind != token.Assign { operator := removeAssign(root.Token.Kind) + leftValueDeref, err := f.evaluateRight(left) - operation := f.Append(&ssa.BinaryOp{ + if err != nil { + return err + } + + rightValue = f.Append(&ssa.BinaryOp{ Op: operator, - Left: leftValue, + Left: leftValueDeref, Right: rightValue, Source: root.Source(), }) + } + + global, isGlobal := leftValue.(*ssa.Global) + + if isGlobal { + zero := f.Append(&ssa.Int{Int: 0}) + + f.Append(&ssa.Store{ + Memory: &ssa.Memory{ + Typ: global.Typ, + Address: global, + Index: zero, + Source: global.Source, + }, + Value: rightValue, + }) - f.Block().Identify(name, operation) return nil } diff --git a/tests/global-math.q b/tests/global-math.q new file mode 100644 index 0000000..13093f0 --- /dev/null +++ b/tests/global-math.q @@ -0,0 +1,16 @@ +main() { + x = 10 + assert x == 10 + x += 1 + assert x == 11 + x -= 1 + assert x == 10 + x *= 2 + assert x == 20 + x /= 2 + assert x == 10 +} + +global { + x int +} \ No newline at end of file diff --git a/tests/global.q b/tests/global.q index 84f69f2..7570816 100644 --- a/tests/global.q +++ b/tests/global.q @@ -1,11 +1,11 @@ main() { assert x == 0 - f() + f(42) assert x == 42 } -f() { - x = 42 +f(n int) { + x = n } global { diff --git a/tests/tests_test.go b/tests/tests_test.go index 2e4de01..0d9e808 100644 --- a/tests/tests_test.go +++ b/tests/tests_test.go @@ -151,6 +151,7 @@ var tests = []run{ {"const-string", nil, "", "Hello", 0}, {"const-color", nil, "", "", 0}, {"global", nil, "", "", 0}, + {"global-math", nil, "", "", 0}, {"c-length", nil, "", "", 0}, {"c-string", nil, "", "", 0}, {"cli-args", nil, "", "", 0}, -- 2.51.2