assert and test changes
This commit is contained in:
@@ -231,15 +231,15 @@ func TestConditionals(t *testing.T) {
|
||||
code.Make(code.OpJumpNotTruthy, 10),
|
||||
// 0004
|
||||
code.Make(code.OpConstant, 0),
|
||||
// 0007
|
||||
// 0008
|
||||
code.Make(code.OpJump, 13),
|
||||
// 0010
|
||||
// 0011
|
||||
code.Make(code.OpConstant, 1),
|
||||
// 0013
|
||||
code.Make(code.OpPop),
|
||||
// 0014
|
||||
code.Make(code.OpConstant, 2),
|
||||
// 0017
|
||||
// 0018
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
@@ -256,28 +256,34 @@ func TestConditionals(t *testing.T) {
|
||||
// 0006
|
||||
code.Make(code.OpTrue),
|
||||
// 0007
|
||||
code.Make(code.OpJumpNotTruthy, 19),
|
||||
code.Make(code.OpJumpNotTruthy, 20),
|
||||
// 0010
|
||||
code.Make(code.OpConstant, 1),
|
||||
// 0013
|
||||
code.Make(code.OpAssignGlobal, 0),
|
||||
// 0016
|
||||
code.Make(code.OpJump, 20),
|
||||
// 0019
|
||||
code.Make(code.OpNull),
|
||||
// 0017
|
||||
code.Make(code.OpJump, 21),
|
||||
// 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
|
||||
// 0021
|
||||
code.Make(code.OpPop),
|
||||
// 0022
|
||||
code.Make(code.OpFalse),
|
||||
// 0023
|
||||
code.Make(code.OpJumpNotTruthy, 36),
|
||||
// 0025
|
||||
code.Make(code.OpConstant, 2),
|
||||
// 0029
|
||||
code.Make(code.OpAssignGlobal, 0),
|
||||
// 0032
|
||||
code.Make(code.OpNull),
|
||||
// 0033
|
||||
code.Make(code.OpJump, 37),
|
||||
// 0036
|
||||
code.Make(code.OpNull),
|
||||
// 0037
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
@@ -487,7 +493,7 @@ func TestIndexExpressions(t *testing.T) {
|
||||
func TestFunctions(t *testing.T) {
|
||||
tests := []compilerTestCase{
|
||||
{
|
||||
input: "fn() { return 5 + 10 }",
|
||||
input: `fn() { return 5 + 10 }`,
|
||||
expectedConstants: []interface{}{
|
||||
5,
|
||||
10,
|
||||
@@ -504,24 +510,7 @@ func TestFunctions(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `fn() { 5 + 10 }`,
|
||||
expectedConstants: []interface{}{
|
||||
5,
|
||||
10,
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpReturn),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpClosure, 2, 0),
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `fn() { 1; 2 }`,
|
||||
input: `fn() { 1; return 2 }`,
|
||||
expectedConstants: []interface{}{
|
||||
1,
|
||||
2,
|
||||
@@ -566,8 +555,8 @@ func TestClosures(t *testing.T) {
|
||||
tests := []compilerTestCase{
|
||||
{
|
||||
input: `fn(a) {
|
||||
fn(b) {
|
||||
a + b
|
||||
return fn(b) {
|
||||
return a + b
|
||||
}
|
||||
}
|
||||
`,
|
||||
@@ -592,9 +581,9 @@ func TestClosures(t *testing.T) {
|
||||
{
|
||||
input: `
|
||||
fn(a) {
|
||||
fn(b) {
|
||||
fn(c) {
|
||||
a + b + c
|
||||
return fn(b) {
|
||||
return fn(c) {
|
||||
return a + b + c
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -632,13 +621,13 @@ func TestClosures(t *testing.T) {
|
||||
fn() {
|
||||
let a = 66;
|
||||
|
||||
fn() {
|
||||
return fn() {
|
||||
let b = 77;
|
||||
|
||||
fn() {
|
||||
return fn() {
|
||||
let c = 88;
|
||||
|
||||
global + a + b + c;
|
||||
return global + a + b + c;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -737,7 +726,7 @@ func TestCompilerScopes(t *testing.T) {
|
||||
func TestFunctionCalls(t *testing.T) {
|
||||
tests := []compilerTestCase{
|
||||
{
|
||||
input: `fn() { 24 }();`,
|
||||
input: `fn() { return 24 }();`,
|
||||
expectedConstants: []interface{}{
|
||||
24,
|
||||
[]code.Instructions{
|
||||
@@ -753,7 +742,7 @@ func TestFunctionCalls(t *testing.T) {
|
||||
},
|
||||
{
|
||||
input: `
|
||||
let noArg = fn() { 24 };
|
||||
let noArg = fn() { return 24 };
|
||||
noArg();
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
@@ -773,7 +762,7 @@ func TestFunctionCalls(t *testing.T) {
|
||||
},
|
||||
{
|
||||
input: `
|
||||
let oneArg = fn(a) { a };
|
||||
let oneArg = fn(a) { return a };
|
||||
oneArg(24);
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
@@ -794,7 +783,7 @@ func TestFunctionCalls(t *testing.T) {
|
||||
},
|
||||
{
|
||||
input: `
|
||||
let manyArg = fn(a, b, c) { a; b; c };
|
||||
let manyArg = fn(a, b, c) { a; b; return c };
|
||||
manyArg(24, 25, 26);
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
@@ -881,7 +870,7 @@ func TestLetStatementScopes(t *testing.T) {
|
||||
{
|
||||
input: `
|
||||
let num = 55;
|
||||
fn() { num }
|
||||
fn() { return num }
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
55,
|
||||
@@ -901,7 +890,7 @@ func TestLetStatementScopes(t *testing.T) {
|
||||
input: `
|
||||
fn() {
|
||||
let num = 55;
|
||||
num
|
||||
return num
|
||||
}
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
@@ -923,7 +912,7 @@ func TestLetStatementScopes(t *testing.T) {
|
||||
fn() {
|
||||
let a = 55;
|
||||
let b = 77;
|
||||
a + b
|
||||
return a + b
|
||||
}
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
@@ -977,11 +966,11 @@ func TestBuiltins(t *testing.T) {
|
||||
`,
|
||||
expectedConstants: []interface{}{1},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpGetBuiltin, 4),
|
||||
code.Make(code.OpGetBuiltin, 5),
|
||||
code.Make(code.OpArray, 0),
|
||||
code.Make(code.OpCall, 1),
|
||||
code.Make(code.OpPop),
|
||||
code.Make(code.OpGetBuiltin, 7),
|
||||
code.Make(code.OpGetBuiltin, 8),
|
||||
code.Make(code.OpArray, 0),
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpCall, 2),
|
||||
@@ -989,10 +978,10 @@ func TestBuiltins(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `fn() { len([]) }`,
|
||||
input: `fn() { return len([]) }`,
|
||||
expectedConstants: []interface{}{
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpGetBuiltin, 4),
|
||||
code.Make(code.OpGetBuiltin, 5),
|
||||
code.Make(code.OpArray, 0),
|
||||
code.Make(code.OpCall, 1),
|
||||
code.Make(code.OpReturn),
|
||||
@@ -1012,9 +1001,9 @@ func TestRecursiveFunctions(t *testing.T) {
|
||||
tests := []compilerTestCase{
|
||||
{
|
||||
input: `
|
||||
let countDown = fn(x) { countDown(x - 1); };
|
||||
countDown(1);
|
||||
`,
|
||||
let countDown = fn(x) { return countDown(x - 1); };
|
||||
countDown(1);
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
1,
|
||||
[]code.Instructions{
|
||||
@@ -1038,12 +1027,12 @@ func TestRecursiveFunctions(t *testing.T) {
|
||||
},
|
||||
{
|
||||
input: `
|
||||
let wrapper = fn() {
|
||||
let countDown = fn(x) { countDown(x - 1); };
|
||||
countDown(1);
|
||||
};
|
||||
wrapper();
|
||||
`,
|
||||
let wrapper = fn() {
|
||||
let countDown = fn(x) { return countDown(x - 1); };
|
||||
return countDown(1);
|
||||
};
|
||||
wrapper();
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
1,
|
||||
[]code.Instructions{
|
||||
@@ -1088,15 +1077,15 @@ func TestIteration(t *testing.T) {
|
||||
// 0000
|
||||
code.Make(code.OpTrue),
|
||||
// 0001
|
||||
code.Make(code.OpJumpNotTruthy, 11),
|
||||
code.Make(code.OpJumpNotTruthy, 10),
|
||||
// 0004
|
||||
code.Make(code.OpConstant, 0),
|
||||
// 0007
|
||||
code.Make(code.OpPop),
|
||||
// 0008
|
||||
code.Make(code.OpJump, 0),
|
||||
// 0011
|
||||
// 0010
|
||||
code.Make(code.OpNoop),
|
||||
// 0011
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user