Skip to content

Commit

Permalink
completed analyzer test coverage except for anyType
Browse files Browse the repository at this point in the history
  • Loading branch information
david518yang committed Apr 26, 2024
1 parent e15575a commit 77a8118
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 180 deletions.
6 changes: 6 additions & 0 deletions src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ export default function analyze(match) {
must(e.type?.kind === 'ArrayType', 'Expected an array', at)
}

<<<<<<< HEAD
// function mustHaveAnOptionalType(e, at) {
// must(e.type?.kind === 'OptionalType', 'Expected an optional', at)
// }

=======
>>>>>>> 2fcb6074c2f809f4cb62ad91e6677f01c2f79ffa
function mustHaveAClassType(e, at) {
must(e.type?.kind === 'ClassType', 'Expected a class', at)
}
Expand Down Expand Up @@ -189,8 +192,11 @@ export default function analyze(match) {
return 'string'
case 'BoolType':
return 'boolean'
<<<<<<< HEAD
case 'AnyType':
return 'any'
=======
>>>>>>> 2fcb6074c2f809f4cb62ad91e6677f01c2f79ffa
case 'ClassType':
return type.name
case 'ArrayType':
Expand Down
3 changes: 3 additions & 0 deletions src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default function generate(program) {
// with newlines and return the result.
const output = []

const standardFunctions = new Map([
[standardLibrary.print, x => `console.log(${x})`]
])

// Variable and function names in JS will be suffixed with _1, _2, _3,
// etc. This is because "switch", for example, is a legal name in Carlos,
Expand Down
354 changes: 174 additions & 180 deletions test/generator.test.js
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)
})
}
})

0 comments on commit 77a8118

Please sign in to comment.