From 7245af0438b8e0786cee0e5b05be0f9a62806453 Mon Sep 17 00:00:00 2001 From: neemek Date: Wed, 23 Sep 2026 09:13:28 +0200 Subject: [PATCH] go fix my shit --- core/compiler.go | 22 +++++++++++----------- core/parser.go | 5 +++-- core/stack_test.go | 5 +++-- core/values.go | 28 +++++++++++++++------------- core/vm.go | 5 +++-- 5 files changed, 35 insertions(+), 30 deletions(-) diff --git a/core/compiler.go b/core/compiler.go index 83098d5..928eff0 100644 --- a/core/compiler.go +++ b/core/compiler.go @@ -3,6 +3,7 @@ package core import ( "errors" "fmt" + "slices" "strings" ) @@ -116,8 +117,7 @@ func (e CompilerError) Format() string { b.WriteString("\nsource trace:") // print import stack trace - for i := len(e.Trace) - 1; i >= 0; i-- { - p := e.Trace[i] + for i, p := range slices.Backward(e.Trace) { b.WriteString(fmt.Sprintf("\n[%d] %s", i, p)) } @@ -180,20 +180,20 @@ func (c *Compiler) Compile(p *Program) (TypeSignature, error) { } func EscapeString(in string) string { - out := "" + var out strings.Builder escaped := false for _, ch := range in { if escaped { switch ch { case 'n': - out += "\n" + out.WriteRune('\n') case 't': - out += "\t" + out.WriteRune('\t') case 'r': - out += "\r" + out.WriteRune('\r') default: - out += string(ch) + out.WriteString(string(ch)) } escaped = false continue @@ -203,11 +203,11 @@ func EscapeString(in string) string { case '\\': escaped = true default: - out += string(ch) + out.WriteString(string(ch)) } } - return out + return out.String() } func (c *Compiler) resolveType(value TypeSignature) TypeSignature { @@ -968,8 +968,8 @@ func (c *Compiler) compileAssignFromStack(to Node, sig TypeSignature, declare bo c.add(InstructionDestructureTuple) // iterate from top to bottom - for i := len(t.items) - 1; i >= 0; i-- { - _, err := c.compileAssignFromStack(t.items[i], tsig.Contents[i], declare) + for i, v := range slices.Backward(t.items) { + _, err := c.compileAssignFromStack(v, tsig.Contents[i], declare) if err != nil { return nil, err } diff --git a/core/parser.go b/core/parser.go index ca360af..7383f51 100644 --- a/core/parser.go +++ b/core/parser.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "math/big" + "slices" "strconv" "strings" ) @@ -68,8 +69,8 @@ func (p ParsingError) Format() string { b.WriteRune('\n') b.WriteRune('\n') - for i := len(p.Trace) - 1; i >= 0; i-- { - b.WriteString(fmt.Sprintf("[%d] %s\n", i, p.Trace[i])) + for i, v := range slices.Backward(p.Trace) { + b.WriteString(fmt.Sprintf("[%d] %s\n", i, v)) } return b.String() diff --git a/core/stack_test.go b/core/stack_test.go index 41f3d79..2bc9f86 100644 --- a/core/stack_test.go +++ b/core/stack_test.go @@ -2,18 +2,19 @@ package core import ( "fmt" + "slices" "testing" ) func CompareScope(t *testing.T, expectedScope []map[string]Value, actualScope *Scope) { s := actualScope - for i := len(expectedScope) - 1; i >= 0; i-- { + for _, e := range slices.Backward(expectedScope) { if s == nil { t.Fatal("scope cut unexpecetantly short") } - for name, value := range expectedScope[i] { + for name, value := range e { v, ok := s.current[name] if !ok { t.Errorf("variable %s not found in correct scope", name) diff --git a/core/values.go b/core/values.go index efb5f41..e0cee7c 100644 --- a/core/values.go +++ b/core/values.go @@ -55,7 +55,7 @@ func (v ValueType) String() string { } // GoToValue convert go values to anglais VM-values. Works for some values (nil, bool, float64, int, string, slices, maps) -func GoToValue(gov interface{}) Value { +func GoToValue(gov any) Value { switch v := gov.(type) { case nil: return &NilValue{} @@ -79,7 +79,7 @@ func GoToValue(gov interface{}) Value { return &StringValue{ v, } - case map[string]interface{}: + case map[string]any: values := map[string]Value{} for key, value := range v { values[key] = GoToValue(value) @@ -447,16 +447,17 @@ func (v *ListValue) Type() ValueType { } func (v *ListValue) String() string { - out := "[" + var out strings.Builder + out.WriteString("[") for i, item := range v.Items { if i != 0 { - out += ", " + out.WriteString(", ") } - out += item.DebugString() + out.WriteString(item.DebugString()) } - out += "]" + out.WriteString("]") - return out + return out.String() } func (v *ListValue) DebugString() string { @@ -613,20 +614,21 @@ func (v *TupleValue) Type() ValueType { } func (v *TupleValue) String() string { - out := "(" + var out strings.Builder + out.WriteString("(") for i, item := range v.Items { if i != 0 { - out += ", " + out.WriteString(", ") } - out += item.DebugString() + out.WriteString(item.DebugString()) } if len(v.Items) <= 1 { - out += "," + out.WriteString(",") } - out += ")" + out.WriteString(")") - return out + return out.String() } func (v *TupleValue) DebugString() string { diff --git a/core/vm.go b/core/vm.go index f07890f..068b1c6 100644 --- a/core/vm.go +++ b/core/vm.go @@ -9,6 +9,7 @@ import ( "math" "math/big" "os" + "slices" "strconv" "strings" ) @@ -926,8 +927,8 @@ func (vm *VM) Next() bool { vm.scope = f.Scope vm.descend() - for i := len(f.Params) - 1; i >= 0; i-- { - vm.addVar(f.Params[i].Name, vm.Stack.Pop()) + for _, v := range slices.Backward(f.Params) { + vm.addVar(v.Name, vm.Stack.Pop()) } if f.Parent != nil {