Skip to content

Commit

Permalink
remove unused code for tuple value return
Browse files Browse the repository at this point in the history
  • Loading branch information
d5 committed Jan 30, 2019
1 parent c628152 commit 64d9980
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 84 deletions.
17 changes: 5 additions & 12 deletions compiler/ast/return_stmt.go
Original file line number Diff line number Diff line change
@@ -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() {}
Expand All @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions compiler/bytecode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 4 additions & 7 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
77 changes: 38 additions & 39 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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);`,
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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),
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion compiler/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ var OpcodeOperands = [...][]int{
OpSliceIndex: {},
OpCall: {1},
OpReturn: {},
OpReturnValue: {1},
OpReturnValue: {},
OpGetLocal: {1},
OpSetLocal: {1},
OpDefineLocal: {1},
Expand Down
6 changes: 3 additions & 3 deletions compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) &&
Expand Down
2 changes: 1 addition & 1 deletion docs/interoperability.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
15 changes: 0 additions & 15 deletions runtime/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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--

Expand All @@ -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
}
Expand Down

0 comments on commit 64d9980

Please sign in to comment.