closures and they can recurse!!!
This commit is contained in:
@@ -503,6 +503,132 @@ func TestFunctionsWithoutReturnValue(t *testing.T) {
|
||||
runCompilerTests(t, tests)
|
||||
}
|
||||
|
||||
func TestClosures(t *testing.T) {
|
||||
tests := []compilerTestCase{
|
||||
{
|
||||
input: `fn(a) {
|
||||
fn(b) {
|
||||
a + b
|
||||
}
|
||||
}
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpGetFree, 0),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpReturnValue),
|
||||
},
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpClosure, 0, 1),
|
||||
code.Make(code.OpReturnValue),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpClosure, 1, 0),
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `
|
||||
fn(a) {
|
||||
fn(b) {
|
||||
fn(c) {
|
||||
a + b + c
|
||||
}
|
||||
}
|
||||
};
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpGetFree, 0),
|
||||
code.Make(code.OpGetFree, 1),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpReturnValue),
|
||||
},
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpGetFree, 0),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpClosure, 0, 2),
|
||||
code.Make(code.OpReturnValue),
|
||||
},
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpClosure, 1, 1),
|
||||
code.Make(code.OpReturnValue),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpClosure, 2, 0),
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `
|
||||
let global = 55;
|
||||
|
||||
fn() {
|
||||
let a = 66;
|
||||
|
||||
fn() {
|
||||
let b = 77;
|
||||
|
||||
fn() {
|
||||
let c = 88;
|
||||
|
||||
global + a + b + c;
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
55,
|
||||
66,
|
||||
77,
|
||||
88,
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpConstant, 3),
|
||||
code.Make(code.OpSetLocal, 0),
|
||||
code.Make(code.OpGetGlobal, 0),
|
||||
code.Make(code.OpGetFree, 0),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpGetFree, 1),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpReturnValue),
|
||||
},
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpConstant, 2),
|
||||
code.Make(code.OpSetLocal, 0),
|
||||
code.Make(code.OpGetFree, 0),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpClosure, 4, 2),
|
||||
code.Make(code.OpReturnValue),
|
||||
},
|
||||
[]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),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpSetGlobal, 0),
|
||||
code.Make(code.OpClosure, 6, 0),
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
runCompilerTests(t, tests)
|
||||
}
|
||||
|
||||
func TestCompilerScopes(t *testing.T) {
|
||||
compiler := New()
|
||||
if compiler.scopeIndex != 0 {
|
||||
@@ -755,6 +881,75 @@ func TestBuiltins(t *testing.T) {
|
||||
runCompilerTests(t, tests)
|
||||
}
|
||||
|
||||
func TestRecursiveFunctions(t *testing.T) {
|
||||
tests := []compilerTestCase{
|
||||
{
|
||||
input: `
|
||||
let countDown = fn(x) { countDown(x - 1); };
|
||||
countDown(1);
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
1,
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpCurrentClosure),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpSub),
|
||||
code.Make(code.OpCall, 1),
|
||||
code.Make(code.OpReturnValue),
|
||||
},
|
||||
1,
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpClosure, 1, 0),
|
||||
code.Make(code.OpSetGlobal, 0),
|
||||
code.Make(code.OpGetGlobal, 0),
|
||||
code.Make(code.OpConstant, 2),
|
||||
code.Make(code.OpCall, 1),
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
{
|
||||
input: `
|
||||
let wrapper = fn() {
|
||||
let countDown = fn(x) { countDown(x - 1); };
|
||||
countDown(1);
|
||||
};
|
||||
wrapper();
|
||||
`,
|
||||
expectedConstants: []interface{}{
|
||||
1,
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpCurrentClosure),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpSub),
|
||||
code.Make(code.OpCall, 1),
|
||||
code.Make(code.OpReturnValue),
|
||||
},
|
||||
1,
|
||||
[]code.Instructions{
|
||||
code.Make(code.OpClosure, 1, 0),
|
||||
code.Make(code.OpSetLocal, 0),
|
||||
code.Make(code.OpGetLocal, 0),
|
||||
code.Make(code.OpConstant, 2),
|
||||
code.Make(code.OpCall, 1),
|
||||
code.Make(code.OpReturnValue),
|
||||
},
|
||||
},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpClosure, 3, 0),
|
||||
code.Make(code.OpSetGlobal, 0),
|
||||
code.Make(code.OpGetGlobal, 0),
|
||||
code.Make(code.OpCall, 0),
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
runCompilerTests(t, tests)
|
||||
}
|
||||
|
||||
func runCompilerTests(t *testing.T, tests []compilerTestCase) {
|
||||
t.Helper()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user