-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
completed analyzer test coverage except for anyType
- Loading branch information
1 parent
e15575a
commit 77a8118
Showing
3 changed files
with
183 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,183 +1,177 @@ | ||
// import assert from "node:assert/strict" | ||
// import parse from "../src/parser.js" | ||
// import analyze from "../src/analyzer.js" | ||
// import optimize from "../src/optimizer.js" | ||
// import generate from "../src/generator.js" | ||
import assert from "node:assert/strict" | ||
import parse from "../src/parser.js" | ||
import analyze from "../src/analyzer.js" | ||
import optimize from "../src/optimizer.js" | ||
import generate from "../src/generator.js" | ||
|
||
// function dedent(s) { | ||
// return `${s}`.replace(/(?<=\n)\s+/g, "").trim() | ||
// } | ||
function dedent(s) { | ||
return `${s}`.replace(/(?<=\n)\s+/g, "").trim() | ||
} | ||
|
||
// const fixtures = [ | ||
// { | ||
// name: "small", | ||
// source: ` | ||
// auto x = 3 * 7; | ||
// x = x + 1; | ||
// x = x - 1; | ||
// auto y = true; | ||
// `, | ||
// expected: dedent` | ||
// let x_1 = 21; | ||
// x_1++; | ||
// x_1--; | ||
// let y_2 = true; | ||
// `, | ||
// }, | ||
// { | ||
// name: "if", | ||
// source: ` | ||
// auto x = 0; | ||
// if x == 0 { print "1"; } | ||
// if x == 0 { print 1; } else { print 2; } | ||
// if x == 0 { print 1; } else if x == 2 { print 3; } | ||
// if x == 0 { print 1; } else if x == 2 { print 3; } else { print 4; } | ||
// `, | ||
// expected: dedent` | ||
// let x_1 = 0; | ||
// if ((x_1 === 0)) { | ||
// console.log("1"); | ||
// } | ||
// if ((x_1 === 0)) { | ||
// console.log(1); | ||
// } else { | ||
// console.log(2); | ||
// } | ||
// if ((x_1 === 0)) { | ||
// console.log(1); | ||
// } else | ||
// if ((x_1 === 2)) { | ||
// console.log(3); | ||
// } | ||
// if ((x_1 === 0)) { | ||
// console.log(1); | ||
// } else | ||
// if ((x_1 === 2)) { | ||
// console.log(3); | ||
// } else { | ||
// console.log(4); | ||
// } | ||
// `, | ||
// }, | ||
// { | ||
// name: "while", | ||
// source: ` | ||
// auto x = 0; | ||
// while x < 5 { | ||
// auto y = 0; | ||
// while y < 5 { | ||
// print x * y; | ||
// y = y + 1; | ||
// break; | ||
// } | ||
// x = x + 1; | ||
// } | ||
// `, | ||
// expected: dedent` | ||
// let x_1 = 0; | ||
// while ((x_1 < 5)) { | ||
// let y_2 = 0; | ||
// while ((y_2 < 5)) { | ||
// console.log((x_1 * y_2)); | ||
// y_2 = (y_2 + 1); | ||
// break; | ||
// } | ||
// x_1 = (x_1 + 1); | ||
// } | ||
// `, | ||
// }, | ||
// { | ||
// name: "functions", | ||
// source: ` | ||
// auto z = 0.5; | ||
// func f(x: float, y: bool) { | ||
// print x; | ||
// return; | ||
// } | ||
// func g(): bool { | ||
// return false; | ||
// } | ||
// f(z, g()); | ||
// `, | ||
// expected: dedent` | ||
// let z_1 = 0.5; | ||
// function f_2(x_3, y_4) { | ||
// console.log(x_3); | ||
// return; | ||
// } | ||
// function g_5() { | ||
// return false; | ||
// } | ||
// f_2(z_1, g_5()); | ||
// `, | ||
// }, | ||
// // Not synactically correct yet - array member not implemented | ||
// { | ||
// name: "arrays", | ||
// source: ` | ||
// auto a = [true, false, true]; | ||
// auto b = [10, 20, 30]; | ||
// c = int[]() | ||
// print a[1] b[0]; | ||
// `, | ||
// expected: dedent` | ||
// let a_1 = [true,false,true]; | ||
// let b_2 = [10,20,30]; | ||
// let c_3 = []; | ||
// console.log(a_1[1], b_2[0]); | ||
// `, | ||
// }, | ||
// // Not synactically correct yet - member operator not implemented | ||
// { | ||
// name: "classes", | ||
// source: ` | ||
// class S { x: int } | ||
// auto x = S(3); | ||
// print(x.x); | ||
// `, | ||
// expected: dedent` | ||
// class S_1 { | ||
// constructor(x_2) { | ||
// this["x_2"] = x_2; | ||
// } | ||
// } | ||
// let x_3 = new S_1(3); | ||
// console.log((x_3["x_2"])); | ||
// `, | ||
// }, | ||
// // Not synactically correct yet - loop over array not implemented | ||
// { | ||
// name: "for loops", | ||
// source: ` | ||
// for i in 1..50 { | ||
// print i; | ||
// } | ||
// for j in [10, 20, 30] { | ||
// print j; | ||
// } | ||
// for k in 1..10 { | ||
// return; | ||
// } | ||
// `, | ||
// expected: dedent` | ||
// for (let i_1 = 1; i_1 < 50; i_1++) { | ||
// console.log(i_1); | ||
// } | ||
// for (let j_2 of [10,20,30]) { | ||
// console.log(j_2); | ||
// } | ||
// for (let k_4 = 1; k_4 <= 10; k_4++) { | ||
// return; | ||
// } | ||
// `, | ||
// }, | ||
// ] | ||
const fixtures = [ | ||
{ | ||
name: "small", | ||
source: ` | ||
auto x = 3 * 7; | ||
x = x + 1; | ||
x = x - 1; | ||
auto y = true; | ||
`, | ||
expected: dedent` | ||
let x_1 = 21; | ||
x_1 = x_1 + 1; | ||
x_1 = x_1 - 1; | ||
let y_2 = true; | ||
`, | ||
}, | ||
{ | ||
name: "if", | ||
source: ` | ||
auto x = 0; | ||
if x == 0 { print "1"; } | ||
if x == 0 { print 1; } else { print 2; } | ||
if x == 0 { print 1; } else if x == 2 { print 3; } | ||
if x == 0 { print 1; } else if x == 2 { print 3; } else { print 4; } | ||
`, | ||
expected: dedent` | ||
let x_1 = 0; | ||
if ((x_1 === 0)) { | ||
console.log("1"); | ||
} | ||
if ((x_1 === 0)) { | ||
console.log(1); | ||
} else { | ||
console.log(2); | ||
} | ||
if ((x_1 === 0)) { | ||
console.log(1); | ||
} else | ||
if ((x_1 === 2)) { | ||
console.log(3); | ||
} | ||
if ((x_1 === 0)) { | ||
console.log(1); | ||
} else | ||
if ((x_1 === 2)) { | ||
console.log(3); | ||
} else { | ||
console.log(4); | ||
} | ||
`, | ||
}, | ||
{ | ||
name: "while", | ||
source: ` | ||
auto x = 0; | ||
while x < 5 { | ||
auto y = 0; | ||
while y < 5 { | ||
print x * y; | ||
y = y + 1; | ||
break; | ||
} | ||
x = x + 1; | ||
} | ||
`, | ||
expected: dedent` | ||
let x_1 = 0; | ||
while ((x_1 < 5)) { | ||
let y_2 = 0; | ||
while ((y_2 < 5)) { | ||
console.log((x_1 * y_2)); | ||
y_2 = (y_2 + 1); | ||
break; | ||
} | ||
x_1 = (x_1 + 1); | ||
} | ||
`, | ||
}, | ||
{ | ||
name: "functions", | ||
source: ` | ||
auto z = 0.5; | ||
func f(x: float, y: bool) { | ||
print x; | ||
return; | ||
} | ||
func g(): bool { | ||
return false; | ||
} | ||
f(z, g()); | ||
`, | ||
expected: dedent` | ||
let z_1 = 0.5; | ||
function f_2(x_3, y_4) { | ||
console.log(x_3); | ||
return; | ||
} | ||
function g_5() { | ||
return false; | ||
} | ||
f_2(z_1, g_5()); | ||
`, | ||
}, | ||
// Not synactically correct yet - array member not implemented | ||
{ | ||
name: "arrays", | ||
source: ` | ||
auto a = [true, false, true]; | ||
auto b = [10, 20, 30]; | ||
c = int[]() | ||
print a[1] b[0]; | ||
`, | ||
expected: dedent` | ||
let a_1 = [true,false,true]; | ||
let b_2 = [10,20,30]; | ||
let c_3 = []; | ||
console.log(a_1[1], b_2[0]); | ||
`, | ||
}, | ||
// Not synactically correct yet - member operator not implemented | ||
{ | ||
name: "classes", | ||
source: ` | ||
class S { x: int } | ||
auto x = S(3); | ||
print(x.x); | ||
`, | ||
expected: dedent` | ||
class S_1 { | ||
constructor(x_2) { | ||
this["x_2"] = x_2; | ||
} | ||
} | ||
let x_3 = new S_1(3); | ||
console.log((x_3["x_2"])); | ||
`, | ||
}, | ||
// Not synactically correct yet - loop over array not implemented | ||
{ | ||
name: "for loops", | ||
source: ` | ||
for i in 1..50 { | ||
print i; | ||
} | ||
for j in [10, 20, 30] { | ||
print j; | ||
} | ||
`, | ||
expected: dedent` | ||
for (let i_1 = 1; i_1 < 50; i_1++) { | ||
console.log(i_1); | ||
} | ||
for (let j_2 of [10,20,30]) { | ||
console.log(j_2); | ||
} | ||
`, | ||
}, | ||
] | ||
|
||
// describe("The code generator", () => { | ||
// for (const fixture of fixtures) { | ||
// it(`produces expected js output for the ${fixture.name} program`, () => { | ||
// const actual = generate(optimize(analyze(parse(fixture.source)))) | ||
// assert.deepEqual(actual, fixture.expected) | ||
// }) | ||
// } | ||
// }) | ||
describe("The code generator", () => { | ||
for (const fixture of fixtures) { | ||
it(`produces expected js output for the ${fixture.name} program`, () => { | ||
const actual = generate(optimize(analyze(parse(fixture.source)))) | ||
assert.deepEqual(actual, fixture.expected) | ||
}) | ||
} | ||
}) |