Simplify return operation
This commit is contained in:
@@ -243,6 +243,44 @@ func TestConditionals(t *testing.T) {
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `
|
||||
let x = 0; if (true) { x = 1; }; if (false) { x = 2; }
|
||||
`,
|
||||
expectedConstants: []interface{}{0, 1, 2},
|
||||
expectedInstructions: []code.Instructions{
|
||||
// 0000
|
||||
code.Make(code.OpConstant, 0),
|
||||
// 0003
|
||||
code.Make(code.OpSetGlobal, 0),
|
||||
// 0006
|
||||
code.Make(code.OpTrue),
|
||||
// 0007
|
||||
code.Make(code.OpJumpNotTruthy, 19),
|
||||
// 0010
|
||||
code.Make(code.OpConstant, 1),
|
||||
// 0013
|
||||
code.Make(code.OpAssignGlobal, 0),
|
||||
// 0016
|
||||
code.Make(code.OpJump, 20),
|
||||
// 0019
|
||||
code.Make(code.OpNull),
|
||||
// 0020
|
||||
code.Make(code.OpFalse),
|
||||
// 0021
|
||||
code.Make(code.OpJumpNotTruthy, 33),
|
||||
// 0024
|
||||
code.Make(code.OpConstant, 2),
|
||||
// 0027
|
||||
code.Make(code.OpAssignGlobal, 0),
|
||||
// 0030
|
||||
code.Make(code.OpJump, 34),
|
||||
// 0033
|
||||
code.Make(code.OpNull),
|
||||
// 0034
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
runCompilerTests(t, tests)
|
||||
@@ -457,7 +495,7 @@ func TestFunctions(t *testing.T) {
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
@@ -474,7 +512,7 @@ func TestFunctions(t *testing.T) {
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
@@ -491,7 +529,7 @@ func TestFunctions(t *testing.T) {
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpPop),
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
@@ -504,12 +542,13 @@ func TestFunctions(t *testing.T) {
|
||||
runCompilerTests(t, tests)
|
||||
}
|
||||
|
||||
func TestFunctionsWithoutReturnValue(t *testing.T) {
|
||||
func TestFunctionsWithoutReturn(t *testing.T) {
|
||||
tests := []compilerTestCase{
|
||||
{
|
||||
input: `fn() { }`,
|
||||
expectedConstants: []interface{}{
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpNull),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
@@ -537,12 +576,12 @@ func TestClosures(t *testing.T) {
|
||||
code.Make(code.OpGetFree, 0),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpClosure, 0, 1),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
@@ -567,18 +606,18 @@ func TestClosures(t *testing.T) {
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpGetFree, 0),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpClosure, 0, 2),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpClosure, 1, 1),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
@@ -619,7 +658,7 @@ func TestClosures(t *testing.T) {
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpConstant, 2),
|
||||
@@ -627,14 +666,14 @@ func TestClosures(t *testing.T) {
|
||||
code.Make(code.OpGetFree, 0),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpClosure, 4, 2),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpSetLocal, 0),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpClosure, 5, 1),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
@@ -703,7 +742,7 @@ func TestFunctionCalls(t *testing.T) {
|
||||
24,
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpConstant, 0), // The literal "24"
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
@@ -721,7 +760,7 @@ func TestFunctionCalls(t *testing.T) {
|
||||
24,
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpConstant, 0), // The literal "24"
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
@@ -740,7 +779,7 @@ func TestFunctionCalls(t *testing.T) {
|
||||
expectedConstants: []interface{}{
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
24,
|
||||
},
|
||||
@@ -765,7 +804,7 @@ func TestFunctionCalls(t *testing.T) {
|
||||
code.Make(code.OpGetLocal, 1),
|
||||
code.Make(code.OpPop),
|
||||
code.Make(code.OpGetLocal, 2),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
24,
|
||||
25,
|
||||
@@ -800,6 +839,7 @@ func TestAssignmentStatementScopes(t *testing.T) {
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpAssignGlobal, 0),
|
||||
code.Make(code.OpNull),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
@@ -822,6 +862,7 @@ func TestAssignmentStatementScopes(t *testing.T) {
|
||||
code.Make(code.OpSetLocal, 0),
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpAssignLocal, 0),
|
||||
code.Make(code.OpNull),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
@@ -846,7 +887,7 @@ func TestLetStatementScopes(t *testing.T) {
|
||||
55,
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpGetGlobal, 0),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
@@ -869,7 +910,7 @@ func TestLetStatementScopes(t *testing.T) {
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpSetLocal, 0),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
@@ -896,7 +937,7 @@ func TestLetStatementScopes(t *testing.T) {
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpGetLocal, 1),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
@@ -954,7 +995,7 @@ func TestBuiltins(t *testing.T) {
|
||||
code.Make(code.OpGetBuiltin, 0),
|
||||
code.Make(code.OpArray, 0),
|
||||
code.Make(code.OpCall, 1),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
@@ -982,7 +1023,7 @@ func TestRecursiveFunctions(t *testing.T) {
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpSub),
|
||||
code.Make(code.OpCall, 1),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
1,
|
||||
},
|
||||
@@ -1011,7 +1052,7 @@ func TestRecursiveFunctions(t *testing.T) {
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpSub),
|
||||
code.Make(code.OpCall, 1),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
1,
|
||||
[]code.Instructions{
|
||||
@@ -1020,7 +1061,7 @@ func TestRecursiveFunctions(t *testing.T) {
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpConstant, 2),
|
||||
code.Make(code.OpCall, 1),
|
||||
code.Make(code.OpReturnValue),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
|
||||
Reference in New Issue
Block a user