-
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.
- Loading branch information
1 parent
73d94b6
commit fad3e20
Showing
18 changed files
with
392 additions
and
271 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,70 +1,29 @@ | ||
/// <reference lib="deno.ns" /> | ||
|
||
import { resolve, join, relative } from "https://deno.land/[email protected]/path/mod.ts"; | ||
import { existsSync } from "https://deno.land/[email protected]/fs/mod.ts"; | ||
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts"; | ||
|
||
import Function from "~/compiler/function.ts"; | ||
import Package from "~/compiler/package.ts"; | ||
import Project from "~/compiler/project.ts"; | ||
import { DisplayTimers, TimerStart, TimerEnd } from "~/helper.ts"; | ||
import { Compile } from "~/compile.ts"; | ||
import { Panic } from "~/compiler/helper.ts"; | ||
import { Test } from "~/test.ts"; | ||
|
||
if (Deno.args.includes("--version")) { | ||
console.log("version: 0.0.0"); | ||
Deno.exit(0); | ||
} | ||
|
||
const verb = Deno.args[0]; | ||
switch (verb) { | ||
case "compile": { | ||
Compile(Deno.args[1] || "", { | ||
time: Deno.args.includes("--time") | ||
}); | ||
break; | ||
} | ||
case "test": { Test(); break; } | ||
default: Panic( | ||
`${colors.red("Error")}: Unknown verb ${verb}` | ||
); | ||
} | ||
if (!Deno.args[0]) { | ||
Panic(`${colors.red("Error")}: Please provide an entry file`); | ||
} | ||
|
||
const cwd = resolve("./"); | ||
const root = join(cwd, Deno.args[0]); | ||
|
||
if (!existsSync(root)) { | ||
Panic(`${colors.red("Error")}: Cannot find entry ${colors.cyan(relative(cwd, root))}`); | ||
} | ||
|
||
const project = new Project(); | ||
const mainPck = new Package(project, root); | ||
if (project.failed) Panic(`Compilation ${colors.red("Failed")}`); | ||
|
||
const mainFile = mainPck.import(root); | ||
const mainFunc = mainFile.namespace["main"]; | ||
if (!(mainFunc instanceof Function)) Panic( | ||
`Main namespace is not a function: ${colors.cyan(mainFunc.constructor.name)}` | ||
); | ||
|
||
TimerStart("compilation"); | ||
mainFunc.compile(); | ||
TimerEnd("compilation"); | ||
|
||
if (project.failed) Panic(`Compilation ${colors.red("Failed")}`); | ||
|
||
if (!mainFunc.ref) Panic(`Main function not compiled correctly`); | ||
project.module.exportFunction("_start", mainFunc.ref); | ||
project.module.exportFunction("main", mainFunc.ref); | ||
project.module.startFunction(mainFunc.ref); | ||
|
||
TimerStart("serialize"); | ||
await Deno.writeFile("out.wasm", project.toBinary()); | ||
TimerEnd("serialize"); | ||
|
||
TimerStart("wasm2wat"); | ||
const command = new Deno.Command( | ||
"wasm2wat", | ||
{ args: ["-v", "out.wasm", "-o", "out.wat", "--enable-all"] } | ||
); | ||
const { code, stdout, stderr } = await command.output(); | ||
if (code !== 0) { | ||
console.error("Invalid wasm generated"); | ||
console.error(new TextDecoder().decode(stderr)); | ||
Deno.exit(1); | ||
} | ||
TimerEnd("wasm2wat"); | ||
console.log(new TextDecoder().decode(stdout)); | ||
|
||
console.log(` out: ${"out.wasm"}\n`); | ||
|
||
if (Deno.args.includes("--time")) DisplayTimers(); |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/// <reference lib="deno.ns" /> | ||
|
||
import { resolve, join, relative } from "https://deno.land/[email protected]/path/mod.ts"; | ||
import { existsSync } from "https://deno.land/[email protected]/fs/mod.ts"; | ||
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts"; | ||
|
||
import Function from "~/compiler/function.ts"; | ||
import Package from "~/compiler/package.ts"; | ||
import Project from "~/compiler/project.ts"; | ||
import { DisplayTimers, TimerStart, TimerEnd } from "~/helper.ts"; | ||
import { Panic } from "~/compiler/helper.ts"; | ||
|
||
|
||
export async function Compile(entry: string, config: { | ||
time: boolean | ||
}) { | ||
const cwd = resolve("./"); | ||
const root = join(cwd, entry); | ||
|
||
if (!existsSync(root)) Panic( | ||
`${colors.red("Error")}: Cannot find entry ${colors.cyan(relative(cwd, root))}` | ||
); | ||
|
||
const project = new Project(); | ||
const mainPck = new Package(project, root); | ||
|
||
const mainFile = mainPck.import(root); | ||
const mainFunc = mainFile.namespace["main"]; | ||
if (!(mainFunc instanceof Function)) Panic( | ||
`Main namespace is not a function: ${colors.cyan(mainFunc.constructor.name)}` | ||
); | ||
|
||
if (config.time) TimerStart("compilation"); | ||
mainFunc.compile(); | ||
if (config.time) TimerEnd("compilation"); | ||
|
||
if (project.failed) Panic(`Compilation ${colors.red("Failed")}`); | ||
|
||
if (!mainFunc.ref) Panic(`Main function not compiled correctly`); | ||
project.module.exportFunction("_start", mainFunc.ref); | ||
project.module.exportFunction("main", mainFunc.ref); | ||
project.module.startFunction(mainFunc.ref); | ||
|
||
if (config.time) TimerStart("serialize"); | ||
await Deno.writeFile("out.wasm", project.toBinary()); | ||
if (config.time) TimerEnd("serialize"); | ||
|
||
if (config.time) TimerStart("wasm2wat"); | ||
const command = new Deno.Command( | ||
"wasm2wat", | ||
{ args: ["-v", "out.wasm", "-o", "out.wat", "--enable-all"] } | ||
); | ||
const { code, stdout, stderr } = await command.output(); | ||
if (code !== 0) { | ||
console.error("Invalid wasm generated"); | ||
console.error(new TextDecoder().decode(stderr)); | ||
Deno.exit(1); | ||
} | ||
if (config.time) TimerEnd("wasm2wat"); | ||
|
||
console.log(new TextDecoder().decode(stdout)); | ||
console.log(` out: "out.wasm" + "out.wat"\n`); | ||
|
||
if (config.time) DisplayTimers(); | ||
} |
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
Oops, something went wrong.