浏览代码

Merge branch 'master' of https://gitlab.com/beoran/muesli

Beoran 5 年之前
父节点
当前提交
e12d34d3c7
共有 2 个文件被更改,包括 20 次插入21 次删除
  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
 }
 
@@ -636,22 +647,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)))
-	}
-	*/
 }
 
 /*
@@ -809,9 +804,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)
@@ -837,8 +834,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
 }