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 (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -116,8 +117,7 @@ func (e CompilerError) Format() string {
|
||||||
|
|
||||||
b.WriteString("\nsource trace:")
|
b.WriteString("\nsource trace:")
|
||||||
// print import stack trace
|
// print import stack trace
|
||||||
for i := len(e.Trace) - 1; i >= 0; i-- {
|
for i, p := range slices.Backward(e.Trace) {
|
||||||
p := e.Trace[i]
|
|
||||||
b.WriteString(fmt.Sprintf("\n[%d] %s", i, p))
|
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 {
|
func EscapeString(in string) string {
|
||||||
out := ""
|
var out strings.Builder
|
||||||
escaped := false
|
escaped := false
|
||||||
|
|
||||||
for _, ch := range in {
|
for _, ch := range in {
|
||||||
if escaped {
|
if escaped {
|
||||||
switch ch {
|
switch ch {
|
||||||
case 'n':
|
case 'n':
|
||||||
out += "\n"
|
out.WriteRune('\n')
|
||||||
case 't':
|
case 't':
|
||||||
out += "\t"
|
out.WriteRune('\t')
|
||||||
case 'r':
|
case 'r':
|
||||||
out += "\r"
|
out.WriteRune('\r')
|
||||||
default:
|
default:
|
||||||
out += string(ch)
|
out.WriteString(string(ch))
|
||||||
}
|
}
|
||||||
escaped = false
|
escaped = false
|
||||||
continue
|
continue
|
||||||
|
|
@ -203,11 +203,11 @@ func EscapeString(in string) string {
|
||||||
case '\\':
|
case '\\':
|
||||||
escaped = true
|
escaped = true
|
||||||
default:
|
default:
|
||||||
out += string(ch)
|
out.WriteString(string(ch))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return out
|
return out.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Compiler) resolveType(value TypeSignature) TypeSignature {
|
func (c *Compiler) resolveType(value TypeSignature) TypeSignature {
|
||||||
|
|
@ -968,8 +968,8 @@ func (c *Compiler) compileAssignFromStack(to Node, sig TypeSignature, declare bo
|
||||||
c.add(InstructionDestructureTuple)
|
c.add(InstructionDestructureTuple)
|
||||||
|
|
||||||
// iterate from top to bottom
|
// iterate from top to bottom
|
||||||
for i := len(t.items) - 1; i >= 0; i-- {
|
for i, v := range slices.Backward(t.items) {
|
||||||
_, err := c.compileAssignFromStack(t.items[i], tsig.Contents[i], declare)
|
_, err := c.compileAssignFromStack(v, tsig.Contents[i], declare)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
@ -68,8 +69,8 @@ func (p ParsingError) Format() string {
|
||||||
b.WriteRune('\n')
|
b.WriteRune('\n')
|
||||||
b.WriteRune('\n')
|
b.WriteRune('\n')
|
||||||
|
|
||||||
for i := len(p.Trace) - 1; i >= 0; i-- {
|
for i, v := range slices.Backward(p.Trace) {
|
||||||
b.WriteString(fmt.Sprintf("[%d] %s\n", i, p.Trace[i]))
|
b.WriteString(fmt.Sprintf("[%d] %s\n", i, v))
|
||||||
}
|
}
|
||||||
|
|
||||||
return b.String()
|
return b.String()
|
||||||
|
|
|
||||||
|
|
@ -2,18 +2,19 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CompareScope(t *testing.T, expectedScope []map[string]Value, actualScope *Scope) {
|
func CompareScope(t *testing.T, expectedScope []map[string]Value, actualScope *Scope) {
|
||||||
s := actualScope
|
s := actualScope
|
||||||
|
|
||||||
for i := len(expectedScope) - 1; i >= 0; i-- {
|
for _, e := range slices.Backward(expectedScope) {
|
||||||
if s == nil {
|
if s == nil {
|
||||||
t.Fatal("scope cut unexpecetantly short")
|
t.Fatal("scope cut unexpecetantly short")
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, value := range expectedScope[i] {
|
for name, value := range e {
|
||||||
v, ok := s.current[name]
|
v, ok := s.current[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Errorf("variable %s not found in correct scope", name)
|
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)
|
// 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) {
|
switch v := gov.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
return &NilValue{}
|
return &NilValue{}
|
||||||
|
|
@ -79,7 +79,7 @@ func GoToValue(gov interface{}) Value {
|
||||||
return &StringValue{
|
return &StringValue{
|
||||||
v,
|
v,
|
||||||
}
|
}
|
||||||
case map[string]interface{}:
|
case map[string]any:
|
||||||
values := map[string]Value{}
|
values := map[string]Value{}
|
||||||
for key, value := range v {
|
for key, value := range v {
|
||||||
values[key] = GoToValue(value)
|
values[key] = GoToValue(value)
|
||||||
|
|
@ -447,16 +447,17 @@ func (v *ListValue) Type() ValueType {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *ListValue) String() string {
|
func (v *ListValue) String() string {
|
||||||
out := "["
|
var out strings.Builder
|
||||||
|
out.WriteString("[")
|
||||||
for i, item := range v.Items {
|
for i, item := range v.Items {
|
||||||
if i != 0 {
|
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 {
|
func (v *ListValue) DebugString() string {
|
||||||
|
|
@ -613,20 +614,21 @@ func (v *TupleValue) Type() ValueType {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *TupleValue) String() string {
|
func (v *TupleValue) String() string {
|
||||||
out := "("
|
var out strings.Builder
|
||||||
|
out.WriteString("(")
|
||||||
for i, item := range v.Items {
|
for i, item := range v.Items {
|
||||||
if i != 0 {
|
if i != 0 {
|
||||||
out += ", "
|
out.WriteString(", ")
|
||||||
}
|
}
|
||||||
out += item.DebugString()
|
out.WriteString(item.DebugString())
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v.Items) <= 1 {
|
if len(v.Items) <= 1 {
|
||||||
out += ","
|
out.WriteString(",")
|
||||||
}
|
}
|
||||||
out += ")"
|
out.WriteString(")")
|
||||||
|
|
||||||
return out
|
return out.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *TupleValue) DebugString() string {
|
func (v *TupleValue) DebugString() string {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
@ -926,8 +927,8 @@ func (vm *VM) Next() bool {
|
||||||
vm.scope = f.Scope
|
vm.scope = f.Scope
|
||||||
vm.descend()
|
vm.descend()
|
||||||
|
|
||||||
for i := len(f.Params) - 1; i >= 0; i-- {
|
for _, v := range slices.Backward(f.Params) {
|
||||||
vm.addVar(f.Params[i].Name, vm.Stack.Pop())
|
vm.addVar(v.Name, vm.Stack.Pop())
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.Parent != nil {
|
if f.Parent != nil {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue