Browse Source

WIP Separate VM and AST better.

Beoran 4 years ago
parent
commit
83295e236d
2 changed files with 20 additions and 21 deletions
  1. 1 1
      ast.go
  2. 19 20
      vm.go

+ 1 - 1
ast.go

@@ -241,7 +241,7 @@ func (astkind AstMetaKindValue) Eval(vm *VM, ast Ast, val ...Value) []Value {
 }
 
 func (astkind AstMetaKindEnd) Eval(vm *VM, ast Ast, val ...Value) []Value {
-	return []Value{EmptyValue{}}
+	return val
 }
 
 func (astkind AstMetaKindError) Eval(vm *VM, ast Ast, val ...Value) []Value {

+ 19 - 20
vm.go

@@ -160,7 +160,8 @@ func NewDefinedValue(name string, params []*Parameter, body *BlockValue) *Define
 }
 
 func (defined *DefinedValue) Call(vm *VM, arguments ...Value) []Value {
-	for i , arg := range arguments {
+	// vm.Logf("Call defined value: %v", defined)
+    for i , arg := range arguments {
 		if i >= len(defined.Parameters) {
 			break
 		}
@@ -569,8 +570,18 @@ func (vm *VM) PopScope() *Scope {
 }
 
 func (block * BlockValue) Call(vm *VM, arguments ...Value) []Value {
-	ast := block.Ast
-	arr := vm.RunAst(*ast, arguments...)
+    ast := block.Ast
+    // Now, don't run the bloc itself but the Statements node
+    // that should be in it.
+    children := ast.Children()
+    if len(children) < 1 {
+        return Fail(fmt.Errorf("Block has no statements."))
+    } 
+    if len(children) > 1 {
+        return Fail(fmt.Errorf("Block has too many statements."))
+    } 
+    statements := children[0]
+	arr := vm.RunAst(*statements, arguments...)
 	return arr
 }
 
@@ -631,22 +642,6 @@ func (vm *VM) CallNamed(name string, arguments ...Value) []Value {
 	} else {
 		return ReturnError(vm.AddTrace(NewErrorValuef("Cannot call %s: %v. Not callable", name, value)))
 	}
-	
-	/*
-	value := vm.Lookup(name)
-	switch toCall := value.(type) {
-	case *BuiltinValue:
-		return vm.CallBuiltin(toCall.Handler, arguments...)
-	case *DefinedValue:
-		return vm.CallDefined(toCall.Body.Ast, arguments...)
-	case *CoverValue:
-		return vm.CallCover(toCall, arguments...)
-	case *BlockValue:
-		return vm.CallBlock(toCall.Ast, arguments...)	
-	default:
-		return ReturnError(vm.AddTrace(NewErrorValuef("Cannot call %s: %v. Not callable", name, value)))
-	}
-	*/
 }
 
 /*
@@ -813,9 +808,11 @@ func (vm *VM) RunChildrenFirstResult(ast Ast, args ...Value) Value {
 
 func (vm *VM) RunAst(ast Ast, args ...Value) []Value {
     var result []Value
-    pos := ast.Token().Position
+    /*pos := ast.Token().Position
     vm.PushNewFrame(&pos)
     defer vm.PopFrame()
+    */ 
+    // vm.Logf("RunAst: %s\n", ast.Kind().String())
     // Leaf nodes, both declared and in practice are self evaluating.
     if ast.Kind().IsLeaf() || ast.CountChildren() < 1 {
         result = ast.Eval(vm)
@@ -841,8 +838,10 @@ func (vm *VM) RunAst(ast Ast, args ...Value) []Value {
                 break;
             }
         }
+        // vm.Logf("RunAst subResult: %v\n", subResult) 
         result = ast.Eval(vm, subResult...)
     }
+    // vm.Logf("RunAst result: %v\n", result) 
 	return result
 }