diff --git a/README.md b/README.md index 8c8ec317..1c919855 100644 --- a/README.md +++ b/README.md @@ -64,9 +64,11 @@ Using the CLI ------------- ``` -Syntax: asc [options] [file ...] +Syntax: asc [options] [entryFile ...] Examples: asc hello.ts + asc hello.ts -b hello.wasm -t hello.wast -a hello.js + asc hello.ts -b > hello.wasm Options: -v, --version Prints the compiler's version. diff --git a/bin/asc.js b/bin/asc.js index 85456b2a..dc4303a0 100644 --- a/bin/asc.js +++ b/bin/asc.js @@ -62,61 +62,70 @@ if (args.help || args._.length < 1) { }); console.log([ "Version " + version, - "Syntax: asc [options] [file ...]", + "Syntax: asc [options] [entryFile ...]", "", "Examples: asc hello.ts", + " asc hello.ts -b hello.wasm -t hello.wast -a hello.js", + " asc hello.ts -b > hello.wasm", "", "Options:" ].concat(options).join("\n")); process.exit(args.help ? 0 : 1); } -var entryPath = args._[0].replace(/\\/g, "/").replace(/(\.ts|\/)$/, ""); -var entryDir = path.dirname(entryPath); -var entryText; -try { - entryText = fs.readFileSync(entryPath + ".ts", { encoding: "utf8" }); -} catch (e) { - try { - entryText = fs.readFileSync(entryPath + "/index.ts", { encoding: "utf8" }); - entryPath = entryPath + "/index"; - } catch (e) { - console.error("File '" + entryPath + ".ts' not found."); - process.exit(1); +var parser = null; + +function checkDiagnostics(parser) { + var diagnostic; + var hasErrors = false; + + while ((diagnostic = assemblyscript.nextDiagnostic(parser)) != null) { + console.error(assemblyscript.formatDiagnostic(diagnostic, process.stderr.isTTY, true)); + if (assemblyscript.isError(diagnostic)) + hasErrors = true; } + if (hasErrors) + process.exit(1); } -var parser = assemblyscript.parseFile(entryText, entryPath); +args._.forEach(filename => { + var entryPath = filename.replace(/\\/g, "/").replace(/(\.ts|\/)$/, ""); + var entryDir = path.dirname(entryPath); + var entryText; -var nextPath; -var nextText; - -while ((nextPath = parser.nextFile()) != null) { try { - nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" }); + entryText = fs.readFileSync(entryPath + ".ts", { encoding: "utf8" }); } catch (e) { try { - nextText = fs.readFileSync(nextPath + "/index.ts", { encoding: "utf8" }); - nextPath = nextPath + "/index"; + entryText = fs.readFileSync(entryPath + "/index.ts", { encoding: "utf8" }); + entryPath = entryPath + "/index"; } catch (e) { - console.error("Imported file '" + nextPath + ".ts' not found."); + console.error("File '" + entryPath + ".ts' not found."); process.exit(1); } } - assemblyscript.parseFile(nextText, nextPath, parser); -} -var diagnostic; -var hasErrors = false; + parser = assemblyscript.parseFile(entryText, entryPath, parser, true); -while ((diagnostic = assemblyscript.nextDiagnostic(parser)) != null) { - console.error(assemblyscript.formatDiagnostic(diagnostic, process.stderr.isTTY, true)); - if (assemblyscript.isError(diagnostic)) - hasErrors = true; -} + var nextPath; + var nextText; -if (hasErrors) - process.exit(1); + while ((nextPath = parser.nextFile()) != null) { + try { + nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" }); + } catch (e) { + try { + nextText = fs.readFileSync(nextPath + "/index.ts", { encoding: "utf8" }); + nextPath = nextPath + "/index"; + } catch (e) { + console.error("Imported file '" + nextPath + ".ts' not found."); + process.exit(1); + } + } + assemblyscript.parseFile(nextText, nextPath, parser); + } + checkDiagnostics(parser); +}); var options = assemblyscript.createOptions(); assemblyscript.setTarget(options, 0); @@ -124,18 +133,7 @@ assemblyscript.setNoTreeShaking(options, args.noTreeShaking); assemblyscript.setNoDebug(options, args.noDebug); var module = assemblyscript.compile(parser, options); - -hasErrors = false; -while ((diagnostic = assemblyscript.nextDiagnostic(parser)) != null) { - console.error(assemblyscript.formatDiagnostic(diagnostic, process.stderr.isTTY, true)); - if (assemblyscript.isError(diagnostic)) - hasErrors = true; -} - -if (hasErrors) { - module.dispose(); - process.exit(1); -} +checkDiagnostics(parser); if (args.validate) if (!module.validate()) { diff --git a/src/index.ts b/src/index.ts index e8e71a76..852abd10 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,8 +26,7 @@ import { Parser } from "./parser"; import { Program } from "./program"; /** Parses a single source file. If `parser` has been omitted a new one is created. */ -export function parseFile(text: string, path: string, parser: Parser | null = null): Parser { - let isEntry: bool = false; +export function parseFile(text: string, path: string, parser: Parser | null = null, isEntry: bool = false): Parser { if (!parser) { parser = new Parser(); isEntry = true;