From 64d99807d8381754305ef3206d2a7c90ffbb7ca6 Mon Sep 17 00:00:00 2001 From: Daniel Kang Date: Wed, 30 Jan 2019 02:01:44 -0800 Subject: [PATCH] remove unused code for tuple value return --- compiler/ast/return_stmt.go | 17 +++----- compiler/bytecode_test.go | 6 +-- compiler/compiler.go | 11 ++--- compiler/compiler_test.go | 77 +++++++++++++++++----------------- compiler/opcodes.go | 2 +- compiler/parser/parser.go | 6 +-- compiler/parser/parser_test.go | 6 +-- docs/interoperability.md | 2 +- runtime/vm.go | 15 ------- 9 files changed, 58 insertions(+), 84 deletions(-) diff --git a/compiler/ast/return_stmt.go b/compiler/ast/return_stmt.go index f82f8f07..592d45b8 100644 --- a/compiler/ast/return_stmt.go +++ b/compiler/ast/return_stmt.go @@ -1,15 +1,13 @@ package ast import ( - "strings" - "github.com/d5/tengo/compiler/source" ) // ReturnStmt represents a return statement. type ReturnStmt struct { ReturnPos source.Pos - Results []Expr + Result Expr } func (s *ReturnStmt) stmtNode() {} @@ -21,21 +19,16 @@ func (s *ReturnStmt) Pos() source.Pos { // End returns the position of first character immediately after the node. func (s *ReturnStmt) End() source.Pos { - if n := len(s.Results); n > 0 { - return s.Results[n-1].End() + if s.Result != nil { + return s.Result.End() } return s.ReturnPos + 6 } func (s *ReturnStmt) String() string { - if len(s.Results) > 0 { - var res []string - for _, e := range s.Results { - res = append(res, e.String()) - } - - return "return " + strings.Join(res, ", ") + if s.Result != nil { + return "return " + s.Result.String() } return "return" diff --git a/compiler/bytecode_test.go b/compiler/bytecode_test.go index 86ac2acc..60eebb48 100644 --- a/compiler/bytecode_test.go +++ b/compiler/bytecode_test.go @@ -69,20 +69,20 @@ func TestBytecode(t *testing.T) { compiler.MakeInstruction(compiler.OpAdd), compiler.MakeInstruction(compiler.OpGetLocal, 0), compiler.MakeInstruction(compiler.OpAdd), - compiler.MakeInstruction(compiler.OpReturnValue, 1)), + compiler.MakeInstruction(compiler.OpReturnValue)), compiledFunction(1, 0, compiler.MakeInstruction(compiler.OpConstant, 2), compiler.MakeInstruction(compiler.OpSetLocal, 0), compiler.MakeInstruction(compiler.OpGetFree, 0), compiler.MakeInstruction(compiler.OpGetLocal, 0), compiler.MakeInstruction(compiler.OpClosure, 4, 2), - compiler.MakeInstruction(compiler.OpReturnValue, 1)), + compiler.MakeInstruction(compiler.OpReturnValue)), compiledFunction(1, 0, compiler.MakeInstruction(compiler.OpConstant, 1), compiler.MakeInstruction(compiler.OpSetLocal, 0), compiler.MakeInstruction(compiler.OpGetLocal, 0), compiler.MakeInstruction(compiler.OpClosure, 5, 1), - compiler.MakeInstruction(compiler.OpReturnValue, 1))))) + compiler.MakeInstruction(compiler.OpReturnValue))))) } func testBytecodeSerialization(t *testing.T, b *compiler.Bytecode) { diff --git a/compiler/compiler.go b/compiler/compiler.go index ff886d4b..9fd2a492 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -426,17 +426,14 @@ func (c *Compiler) Compile(node ast.Node) error { return fmt.Errorf("return statement outside function") } - switch len(node.Results) { - case 0: + if node.Result == nil { c.emit(OpReturn) - case 1: - if err := c.Compile(node.Results[0]); err != nil { + } else { + if err := c.Compile(node.Result); err != nil { return err } - c.emit(OpReturnValue, 1) - default: - return fmt.Errorf("multi-value return not implemented") + c.emit(OpReturnValue) } case *ast.CallExpr: diff --git a/compiler/compiler_test.go b/compiler/compiler_test.go index f05aedf5..6e16db96 100644 --- a/compiler/compiler_test.go +++ b/compiler/compiler_test.go @@ -446,7 +446,7 @@ func TestCompiler_Compile(t *testing.T) { compiler.MakeInstruction(compiler.OpConstant, 0), compiler.MakeInstruction(compiler.OpConstant, 1), compiler.MakeInstruction(compiler.OpAdd), - compiler.MakeInstruction(compiler.OpReturnValue, 1))))) + compiler.MakeInstruction(compiler.OpReturnValue))))) expect(t, `func() { 5 + 10 }`, bytecode( @@ -490,7 +490,7 @@ func TestCompiler_Compile(t *testing.T) { compiler.MakeInstruction(compiler.OpConstant, 0), compiler.MakeInstruction(compiler.OpPop), compiler.MakeInstruction(compiler.OpConstant, 1), - compiler.MakeInstruction(compiler.OpReturnValue, 1))))) + compiler.MakeInstruction(compiler.OpReturnValue))))) expect(t, `func() { if(true) { return 1 } else { return 2 } }`, bytecode( @@ -501,13 +501,13 @@ func TestCompiler_Compile(t *testing.T) { intObject(1), intObject(2), compiledFunction(0, 0, - compiler.MakeInstruction(compiler.OpTrue), // 0000 - compiler.MakeInstruction(compiler.OpJumpFalsy, 12), // 0001 - compiler.MakeInstruction(compiler.OpConstant, 0), // 0004 - compiler.MakeInstruction(compiler.OpReturnValue, 1), // 0007 - compiler.MakeInstruction(compiler.OpJump, 17), // 0009 - compiler.MakeInstruction(compiler.OpConstant, 1), // 0012 - compiler.MakeInstruction(compiler.OpReturnValue, 1))))) // 0014 + compiler.MakeInstruction(compiler.OpTrue), // 0000 + compiler.MakeInstruction(compiler.OpJumpFalsy, 11), // 0001 + compiler.MakeInstruction(compiler.OpConstant, 0), // 0004 + compiler.MakeInstruction(compiler.OpReturnValue), // 0007 + compiler.MakeInstruction(compiler.OpJump, 15), // 0008 + compiler.MakeInstruction(compiler.OpConstant, 1), // 0011 + compiler.MakeInstruction(compiler.OpReturnValue))))) // 0014 expect(t, `func() { 1; if(true) { 2 } else { 3 }; 4 }`, bytecode( @@ -564,7 +564,7 @@ func TestCompiler_Compile(t *testing.T) { intObject(24), compiledFunction(0, 0, compiler.MakeInstruction(compiler.OpConstant, 0), - compiler.MakeInstruction(compiler.OpReturnValue, 1))))) + compiler.MakeInstruction(compiler.OpReturnValue))))) expect(t, `noArg := func() { 24 }; noArg();`, bytecode( @@ -593,7 +593,7 @@ func TestCompiler_Compile(t *testing.T) { intObject(24), compiledFunction(0, 0, compiler.MakeInstruction(compiler.OpConstant, 0), - compiler.MakeInstruction(compiler.OpReturnValue, 1))))) + compiler.MakeInstruction(compiler.OpReturnValue))))) expect(t, `n := 55; func() { n };`, bytecode( @@ -620,23 +620,7 @@ func TestCompiler_Compile(t *testing.T) { compiler.MakeInstruction(compiler.OpConstant, 0), compiler.MakeInstruction(compiler.OpDefineLocal, 0), compiler.MakeInstruction(compiler.OpGetLocal, 0), - compiler.MakeInstruction(compiler.OpReturnValue, 1))))) - - expect(t, `func() { n := 55; n = 23; return n }`, - bytecode( - concat( - compiler.MakeInstruction(compiler.OpConstant, 2), - compiler.MakeInstruction(compiler.OpPop)), - objectsArray( - intObject(55), - intObject(23), - compiledFunction(1, 0, - compiler.MakeInstruction(compiler.OpConstant, 0), - compiler.MakeInstruction(compiler.OpDefineLocal, 0), - compiler.MakeInstruction(compiler.OpConstant, 1), - compiler.MakeInstruction(compiler.OpSetLocal, 0), - compiler.MakeInstruction(compiler.OpGetLocal, 0), - compiler.MakeInstruction(compiler.OpReturnValue, 1))))) + compiler.MakeInstruction(compiler.OpReturnValue))))) expect(t, `func() { a := 55; b := 77; return a + b }`, bytecode( @@ -654,7 +638,7 @@ func TestCompiler_Compile(t *testing.T) { compiler.MakeInstruction(compiler.OpGetLocal, 0), compiler.MakeInstruction(compiler.OpGetLocal, 1), compiler.MakeInstruction(compiler.OpAdd), - compiler.MakeInstruction(compiler.OpReturnValue, 1))))) + compiler.MakeInstruction(compiler.OpReturnValue))))) expect(t, `f1 := func(a) { return a }; f1(24);`, bytecode( @@ -668,7 +652,7 @@ func TestCompiler_Compile(t *testing.T) { objectsArray( compiledFunction(1, 1, compiler.MakeInstruction(compiler.OpGetLocal, 0), - compiler.MakeInstruction(compiler.OpReturnValue, 1)), + compiler.MakeInstruction(compiler.OpReturnValue)), intObject(24)))) expect(t, `f1 := func(a, b, c) { a; b; return c; }; f1(24, 25, 26);`, @@ -689,11 +673,26 @@ func TestCompiler_Compile(t *testing.T) { compiler.MakeInstruction(compiler.OpGetLocal, 1), compiler.MakeInstruction(compiler.OpPop), compiler.MakeInstruction(compiler.OpGetLocal, 2), - compiler.MakeInstruction(compiler.OpReturnValue, 1)), + compiler.MakeInstruction(compiler.OpReturnValue)), intObject(24), intObject(25), intObject(26)))) + expect(t, `func() { n := 55; n = 23; return n }`, + bytecode( + concat( + compiler.MakeInstruction(compiler.OpConstant, 2), + compiler.MakeInstruction(compiler.OpPop)), + objectsArray( + intObject(55), + intObject(23), + compiledFunction(1, 0, + compiler.MakeInstruction(compiler.OpConstant, 0), + compiler.MakeInstruction(compiler.OpDefineLocal, 0), + compiler.MakeInstruction(compiler.OpConstant, 1), + compiler.MakeInstruction(compiler.OpSetLocal, 0), + compiler.MakeInstruction(compiler.OpGetLocal, 0), + compiler.MakeInstruction(compiler.OpReturnValue))))) expect(t, `len([]);`, bytecode( concat( @@ -713,7 +712,7 @@ func TestCompiler_Compile(t *testing.T) { compiler.MakeInstruction(compiler.OpGetBuiltin, 3), compiler.MakeInstruction(compiler.OpArray, 0), compiler.MakeInstruction(compiler.OpCall, 1), - compiler.MakeInstruction(compiler.OpReturnValue, 1))))) + compiler.MakeInstruction(compiler.OpReturnValue))))) expect(t, `func(a) { func(b) { return a + b } }`, bytecode( @@ -725,7 +724,7 @@ func TestCompiler_Compile(t *testing.T) { compiler.MakeInstruction(compiler.OpGetFree, 0), compiler.MakeInstruction(compiler.OpGetLocal, 0), compiler.MakeInstruction(compiler.OpAdd), - compiler.MakeInstruction(compiler.OpReturnValue, 1)), + compiler.MakeInstruction(compiler.OpReturnValue)), compiledFunction(1, 1, compiler.MakeInstruction(compiler.OpGetLocal, 0), compiler.MakeInstruction(compiler.OpClosure, 0, 1), @@ -751,16 +750,16 @@ func(a) { compiler.MakeInstruction(compiler.OpAdd), compiler.MakeInstruction(compiler.OpGetLocal, 0), compiler.MakeInstruction(compiler.OpAdd), - compiler.MakeInstruction(compiler.OpReturnValue, 1)), + compiler.MakeInstruction(compiler.OpReturnValue)), compiledFunction(1, 1, compiler.MakeInstruction(compiler.OpGetFree, 0), compiler.MakeInstruction(compiler.OpGetLocal, 0), compiler.MakeInstruction(compiler.OpClosure, 0, 2), - compiler.MakeInstruction(compiler.OpReturnValue, 1)), + compiler.MakeInstruction(compiler.OpReturnValue)), compiledFunction(1, 1, compiler.MakeInstruction(compiler.OpGetLocal, 0), compiler.MakeInstruction(compiler.OpClosure, 1, 1), - compiler.MakeInstruction(compiler.OpReturnValue, 1))))) + compiler.MakeInstruction(compiler.OpReturnValue))))) expect(t, ` g := 55; @@ -799,20 +798,20 @@ func() { compiler.MakeInstruction(compiler.OpAdd), compiler.MakeInstruction(compiler.OpGetLocal, 0), compiler.MakeInstruction(compiler.OpAdd), - compiler.MakeInstruction(compiler.OpReturnValue, 1)), + compiler.MakeInstruction(compiler.OpReturnValue)), compiledFunction(1, 0, compiler.MakeInstruction(compiler.OpConstant, 2), compiler.MakeInstruction(compiler.OpDefineLocal, 0), compiler.MakeInstruction(compiler.OpGetFree, 0), compiler.MakeInstruction(compiler.OpGetLocal, 0), compiler.MakeInstruction(compiler.OpClosure, 4, 2), - compiler.MakeInstruction(compiler.OpReturnValue, 1)), + compiler.MakeInstruction(compiler.OpReturnValue)), compiledFunction(1, 0, compiler.MakeInstruction(compiler.OpConstant, 1), compiler.MakeInstruction(compiler.OpDefineLocal, 0), compiler.MakeInstruction(compiler.OpGetLocal, 0), compiler.MakeInstruction(compiler.OpClosure, 5, 1), - compiler.MakeInstruction(compiler.OpReturnValue, 1))))) + compiler.MakeInstruction(compiler.OpReturnValue))))) expect(t, `for i:=0; i<10; i++ {}`, bytecode( diff --git a/compiler/opcodes.go b/compiler/opcodes.go index eaa4892d..d64a0306 100644 --- a/compiler/opcodes.go +++ b/compiler/opcodes.go @@ -157,7 +157,7 @@ var OpcodeOperands = [...][]int{ OpSliceIndex: {}, OpCall: {1}, OpReturn: {}, - OpReturnValue: {1}, + OpReturnValue: {}, OpGetLocal: {1}, OpSetLocal: {1}, OpDefineLocal: {1}, diff --git a/compiler/parser/parser.go b/compiler/parser/parser.go index df84d96f..b8ae4bad 100644 --- a/compiler/parser/parser.go +++ b/compiler/parser/parser.go @@ -862,15 +862,15 @@ func (p *Parser) parseReturnStmt() ast.Stmt { pos := p.pos p.expect(token.Return) - var x []ast.Expr + var x ast.Expr if p.token != token.Semicolon && p.token != token.RBrace { - x = p.parseExprList() + x = p.parseExpr() } p.expectSemi() return &ast.ReturnStmt{ ReturnPos: pos, - Results: x, + Result: x, } } diff --git a/compiler/parser/parser_test.go b/compiler/parser/parser_test.go index 8ad75b8c..2e975ebd 100644 --- a/compiler/parser/parser_test.go +++ b/compiler/parser/parser_test.go @@ -144,8 +144,8 @@ func emptyStmt(implicit bool, pos source.Pos) *ast.EmptyStmt { return &ast.EmptyStmt{Implicit: implicit, Semicolon: pos} } -func returnStmt(pos source.Pos, results ...ast.Expr) *ast.ReturnStmt { - return &ast.ReturnStmt{Results: results, ReturnPos: pos} +func returnStmt(pos source.Pos, result ast.Expr) *ast.ReturnStmt { + return &ast.ReturnStmt{Result: result, ReturnPos: pos} } func forStmt(init ast.Stmt, cond ast.Expr, post ast.Stmt, body *ast.BlockStmt, pos source.Pos) *ast.ForStmt { @@ -309,7 +309,7 @@ func equalStmt(t *testing.T, expected, actual ast.Stmt) bool { equalStmt(t, expected.Body, actual.(*ast.ForInStmt).Body) && assert.Equal(t, expected.ForPos, actual.(*ast.ForInStmt).ForPos) case *ast.ReturnStmt: - return equalExprs(t, expected.Results, actual.(*ast.ReturnStmt).Results) && + return equalExpr(t, expected.Result, actual.(*ast.ReturnStmt).Result) && assert.Equal(t, expected.ReturnPos, actual.(*ast.ReturnStmt).ReturnPos) case *ast.BranchStmt: return equalExpr(t, expected.Label, actual.(*ast.BranchStmt).Label) && diff --git a/docs/interoperability.md b/docs/interoperability.md index 8d450503..0be8bd56 100644 --- a/docs/interoperability.md +++ b/docs/interoperability.md @@ -62,7 +62,7 @@ func main() { } // run the compiled bytecode - // a compiled bytecode 'c' can be executed multiple without re-compiling it + // a compiled bytecode 'c' can be executed multiple times without re-compiling it if err := c.Run(); err != nil { panic(err) } diff --git a/runtime/vm.go b/runtime/vm.go index b4be7141..bbc230f0 100644 --- a/runtime/vm.go +++ b/runtime/vm.go @@ -785,16 +785,6 @@ func (v *VM) Run() error { } case compiler.OpReturnValue: - //numRets := int(compiler.ReadUint8(v.curInsts[v.ip+1:])) - //_ = int64(compiler.ReadUint8(v.curInsts[v.ip+1:])) - v.ip++ - - // TODO: multi-value return is not fully implemented yet - //var rets []*objects.Object - //for i := 0; i < numRets; i++ { - // val := v.pop() - // rets = append(rets, val) - //} retVal := v.stack[v.sp-1] //v.sp-- @@ -808,11 +798,6 @@ func (v *VM) Run() error { //v.sp = lastFrame.basePointer - 1 v.sp = lastFrame.basePointer - //for _, retVal := range rets { - // if err := v.push(retVal); err != nil { - // return err - // } - //} if v.sp-1 >= StackSize { return ErrStackOverflow }