Cleanup; Documentation

This commit is contained in:
dcodeIO 2017-12-18 03:46:36 +01:00
parent c70eb7792f
commit aa563a9a5a
15 changed files with 609 additions and 332 deletions

File diff suppressed because it is too large Load Diff

View File

@ -107,7 +107,7 @@ export function initialize(program: Program): void {
}
}
/** Adds a built-in global to the specified program. */
/** Adds a built-in constant to the specified program. */
function addConstant(program: Program, name: string, type: Type): Global {
const global: Global = new Global(program, name, null, null);
global.isBuiltIn = true;

View File

@ -58,6 +58,7 @@ import {
ForStatement,
IfStatement,
ImportStatement,
InterfaceDeclaration,
MethodDeclaration,
ModifierKind,
NamespaceDeclaration,
@ -93,8 +94,7 @@ import {
UnaryPrefixExpression,
// utility
hasModifier,
InterfaceDeclaration
hasModifier
} from "./ast";
import {
@ -314,7 +314,7 @@ export class Compiler extends DiagnosticEmitter {
return null;
if (isModuleExport(element, declaration)) {
if ((<Global>element).hasConstantValue)
this.module.addGlobalExport(element.internalName, declaration.identifier.name);
this.module.addGlobalExport(element.internalName, declaration.name.name);
else
this.warning(DiagnosticCode.Cannot_export_a_mutable_global, declaration.range);
}
@ -334,7 +334,7 @@ export class Compiler extends DiagnosticEmitter {
if (!declaration)
throw new Error("unexpected missing declaration");
if (!declaration.type) { // TODO: infer type
this.error(DiagnosticCode.Type_expected, declaration.identifier.range);
this.error(DiagnosticCode.Type_expected, declaration.name.range);
return false;
}
type = this.program.resolveType(declaration.type); // reports
@ -489,7 +489,7 @@ export class Compiler extends DiagnosticEmitter {
if (!instance)
return;
if (isModuleExport(instance, declaration))
this.module.addFunctionExport(instance.internalName, declaration.identifier.name);
this.module.addFunctionExport(instance.internalName, declaration.name.name);
}
compileFunctionUsingTypeArguments(prototype: FunctionPrototype, typeArguments: TypeNode[], contextualTypeArguments: Map<string,Type> | null = null, alternativeReportNode: Node | null = null): Function | null {
@ -509,12 +509,12 @@ export class Compiler extends DiagnosticEmitter {
if (instance.isDeclared) {
if (declaration.statements) {
this.error(DiagnosticCode.An_implementation_cannot_be_declared_in_ambient_contexts, declaration.identifier.range);
this.error(DiagnosticCode.An_implementation_cannot_be_declared_in_ambient_contexts, declaration.name.range);
return false;
}
} else {
if (!declaration.statements) {
this.error(DiagnosticCode.Function_implementation_is_missing_or_not_immediately_following_the_declaration, declaration.identifier.range);
this.error(DiagnosticCode.Function_implementation_is_missing_or_not_immediately_following_the_declaration, declaration.name.range);
return false;
}
}
@ -546,7 +546,7 @@ export class Compiler extends DiagnosticEmitter {
// create the function
const internalName: string = instance.internalName;
if (instance.isDeclared) { // TODO: use parent namespace as externalModuleName, if applicable
this.module.addFunctionImport(internalName, "env", declaration.identifier.name, typeRef);
this.module.addFunctionImport(internalName, "env", declaration.name.name, typeRef);
} else {
this.module.addFunction(internalName, typeRef, typesToNativeTypes(instance.additionalLocals), this.module.createBlock(null, <ExpressionRef[]>stmts, NativeType.None));
}
@ -871,14 +871,14 @@ export class Compiler extends DiagnosticEmitter {
compileIfStatement(statement: IfStatement): ExpressionRef {
const condition: ExpressionRef = this.compileExpression(statement.condition, Type.i32);
const ifTrue: ExpressionRef = this.compileStatement(statement.statement);
const ifFalse: ExpressionRef = statement.elseStatement ? this.compileStatement(<Statement>statement.elseStatement) : 0;
const ifTrue: ExpressionRef = this.compileStatement(statement.ifTrue);
const ifFalse: ExpressionRef = statement.ifFalse ? this.compileStatement(<Statement>statement.ifFalse) : 0;
return this.module.createIf(condition, ifTrue, ifFalse);
}
compileReturnStatement(statement: ReturnStatement): ExpressionRef {
if (this.currentFunction) {
const expression: ExpressionRef = statement.expression ? this.compileExpression(<Expression>statement.expression, this.currentFunction.returnType) : 0;
const expression: ExpressionRef = statement.value ? this.compileExpression(<Expression>statement.value, this.currentFunction.returnType) : 0;
return this.module.createReturn(expression);
}
return this.module.createUnreachable();
@ -895,7 +895,7 @@ export class Compiler extends DiagnosticEmitter {
// prepend initializer to inner block
const breaks: ExpressionRef[] = new Array(1 + k);
breaks[0] = this.module.createSetLocal(tempLocal.index, this.compileExpression(statement.expression, Type.i32)); // initializer
breaks[0] = this.module.createSetLocal(tempLocal.index, this.compileExpression(statement.condition, Type.i32)); // initializer
// make one br_if per (possibly dynamic) labeled case (binaryen optimizes to br_table where possible)
let breakIndex: i32 = 1;
@ -966,18 +966,18 @@ export class Compiler extends DiagnosticEmitter {
for (let i: i32 = 0, k = declarations.length; i < k; ++i) {
const declaration: VariableDeclaration = declarations[i];
if (declaration.type) {
const name: string = declaration.identifier.name;
const name: string = declaration.name.name;
const type: Type | null = this.program.resolveType(<TypeNode>declaration.type, this.currentFunction.contextualTypeArguments, true); // reports
if (type) {
if (this.currentFunction.locals.has(name))
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, name); // recoverable
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, name); // recoverable
else
this.currentFunction.addLocal(<Type>type, name);
if (declaration.initializer)
initializers.push(this.compileAssignment(declaration.identifier, <Expression>declaration.initializer, Type.void));
initializers.push(this.compileAssignment(declaration.name, <Expression>declaration.initializer, Type.void));
}
} else {
this.error(DiagnosticCode.Type_expected, declaration.identifier.range);
this.error(DiagnosticCode.Type_expected, declaration.name.range);
}
}
return initializers.length ? this.module.createBlock(null, initializers, NativeType.None) : this.module.createNop();
@ -1984,7 +1984,7 @@ export class Compiler extends DiagnosticEmitter {
const operator: Token = expression.operator;
// make a getter for the expression (also obtains the type)
const getValue: ExpressionRef = this.compileExpression(expression.expression, contextualType, contextualType == Type.void ? ConversionKind.NONE : ConversionKind.IMPLICIT);
const getValue: ExpressionRef = this.compileExpression(expression.operand, contextualType, contextualType == Type.void ? ConversionKind.NONE : ConversionKind.IMPLICIT);
// use a temp local for the intermediate value
const tempLocal: Local = this.currentFunction.getTempLocal(this.currentType);
@ -2015,7 +2015,7 @@ export class Compiler extends DiagnosticEmitter {
}
// make a setter that sets the new value (temp value +/- 1)
const setValue: ExpressionRef = this.compileAssignmentWithValue(expression.expression,
const setValue: ExpressionRef = this.compileAssignmentWithValue(expression.operand,
this.module.createBinary(op,
this.module.createGetLocal(tempLocal.index, nativeType),
nativeOne
@ -2034,7 +2034,7 @@ export class Compiler extends DiagnosticEmitter {
}
compileUnaryPrefixExpression(expression: UnaryPrefixExpression, contextualType: Type): ExpressionRef {
const operandExpression: Expression = expression.expression;
const operandExpression: Expression = expression.operand;
let operand: ExpressionRef;
let op: UnaryOp;

View File

@ -64,21 +64,30 @@ import {
VariableDeclaration,
WhileStatement,
hasModifier
addModifier,
getModifier,
hasModifier,
setReusableModifiers
} from "./ast";
/** Parser interface. */
export class Parser extends DiagnosticEmitter {
/** Program being created. */
program: Program;
/** Log of source file names to be requested. */
backlog: string[] = new Array();
/** Log of source file names already processed. */
seenlog: Set<string> = new Set();
/** Constructs a new parser. */
constructor() {
super();
this.program = new Program(this.diagnostics);
}
/** Parses a file and adds its definitions to the program. */
parseFile(text: string, path: string, isEntry: bool): void {
const normalizedPath: string = normalizePath(path);
for (let i: i32 = 0, k: i32 = this.program.sources.length; i < k; ++i)
@ -99,7 +108,6 @@ export class Parser extends DiagnosticEmitter {
statement.parent = source;
source.statements.push(statement);
}
reusableModifiers = null;
}
parseTopLevelStatement(tn: Tokenizer, isNamespaceMember: bool = false): Statement | null {
@ -129,6 +137,7 @@ export class Parser extends DiagnosticEmitter {
tn.mark();
let statement: Statement | null = null;
let modifier: Modifier | null;
switch (tn.next()) {
case Token.CONST:
@ -175,12 +184,12 @@ export class Parser extends DiagnosticEmitter {
break;
case Token.IMPORT:
if (hasModifier(ModifierKind.EXPORT, modifiers)) {
statement = this.parseExportImport(tn, getModifier(ModifierKind.EXPORT, <Modifier[]>modifiers).range);
if (modifier = getModifier(ModifierKind.EXPORT, modifiers)) {
statement = this.parseExportImport(tn, modifier.range);
} else
statement = this.parseImport(tn);
if (modifiers)
reusableModifiers = modifiers;
setReusableModifiers(modifiers);
break;
case Token.TYPE:
@ -192,9 +201,9 @@ export class Parser extends DiagnosticEmitter {
statement = this.parseExport(tn, modifiers); // TODO: why exactly does this have modifiers again?
} else {
if (modifiers) {
if (hasModifier(ModifierKind.DECLARE, modifiers))
this.error(DiagnosticCode._0_modifier_cannot_be_used_here, getModifier(ModifierKind.DECLARE, modifiers).range, "declare"); // recoverable
reusableModifiers = modifiers;
if (modifier = getModifier(ModifierKind.DECLARE, modifiers))
this.error(DiagnosticCode._0_modifier_cannot_be_used_here, modifier.range, "declare"); // recoverable
setReusableModifiers(modifiers);
}
tn.reset();
if (!isNamespaceMember)
@ -210,6 +219,7 @@ export class Parser extends DiagnosticEmitter {
return statement;
}
/** Obtains the next file to parse. */
nextFile(): string | null {
if (this.backlog.length) {
const filename: string = this.backlog[0];
@ -221,6 +231,7 @@ export class Parser extends DiagnosticEmitter {
return null;
}
/** Finishes parsing and returns the program. */
finish(): Program {
if (this.backlog.length)
throw new Error("backlog is not empty");
@ -775,12 +786,13 @@ export class Parser extends DiagnosticEmitter {
// field: (':' Type)? ('=' Expression)? ';'?
} else {
if (hasModifier(ModifierKind.ABSTRACT, modifiers))
this.error(DiagnosticCode._0_modifier_cannot_be_used_here, getModifier(ModifierKind.ABSTRACT, <Modifier[]>modifiers).range, "abstract"); // recoverable
if (hasModifier(ModifierKind.GET, modifiers))
this.error(DiagnosticCode._0_modifier_cannot_be_used_here, getModifier(ModifierKind.GET, <Modifier[]>modifiers).range, "get"); // recoverable
if (hasModifier(ModifierKind.SET, modifiers))
this.error(DiagnosticCode._0_modifier_cannot_be_used_here,getModifier(ModifierKind.SET, <Modifier[]>modifiers).range, "set"); // recoverable
let modifier: Modifier | null;
if (modifier = getModifier(ModifierKind.ABSTRACT, modifiers))
this.error(DiagnosticCode._0_modifier_cannot_be_used_here, modifier.range, "abstract"); // recoverable
if (modifier = getModifier(ModifierKind.GET, modifiers))
this.error(DiagnosticCode._0_modifier_cannot_be_used_here, modifier.range, "get"); // recoverable
if (modifier = getModifier(ModifierKind.SET, modifiers))
this.error(DiagnosticCode._0_modifier_cannot_be_used_here, modifier.range, "set"); // recoverable
let type: TypeNode | null = null;
if (tn.skip(Token.COLON)) {
type = this.parseType(tn);
@ -1323,7 +1335,7 @@ export class Parser extends DiagnosticEmitter {
// expressions
// see: http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm#climbing
parseExpressionPrefix(tn: Tokenizer): Expression | null {
parseExpressionStart(tn: Tokenizer): Expression | null {
const token: Token = tn.next();
const startPos: i32 = tn.tokenPos;
let expr: Expression | null = null;
@ -1335,7 +1347,7 @@ export class Parser extends DiagnosticEmitter {
if (token == Token.FALSE)
return Node.createFalse(tn.range());
let p: Precedence = determinePrecedencePrefix(token);
let p: Precedence = determinePrecedenceStart(token);
if (p != Precedence.INVALID) {
let operand: Expression | null
@ -1487,7 +1499,7 @@ export class Parser extends DiagnosticEmitter {
}
parseExpression(tn: Tokenizer, precedence: Precedence = 0): Expression | null {
let expr: Expression | null = this.parseExpressionPrefix(tn);
let expr: Expression | null = this.parseExpressionStart(tn);
if (!expr)
return null;
@ -1581,7 +1593,7 @@ export class Parser extends DiagnosticEmitter {
}
}
enum Precedence {
const enum Precedence {
COMMA,
SPREAD,
YIELD,
@ -1606,7 +1618,8 @@ enum Precedence {
INVALID = -1
}
function determinePrecedencePrefix(kind: Token): i32 {
/** Determines the precedence of a starting token. */
function determinePrecedenceStart(kind: Token): i32 {
switch (kind) {
case Token.DOT_DOT_DOT:
@ -1634,7 +1647,8 @@ function determinePrecedencePrefix(kind: Token): i32 {
}
}
function determinePrecedence(kind: Token): i32 { // non-prefix
/** Determines the precende of a non-starting token. */
function determinePrecedence(kind: Token): i32 {
switch (kind) {
case Token.COMMA:
@ -1718,7 +1732,8 @@ function determinePrecedence(kind: Token): i32 { // non-prefix
}
}
function isRightAssociative(kind: Token): bool { // non-prefix
/** Determines whether a non-starting token is right associative. */
function isRightAssociative(kind: Token): bool {
switch (kind) {
case Token.EQUALS:
@ -1742,30 +1757,3 @@ function isRightAssociative(kind: Token): bool { // non-prefix
return false;
}
}
let reusableModifiers: Modifier[] | null = null;
function createModifiers(): Modifier[] {
let ret: Modifier[];
if (reusableModifiers != null) {
ret = reusableModifiers;
reusableModifiers = null;
} else
ret = new Array(1);
ret.length = 0;
return ret;
}
function addModifier(modifier: Modifier, modifiers: Modifier[] | null): Modifier[] {
if (modifiers == null)
modifiers = createModifiers();
modifiers.push(modifier);
return modifiers;
}
function getModifier(kind: ModifierKind, modifiers: Modifier[]): Modifier {
for (let i: i32 = 0, k: i32 = modifiers.length; i < k; ++i)
if (modifiers[i].modifierKind == kind)
return modifiers[i];
throw new Error("no such modifier");
}

View File

@ -216,31 +216,31 @@ export class Program extends DiagnosticEmitter {
private initializeClass(declaration: ClassDeclaration, namespace: Element | null = null): void {
const internalName: string = declaration.internalName;
if (this.elements.has(internalName)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
return;
}
const prototype: ClassPrototype = new ClassPrototype(this, declaration.identifier.name, internalName, declaration);
const prototype: ClassPrototype = new ClassPrototype(this, declaration.name.name, internalName, declaration);
this.elements.set(internalName, prototype);
if (hasDecorator("global", declaration.decorators)) {
if (this.elements.has(declaration.identifier.name))
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
if (this.elements.has(declaration.name.name))
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
else
this.elements.set(declaration.identifier.name, prototype);
this.elements.set(declaration.name.name, prototype);
}
if (namespace) {
if (namespace.members) {
if (namespace.members.has(declaration.identifier.name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
if (namespace.members.has(declaration.name.name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
return;
}
} else
namespace.members = new Map();
namespace.members.set(declaration.identifier.name, prototype);
namespace.members.set(declaration.name.name, prototype);
} else if (prototype.isExported) {
if (this.exports.has(internalName)) {
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.name.range, internalName);
return;
}
this.exports.set(internalName, prototype);
@ -265,18 +265,18 @@ export class Program extends DiagnosticEmitter {
}
private initializeField(declaration: FieldDeclaration, classPrototype: ClassPrototype): void {
const name: string = declaration.identifier.name;
const name: string = declaration.name.name;
const internalName: string = declaration.internalName;
// static fields become global variables
if (hasModifier(ModifierKind.STATIC, declaration.modifiers)) {
if (this.elements.has(internalName)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, declaration.internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, declaration.internalName);
return;
}
if (classPrototype.members) {
if (classPrototype.members.has(name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, declaration.internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, declaration.internalName);
return;
}
} else
@ -289,7 +289,7 @@ export class Program extends DiagnosticEmitter {
} else {
if (classPrototype.instanceMembers) {
if (classPrototype.instanceMembers.has(name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, declaration.internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, declaration.internalName);
return;
}
} else
@ -300,18 +300,18 @@ export class Program extends DiagnosticEmitter {
}
private initializeMethod(declaration: MethodDeclaration, classPrototype: ClassPrototype): void {
let name: string = declaration.identifier.name;
let name: string = declaration.name.name;
const internalName: string = declaration.internalName;
// static methods become global functions
if (hasModifier(ModifierKind.STATIC, declaration.modifiers)) {
if (this.elements.has(internalName)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, declaration.internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, declaration.internalName);
return;
}
if (classPrototype.members) {
if (classPrototype.members.has(name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, declaration.internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, declaration.internalName);
return;
}
} else
@ -324,7 +324,7 @@ export class Program extends DiagnosticEmitter {
} else {
if (classPrototype.instanceMembers) {
if (classPrototype.instanceMembers.has(name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, declaration.internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, declaration.internalName);
return;
}
} else
@ -337,7 +337,7 @@ export class Program extends DiagnosticEmitter {
private initializeEnum(declaration: EnumDeclaration, namespace: Element | null = null): void {
const internalName: string = declaration.internalName;
if (this.elements.has(internalName)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
return;
}
const enm: Enum = new Enum(this, internalName, declaration);
@ -345,32 +345,32 @@ export class Program extends DiagnosticEmitter {
if (namespace) {
if (namespace.members) {
if (namespace.members.has(declaration.identifier.name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
if (namespace.members.has(declaration.name.name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
return;
}
} else
namespace.members = new Map();
namespace.members.set(declaration.identifier.name, enm);
namespace.members.set(declaration.name.name, enm);
} else if (enm.isExported) {
if (this.exports.has(internalName)) {
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.name.range, internalName);
return;
}
this.exports.set(internalName, enm);
}
const values: EnumValueDeclaration[] = declaration.members;
const values: EnumValueDeclaration[] = declaration.values;
for (let i: i32 = 0, k: i32 = values.length; i < k; ++i)
this.initializeEnumValue(values[i], enm);
}
private initializeEnumValue(declaration: EnumValueDeclaration, enm: Enum): void {
const name: string = declaration.identifier.name;
const name: string = declaration.name.name;
const internalName: string = declaration.internalName;
if (enm.members) {
if (enm.members.has(name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
return;
}
} else
@ -462,31 +462,31 @@ export class Program extends DiagnosticEmitter {
private initializeFunction(declaration: FunctionDeclaration, namespace: Element | null = null): void {
const internalName: string = declaration.internalName;
if (this.elements.has(internalName)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
return;
}
const prototype: FunctionPrototype = new FunctionPrototype(this, declaration.identifier.name, internalName, declaration, null);
const prototype: FunctionPrototype = new FunctionPrototype(this, declaration.name.name, internalName, declaration, null);
this.elements.set(internalName, prototype);
if (hasDecorator("global", declaration.decorators)) {
if (this.elements.has(declaration.identifier.name))
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
if (this.elements.has(declaration.name.name))
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
else
this.elements.set(declaration.identifier.name, prototype);
this.elements.set(declaration.name.name, prototype);
}
if (namespace) {
if (namespace.members) {
if (namespace.members.has(declaration.identifier.name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
if (namespace.members.has(declaration.name.name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
return;
}
} else
namespace.members = new Map();
namespace.members.set(declaration.identifier.name, prototype);
namespace.members.set(declaration.name.name, prototype);
} else if (prototype.isExported) {
if (this.exports.has(internalName)) {
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.name.range, internalName);
return;
}
this.exports.set(internalName, prototype);
@ -504,7 +504,7 @@ export class Program extends DiagnosticEmitter {
private initializeImport(declaration: ImportDeclaration, internalPath: string, queuedExports: Map<string,QueuedExport>, queuedImports: QueuedImport[]): void {
const internalName: string = declaration.internalName;
if (this.elements.has(internalName)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
return;
}
@ -548,10 +548,10 @@ export class Program extends DiagnosticEmitter {
private initializeInterface(declaration: InterfaceDeclaration, namespace: Element | null = null): void {
const internalName: string = declaration.internalName;
const prototype: InterfacePrototype = new InterfacePrototype(this, declaration.identifier.name, internalName, declaration);
const prototype: InterfacePrototype = new InterfacePrototype(this, declaration.name.name, internalName, declaration);
if (this.elements.has(internalName)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
return;
}
this.elements.set(internalName, prototype);
@ -559,7 +559,7 @@ export class Program extends DiagnosticEmitter {
if (namespace) {
if (namespace.members) {
if (namespace.members.has(prototype.internalName)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
return;
}
} else
@ -567,7 +567,7 @@ export class Program extends DiagnosticEmitter {
namespace.members.set(prototype.internalName, prototype);
} else if (prototype.isExported) {
if (this.exports.has(internalName)) {
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.name.range, internalName);
return;
}
this.exports.set(internalName, prototype);
@ -602,16 +602,16 @@ export class Program extends DiagnosticEmitter {
if (parentNamespace) {
if (parentNamespace.members) {
if (parentNamespace.members.has(declaration.identifier.name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
if (parentNamespace.members.has(declaration.name.name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
return;
}
} else
parentNamespace.members = new Map();
parentNamespace.members.set(declaration.identifier.name, namespace);
parentNamespace.members.set(declaration.name.name, namespace);
} else if (namespace.isExported) {
if (this.exports.has(internalName)) {
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, declaration.name.range, internalName);
return;
}
this.exports.set(internalName, namespace);
@ -657,7 +657,7 @@ export class Program extends DiagnosticEmitter {
const declaration: VariableDeclaration = declarations[i];
const internalName: string = declaration.internalName;
if (this.elements.has(internalName)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
continue;
}
@ -665,24 +665,24 @@ export class Program extends DiagnosticEmitter {
this.elements.set(internalName, global);
if (hasDecorator("global", declaration.decorators)) {
if (this.elements.has(declaration.identifier.name))
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
if (this.elements.has(declaration.name.name))
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
else
this.elements.set(declaration.identifier.name, global);
this.elements.set(declaration.name.name, global);
}
if (namespace) {
if (namespace.members) {
if (namespace.members.has(declaration.identifier.name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
if (namespace.members.has(declaration.name.name)) {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
continue;
}
} else
namespace.members = new Map();
namespace.members.set(declaration.identifier.name, global);
namespace.members.set(declaration.name.name, global);
} else if (global.isExported) {
if (this.exports.has(internalName))
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.identifier.range, internalName);
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
else
this.exports.set(internalName, global);
}
@ -817,7 +817,7 @@ function hasDecorator(name: string, decorators: Decorator[] | null): bool {
if (decorators)
for (let i: i32 = 0, k: i32 = decorators.length; i < k; ++i) {
const decorator: Decorator = decorators[i];
const expression: Expression = decorator.expression;
const expression: Expression = decorator.name;
const args: Expression[] = decorator.arguments;
if (expression.kind == NodeKind.IDENTIFIER && args.length <= 1 && (<IdentifierExpression>expression).name == name)
return true;
@ -1192,7 +1192,7 @@ export class FunctionPrototype extends Element {
if (typeNode) {
const type: Type | null = this.program.resolveType(<TypeNode>typeNode, contextualTypeArguments, true); // reports
if (type) {
parameters[i] = new Parameter(declaration.parameters[i].identifier.name, type);
parameters[i] = new Parameter(declaration.parameters[i].name.name, type);
parameterTypes[i] = <Type>type;
} else
return null;

File diff suppressed because one or more lines are too long

View File

@ -91,11 +91,11 @@ export class Type {
}
/** Composes the respective nullable type of this type. */
asNullable(): Type {
asNullable(): Type | null {
assert(this.kind == TypeKind.USIZE);
if (this.isNullable && !this.nullableType)
(this.nullableType = new Type(this.kind, this.size)).isNullable = true;
return <Type>this.nullableType;
return this.nullableType;
}
/** Converts this type to its TypeScript representation. */
@ -119,7 +119,7 @@ export class Type {
case TypeKind.F32: return "f32";
case TypeKind.F64: return "f64";
case TypeKind.VOID: return "void";
default: assert(false); return "INVALID";
default: assert(false); return "";
}
}

File diff suppressed because one or more lines are too long

View File

@ -9,6 +9,7 @@
*/
// TODO: div/mod
// another option is to use a wasm-based polyfill, see examples/i64-polyfill.
const I64_MIN_LO: i32 = 0;
const I64_MIN_HI: i32 = 0x80000000 | 0;

View File

@ -1,5 +1,6 @@
import { CharCode } from "./charcode";
/** Normalizes the specified path, removing interior placeholders. Expects a posix-formatted string / not Windows compatible. */
export function normalize(path: string, trimExtension: bool = false, separator: CharCode = CharCode.SLASH): string {
// expects a relative path
@ -77,10 +78,12 @@ export function normalize(path: string, trimExtension: bool = false, separator:
return len > 0 ? path : ".";
}
/** Resolves the specified path to a normalized path relative to the specified origin. */
export function resolve(normalizedPath: string, normalizedOrigin: string, separator: CharCode = CharCode.SLASH): string {
return normalize(dirname(normalizedOrigin, separator) + String.fromCharCode(separator) + normalizedPath);
}
/** Obtains the directory portion of a normalized path. */
export function dirname(normalizedPath: string, separator: CharCode = CharCode.SLASH): string {
let pos: i32 = normalizedPath.length;
while (--pos > 0)

View File

@ -1,2 +1,2 @@
/** Shared string builder. */
/** A shared string builder utilized to reduce overall array allocations. */
export const sb: string[] = [];

View File

@ -61,9 +61,9 @@
heap/HEAP_OFFSET
heap/Heap
Heap
heap/Heap.get_used
heap/Heap.get_free
heap/Heap.get_size
heap/Heap.used
heap/Heap.free
heap/Heap.size
heap/Heap.allocate
heap/Heap.dispose
heap/Heap.copy

View File

@ -183,9 +183,9 @@
heap/HEAP_OFFSET
heap/Heap
Heap
heap/Heap.get_used
heap/Heap.get_free
heap/Heap.get_size
heap/Heap.used
heap/Heap.free
heap/Heap.size
heap/Heap.allocate
heap/Heap.dispose
heap/Heap.copy

View File

@ -1,3 +1,7 @@
0 ? unreachable() : 1;
1 ? 1 : unreachable();
(0 ? unreachable() : 1) ? 1 : unreachable();
let a: i32;
a = 0 ? unreachable() : 1;

View File

@ -6,6 +6,31 @@
(export "memory" (memory $0))
(start $start)
(func $start (; 0 ;) (type $v)
(if
(i32.const 0)
(unreachable)
(drop
(i32.const 1)
)
)
(if
(i32.const 1)
(drop
(i32.const 1)
)
(unreachable)
)
(if
(if (result i32)
(i32.const 0)
(unreachable)
(i32.const 1)
)
(drop
(i32.const 1)
)
(unreachable)
)
(set_global $ternary/a
(if (result i32)
(i32.const 0)