This commit is contained in:
parent
0db420aae4
commit
7245af0438
5 changed files with 35 additions and 30 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue