Change assignment into expressions
Some checks failed
Test / build (push) Waiting to run
Build / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-19 20:30:30 -04:00
parent aa0582ed72
commit be81b9a6d6
12 changed files with 478 additions and 153 deletions

View File

@@ -267,6 +267,8 @@ func TestConditionals(t *testing.T) {
{"if (false) { 10 } else { 10; let b = 5; }", Null},
{"if (true) { let a = 5; } else { 10 }", Null},
{"let x = 0; if (true) { x = 1; }; if (false) { x = 2; }; x", 1},
{"if (1 < 2) { 10 } else if (1 == 2) { 20 }", 10},
{"if (1 > 2) { 10 } else if (1 == 2) { 20 } else { 30 }", 30},
}
runVmTests(t, tests)
@@ -437,7 +439,7 @@ func TestFirstClassFunctions(t *testing.T) {
input: `
let returnsOneReturner = fn() {
let returnsOne = fn() { return 1; };
returnsOne;
return returnsOne;
};
returnsOneReturner()();
`,
@@ -450,13 +452,13 @@ func TestFirstClassFunctions(t *testing.T) {
func TestCallingFunctionsWithBindings(t *testing.T) {
tests := []vmTestCase{
{
input: `
let one = fn() { let one = 1; return one };
one();
`,
expected: 1,
},
//{
// input: `
// let one = fn() { let one = 1; return one };
// one();
// `,
// expected: 1,
//},
{
input: `
let oneAndTwo = fn() { let one = 1; let two = 2; return one + two; };
@@ -664,7 +666,7 @@ func TestClosures(t *testing.T) {
{
input: `
let newClosure = fn(a) {
fn() { return a; };
return fn() { return a; };
};
let closure = newClosure(99);
closure();
@@ -674,7 +676,7 @@ func TestClosures(t *testing.T) {
{
input: `
let newAdder = fn(a, b) {
fn(c) { return a + b + c };
return fn(c) { return a + b + c };
};
let adder = newAdder(1, 2);
adder(8);
@@ -685,7 +687,7 @@ func TestClosures(t *testing.T) {
input: `
let newAdder = fn(a, b) {
let c = a + b;
fn(d) { return c + d };
return fn(d) { return c + d };
};
let adder = newAdder(1, 2);
adder(8);
@@ -819,6 +821,8 @@ func TestIterations(t *testing.T) {
{"while (false) { }", nil},
{"let n = 0; while (n < 10) { let n = n + 1 }; n", 10},
{"let n = 10; while (n > 0) { let n = n - 1 }; n", 0},
{"let n = 0; while (n < 10) { let n = n + 1 }", nil},
{"let n = 10; while (n > 0) { let n = n - 1 }", nil},
{"let n = 0; while (n < 10) { n = n + 1 }; n", 10},
{"let n = 10; while (n > 0) { n = n - 1 }; n", 0},
{"let n = 0; while (n < 10) { n = n + 1 }", nil},
@@ -828,9 +832,27 @@ func TestIterations(t *testing.T) {
runVmTests(t, tests)
}
func TestAssignmentStatements(t *testing.T) {
func TestIndexAssignmentStatements(t *testing.T) {
tests := []vmTestCase{
{"let one = 0; one = 1", 1},
{"let xs = [1, 2, 3]; xs[1] = 4; xs[1];", 4},
}
runVmTests(t, tests)
}
func TestAssignmentExpressions(t *testing.T) {
tests := []vmTestCase{
{"let a = 0; a = 5;", nil},
{"let a = 0; a = 5; a;", 5},
{"let a = 0; a = 5 * 5;", nil},
{"let a = 0; a = 5 * 5; a;", 25},
{"let a = 0; a = 5; let b = 0; b = a;", nil},
{"let a = 0; a = 5; let b = 0; b = a; b;", 5},
{"let a = 0; a = 5; let b = 0; b = a; let c = 0; c = a + b + 5;", nil},
{"let a = 0; a = 5; let b = 0; b = a; let c = 0; c = a + b + 5; c;", 15},
{"let a = 5; let b = a; a = 0;", nil},
{"let a = 5; let b = a; a = 0; b;", 5},
{"let one = 0; one = 1", nil},
{"let one = 0; one = 1; one", 1},
{"let one = 0; one = 1; let two = 0; two = 2; one + two", 3},
{"let one = 0; one = 1; let two = 0; two = one + one; one + two", 3},