소스 검색

WIP returns.

Beoran 5 년 전
부모
커밋
3b9fdb03f9
2개의 변경된 파일24개의 추가작업 그리고 19개의 파일을 삭제
  1. 2 2
      ast.go
  2. 22 17
      vm.go

+ 2 - 2
ast.go

@@ -169,8 +169,8 @@ func (astkind AstMetaKindExpression) Run(vm *VM, ast Ast, val ...Value) []Value
 }
 
 func (astkind AstMetaKindBlock) Run(vm *VM, ast Ast, val ...Value) []Value {
-	// A block is not executed. It results in an anonymous runnable.
-	ast.Display()
+	// A block encountered in the AST is not executed. 
+    // It results in an anonymous runnable with a clone of the AST embedded.
 	return Ok(NewBlockValue(&ast))
 }
 

+ 22 - 17
vm.go

@@ -553,15 +553,12 @@ func (vm *VM) Return(results ...Value) []Value {
 func (vm *VM) PopFrame() *Frame {
 	if (vm.Frame != vm.TopFrame) && (vm.Frame.parent != nil) {
 		frame := vm.Frame
-		if frame.returned || frame.failed {
-			
+		if frame.failed && frame.parent != nil {
+           // failures are like panics and propagate up the call tree
+           frame.parent.failed = true
+           frame.parent.results = frame.results 
 		}
-		
-		
 		vm.Frame = frame.parent
-		vm.Frame.returned = frame.returned
-		vm.Frame.failed = frame.failed
-		vm.Frame.results = frame.results
 		return frame
 	}
 	return nil
@@ -621,9 +618,9 @@ func (vm *VM) AddTrace(err error) error {
 }
 
 func (vm *VM) CallCallable(callable Callable, arguments ...Value) []Value {
-	defer vm.PopFrame()
+	// defer vm.PopFrame()
 	defer vm.PopScope()
-	vm.PushNewFrame(callable.Position())
+	// vm.PushNewFrame(callable.Position())
 	vm.PushNewScope()
 	return callable.Call(vm, arguments...)
 }
@@ -726,10 +723,12 @@ func (vm *VM) RunChildren(ast Ast, args ...Value) []Value {
 	*/	
 	result := []Value{}
 	for _, child := range ast.Children() {
+        position := ast.Token().Position
+        vm.PushNewFrame(&position)
+        defer vm.PopFrame()
 		val := child.Run(vm, args...)
 		
 		if vm.Frame.returned || vm.Frame.failed {
-			fmt.Printf("Returned.\n")
 			return vm.Frame.results
 		}
 		
@@ -757,10 +756,13 @@ func (vm *VM) RunChildren(ast Ast, args ...Value) []Value {
 func (vm *VM) RunChildrenLastResult(ast Ast, args ...Value) Value {
 	var result Value = EmptyValue{}
 	for _, child := range ast.Children() {
-		val := child.Run(vm, args...)
+        position := ast.Token().Position
+        vm.PushNewFrame(&position)
+        defer vm.PopFrame()
+		
+        val := child.Run(vm, args...)
 		
 		if vm.Frame.returned || vm.Frame.failed {
-			fmt.Printf("Returned.\n")
 			res := vm.Frame.results
 			return res[len(res)-1]
 		}
@@ -790,14 +792,17 @@ func (vm *VM) RunChildrenLastResult(ast Ast, args ...Value) Value {
 func (vm *VM) RunChildrenFirstResult(ast Ast, args ...Value) Value {
 	var result Value = EmptyValue{}
 	for _, child := range ast.Children() {
-		val := child.Run(vm, args...)
+        position := ast.Token().Position
+        vm.PushNewFrame(&position)
+        defer vm.PopFrame()
+		
+        val := child.Run(vm, args...)
 		
 		if vm.Frame.returned || vm.Frame.failed {
-			fmt.Printf("Returned.\n")
 			res := vm.Frame.results
 			return res[0]
 		}
-						
+		/*				
 		// skip empty results
 		if len(val) < 1  {
 			continue
@@ -806,9 +811,9 @@ func (vm *VM) RunChildrenFirstResult(ast Ast, args ...Value) Value {
 		first := val[0]
 		if _, isEmpty := first.(EmptyValue); isEmpty  {
 			continue
-		}
+		}*/
 		// otherwise if non empty return the result. 
-		return first
+		return val[0]
 	}
 	return result
 }