Skip to content

Commit 64db0d5

Browse files
committed
2 parents 92e8b17 + 950b5ba commit 64db0d5

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/compiler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ export default function compile(source, outputType) {
1313
if (outputType === "analyzed") return analyzed
1414
const optimized = optimize(analyzed)
1515
if (outputType === "optimized") return optimized
16-
return generate(optimized)
16+
const generated = generate(optimized)
17+
if (outputType === "js") return generated
1718
}

test/compiler.test.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1-
import { describe, it } from "node:test";
2-
import assert from "node:assert/strict";
3-
describe("The project setup", () => {
4-
it("can at least run tests", () => {
5-
assert.deepEqual(1, 1);
6-
});
7-
});
1+
import { describe, it } from "node:test"
2+
import assert from "node:assert/strict"
3+
import compile from "../src/compiler.js"
4+
5+
const sampleProgram = "print(0)"
6+
7+
describe("The compiler", () => {
8+
it("throws when the output type is missing", () => {
9+
assert.throws(() => compile(sampleProgram), /Unknown output type/)
10+
})
11+
it("throws when the output type is unknown", () => {
12+
assert.throws(() => compile(sampleProgram, "no such type"), /Unknown output type/)
13+
})
14+
it("accepts the parsed option", () => {
15+
const compiled = compile(sampleProgram, "parsed")
16+
assert(compiled.startsWith("Syntax is ok"))
17+
})
18+
it("accepts the analyzed option", () => {
19+
const compiled = compile(sampleProgram, "analyzed")
20+
assert(compiled.kind === "Program")
21+
})
22+
it("accepts the optimized option", () => {
23+
const compiled = compile(sampleProgram, "optimized")
24+
assert(compiled.kind === "Program")
25+
})
26+
it("generates js code when given the js option", () => {
27+
const compiled = compile(sampleProgram, "js")
28+
assert(compiled.startsWith("console.log(0)"))
29+
})
30+
})

0 commit comments

Comments
 (0)