remove unused code for tuple value return
This commit is contained in:
parent
c628152b9f
commit
64d99807d8
9 changed files with 58 additions and 84 deletions
|
@ -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"
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -157,7 +157,7 @@ var OpcodeOperands = [...][]int{
|
|||
OpSliceIndex: {},
|
||||
OpCall: {1},
|
||||
OpReturn: {},
|
||||
OpReturnValue: {1},
|
||||
OpReturnValue: {},
|
||||
OpGetLocal: {1},
|
||||
OpSetLocal: {1},
|
||||
OpDefineLocal: {1},
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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) &&
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue