Support compiling multiple entry files

This commit is contained in:
dcodeIO 2017-12-09 02:52:20 +01:00
parent 732068e981
commit 5ff88e126e
3 changed files with 47 additions and 48 deletions

View File

@ -64,9 +64,11 @@ Using the CLI
------------- -------------
``` ```
Syntax: asc [options] [file ...] Syntax: asc [options] [entryFile ...]
Examples: asc hello.ts Examples: asc hello.ts
asc hello.ts -b hello.wasm -t hello.wast -a hello.js
asc hello.ts -b > hello.wasm
Options: Options:
-v, --version Prints the compiler's version. -v, --version Prints the compiler's version.

View File

@ -62,21 +62,40 @@ if (args.help || args._.length < 1) {
}); });
console.log([ console.log([
"Version " + version, "Version " + version,
"Syntax: asc [options] [file ...]", "Syntax: asc [options] [entryFile ...]",
"", "",
"Examples: asc hello.ts", "Examples: asc hello.ts",
" asc hello.ts -b hello.wasm -t hello.wast -a hello.js",
" asc hello.ts -b > hello.wasm",
"", "",
"Options:" "Options:"
].concat(options).join("\n")); ].concat(options).join("\n"));
process.exit(args.help ? 0 : 1); process.exit(args.help ? 0 : 1);
} }
var entryPath = args._[0].replace(/\\/g, "/").replace(/(\.ts|\/)$/, ""); var parser = null;
var entryDir = path.dirname(entryPath);
var entryText; function checkDiagnostics(parser) {
try { 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);
}
args._.forEach(filename => {
var entryPath = filename.replace(/\\/g, "/").replace(/(\.ts|\/)$/, "");
var entryDir = path.dirname(entryPath);
var entryText;
try {
entryText = fs.readFileSync(entryPath + ".ts", { encoding: "utf8" }); entryText = fs.readFileSync(entryPath + ".ts", { encoding: "utf8" });
} catch (e) { } catch (e) {
try { try {
entryText = fs.readFileSync(entryPath + "/index.ts", { encoding: "utf8" }); entryText = fs.readFileSync(entryPath + "/index.ts", { encoding: "utf8" });
entryPath = entryPath + "/index"; entryPath = entryPath + "/index";
@ -84,14 +103,14 @@ try {
console.error("File '" + entryPath + ".ts' not found."); console.error("File '" + entryPath + ".ts' not found.");
process.exit(1); process.exit(1);
} }
} }
var parser = assemblyscript.parseFile(entryText, entryPath); parser = assemblyscript.parseFile(entryText, entryPath, parser, true);
var nextPath; var nextPath;
var nextText; var nextText;
while ((nextPath = parser.nextFile()) != null) { while ((nextPath = parser.nextFile()) != null) {
try { try {
nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" }); nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" });
} catch (e) { } catch (e) {
@ -104,19 +123,9 @@ while ((nextPath = parser.nextFile()) != null) {
} }
} }
assemblyscript.parseFile(nextText, nextPath, parser); assemblyscript.parseFile(nextText, nextPath, parser);
} }
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 options = assemblyscript.createOptions(); var options = assemblyscript.createOptions();
assemblyscript.setTarget(options, 0); assemblyscript.setTarget(options, 0);
@ -124,18 +133,7 @@ assemblyscript.setNoTreeShaking(options, args.noTreeShaking);
assemblyscript.setNoDebug(options, args.noDebug); assemblyscript.setNoDebug(options, args.noDebug);
var module = assemblyscript.compile(parser, options); var module = assemblyscript.compile(parser, options);
checkDiagnostics(parser);
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);
}
if (args.validate) if (args.validate)
if (!module.validate()) { if (!module.validate()) {

View File

@ -26,8 +26,7 @@ import { Parser } from "./parser";
import { Program } from "./program"; import { Program } from "./program";
/** Parses a single source file. If `parser` has been omitted a new one is created. */ /** 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 { export function parseFile(text: string, path: string, parser: Parser | null = null, isEntry: bool = false): Parser {
let isEntry: bool = false;
if (!parser) { if (!parser) {
parser = new Parser(); parser = new Parser();
isEntry = true; isEntry = true;