assemblyscript/src/index.ts

126 lines
3.4 KiB
TypeScript
Raw Normal View History

//////////////////////// Low-level C-Like Compiler API /////////////////////////
2017-12-24 03:19:47 +01:00
import {
Compiler,
Options,
Target
} from "./compiler";
import {
Decompiler
} from "./decompiler";
2017-12-24 03:19:47 +01:00
import {
DiagnosticMessage,
DiagnosticCategory,
formatDiagnosticMessage
} from "./diagnostics";
import {
Module
} from "./module";
2017-12-24 03:19:47 +01:00
import {
Parser
} from "./parser";
/** Parses a source file. If `parser` has been omitted a new one is created. */
export function parseFile(text: string, path: string, isEntry: bool = false,
parser: Parser | null = null
): Parser {
2017-09-28 13:08:25 +02:00
if (!parser) {
parser = new Parser();
isEntry = true;
}
parser.parseFile(text, path, isEntry);
return parser;
}
/** Obtains the next required file's path. Returns `null` once complete. */
2017-09-28 13:08:25 +02:00
export function nextFile(parser: Parser): string | null {
return parser.nextFile();
}
/** Obtains the next diagnostic message. Returns `null` once complete. */
2017-09-28 13:08:25 +02:00
export function nextDiagnostic(parser: Parser): DiagnosticMessage | null {
var program = parser.program;
return program.diagnosticsOffset < program.diagnostics.length
? program.diagnostics[program.diagnosticsOffset++]
: null;
2017-09-28 13:08:25 +02:00
}
/** Formats a diagnostic message to a string. */
export { formatDiagnosticMessage as formatDiagnostic };
/** Tests whether a diagnostic is informatory. */
export function isInfo(message: DiagnosticMessage): bool {
return message.category == DiagnosticCategory.INFO;
}
/** Tests whether a diagnostic is a warning. */
export function isWarning(message: DiagnosticMessage): bool {
return message.category == DiagnosticCategory.WARNING;
}
/** Tests whether a diagnostic is an error. */
2017-09-28 13:08:25 +02:00
export function isError(message: DiagnosticMessage): bool {
return message.category == DiagnosticCategory.ERROR;
}
/** Creates a new set of compiler options. */
export function createOptions(): Options {
return new Options();
2017-09-28 13:08:25 +02:00
}
/** Sets the `target` option. */
export function setTarget(options: Options, target: Target): void {
options.target = target;
}
/** Sets the `noTreeShaking` option. */
export function setNoTreeShaking(options: Options, noTreeShaking: bool): void {
options.noTreeShaking = noTreeShaking;
}
2017-12-13 23:24:13 +01:00
/** Sets the `noAssert` option. */
export function setNoAssert(options: Options, noAssert: bool): void {
options.noAssert = noAssert;
}
/** Sets the `noMemory` option. */
export function setNoMemory(options: Options, noMemory: bool): void {
options.noMemory = noMemory;
}
/** Sets the `importMemory` option. */
export function setImportMemory(options: Options, importMemory: bool): void {
options.importMemory = importMemory;
}
/** Sets the `sourceMap` option. */
export function setSourceMap(options: Options, sourceMap: bool): void {
options.sourceMap = sourceMap;
}
/** Sets the `memoryBase` option. */
export function setMemoryBase(options: Options, memoryBase: u32): void {
options.memoryBase = memoryBase;
}
/** Compiles the sources computed by the parser to a module. */
export function compile(parser: Parser, options: Options | null = null): Module {
var program = parser.finish();
var compiler = new Compiler(program, options);
return compiler.compile();
}
2017-12-12 04:35:30 +01:00
/** Decompiles a module to its (low level) source. */
export function decompile(module: Module): string {
var decompiler = new Decompiler();
2017-12-12 04:35:30 +01:00
decompiler.decompile(module);
return decompiler.finish();
}
/** Prefix indicating a library file. */
export { LIBRARY_PREFIX } from "./program";