|
@@ -4,6 +4,7 @@ package muesli
|
|
|
import (
|
|
|
"fmt"
|
|
|
"strings"
|
|
|
+ "log"
|
|
|
)
|
|
|
|
|
|
type AstBasicMetaKind string
|
|
@@ -39,21 +40,21 @@ const (
|
|
|
AstKindProgram = AstMetaKindProgram("Program")
|
|
|
AstKindStatements = AstMetaKindStatements("Statements")
|
|
|
AstKindStatement = AstMetaKindStatement("Statement")
|
|
|
- AstKindClosed = AstMetaKindSet("Closed")
|
|
|
+ // AstKindClosed = AstMetaKindClosed("Closed")
|
|
|
AstKindSet = AstMetaKindSet("Set")
|
|
|
AstKindGet = AstMetaKindGet("Get")
|
|
|
AstKindTarget = AstMetaKindTarget("Target")
|
|
|
AstKindCommand = AstMetaKindCommand("Command")
|
|
|
AstKindArguments = AstMetaKindArguments("Arguments")
|
|
|
- AstKindArgument = AstMetaKindArgument("Argument")
|
|
|
- AstKindExpression = AstMetaKindExpression("Expression")
|
|
|
+ // AstKindArgument = AstMetaKindArgument("Argument")
|
|
|
+ // AstKindExpression = AstMetaKindExpression("Expression")
|
|
|
AstKindBlock = AstMetaKindBlock("Block")
|
|
|
AstKindParenthesis = AstMetaKindParenthesis("Parenthesis")
|
|
|
AstKindList = AstMetaKindList("List")
|
|
|
- AstKindCapture = AstMetaKindCapture("Capture")
|
|
|
+ // AstKindCapture = AstMetaKindCapture("Capture")
|
|
|
AstKindWordValue = AstMetaKindWordValue("WordValue")
|
|
|
AstKindWord = AstMetaKindWord("Word")
|
|
|
- AstKindType = AstMetaKindType("Type")
|
|
|
+ // AstKindType = AstMetaKindType("Type")
|
|
|
AstKindValue = AstMetaKindValue("Value")
|
|
|
AstKindEnd = AstMetaKindEnd("End")
|
|
|
AstKindError = AstMetaKindError("Error")
|
|
@@ -65,8 +66,8 @@ func (astkind AstBasicMetaKind) String() string {
|
|
|
return string(astkind)
|
|
|
}
|
|
|
|
|
|
-func (astkind AstBasicMetaKind) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+func (astkind AstBasicMetaKind) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return EmptyListValue()
|
|
|
}
|
|
|
|
|
|
func (astkind AstMetaKindNone) String() string { return "AstNone " }
|
|
@@ -94,71 +95,107 @@ func (astkind AstMetaKindError) String() string { return "AstError "
|
|
|
func (astkind AstMetaKindFlatten) String() string { return "AstFlatten " }
|
|
|
|
|
|
|
|
|
-func (astkind AstMetaKindNone) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
|
|
|
+func (astkind AstMetaKindNone) Run(vm *VM, ast Ast, val ListValue) Value {return EmptyListValue() }
|
|
|
|
|
|
-func (astkind AstMetaKindProgram) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- res := EmptyValueArray()
|
|
|
+func (astkind AstMetaKindProgram) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ res := EmptyListValue()
|
|
|
for _, child := range ast.Children() {
|
|
|
- res = append(res, child.Run(vm, val)...)
|
|
|
+ res.Append(child.Run(vm, val))
|
|
|
}
|
|
|
return res
|
|
|
}
|
|
|
|
|
|
-func (astkind AstMetaKindStatements) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+func (astkind AstMetaKindStatements) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ res := EmptyListValue()
|
|
|
+ for _, child := range ast.Children() {
|
|
|
+ res.Append(child.Run(vm, val))
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+func (astkind AstMetaKindStatement) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return vm.RunChildren(ast, val)
|
|
|
+}
|
|
|
+
|
|
|
+func (astkind AstMetaKindClosed) Run(vm *VM, ast Ast, val ListValue) Value {return EmptyListValue() }
|
|
|
+func (astkind AstMetaKindSet) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ varName := ast.Value()
|
|
|
+ value := vm.RunChildren(ast, val)
|
|
|
+ vm.Register(varName.String(), value)
|
|
|
+ return value
|
|
|
+}
|
|
|
+
|
|
|
+func (astkind AstMetaKindGet) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ varName := ast.Value()
|
|
|
+ return NewListValue(vm.Lookup(varName.String()))
|
|
|
}
|
|
|
-func (astkind AstMetaKindStatement) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+
|
|
|
+func (astkind AstMetaKindTarget) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return NewListValue(ast.Value())
|
|
|
}
|
|
|
-func (astkind AstMetaKindClosed) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
|
|
|
-func (astkind AstMetaKindSet) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
|
|
|
-func (astkind AstMetaKindGet) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
|
|
|
-func (astkind AstMetaKindTarget) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+func (astkind AstMetaKindCommand) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ commandName := ast.Value()
|
|
|
+ arglist, _ := vm.RunChildren(ast, val).(ListValue)
|
|
|
+ arguments := arglist.First()
|
|
|
+ log.Printf("Command execute: %s %v", commandName.String(), arguments)
|
|
|
+ return vm.CallNamed(commandName.String(), arguments)
|
|
|
}
|
|
|
-func (astkind AstMetaKindCommand) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+func (astkind AstMetaKindArguments) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return vm.RunChildren(ast, val)
|
|
|
}
|
|
|
-func (astkind AstMetaKindArguments) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+func (astkind AstMetaKindArgument) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ panic("AstMetaKindArgument")
|
|
|
+ vm.RunChildren(ast, val)
|
|
|
+ reslist, _ := vm.RunChildren(ast, val).(ListValue)
|
|
|
+ return reslist.First()
|
|
|
}
|
|
|
-func (astkind AstMetaKindArgument) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+func (astkind AstMetaKindExpression) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return vm.RunChildren(ast, val)
|
|
|
}
|
|
|
-func (astkind AstMetaKindExpression) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+func (astkind AstMetaKindBlock) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return vm.RunChildren(ast, val)
|
|
|
}
|
|
|
-func (astkind AstMetaKindBlock) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+func (astkind AstMetaKindParenthesis) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return vm.RunChildren(ast, val)
|
|
|
}
|
|
|
-func (astkind AstMetaKindParenthesis) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+func (astkind AstMetaKindList) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return vm.RunChildren(ast, val)
|
|
|
}
|
|
|
-func (astkind AstMetaKindList) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
|
|
|
-func (astkind AstMetaKindCapture) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+
|
|
|
+func (astkind AstMetaKindCapture) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return vm.RunChildren(ast, val)
|
|
|
}
|
|
|
-func (astkind AstMetaKindWordValue) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+func (astkind AstMetaKindWordValue) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return ast.Value()
|
|
|
}
|
|
|
-func (astkind AstMetaKindWord) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
|
|
|
-func (astkind AstMetaKindType) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
|
|
|
-func (astkind AstMetaKindValue) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+
|
|
|
+func (astkind AstMetaKindWord) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return ast.Value()
|
|
|
}
|
|
|
-func (astkind AstMetaKindEnd) Run(vm *VM, ast Ast, val []Value) []Value { return EmptyValueArray() }
|
|
|
-func (astkind AstMetaKindError) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+func (astkind AstMetaKindType) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return ast.Value()
|
|
|
}
|
|
|
-func (astkind AstMetaKindFlatten) Run(vm *VM, ast Ast, val []Value) []Value {
|
|
|
- return EmptyValueArray()
|
|
|
+func (astkind AstMetaKindValue) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return ast.Value()
|
|
|
+}
|
|
|
+
|
|
|
+func (astkind AstMetaKindEnd) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return EmptyListValue()
|
|
|
+}
|
|
|
+
|
|
|
+func (astkind AstMetaKindError) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return NewListValue(ast.Value())
|
|
|
+}
|
|
|
+
|
|
|
+func (astkind AstMetaKindFlatten) Run(vm *VM, ast Ast, val ListValue) Value {
|
|
|
+ return EmptyListValue()
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type AstKind interface {
|
|
|
String() string
|
|
|
- Run(vm *VM, ast Ast, val []Value) []Value
|
|
|
+ Run(vm *VM, ast Ast, val ListValue) Value
|
|
|
}
|
|
|
|
|
|
/*
|
|
@@ -230,6 +267,10 @@ type Ast struct {
|
|
|
token Token
|
|
|
}
|
|
|
|
|
|
+func (ast Ast) Value() Value {
|
|
|
+ return ast.token.Value
|
|
|
+}
|
|
|
+
|
|
|
func NewAst(kind AstKind, parent *Ast, children []*Ast, token Token) *Ast {
|
|
|
ast := &Ast{AstKind: kind, parent: parent, token: token}
|
|
|
return ast.AppendChildren(children...)
|
|
@@ -299,7 +340,10 @@ func (ast Ast) Child(index int) *Ast {
|
|
|
return ast.children[index]
|
|
|
}
|
|
|
|
|
|
-func (ast Ast) Run(vm *VM, val []Value) []Value {
|
|
|
+func (ast Ast) Run(vm *VM, val ListValue) Value {
|
|
|
+ if vm != nil && vm.Tracer != nil {
|
|
|
+ vm.Trace(*vm, ast, val)
|
|
|
+ }
|
|
|
return ast.AstKind.Run(vm, ast, val)
|
|
|
}
|
|
|
|
|
@@ -332,6 +376,22 @@ func (ast Ast) Display() {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+func (ast Ast) Dump() string {
|
|
|
+ result := ""
|
|
|
+ ast.Walk(func(node *Ast) *Ast {
|
|
|
+ depth := node.Depth()
|
|
|
+ result += fmt.Sprintf("%s", strings.Repeat("--", depth))
|
|
|
+ if node != nil {
|
|
|
+ result += fmt.Sprintf("Ast: %s\n", node.String())
|
|
|
+ } else {
|
|
|
+ result += fmt.Sprintf("Ast: nil node\n")
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ return result
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
func (ast Ast) Depth() int {
|
|
|
var depth int = 0
|
|
|
parent := ast.Parent()
|
|
@@ -377,6 +437,21 @@ func NewAstWithToken(astkind AstKind, token Token) *Ast {
|
|
|
return NewAst(astkind, nil, EmptyAstArray(), token)
|
|
|
}
|
|
|
|
|
|
+// If AST has errors, return it as a merged error, otherwise returns nil
|
|
|
+func (ast *Ast) ToError() error {
|
|
|
+ errlist := ast.Errors()
|
|
|
+ if len(errlist) < 1 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ sep := ""
|
|
|
+ res := ""
|
|
|
+ for _, err := range errlist {
|
|
|
+ res = fmt.Sprintf("%s%s%s", res, sep, err)
|
|
|
+ sep = "\n"
|
|
|
+ }
|
|
|
+ return fmt.Errorf("%s", res)
|
|
|
+}
|
|
|
+
|
|
|
/*
|
|
|
type AstProgram struct{ Ast }
|
|
|
type AstStatements struct{ Ast }
|