diff --git a/src/ast.ts b/src/ast.ts index 43d3809b..65ddded5 100644 --- a/src/ast.ts +++ b/src/ast.ts @@ -108,7 +108,7 @@ export abstract class Node { // types static createType(identifier: IdentifierExpression, typeArguments: TypeNode[], isNullable: bool, range: Range): TypeNode { - const type: TypeNode = new TypeNode(); + var type = new TypeNode(); type.range = range; type.identifier = identifier; type.typeArguments = typeArguments; @@ -119,22 +119,23 @@ export abstract class Node { // expressions static createIdentifier(name: string, range: Range): IdentifierExpression { - const expr: IdentifierExpression = new IdentifierExpression(); + var expr = new IdentifierExpression(); expr.range = range; expr.name = name; return expr; } static createArrayLiteral(elementExpressions: (Expression | null)[], range: Range): ArrayLiteralExpression { - const expr: ArrayLiteralExpression = new ArrayLiteralExpression(); + var expr = new ArrayLiteralExpression(); expr.range = range; - for (let i: i32 = 0, k: i32 = (expr.elementExpressions = elementExpressions).length; i < k; ++i) - if (elementExpressions[i]) (elementExpressions[i]).parent = expr; + for (var i = 0, k = (expr.elementExpressions = elementExpressions).length; i < k; ++i) + if (elementExpressions[i]) + (elementExpressions[i]).parent = expr; return expr; } static createAssertion(assertionKind: AssertionKind, expression: Expression, toType: TypeNode, range: Range): AssertionExpression { - const expr: AssertionExpression = new AssertionExpression(); + var expr = new AssertionExpression(); expr.range = range; expr.assertionKind = assertionKind; (expr.expression = expression).parent = expr; @@ -143,7 +144,7 @@ export abstract class Node { } static createBinary(operator: Token, left: Expression, right: Expression, range: Range): BinaryExpression { - const expr: BinaryExpression = new BinaryExpression(); + var expr = new BinaryExpression(); expr.range = range; expr.operator = operator; (expr.left = left).parent = expr; @@ -152,23 +153,24 @@ export abstract class Node { } static createCall(expression: Expression, typeArguments: TypeNode[], args: Expression[], range: Range): CallExpression { - const expr: CallExpression = new CallExpression(); + var expr = new CallExpression(); expr.range = range; (expr.expression = expression).parent = expr; - let i: i32, k: i32; - for (i = 0, k = (expr.typeArguments = typeArguments).length; i < k; ++i) typeArguments[i].parent = expr; - for (i = 0, k = (expr.arguments = args).length; i < k; ++i) args[i].parent = expr; + for (var i = 0, k = (expr.typeArguments = typeArguments).length; i < k; ++i) + typeArguments[i].parent = expr; + for (i = 0, k = (expr.arguments = args).length; i < k; ++i) + args[i].parent = expr; return expr; } static createConstructor(range: Range): ConstructorExpression { - const expr: ConstructorExpression = new ConstructorExpression(); + var expr = new ConstructorExpression(); expr.range = range; return expr; } static createElementAccess(expression: Expression, element: Expression, range: Range): ElementAccessExpression { - const expr: ElementAccessExpression = new ElementAccessExpression(); + var expr = new ElementAccessExpression(); expr.range = range; (expr.expression = expression).parent = expr; (expr.elementExpression = element).parent = expr; @@ -176,50 +178,51 @@ export abstract class Node { } static createFalse(range: Range): FalseExpression { - const expr: FalseExpression = new FalseExpression(); + var expr = new FalseExpression(); expr.range = range; return expr; } static createFloatLiteral(value: f64, range: Range): FloatLiteralExpression { - const expr: FloatLiteralExpression = new FloatLiteralExpression(); + var expr = new FloatLiteralExpression(); expr.range = range; expr.value = value; return expr; } static createIntegerLiteral(value: I64, range: Range): IntegerLiteralExpression { - const expr: IntegerLiteralExpression = new IntegerLiteralExpression(); + var expr = new IntegerLiteralExpression(); expr.range = range; expr.value = value; return expr; } static createNew(expression: Expression, typeArguments: TypeNode[], args: Expression[], range: Range): NewExpression { - const expr: NewExpression = new NewExpression(); + var expr = new NewExpression(); expr.range = range; (expr.expression = expression).parent = expr; - let i: i32, k: i32; - for (i = 0, k = (expr.typeArguments = typeArguments).length; i < k; ++i) typeArguments[i].parent = expr; - for (i = 0, k = (expr.arguments = args).length; i < k; ++i) args[i].parent = expr; + for (var i = 0, k = (expr.typeArguments = typeArguments).length; i < k; ++i) + typeArguments[i].parent = expr; + for (i = 0, k = (expr.arguments = args).length; i < k; ++i) + args[i].parent = expr; return expr; } static createNull(range: Range): NullExpression { - const expr: NullExpression = new NullExpression(); + var expr = new NullExpression(); expr.range = range; return expr; } static createParenthesized(expression: Expression, range: Range): ParenthesizedExpression { - const expr: ParenthesizedExpression = new ParenthesizedExpression(); + var expr = new ParenthesizedExpression(); expr.range = range; (expr.expression = expression).parent = expr; return expr; } static createPropertyAccess(expression: Expression, property: IdentifierExpression, range: Range): PropertyAccessExpression { - const expr: PropertyAccessExpression = new PropertyAccessExpression(); + var expr = new PropertyAccessExpression(); expr.range = range; (expr.expression = expression).parent = expr; (expr.property = property).parent = expr; @@ -227,14 +230,14 @@ export abstract class Node { } static createRegexpLiteral(value: string, range: Range): RegexpLiteralExpression { - const expr: RegexpLiteralExpression = new RegexpLiteralExpression(); + var expr = new RegexpLiteralExpression(); expr.range = range; expr.value = value; return expr; } static createTernary(condition: Expression, ifThen: Expression, ifElse: Expression, range: Range): TernaryExpression { - const expr: TernaryExpression = new TernaryExpression(); + var expr = new TernaryExpression(); expr.range = range; (expr.condition = condition).parent = expr; (expr.ifThen = ifThen).parent = expr; @@ -243,32 +246,32 @@ export abstract class Node { } static createStringLiteral(value: string, range: Range): StringLiteralExpression { - const expr: StringLiteralExpression = new StringLiteralExpression(); + var expr = new StringLiteralExpression(); expr.range = range; expr.value = value; return expr; } static createSuper(range: Range): SuperExpression { - const expr: SuperExpression = new SuperExpression(); + var expr = new SuperExpression(); expr.range = range; return expr; } static createThis(range: Range): ThisExpression { - const expr: ThisExpression = new ThisExpression(); + var expr = new ThisExpression(); expr.range = range; return expr; } static createTrue(range: Range): TrueExpression { - const expr: TrueExpression = new TrueExpression(); + var expr = new TrueExpression(); expr.range = range; return expr; } static createUnaryPostfix(operator: Token, expression: Expression, range: Range): UnaryPostfixExpression { - const expr: UnaryPostfixExpression = new UnaryPostfixExpression(); + var expr = new UnaryPostfixExpression(); expr.range = range; expr.operator = operator; (expr.operand = expression).parent = expr; @@ -276,7 +279,7 @@ export abstract class Node { } static createUnaryPrefix(operator: Token, expression: Expression, range: Range): UnaryPrefixExpression { - const expr: UnaryPrefixExpression = new UnaryPrefixExpression(); + var expr = new UnaryPrefixExpression(); expr.range = range; expr.operator = operator; (expr.operand = expression).parent = expr; @@ -286,50 +289,61 @@ export abstract class Node { // statements static createBlock(statements: Statement[], range: Range): BlockStatement { - const stmt: BlockStatement = new BlockStatement(); + var stmt = new BlockStatement(); stmt.range = range; - for (let i: i32 = 0, k: i32 = (stmt.statements = statements).length; i < k; ++i) statements[i].parent = stmt; + for (var i: i32 = 0, k: i32 = (stmt.statements = statements).length; i < k; ++i) + statements[i].parent = stmt; return stmt; } static createBreak(label: IdentifierExpression | null, range: Range): BreakStatement { - const stmt: BreakStatement = new BreakStatement(); + var stmt = new BreakStatement(); stmt.range = range; - if (stmt.label = label) (label).parent = stmt; + if (stmt.label = label) + (label).parent = stmt; return stmt; } static createClass(identifier: IdentifierExpression, typeParameters: TypeParameter[], extendsType: TypeNode | null, implementsTypes: TypeNode[], members: DeclarationStatement[], modifiers: Modifier[] | null, decorators: Decorator[] | null, range: Range): ClassDeclaration { - const stmt: ClassDeclaration = new ClassDeclaration(); + var stmt = new ClassDeclaration(); stmt.range = range; - let i: i32, k: i32; (stmt.name = identifier).parent = stmt; - for (i = 0, k = (stmt.typeParameters = typeParameters).length; i < k; ++i) typeParameters[i].parent = stmt; - if (stmt.extendsType = extendsType) (extendsType).parent = stmt; - for (i = 0, k = (stmt.implementsTypes = implementsTypes).length; i < k; ++i) implementsTypes[i].parent = stmt; - for (i = 0, k = (stmt.members = members).length; i < k; ++i) members[i].parent = stmt; - if (stmt.modifiers = modifiers) for (i = 0, k = (modifiers).length; i < k; ++i) (modifiers)[i].parent = stmt; - if (stmt.decorators = decorators) for (i = 0, k = (decorators).length; i < k; ++i) (decorators)[i].parent = stmt; + for (var i = 0, k = (stmt.typeParameters = typeParameters).length; i < k; ++i) + typeParameters[i].parent = stmt; + if (stmt.extendsType = extendsType) + (extendsType).parent = stmt; + for (i = 0, k = (stmt.implementsTypes = implementsTypes).length; i < k; ++i) + implementsTypes[i].parent = stmt; + for (i = 0, k = (stmt.members = members).length; i < k; ++i) + members[i].parent = stmt; + if (stmt.modifiers = modifiers) + for (i = 0, k = (modifiers).length; i < k; ++i) + (modifiers)[i].parent = stmt; + if (stmt.decorators = decorators) + for (i = 0, k = (decorators).length; i < k; ++i) + (decorators)[i].parent = stmt; return stmt; } static createContinue(label: IdentifierExpression | null, range: Range): ContinueStatement { - const stmt: ContinueStatement = new ContinueStatement(); + var stmt = new ContinueStatement(); stmt.range = range; - if (stmt.label = label) (label).parent = stmt; + if (stmt.label = label) + (label).parent = stmt; return stmt; } static createDecorator(expression: Expression, args: Expression[], range: Range): Decorator { - const stmt: Decorator = new Decorator(); + var stmt = new Decorator(); stmt.range = range; (stmt.name = expression).parent = stmt; - for (let i: i32 = 0, k: i32 = (stmt.arguments = args).length; i < k; ++i) args[i].parent = stmt; + for (var i: i32 = 0, k: i32 = (stmt.arguments = args).length; i < k; ++i) + args[i].parent = stmt; return stmt; } static createDo(statement: Statement, condition: Expression, range: Range): DoStatement { - const stmt: DoStatement = new DoStatement(); + var stmt = new DoStatement(); stmt.range = range; (stmt.statement = statement).parent = stmt; (stmt.condition = condition).parent = stmt; @@ -337,44 +351,50 @@ export abstract class Node { } static createEmpty(range: Range): EmptyStatement { - const stmt: EmptyStatement = new EmptyStatement(); + var stmt = new EmptyStatement(); stmt.range = range; return stmt; } static createEnum(identifier: IdentifierExpression, members: EnumValueDeclaration[], modifiers: Modifier[] | null, decorators: Decorator[] | null, range: Range): EnumDeclaration { - const stmt: EnumDeclaration = new EnumDeclaration(); + var stmt = new EnumDeclaration(); stmt.range = range; - let i: i32, k: i32; (stmt.name = identifier).parent = stmt; - for (i = 0, k = (stmt.values = members).length; i < k; ++i) members[i].parent = stmt; - if (stmt.modifiers = modifiers) for (i = 0, k = (modifiers).length; i < k; ++i) (modifiers)[i].parent = stmt; - if (stmt.decorators = decorators) for (i = 0, k = (decorators).length; i < k; ++i) (decorators)[i].parent = stmt; + for (var i = 0, k = (stmt.values = members).length; i < k; ++i) + members[i].parent = stmt; + if (stmt.modifiers = modifiers) + for (i = 0, k = (modifiers).length; i < k; ++i) + (modifiers)[i].parent = stmt; + if (stmt.decorators = decorators) + for (i = 0, k = (decorators).length; i < k; ++i) + (decorators)[i].parent = stmt; return stmt; } static createEnumValue(identifier: IdentifierExpression, value: Expression | null, range: Range): EnumValueDeclaration { - const stmt: EnumValueDeclaration = new EnumValueDeclaration(); + var stmt = new EnumValueDeclaration(); stmt.range = range; (stmt.name = identifier).parent = stmt; - if (stmt.value = value) (value).parent = stmt; + if (stmt.value = value) + (value).parent = stmt; return stmt; } static createExport(members: ExportMember[], path: StringLiteralExpression | null, modifiers: Modifier[] | null, range: Range): ExportStatement { - const stmt: ExportStatement = new ExportStatement(); + var stmt = new ExportStatement(); stmt.range = range; - let i: i32, k: i32; - for (i = 0, k = (stmt.members = members).length; i < k; ++i) members[i].parent = stmt; + for (var i = 0, k = (stmt.members = members).length; i < k; ++i) members[i].parent = stmt; stmt.path = path; stmt.normalizedPath = path ? resolvePath(normalizePath(path.value), range.source.normalizedPath) : null; stmt.internalPath = stmt.normalizedPath ? mangleInternalPath(stmt.normalizedPath) : null; - if (stmt.modifiers = modifiers) for (i = 0, k = (modifiers).length; i < k; ++i) (modifiers)[i].parent = stmt; + if (stmt.modifiers = modifiers) + for (i = 0, k = (modifiers).length; i < k; ++i) + (modifiers)[i].parent = stmt; return stmt; } static createExportImport(identifier: IdentifierExpression, asIdentifier: IdentifierExpression, range: Range): ExportImportStatement { - const stmt: ExportImportStatement = new ExportImportStatement(); + var stmt = new ExportImportStatement(); stmt.range = range; (stmt.identifier = identifier).parent = stmt; (stmt.externalIdentifier = asIdentifier).parent = stmt; @@ -382,7 +402,7 @@ export abstract class Node { } static createExportMember(identifier: IdentifierExpression, externalIdentifier: IdentifierExpression | null, range: Range): ExportMember { - const elem: ExportMember = new ExportMember(); + var elem = new ExportMember(); elem.range = range; (elem.identifier = identifier).parent = elem; (elem.externalIdentifier = externalIdentifier ? externalIdentifier : identifier).parent = elem; @@ -391,25 +411,27 @@ export abstract class Node { /** Creates an expression statement. */ static createExpression(expression: Expression): ExpressionStatement { - const stmt: ExpressionStatement = new ExpressionStatement(); + var stmt = new ExpressionStatement(); stmt.range = expression.range; (stmt.expression = expression).parent = stmt; return stmt; } - static createIf(condition: Expression, statement: Statement, elseStatement: Statement | null, range: Range): IfStatement { - const stmt: IfStatement = new IfStatement(); + static createIf(condition: Expression, ifTrue: Statement, ifFalse: Statement | null, range: Range): IfStatement { + var stmt = new IfStatement(); stmt.range = range; (stmt.condition = condition).parent = stmt; - (stmt.ifTrue = statement).parent = stmt; - if (stmt.ifFalse = elseStatement) (elseStatement).parent = stmt; + (stmt.ifTrue = ifTrue).parent = stmt; + if (stmt.ifFalse = ifFalse) + (ifFalse).parent = stmt; return stmt; } static createImport(declarations: ImportDeclaration[], path: StringLiteralExpression, range: Range): ImportStatement { - const stmt: ImportStatement = new ImportStatement(); + var stmt = new ImportStatement(); stmt.range = range; - for (let i: i32 = 0, k: i32 = (stmt.declarations = declarations).length; i < k; ++i) declarations[i].parent = stmt; + for (var i: i32 = 0, k: i32 = (stmt.declarations = declarations).length; i < k; ++i) + declarations[i].parent = stmt; stmt.namespaceName = null; stmt.path = path; stmt.normalizedPath = resolvePath(normalizePath(path.value), range.source.normalizedPath); @@ -418,7 +440,7 @@ export abstract class Node { } static createImportAll(identifier: IdentifierExpression, path: StringLiteralExpression, range: Range): ImportStatement { - const stmt: ImportStatement = new ImportStatement(); + var stmt = new ImportStatement(); stmt.range = range; stmt.declarations = null; stmt.namespaceName = identifier; @@ -429,7 +451,7 @@ export abstract class Node { } static createImportDeclaration(externalIdentifier: IdentifierExpression, identifier: IdentifierExpression | null, range: Range): ImportDeclaration { - const elem: ImportDeclaration = new ImportDeclaration(); + var elem = new ImportDeclaration(); elem.range = range; (elem.name = identifier ? identifier : externalIdentifier).parent = elem; (elem.externalIdentifier = externalIdentifier).parent = elem; @@ -437,177 +459,229 @@ export abstract class Node { } static createInterface(identifier: IdentifierExpression, extendsType: TypeNode | null, members: DeclarationStatement[], modifiers: Modifier[] | null, range: Range): InterfaceDeclaration { - const stmt: InterfaceDeclaration = new InterfaceDeclaration(); + var stmt = new InterfaceDeclaration(); stmt.range = range; - let i: i32, k: i32; (stmt.name = identifier).parent = stmt; - if (stmt.extendsType = extendsType) (extendsType).parent = stmt; - for (i = 0, k = (stmt.members = members).length; i < k; ++i) members[i].parent = stmt; - if (stmt.modifiers = modifiers) for (i = 0, k = (modifiers).length; i < k; ++i) (modifiers)[i].parent = stmt; + if (stmt.extendsType = extendsType) + (extendsType).parent = stmt; + for (var i = 0, k = (stmt.members = members).length; i < k; ++i) + members[i].parent = stmt; + if (stmt.modifiers = modifiers) + for (i = 0, k = (modifiers).length; i < k; ++i) + (modifiers)[i].parent = stmt; return stmt; } static createField(identifier: IdentifierExpression, type: TypeNode | null, initializer: Expression | null, modifiers: Modifier[] | null, decorators: Decorator[] | null, range: Range): FieldDeclaration { - const stmt: FieldDeclaration = new FieldDeclaration(); + var stmt = new FieldDeclaration(); stmt.range = range; - let i: i32, k: i32; (stmt.name = identifier).parent = stmt; - if (stmt.type = type) (type).parent = stmt; - if (stmt.initializer = initializer) (initializer).parent = stmt; - if (stmt.modifiers = modifiers) for (i = 0, k = (modifiers).length; i < k; ++i) (modifiers)[i].parent = stmt; - if (stmt.decorators = decorators) for (i = 0, k = (decorators).length; i < k; ++i) (decorators)[i].parent = stmt; + if (stmt.type = type) + (type).parent = stmt; + if (stmt.initializer = initializer) + (initializer).parent = stmt; + if (stmt.modifiers = modifiers) + for (var i = 0, k = (modifiers).length; i < k; ++i) + (modifiers)[i].parent = stmt; + if (stmt.decorators = decorators) + for (i = 0, k = (decorators).length; i < k; ++i) + (decorators)[i].parent = stmt; return stmt; } static createFor(initializer: Statement | null, condition: Expression | null, incrementor: Expression | null, statement: Statement, range: Range): ForStatement { - const stmt: ForStatement = new ForStatement(); + var stmt = new ForStatement(); stmt.range = range; - if (stmt.initializer = initializer) (initializer).parent = stmt; - if (stmt.condition = condition) (condition).parent = stmt; - if (stmt.incrementor = incrementor) (incrementor).parent = stmt; + if (stmt.initializer = initializer) + (initializer).parent = stmt; + if (stmt.condition = condition) + (condition).parent = stmt; + if (stmt.incrementor = incrementor) + (incrementor).parent = stmt; (stmt.statement = statement).parent = stmt; return stmt; } static createTypeParameter(identifier: IdentifierExpression, extendsType: TypeNode | null, range: Range): TypeParameter { - const elem: TypeParameter = new TypeParameter(); + var elem = new TypeParameter(); elem.range = range; (elem.identifier = identifier).parent = elem; - if (elem.extendsType = extendsType) (extendsType).parent = elem; + if (elem.extendsType = extendsType) + (extendsType).parent = elem; return elem; } static createParameter(identifier: IdentifierExpression, type: TypeNode | null, initializer: Expression | null, isRest: bool, range: Range): Parameter { - const elem: Parameter = new Parameter(); + var elem = new Parameter(); elem.range = range; (elem.name = identifier).parent = elem; - if (elem.type = type) (type).parent = elem; - if (elem.initializer = initializer) (initializer).parent = elem; + if (elem.type = type) + (type).parent = elem; + if (elem.initializer = initializer) + (initializer).parent = elem; elem.isRest = isRest; return elem; } static createFunction(identifier: IdentifierExpression, typeParameters: TypeParameter[], parameters: Parameter[], returnType: TypeNode | null, statements: Statement[] | null, modifiers: Modifier[] | null, decorators: Decorator[] | null, range: Range): FunctionDeclaration { - const stmt: FunctionDeclaration = new FunctionDeclaration(); + var stmt = new FunctionDeclaration(); stmt.range = range; - let i: i32, k: i32; (stmt.name = identifier).parent = stmt; - for (i = 0, k = (stmt.typeParameters = typeParameters).length; i < k; ++i) typeParameters[i].parent = stmt; - for (i = 0, k = (stmt.parameters = parameters).length; i < k; ++i) parameters[i].parent = stmt; - if (stmt.returnType = returnType) (returnType).parent = stmt; - if (stmt.statements = statements) for (i = 0, k = (statements).length; i < k; ++i) (statements)[i].parent = stmt; - if (stmt.modifiers = modifiers) for (i = 0, k = (modifiers).length; i < k; ++i) (modifiers)[i].parent = stmt; - if (stmt.decorators = decorators) for (i = 0, k = (decorators).length; i < k; ++i) (decorators)[i].parent = stmt; + for (var i = 0, k = (stmt.typeParameters = typeParameters).length; i < k; ++i) + typeParameters[i].parent = stmt; + for (i = 0, k = (stmt.parameters = parameters).length; i < k; ++i) + parameters[i].parent = stmt; + if (stmt.returnType = returnType) + (returnType).parent = stmt; + if (stmt.statements = statements) + for (i = 0, k = (statements).length; i < k; ++i) + (statements)[i].parent = stmt; + if (stmt.modifiers = modifiers) + for (i = 0, k = (modifiers).length; i < k; ++i) + (modifiers)[i].parent = stmt; + if (stmt.decorators = decorators) + for (i = 0, k = (decorators).length; i < k; ++i) + (decorators)[i].parent = stmt; return stmt; } static createMethod(identifier: IdentifierExpression, typeParameters: TypeParameter[], parameters: Parameter[], returnType: TypeNode | null, statements: Statement[] | null, modifiers: Modifier[] | null, decorators: Decorator[] | null, range: Range): MethodDeclaration { - const stmt: MethodDeclaration = new MethodDeclaration(); + var stmt = new MethodDeclaration(); stmt.range = range; - let i: i32, k: i32; (stmt.name = identifier).parent = stmt; - for (i = 0, k = (stmt.typeParameters = typeParameters).length; i < k; ++i) typeParameters[i].parent = stmt; - for (i = 0, k = (stmt.parameters = parameters).length; i < k; ++i) parameters[i].parent = stmt; - if (stmt.returnType = returnType) (returnType).parent = stmt; - if (stmt.statements = statements) for (i = 0, k = (statements).length; i < k; ++i) (statements)[i].parent = stmt; - if (stmt.modifiers = modifiers) for (i = 0, k = (modifiers).length; i < k; ++i) (modifiers)[i].parent = stmt; - if (stmt.decorators = decorators) for (i = 0, k = (decorators).length; i < k; ++i) (decorators)[i].parent = stmt; + for (var i = 0, k = (stmt.typeParameters = typeParameters).length; i < k; ++i) + typeParameters[i].parent = stmt; + for (i = 0, k = (stmt.parameters = parameters).length; i < k; ++i) + parameters[i].parent = stmt; + if (stmt.returnType = returnType) + (returnType).parent = stmt; + if (stmt.statements = statements) + for (i = 0, k = (statements).length; i < k; ++i) + (statements)[i].parent = stmt; + if (stmt.modifiers = modifiers) + for (i = 0, k = (modifiers).length; i < k; ++i) + (modifiers)[i].parent = stmt; + if (stmt.decorators = decorators) + for (i = 0, k = (decorators).length; i < k; ++i) + (decorators)[i].parent = stmt; return stmt; } static createModifier(kind: ModifierKind, range: Range): Modifier { - const elem: Modifier = new Modifier(); + var elem = new Modifier(); elem.range = range; elem.modifierKind = kind; return elem; } static createNamespace(identifier: IdentifierExpression, members: Statement[], modifiers: Modifier[] | null, decorators: Decorator[] | null, range: Range): NamespaceDeclaration { - const stmt: NamespaceDeclaration = new NamespaceDeclaration(); + var stmt = new NamespaceDeclaration(); stmt.range = range; - let i: i32, k: i32; (stmt.name = identifier).parent = stmt; - for (i = 0, k = (stmt.members = members).length; i < k; ++i) members[i].parent = stmt; - if (stmt.modifiers = modifiers) for (i = 0, k = (modifiers).length; i < k; ++i) (modifiers)[i].parent = stmt; - if (stmt.decorators = decorators) for (i = 0, k = (decorators).length; i < k; ++i) (decorators)[i].parent = stmt; + for (var i = 0, k = (stmt.members = members).length; i < k; ++i) + members[i].parent = stmt; + if (stmt.modifiers = modifiers) + for (i = 0, k = (modifiers).length; i < k; ++i) + (modifiers)[i].parent = stmt; + if (stmt.decorators = decorators) + for (i = 0, k = (decorators).length; i < k; ++i) + (decorators)[i].parent = stmt; return stmt; } static createReturn(expression: Expression | null, range: Range): ReturnStatement { - const stmt: ReturnStatement = new ReturnStatement(); + var stmt = new ReturnStatement(); stmt.range = range; - if (stmt.value = expression) (expression).parent = stmt; + if (stmt.value = expression) + (expression).parent = stmt; return stmt; } static createSwitch(expression: Expression, cases: SwitchCase[], range: Range): SwitchStatement { - const stmt: SwitchStatement = new SwitchStatement(); + var stmt = new SwitchStatement(); stmt.range = range; (stmt.condition = expression).parent = stmt; - for (let i: i32 = 0, k: i32 = (stmt.cases = cases).length; i < k; ++i) cases[i].parent = stmt; + for (var i: i32 = 0, k: i32 = (stmt.cases = cases).length; i < k; ++i) + cases[i].parent = stmt; return stmt; } static createSwitchCase(label: Expression | null, statements: Statement[], range: Range): SwitchCase { - const elem: SwitchCase = new SwitchCase(); + var elem = new SwitchCase(); elem.range = range; - if (elem.label = label) (label).parent = elem; - for (let i: i32 = 0, k: i32 = (elem.statements = statements).length; i < k; ++i) statements[i].parent = elem; + if (elem.label = label) + (label).parent = elem; + for (var i: i32 = 0, k: i32 = (elem.statements = statements).length; i < k; ++i) + statements[i].parent = elem; return elem; } static createThrow(expression: Expression, range: Range): ThrowStatement { - const stmt: ThrowStatement = new ThrowStatement(); + var stmt = new ThrowStatement(); stmt.range = range; (stmt.value = expression).parent = stmt; return stmt; } static createTry(statements: Statement[], catchVariable: IdentifierExpression | null, catchStatements: Statement[] | null, finallyStatements: Statement[] | null, range: Range): TryStatement { - const stmt: TryStatement = new TryStatement(); + var stmt = new TryStatement(); stmt.range = range; - let i: i32, k: i32; - for (i = 0, k = (stmt.statements = statements).length; i < k; ++i) statements[i].parent = stmt; - if (stmt.catchVariable = catchVariable) (catchVariable).parent = stmt; - if (stmt.catchStatements = catchStatements) for (i = 0, k = (catchStatements).length; i < k; ++i) (catchStatements)[i].parent = stmt; - if (stmt.finallyStatements = finallyStatements) for (i = 0, k = (finallyStatements).length; i < k; ++i) (finallyStatements)[i].parent = stmt; + for (var i = 0, k = (stmt.statements = statements).length; i < k; ++i) + statements[i].parent = stmt; + if (stmt.catchVariable = catchVariable) + (catchVariable).parent = stmt; + if (stmt.catchStatements = catchStatements) + for (i = 0, k = (catchStatements).length; i < k; ++i) + (catchStatements)[i].parent = stmt; + if (stmt.finallyStatements = finallyStatements) + for (i = 0, k = (finallyStatements).length; i < k; ++i) + (finallyStatements)[i].parent = stmt; return stmt; } static createTypeDeclaration(identifier: IdentifierExpression, alias: TypeNode, modifiers: Modifier[] | null, decorators: Decorator[] | null, range: Range): TypeDeclaration { - const stmt: TypeDeclaration = new TypeDeclaration(); + var stmt = new TypeDeclaration(); stmt.range = range; (stmt.name = identifier).parent = stmt; (stmt.alias = alias).parent = stmt; - let i: i32, k: i32; - if (stmt.modifiers = modifiers) for (i = 0, k = (modifiers).length; i < k; ++i) (modifiers)[i].parent = stmt; - if (stmt.decorators = decorators) for (i = 0, k = (decorators).length; i < k; ++i) (decorators)[i].parent = stmt; + if (stmt.modifiers = modifiers) + for (var i = 0, k = (modifiers).length; i < k; ++i) + (modifiers)[i].parent = stmt; + if (stmt.decorators = decorators) + for (i = 0, k = (decorators).length; i < k; ++i) + (decorators)[i].parent = stmt; return stmt; } static createVariable(declarations: VariableDeclaration[], modifiers: Modifier[] | null, decorators: Decorator[] | null, range: Range): VariableStatement { - const stmt: VariableStatement = new VariableStatement(); + var stmt = new VariableStatement(); stmt.range = range; - let i: i32, k: i32; - for (i = 0, k = (stmt.declarations = declarations).length; i < k; ++i) declarations[i].parent = stmt; - if (stmt.modifiers = modifiers) for (i = 0, k = (modifiers).length; i < k; ++i) (modifiers)[i].parent = stmt; - if (stmt.decorators = decorators) for (i = 0, k = (decorators).length; i < k; ++i) (decorators)[i].parent = stmt; + for (var i = 0, k = (stmt.declarations = declarations).length; i < k; ++i) + declarations[i].parent = stmt; + if (stmt.modifiers = modifiers) + for (i = 0, k = (modifiers).length; i < k; ++i) + (modifiers)[i].parent = stmt; + if (stmt.decorators = decorators) + for (i = 0, k = (decorators).length; i < k; ++i) + (decorators)[i].parent = stmt; return stmt; } static createVariableDeclaration(name: IdentifierExpression, type: TypeNode | null, initializer: Expression | null, modifiers: Modifier[] | null, decorators: Decorator[] | null, range: Range): VariableDeclaration { - const elem: VariableDeclaration = new VariableDeclaration(); + var elem = new VariableDeclaration(); elem.range = range; (elem.name = name).parent = elem; - if (elem.type = type) (type).parent = elem; - if (elem.initializer = initializer) (initializer).parent = elem; + if (elem.type = type) + (type).parent = elem; + if (elem.initializer = initializer) + (initializer).parent = elem; elem.modifiers = modifiers; elem.decorators = decorators; return elem; } static createWhile(condition: Expression, statement: Statement, range: Range): WhileStatement { - const stmt: WhileStatement = new WhileStatement(); + var stmt = new WhileStatement(); stmt.range = range; (stmt.condition = condition).parent = stmt; (stmt.statement = statement).parent = stmt; @@ -633,7 +707,7 @@ export class TypeNode extends Node { this.identifier.serialize(sb); if (this.typeArguments.length) { sb.push("<"); - for (let i: i32 = 0, k: i32 = this.typeArguments.length; i < k; ++i) { + for (var i = 0, k = this.typeArguments.length; i < k; ++i) { if (i > 0) sb.push(", "); this.typeArguments[i].serialize(sb); @@ -712,7 +786,7 @@ export class ArrayLiteralExpression extends LiteralExpression { serialize(sb: string[]): void { sb.push("["); - for (let i: i32 = 0, k: i32 = this.elementExpressions.length; i < k; ++i) { + for (var i = 0, k = this.elementExpressions.length; i < k; ++i) { if (i > 0) sb.push(", "); if (this.elementExpressions[i]) @@ -789,10 +863,10 @@ export class CallExpression extends Expression { serialize(sb: string[]): void { this.expression.serialize(sb); - let i: i32, k: i32 = this.typeArguments.length; + var k = this.typeArguments.length; if (k) { sb.push("<"); - for (i = 0; i < k; ++i) { + for (var i = 0; i < k; ++i) { if (i > 0) sb.push(", "); this.typeArguments[i].serialize(sb); @@ -954,7 +1028,7 @@ export class StringLiteralExpression extends LiteralExpression { value: string; serialize(sb: string[]): void { - sb.push(JSON.stringify(this.value)); + sb.push(escapeString(this.value)); } } @@ -1075,9 +1149,8 @@ export class Source extends Node { } serialize(sb: string[]): void { - for (let i: i32 = 0, k: i32 = this.statements.length; i < k; ++i) { - const statement: Statement = this.statements[i]; - statement.serialize(sb); + for (var i: i32 = 0, k: i32 = this.statements.length; i < k; ++i) { + this.statements[i].serialize(sb); if (builderEndsWith(sb, CharCode.CLOSEBRACE)) sb.push("\n"); else @@ -1125,7 +1198,7 @@ export class BlockStatement extends Statement { serialize(sb: string[]): void { sb.push("{\n"); - for (let i: i32 = 0, k: i32 = this.statements.length; i < k; ++i) { + for (var i = 0, k = this.statements.length; i < k; ++i) { this.statements[i].serialize(sb); if (builderEndsWith(sb, CharCode.CLOSEBRACE)) sb.push("\n"); @@ -1168,9 +1241,8 @@ export class ClassDeclaration extends DeclarationStatement { members: DeclarationStatement[]; serialize(sb: string[]): void { - let i: i32, k: i32; if (this.decorators) - for (i = 0, k = this.decorators.length; i < k; ++i) { + for (var i = 0, k = this.decorators.length; i < k; ++i) { this.decorators[i].serialize(sb); sb.push("\n"); } @@ -1245,7 +1317,7 @@ export class Decorator extends Statement { sb.push("@"); this.name.serialize(sb); sb.push("("); - for (let i: i32 = 0, k: i32 = this.arguments.length; i < k; ++i) { + for (var i = 0, k = this.arguments.length; i < k; ++i) { if (i > 0) sb.push(", "); this.arguments[i].serialize(sb); @@ -1293,9 +1365,8 @@ export class EnumDeclaration extends DeclarationStatement { values: EnumValueDeclaration[]; serialize(sb: string[]): void { - let i: i32, k: i32; if (this.modifiers) - for (i = 0, k = (this.modifiers).length; i < k; ++i) { + for (var i = 0, k = (this.modifiers).length; i < k; ++i) { (this.modifiers)[i].serialize(sb); sb.push(" "); } @@ -1384,9 +1455,8 @@ export class ExportStatement extends Statement { internalPath: string | null; serialize(sb: string[]): void { - let i: i32, k: i32; if (this.modifiers) - for (i = 0, k = (this.modifiers).length; i < k; ++i) { + for (var i = 0, k = (this.modifiers).length; i < k; ++i) { (this.modifiers)[i].serialize(sb); sb.push(" "); } @@ -1423,9 +1493,8 @@ export class FieldDeclaration extends VariableLikeDeclarationStatement { kind = NodeKind.FIELD; serialize(sb: string[]): void { - let i: i32, k: i32; if (this.decorators) - for (i = 0, k = this.decorators.length; i < k; ++i) { + for (var i = 0, k = this.decorators.length; i < k; ++i) { this.decorators[i].serialize(sb); sb.push("\n"); } @@ -1494,9 +1563,8 @@ export class FunctionDeclaration extends DeclarationStatement { statements: Statement[] | null; serialize(sb: string[]): void { - let i: i32, k: i32; if (this.decorators) - for (i = 0, k = this.decorators.length; i < k; ++i) { + for (var i = 0, k = this.decorators.length; i < k; ++i) { this.decorators[i].serialize(sb); sb.push("\n"); } @@ -1511,10 +1579,9 @@ export class FunctionDeclaration extends DeclarationStatement { protected serializeCommon(sb: string[]): void { this.name.serialize(sb); - let i: i32, k: i32; if (this.typeParameters.length) { sb.push("<"); - for (i = 0, k = this.typeParameters.length; i < k; ++i) { + for (var i = 0, k = this.typeParameters.length; i < k; ++i) { if (i > 0) sb.push(", "); this.typeParameters[i].serialize(sb); @@ -1535,7 +1602,7 @@ export class FunctionDeclaration extends DeclarationStatement { if (this.statements) { sb.push(" {\n"); for (i = 0, k = (this.statements).length; i < k; ++i) { - const statement: Statement = (this.statements)[i]; + var statement: Statement = (this.statements)[i]; statement.serialize(sb); if (builderEndsWith(sb, CharCode.CLOSEBRACE)) sb.push("\n"); @@ -1613,7 +1680,7 @@ export class ImportStatement extends Statement { serialize(sb: string[]): void { if (this.declarations) { sb.push("import {\n"); - for (let i: i32 = 0, k: i32 = this.declarations.length; i < k; ++i) { + for (var i: i32 = 0, k: i32 = this.declarations.length; i < k; ++i) { if (i > 0) sb.push(",\n"); this.declarations[i].serialize(sb); @@ -1637,15 +1704,14 @@ export class InterfaceDeclaration extends ClassDeclaration { kind = NodeKind.INTERFACE; serialize(sb: string[]): void { - let i: i32, k: i32; if (this.decorators) - for (i = 0, k = this.decorators.length; i < k; ++i) { + for (var i = 0, k = this.decorators.length; i < k; ++i) { this.decorators[i].serialize(sb); sb.push("\n"); } if (this.modifiers) - for (i = 0, k = (this.modifiers).length; i < k; ++i) { - (this.modifiers)[i].serialize(sb); + for (i = 0, k = this.modifiers.length; i < k; ++i) { + this.modifiers[i].serialize(sb); sb.push(" "); } sb.push("interface "); @@ -1661,7 +1727,7 @@ export class InterfaceDeclaration extends ClassDeclaration { } if (this.extendsType) { sb.push(" extends "); - (this.extendsType).serialize(sb); + this.extendsType.serialize(sb); } sb.push(" {\n"); for (i = 0, k = this.members.length; i < k; ++i) { @@ -1681,15 +1747,14 @@ export class MethodDeclaration extends FunctionDeclaration { kind = NodeKind.METHOD; serialize(sb: string[]): void { - let i: i32, k: i32; if (this.decorators) - for (i = 0, k = this.decorators.length; i < k; ++i) { + for (var i = 0, k = this.decorators.length; i < k; ++i) { this.decorators[i].serialize(sb); sb.push("\n"); } if (this.modifiers) - for (i = 0, k = (this.modifiers).length; i < k; ++i) { - (this.modifiers)[i].serialize(sb); + for (i = 0, k = this.modifiers.length; i < k; ++i) { + this.modifiers[i].serialize(sb); sb.push(" "); } super.serializeCommon(sb); @@ -1705,15 +1770,14 @@ export class NamespaceDeclaration extends DeclarationStatement { members: Statement[]; serialize(sb: string[]): void { - let i: i32, k: i32; if (this.decorators) - for (i = 0, k = this.decorators.length; i < k; ++i) { + for (var i = 0, k = this.decorators.length; i < k; ++i) { this.decorators[i].serialize(sb); sb.push("\n"); } if (this.modifiers) - for (i = 0, k = (this.modifiers).length; i < k; ++i) { - (this.modifiers)[i].serialize(sb); + for (i = 0, k = this.modifiers.length; i < k; ++i) { + this.modifiers[i].serialize(sb); sb.push(" "); } sb.push("namespace "); @@ -1750,11 +1814,11 @@ export class Parameter extends Node { this.name.serialize(sb); if (this.type) { sb.push(": "); - (this.type).serialize(sb); + this.type.serialize(sb); } if (this.initializer) { sb.push(" = "); - (this.initializer).serialize(sb); + this.initializer.serialize(sb); } } } @@ -1803,7 +1867,7 @@ export class ReturnStatement extends Statement { serialize(sb: string[]): void { if (this.value) { sb.push("return "); - (this.value).serialize(sb); + this.value.serialize(sb); } else sb.push("return"); } @@ -1822,11 +1886,11 @@ export class SwitchCase extends Node { serialize(sb: string[]): void { if (this.label) { sb.push("case "); - (this.label).serialize(sb); + this.label.serialize(sb); sb.push(":\n"); } else sb.push("default:\n"); - for (let i: i32 = 0, k: i32 = this.statements.length; i < k; ++i) { + for (var i = 0, k = this.statements.length; i < k; ++i) { if (i > 0) sb.push("\n"); this.statements[i].serialize(sb); @@ -1852,7 +1916,7 @@ export class SwitchStatement extends Statement { sb.push("switch ("); this.condition.serialize(sb); sb.push(") {\n"); - for (let i: i32 = 0, k: i32 = this.cases.length; i < k; ++i) { + for (var i = 0, k = this.cases.length; i < k; ++i) { this.cases[i].serialize(sb); sb.push("\n"); } @@ -1891,8 +1955,7 @@ export class TryStatement extends Statement { serialize(sb: string[]): void { sb.push("try {\n"); - let i: i32, k: i32; - for (i = 0, k = this.statements.length; i < k; ++i) { + for (var i = 0, k = this.statements.length; i < k; ++i) { this.statements[i].serialize(sb); sb.push(";\n"); } @@ -1926,9 +1989,8 @@ export class TypeDeclaration extends DeclarationStatement { alias: TypeNode; serialize(sb: string[]): void { - let i: i32, k: i32; if (this.decorators) - for (i = 0, k = this.decorators.length; i < k; ++i) { + for (var i = 0, k = this.decorators.length; i < k; ++i) { this.decorators[i].serialize(sb); sb.push("\n"); } @@ -1978,18 +2040,17 @@ export class VariableStatement extends Statement { declarations: VariableDeclaration[]; serialize(sb: string[]): void { - let isConst: bool = false; - let i: i32, k: i32; + var isConst = false; if (this.decorators) - for (i = 0, k = this.decorators.length; i < k; ++i) { + for (var i = 0, k = this.decorators.length; i < k; ++i) { this.decorators[i].serialize(sb); sb.push("\n"); } if (this.modifiers) - for (i = 0, k = (this.modifiers).length; i < k; ++i) { - (this.modifiers)[i].serialize(sb); + for (i = 0, k = this.modifiers.length; i < k; ++i) { + this.modifiers[i].serialize(sb); sb.push(" "); - if ((this.modifiers)[i].modifierKind == ModifierKind.CONST) + if (this.modifiers[i].modifierKind == ModifierKind.CONST) isConst = true; } if (!isConst) @@ -2021,7 +2082,7 @@ export class WhileStatement extends Statement { } /** Cached unused modifiers for reuse. */ -let reusableModifiers: Modifier[] | null = null; +var reusableModifiers: Modifier[] | null = null; export function setReusableModifiers(modifiers: Modifier[]) { reusableModifiers = modifiers; @@ -2029,7 +2090,7 @@ export function setReusableModifiers(modifiers: Modifier[]) { /** Creates a new modifiers array. */ export function createModifiers(): Modifier[] { - let ret: Modifier[]; + var ret: Modifier[]; if (reusableModifiers != null) { ret = reusableModifiers; reusableModifiers = null; @@ -2050,7 +2111,7 @@ export function addModifier(modifier: Modifier, modifiers: Modifier[] | null): M /** Gets a specific modifier from the specified array of modifiers. */ export function getModifier(kind: ModifierKind, modifiers: Modifier[] | null): Modifier | null { if (modifiers) - for (let i: i32 = 0, k: i32 = modifiers.length; i < k; ++i) + for (var i = 0, k = modifiers.length; i < k; ++i) if (modifiers[i].modifierKind == kind) return modifiers[i]; return null; @@ -2064,9 +2125,9 @@ export function hasModifier(kind: ModifierKind, modifiers: Modifier[] | null): b /** Gets a specific decorator within the specified decorators, if present. */ export function getDecorator(name: string, decorators: Decorator[] | null): Decorator | null { if (decorators) - for (let i: i32 = 0, k: i32 = decorators.length; i < k; ++i) { - const decorator: Decorator = decorators[i]; - const expression: Expression = decorator.name; + for (var i = 0, k = decorators.length; i < k; ++i) { + var decorator = decorators[i]; + var expression = decorator.name; if (expression.kind == NodeKind.IDENTIFIER && (expression).name == name) return decorator; } @@ -2080,7 +2141,7 @@ export function hasDecorator(name: string, decorators: Decorator[] | null): bool /** Serializes the specified node to its TypeScript representation. */ export function serialize(node: Node): string { - const sb: string[] = new Array(); // shared builder could grow too much + var sb = new Array(); // shared builder could grow too much node.serialize(sb); return sb.join(""); } @@ -2097,8 +2158,8 @@ export function mangleInternalPath(path: string): string { /** Mangles a declaration's name to an internal name. */ export function mangleInternalName(declaration: DeclarationStatement): string { - let name: string = declaration.name.name; - let parent: Node | null = declaration.parent; + var name = declaration.name.name; + var parent = declaration.parent; if (!parent) return name; if (declaration.kind == NodeKind.VARIABLEDECLARATION && parent.kind == NodeKind.VARIABLE) // skip over @@ -2114,8 +2175,26 @@ export function mangleInternalName(declaration: DeclarationStatement): string { /** Tests if the specified builder ends with the specified char code. */ function builderEndsWith(sb: string[], code: CharCode): bool { if (sb.length) { - const last: string = sb[sb.length - 1]; + var last = sb[sb.length - 1]; return select(last.charCodeAt(last.length - 1) == code, false, last.length > 0); } return false; } + +/** Escapes a string to a string literal. */ +export function escapeString(str: string): string { + var k = str.length; + var ret = new Array(k); + ret.length = 0; + for (var i = 0, c: string; i < k; ++i) { + switch (c = str.charAt(i)) { + case "\\": ret.push("\\\\"); break; + case "\"": ret.push("\\\""); break; + case "\r": ret.push("\\r"); break; + case "\n": ret.push("\\n"); break; + case "\0": ret.push("\\0"); break; + default: ret.push(c); + } + } + return "\"" + ret.join("") + "\""; +} diff --git a/src/builtins.ts b/src/builtins.ts index 576a5e59..c118e589 100644 --- a/src/builtins.ts +++ b/src/builtins.ts @@ -83,7 +83,7 @@ export function initialize(program: Program): void { addFunction(program, "assert"); // conversions and limits - let i32Func: FunctionPrototype, + var i32Func: FunctionPrototype, u32Func: FunctionPrototype, i64Func: FunctionPrototype, u64Func: FunctionPrototype; @@ -144,7 +144,7 @@ export function initialize(program: Program): void { /** 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, name, null); + var global = new Global(program, name, name, null); global.isBuiltIn = true; global.isConstant = true; global.type = type; @@ -154,9 +154,10 @@ function addConstant(program: Program, name: string, type: Type): Global { /** Adds a built-in function to the specified program. */ function addFunction(program: Program, name: string, isGeneric: bool = false): FunctionPrototype { - let prototype: FunctionPrototype = new FunctionPrototype(program, name, name, null, null); + var prototype = new FunctionPrototype(program, name, name, null, null); prototype.isBuiltIn = true; - if (isGeneric) prototype.isGeneric = true; + if (isGeneric) + prototype.isGeneric = true; program.elements.set(name, prototype); return prototype; } @@ -187,18 +188,18 @@ export function compileGetConstant(compiler: Compiler, global: Global): Expressi /** Compiles a call to a built-in function. */ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, typeArguments: Type[], operands: Expression[], reportNode: Node): ExpressionRef { - const module: Module = compiler.module; - const usizeType: Type = select(Type.usize64, Type.usize32, compiler.options.target == Target.WASM64); - const nativeUsizeType: NativeType = select(NativeType.I64, NativeType.I32, compiler.options.target == Target.WASM64); + var module = compiler.module; + var usizeType = select(Type.usize64, Type.usize32, compiler.options.target == Target.WASM64); + var nativeUsizeType = select(NativeType.I64, NativeType.I32, compiler.options.target == Target.WASM64); - let arg0: ExpressionRef, + var arg0: ExpressionRef, arg1: ExpressionRef, arg2: ExpressionRef; - let tempLocal0: Local; - let tempLocal1: Local; + var tempLocal0: Local, + tempLocal1: Local; - let ftype: FunctionTypeRef; + var ftype: FunctionTypeRef; switch (prototype.internalName) { diff --git a/src/compiler.ts b/src/compiler.ts index 06826393..f567dbe6 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -195,8 +195,8 @@ export class Compiler extends DiagnosticEmitter { this.module = this.options.noEmit ? Module.createStub() : Module.create(); // set up start function - const startFunctionTemplate: FunctionPrototype = new FunctionPrototype(program, "start", "start", null); - const startFunctionInstance: Function = new Function(startFunctionTemplate, startFunctionTemplate.internalName, [], [], Type.void, null); + var startFunctionTemplate = new FunctionPrototype(program, "start", "start", null); + var startFunctionInstance = new Function(startFunctionTemplate, startFunctionTemplate.internalName, [], [], Type.void, null); this.currentFunction = this.startFunction = startFunctionInstance; } @@ -207,15 +207,14 @@ export class Compiler extends DiagnosticEmitter { this.program.initialize(this.options.target); // compile entry file (exactly one, usually) - const sources: Source[] = this.program.sources; - let i: i32, k = sources.length; - for (i = 0; i < k; ++i) + var sources = this.program.sources; + for (var i = 0, k = sources.length; i < k; ++i) if (sources[i].isEntry) this.compileSource(sources[i]); // make start function if not empty if (this.startFunctionBody.length) { - let typeRef = this.module.getFunctionTypeBySignature(NativeType.None, []); + var typeRef = this.module.getFunctionTypeBySignature(NativeType.None, []); if (!typeRef) typeRef = this.module.addFunctionType("v", NativeType.None, []); this.module.setStart( @@ -227,14 +226,14 @@ export class Compiler extends DiagnosticEmitter { // set up memory if (!this.options.noMemory) { - const initial = this.memoryOffset.clone(); + var initial = this.memoryOffset.clone(); if (this.options.target == Target.WASM64) this.module.addGlobal("HEAP_BASE", NativeType.I64, false, this.module.createI64(initial.lo, initial.hi)); else this.module.addGlobal("HEAP_BASE", NativeType.I32, false, this.module.createI32(initial.lo)); // determine initial page size - const initialOverlaps = initial.clone(); + var initialOverlaps = initial.clone(); initialOverlaps.and32(0xffff); if (!initialOverlaps.isZero) { initial.or32(0xffff); @@ -249,8 +248,8 @@ export class Compiler extends DiagnosticEmitter { // sources compileSourceByPath(normalizedPath: string, reportNode: Node): void { - for (let i = 0, k = this.program.sources.length; i < k; ++i) { - const importedSource = this.program.sources[i]; + for (var i = 0, k = this.program.sources.length; i < k; ++i) { + var importedSource = this.program.sources[i]; if (importedSource.normalizedPath == normalizedPath) { this.compileSource(importedSource); return; @@ -264,9 +263,9 @@ export class Compiler extends DiagnosticEmitter { return; this.files.add(source.normalizedPath); - const noTreeShaking = this.options.noTreeShaking; - for (let i = 0, k = source.statements.length; i < k; ++i) { - const statement: Statement = source.statements[i]; + var noTreeShaking = this.options.noTreeShaking; + for (var i = 0, k = source.statements.length; i < k; ++i) { + var statement = source.statements[i]; switch (statement.kind) { case NodeKind.CLASS: @@ -306,15 +305,14 @@ export class Compiler extends DiagnosticEmitter { break; // otherwise a top-level statement that is part of the start function's body - default: { - const previousFunction = this.currentFunction; + default: + var previousFunction = this.currentFunction; this.currentFunction = this.startFunction; - const expr = this.compileStatement(statement); + var expr = this.compileStatement(statement); if (!this.module.noEmit) this.startFunctionBody.push(expr); this.currentFunction = previousFunction; break; - } } } } @@ -322,7 +320,7 @@ export class Compiler extends DiagnosticEmitter { // globals compileGlobalDeclaration(declaration: VariableDeclaration, isConst: bool): Global | null { - const element = this.program.elements.get(declaration.internalName); + var element = this.program.elements.get(declaration.internalName); if (!element || element.kind != ElementKind.GLOBAL) throw new Error("global expected"); if (!this.compileGlobal(element)) // reports @@ -340,8 +338,8 @@ export class Compiler extends DiagnosticEmitter { if (global.isCompiled || (global.isBuiltIn && compileBuiltinGetConstant(this, global))) return true; - const declaration = global.declaration; - let initExpr: ExpressionRef = 0; + var declaration = global.declaration; + var initExpr: ExpressionRef = 0; if (!global.type) { // infer type if (declaration) { @@ -362,7 +360,7 @@ export class Compiler extends DiagnosticEmitter { throw new Error("declaration expected"); } - const nativeType = global.type.toNativeType(); + var nativeType = global.type.toNativeType(); if (global.isDeclared) { if (global.isConstant) { @@ -375,7 +373,7 @@ export class Compiler extends DiagnosticEmitter { return false; } - let initializeInStart = false; + var initializeInStart = false; if (global.hasConstantValue) { if (global.type.isLongInteger) @@ -386,7 +384,7 @@ export class Compiler extends DiagnosticEmitter { initExpr = this.module.createF64(global.constantFloatValue); else if (global.type.isSmallInteger) { if (global.type.isSignedInteger) { - const shift = global.type.smallIntegerShift; + var shift = global.type.smallIntegerShift; initExpr = this.module.createI32(global.constantIntegerValue ? global.constantIntegerValue.toI32() << shift >> shift : 0); } else initExpr = this.module.createI32(global.constantIntegerValue ? global.constantIntegerValue.toI32() & global.type.smallIntegerMask: 0); @@ -411,16 +409,16 @@ export class Compiler extends DiagnosticEmitter { } else throw new Error("declaration expected"); - const internalName = global.internalName; + var internalName = global.internalName; if (initializeInStart) { this.module.addGlobal(internalName, nativeType, true, global.type.toNativeZero(this.module)); - const setExpr = this.module.createSetGlobal(internalName, initExpr); + var setExpr = this.module.createSetGlobal(internalName, initExpr); if (!this.module.noEmit) this.startFunctionBody.push(setExpr); } else { this.module.addGlobal(internalName, nativeType, global.isMutable, initExpr); if (!global.isMutable && !this.module.noEmit) { - const exprType: NativeType = _BinaryenExpressionGetType(initExpr); + var exprType = _BinaryenExpressionGetType(initExpr); switch (exprType) { case NativeType.I32: global.constantIntegerValue = new I64(_BinaryenConstGetValueI32(initExpr), 0); @@ -447,7 +445,7 @@ export class Compiler extends DiagnosticEmitter { // enums compileEnumDeclaration(declaration: EnumDeclaration): void { - const element = this.program.elements.get(declaration.internalName); + var element = this.program.elements.get(declaration.internalName); if (!element || element.kind != ElementKind.ENUM) throw new Error("enum expected"); this.compileEnum(element); @@ -457,18 +455,18 @@ export class Compiler extends DiagnosticEmitter { if (element.isCompiled) return; - let previousValue: EnumValue | null = null; + var previousValue: EnumValue | null = null; if (element.members) - for (let [key, member] of element.members) { + for (var member of element.members.values()) { if (member.kind != ElementKind.ENUMVALUE) continue; - const val = member; + var val = member; if (val.hasConstantValue) { this.module.addGlobal(val.internalName, NativeType.I32, false, this.module.createI32(val.constantValue)); } else if (val.declaration) { - const declaration = val.declaration; - let initExpr: ExpressionRef; - let initInStart = false; + var declaration = val.declaration; + var initExpr: ExpressionRef; + var initInStart = false; if (declaration.value) { initExpr = this.compileExpression(declaration.value, Type.i32); if (!this.module.noEmit && _BinaryenExpressionGetId(initExpr) != ExpressionId.Const) { @@ -495,7 +493,7 @@ export class Compiler extends DiagnosticEmitter { } if (initInStart) { this.module.addGlobal(val.internalName, NativeType.I32, true, this.module.createI32(0)); - const setExpr = this.module.createSetGlobal(val.internalName, initExpr); + var setExpr = this.module.createSetGlobal(val.internalName, initExpr); if (!this.module.noEmit) this.startFunctionBody.push(setExpr); } else { @@ -518,11 +516,11 @@ export class Compiler extends DiagnosticEmitter { // functions compileFunctionDeclaration(declaration: FunctionDeclaration, typeArguments: TypeNode[], contextualTypeArguments: Map | null = null, alternativeReportNode: Node | null = null): void { - const internalName = declaration.internalName; - const element = this.program.elements.get(internalName); + var internalName = declaration.internalName; + var element = this.program.elements.get(internalName); if (!element || element.kind != ElementKind.FUNCTION_PROTOTYPE) throw new Error("function expected"); - const instance = this.compileFunctionUsingTypeArguments(element, typeArguments, contextualTypeArguments, alternativeReportNode); // reports + var instance = this.compileFunctionUsingTypeArguments(element, typeArguments, contextualTypeArguments, alternativeReportNode); // reports if (!instance) return; if (isModuleExport(instance, declaration)) @@ -530,7 +528,7 @@ export class Compiler extends DiagnosticEmitter { } compileFunctionUsingTypeArguments(prototype: FunctionPrototype, typeArguments: TypeNode[], contextualTypeArguments: Map | null = null, alternativeReportNode: Node | null = null): Function | null { - const instance = prototype.resolveInclTypeArguments(typeArguments, contextualTypeArguments, alternativeReportNode); // reports + var instance = prototype.resolveInclTypeArguments(typeArguments, contextualTypeArguments, alternativeReportNode); // reports if (!instance) return null; return this.compileFunction(instance) ? instance : null; @@ -540,7 +538,7 @@ export class Compiler extends DiagnosticEmitter { if (instance.isCompiled) return true; - const declaration = instance.prototype.declaration; + var declaration = instance.prototype.declaration; if (!declaration) throw new Error("declaration expected"); // built-ins are not compiled here @@ -558,25 +556,25 @@ export class Compiler extends DiagnosticEmitter { instance.isCompiled = true; // compile statements - let stmts: ExpressionRef[] | null = null; + var stmts: ExpressionRef[] | null = null; if (!instance.isDeclared) { - const previousFunction = this.currentFunction; + var previousFunction = this.currentFunction; this.currentFunction = instance; stmts = this.compileStatements(declaration.statements); this.currentFunction = previousFunction; } // create the function type - let k = instance.parameters.length; - const nativeResultType = instance.returnType.toNativeType(); - const nativeParamTypes = new Array(k); - const signatureNameParts = new Array(k + 1); - for (let i = 0; i < k; ++i) { + var k = instance.parameters.length; + var nativeResultType = instance.returnType.toNativeType(); + var nativeParamTypes = new Array(k); + var signatureNameParts = new Array(k + 1); + for (var i = 0; i < k; ++i) { nativeParamTypes[i] = instance.parameters[i].type.toNativeType(); signatureNameParts[i] = instance.parameters[i].type.toSignatureName(); } signatureNameParts[k] = instance.returnType.toSignatureName(); - let typeRef = this.module.getFunctionTypeBySignature(nativeResultType, nativeParamTypes); + var typeRef = this.module.getFunctionTypeBySignature(nativeResultType, nativeParamTypes); if (!typeRef) typeRef = this.module.addFunctionType(signatureNameParts.join(""), nativeResultType, nativeParamTypes); @@ -593,10 +591,10 @@ export class Compiler extends DiagnosticEmitter { // namespaces compileNamespaceDeclaration(declaration: NamespaceDeclaration): void { - const members = declaration.members; - const noTreeShaking = this.options.noTreeShaking; - for (let i = 0, k = members.length; i < k; ++i) { - const member = members[i]; + var members = declaration.members; + var noTreeShaking = this.options.noTreeShaking; + for (var i = 0, k = members.length; i < k; ++i) { + var member = members[i]; switch (member.kind) { case NodeKind.CLASS: @@ -639,8 +637,8 @@ export class Compiler extends DiagnosticEmitter { if (!ns.members) return; - const noTreeShaking = this.options.noTreeShaking; - for (let [name, element] of ns.members) { + var noTreeShaking = this.options.noTreeShaking; + for (var element of ns.members.values()) { switch (element.kind) { case ElementKind.CLASS_PROTOTYPE: @@ -671,11 +669,11 @@ export class Compiler extends DiagnosticEmitter { // exports compileExportStatement(statement: ExportStatement): void { - const members = statement.members; - for (let i = 0, k = members.length; i < k; ++i) { - const member = members[i]; - const internalExportName = statement.range.source.internalPath + PATH_DELIMITER + member.externalIdentifier.name; - const element = this.program.exports.get(internalExportName); + var members = statement.members; + for (var i = 0, k = members.length; i < k; ++i) { + var member = members[i]; + var internalExportName = statement.range.source.internalPath + PATH_DELIMITER + member.externalIdentifier.name; + var element = this.program.exports.get(internalExportName); if (!element) // reported in Program#initialize continue; switch (element.kind) { @@ -691,7 +689,7 @@ export class Compiler extends DiagnosticEmitter { case ElementKind.FUNCTION_PROTOTYPE: if (!(element).isGeneric) { - const functionInstance = this.compileFunctionUsingTypeArguments(element, []); + var functionInstance = this.compileFunctionUsingTypeArguments(element, []); if (functionInstance && statement.range.source.isEntry) this.module.addFunctionExport(functionInstance.internalName, member.externalIdentifier.name); } @@ -716,15 +714,15 @@ export class Compiler extends DiagnosticEmitter { // classes compileClassDeclaration(declaration: ClassDeclaration, typeArguments: TypeNode[], contextualTypeArguments: Map | null = null, alternativeReportNode: Node | null = null): void { - const internalName = declaration.internalName; - const element = this.program.elements.get(internalName); + var internalName = declaration.internalName; + var element = this.program.elements.get(internalName); if (!element || element.kind != ElementKind.CLASS_PROTOTYPE) throw new Error("class expected"); this.compileClassUsingTypeArguments(element, typeArguments, contextualTypeArguments, alternativeReportNode); } compileClassUsingTypeArguments(prototype: ClassPrototype, typeArguments: TypeNode[], contextualTypeArguments: Map | null = null, alternativeReportNode: Node | null = null): void { - const instance = prototype.resolveInclTypeArguments(typeArguments, contextualTypeArguments, alternativeReportNode); + var instance = prototype.resolveInclTypeArguments(typeArguments, contextualTypeArguments, alternativeReportNode); if (!instance) return; this.compileClass(instance); @@ -746,7 +744,7 @@ export class Compiler extends DiagnosticEmitter { this.memoryOffset.or32(7); this.memoryOffset.add32(1); } - const segment = MemorySegment.create(buffer, this.memoryOffset.clone()); + var segment = MemorySegment.create(buffer, this.memoryOffset.clone()); this.memorySegments.push(segment); this.memoryOffset.add32(buffer.length); return segment; @@ -810,15 +808,15 @@ export class Compiler extends DiagnosticEmitter { } compileStatements(statements: Statement[]): ExpressionRef[] { - const k = statements.length; - const stmts = new Array(k); - for (let i = 0; i < k; ++i) + var k = statements.length; + var stmts = new Array(k); + for (var i = 0; i < k; ++i) stmts[i] = this.compileStatement(statements[i]); return stmts; // array of 0-es in noEmit-mode } compileBlockStatement(statement: BlockStatement): ExpressionRef { - const statements = statement.statements; + var statements = statement.statements; if (statements.length == 0) return this.module.createNop(); if (statements.length == 1) @@ -831,7 +829,7 @@ export class Compiler extends DiagnosticEmitter { this.error(DiagnosticCode.Operation_not_supported, statement.label.range); return this.module.createUnreachable(); } - const context = this.currentFunction.breakContext; + var context = this.currentFunction.breakContext; if (context != null) return this.module.createBreak("break|" + (context)); @@ -844,7 +842,7 @@ export class Compiler extends DiagnosticEmitter { this.error(DiagnosticCode.Operation_not_supported, statement.label.range); return this.module.createUnreachable(); } - const context = this.currentFunction.breakContext; + var context = this.currentFunction.breakContext; if (context != null && !this.disallowContinue) return this.module.createBreak("continue|" + (context)); @@ -853,12 +851,12 @@ export class Compiler extends DiagnosticEmitter { } compileDoStatement(statement: DoStatement): ExpressionRef { - const label = this.currentFunction.enterBreakContext(); - const condition = this.compileExpression(statement.condition, Type.i32); - const body = this.compileStatement(statement.statement); + var label = this.currentFunction.enterBreakContext(); + var condition = this.compileExpression(statement.condition, Type.i32); + var body = this.compileStatement(statement.statement); this.currentFunction.leaveBreakContext(); - const breakLabel = "break|" + label; - const continueLabel = "continue|" + label; + var breakLabel = "break|" + label; + var continueLabel = "continue|" + label; return this.module.createBlock(breakLabel, [ this.module.createLoop(continueLabel, this.module.createBlock(null, [ @@ -873,7 +871,7 @@ export class Compiler extends DiagnosticEmitter { } compileExpressionStatement(statement: ExpressionStatement): ExpressionRef { - let expr = this.compileExpression(statement.expression, Type.void, ConversionKind.NONE); + var expr = this.compileExpression(statement.expression, Type.void, ConversionKind.NONE); if (this.currentType != Type.void) { expr = this.module.createDrop(expr); this.currentType = Type.void; @@ -882,18 +880,18 @@ export class Compiler extends DiagnosticEmitter { } compileForStatement(statement: ForStatement): ExpressionRef { - const context = this.currentFunction.enterBreakContext(); - const variableWasLocal = this.variableIsLocal; + var context = this.currentFunction.enterBreakContext(); + var variableWasLocal = this.variableIsLocal; if (this.currentFunction == this.startFunction) this.variableIsLocal = true; - const initializer = statement.initializer ? this.compileStatement(statement.initializer) : this.module.createNop(); + var initializer = statement.initializer ? this.compileStatement(statement.initializer) : this.module.createNop(); this.variableIsLocal = variableWasLocal; - const condition = statement.condition ? this.compileExpression(statement.condition, Type.i32) : this.module.createI32(1); - const incrementor = statement.incrementor ? this.compileExpression(statement.incrementor, Type.void) : this.module.createNop(); - const body = this.compileStatement(statement.statement); + var condition = statement.condition ? this.compileExpression(statement.condition, Type.i32) : this.module.createI32(1); + var incrementor = statement.incrementor ? this.compileExpression(statement.incrementor, Type.void) : this.module.createNop(); + var body = this.compileStatement(statement.statement); this.currentFunction.leaveBreakContext(); - const continueLabel = "continue|" + context; - const breakLabel = "break|" + context; + var continueLabel = "continue|" + context; + var breakLabel = "break|" + context; return this.module.createBlock(breakLabel, [ initializer, this.module.createLoop(continueLabel, this.module.createBlock(null, [ @@ -907,38 +905,38 @@ export class Compiler extends DiagnosticEmitter { } compileIfStatement(statement: IfStatement): ExpressionRef { - const condition = this.compileExpression(statement.condition, Type.i32); - const ifTrue = this.compileStatement(statement.ifTrue); - const ifFalse = statement.ifFalse ? this.compileStatement(statement.ifFalse) : 0; + var condition = this.compileExpression(statement.condition, Type.i32); + var ifTrue = this.compileStatement(statement.ifTrue); + var ifFalse = statement.ifFalse ? this.compileStatement(statement.ifFalse) : 0; return this.module.createIf(condition, ifTrue, ifFalse); } compileReturnStatement(statement: ReturnStatement): ExpressionRef { if (this.currentFunction) { - const expression = statement.value ? this.compileExpression(statement.value, this.currentFunction.returnType) : 0; + var expression = statement.value ? this.compileExpression(statement.value, this.currentFunction.returnType) : 0; return this.module.createReturn(expression); } return this.module.createUnreachable(); } compileSwitchStatement(statement: SwitchStatement): ExpressionRef { - const context = this.currentFunction.enterBreakContext(); - const previousDisallowContinue = this.disallowContinue; + var context = this.currentFunction.enterBreakContext(); + var previousDisallowContinue = this.disallowContinue; this.disallowContinue = true; // introduce a local for evaluating the condition (exactly once) - const tempLocal = this.currentFunction.getTempLocal(Type.i32); - let i: i32, k = statement.cases.length; + var tempLocal = this.currentFunction.getTempLocal(Type.i32); + var i: i32, k = statement.cases.length; // prepend initializer to inner block - const breaks = new Array(1 + k); + var breaks = new Array(1 + k); 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 = 1; - let defaultIndex = -1; + var breakIndex = 1; + var defaultIndex = -1; for (i = 0; i < k; ++i) { - const case_ = statement.cases[i]; + var case_ = statement.cases[i]; if (case_.label) { breaks[breakIndex++] = this.module.createBreak("case" + i.toString(10) + "|" + context, this.module.createBinary(BinaryOp.EqI32, @@ -959,14 +957,14 @@ export class Compiler extends DiagnosticEmitter { ) + "|" + context); // nest blocks in order - let currentBlock = this.module.createBlock("case0|" + context, breaks, NativeType.None); + var currentBlock = this.module.createBlock("case0|" + context, breaks, NativeType.None); for (i = 0; i < k; ++i) { - const case_ = statement.cases[i]; - const nextLabel = i == k - 1 ? "break|" + context : "case" + (i + 1).toString(10) + "|" + context; - const l = case_.statements.length; - const body = new Array(1 + l); + case_ = statement.cases[i]; + var nextLabel = i == k - 1 ? "break|" + context : "case" + (i + 1).toString(10) + "|" + context; + var l = case_.statements.length; + var body = new Array(1 + l); body[0] = currentBlock; - for (let j = 0; j < l; ++j) + for (var j = 0; j < l; ++j) body[j + 1] = this.compileStatement(case_.statements[j]); currentBlock = this.module.createBlock(nextLabel, body, NativeType.None); } @@ -987,22 +985,22 @@ export class Compiler extends DiagnosticEmitter { } compileVariableStatement(statement: VariableStatement): ExpressionRef { - const declarations = statement.declarations; + var declarations = statement.declarations; // top-level variables become globals if (this.currentFunction == this.startFunction && !this.variableIsLocal) { - const isConst = hasModifier(ModifierKind.CONST, statement.modifiers); - for (let i = 0, k = declarations.length; i < k; ++i) + var isConst = hasModifier(ModifierKind.CONST, statement.modifiers); + for (var i = 0, k = declarations.length; i < k; ++i) this.compileGlobalDeclaration(declarations[i], isConst); return this.module.createNop(); } // other variables become locals - const initializers = new Array(); - for (let i = 0, k = declarations.length; i < k; ++i) { - const declaration = declarations[i]; - const name = declaration.name.name; - let type: Type | null = null; - let init: ExpressionRef = 0; + var initializers = new Array(); + for (i = 0, k = declarations.length; i < k; ++i) { + var declaration = declarations[i]; + var name = declaration.name.name; + var type: Type | null = null; + var init: ExpressionRef = 0; if (declaration.type) { type = this.program.resolveType(declaration.type, this.currentFunction.contextualTypeArguments, true); // reports if (!type) @@ -1029,11 +1027,11 @@ export class Compiler extends DiagnosticEmitter { } compileWhileStatement(statement: WhileStatement): ExpressionRef { - const label = this.currentFunction.enterBreakContext(); - const condition = this.compileExpression(statement.condition, Type.i32); - const breakLabel = "break|" + label; - const continueLabel = "continue|" + label; - const body = this.compileStatement(statement.statement); + var label = this.currentFunction.enterBreakContext(); + var condition = this.compileExpression(statement.condition, Type.i32); + var breakLabel = "break|" + label; + var continueLabel = "continue|" + label; + var body = this.compileStatement(statement.statement); this.currentFunction.leaveBreakContext(); return this.module.createBlock(breakLabel, [ this.module.createLoop(continueLabel, @@ -1050,7 +1048,7 @@ export class Compiler extends DiagnosticEmitter { compileExpression(expression: Expression, contextualType: Type, conversionKind: ConversionKind = ConversionKind.IMPLICIT): ExpressionRef { this.currentType = contextualType; - let expr: ExpressionRef; + var expr: ExpressionRef; switch (expression.kind) { case NodeKind.ASSERTION: @@ -1121,13 +1119,13 @@ export class Compiler extends DiagnosticEmitter { } precomputeExpressionRef(expr: ExpressionRef): ExpressionRef { - const nativeType = this.currentType.toNativeType(); - let typeRef = this.module.getFunctionTypeBySignature(nativeType, []); + var nativeType = this.currentType.toNativeType(); + var typeRef = this.module.getFunctionTypeBySignature(nativeType, []); if (!typeRef) typeRef = this.module.addFunctionType(this.currentType.toSignatureName(), nativeType, []); - const funcRef = this.module.addFunction("__precompute", typeRef, [], expr); + var funcRef = this.module.addFunction("__precompute", typeRef, [], expr); this.module.runPasses([ "precompute" ], funcRef); - const ret = _BinaryenFunctionGetBody(funcRef); + var ret = _BinaryenFunctionGetBody(funcRef); this.module.removeFunction("__precompute"); // TODO: also remove the function type somehow if no longer used or make the C-API accept // a `null` typeRef, using an implicit type. @@ -1148,11 +1146,11 @@ export class Compiler extends DiagnosticEmitter { if (toType.kind == TypeKind.VOID) return this.module.createDrop(expr); - const fromFloat = fromType.isAnyFloat; - const toFloat = toType.isAnyFloat; + var fromFloat = fromType.isAnyFloat; + var toFloat = toType.isAnyFloat; - const mod = this.module; - let losesInformation = false; + var mod = this.module; + var losesInformation = false; if (fromFloat) { @@ -1298,20 +1296,20 @@ export class Compiler extends DiagnosticEmitter { } compileAssertionExpression(expression: AssertionExpression, contextualType: Type): ExpressionRef { - const toType = this.program.resolveType(expression.toType, this.currentFunction.contextualTypeArguments); // reports + var toType = this.program.resolveType(expression.toType, this.currentFunction.contextualTypeArguments); // reports if (!toType) return this.module.createUnreachable(); return this.compileExpression(expression.expression, toType, ConversionKind.EXPLICIT); } compileBinaryExpression(expression: BinaryExpression, contextualType: Type): ExpressionRef { - let op: BinaryOp; - let left: ExpressionRef; - let right: ExpressionRef; - let compound: Token = 0; + var op: BinaryOp; + var left: ExpressionRef; + var right: ExpressionRef; + var compound: Token = 0; - let condition: ExpressionRef; - let tempLocal: Local; + var condition: ExpressionRef; + var tempLocal: Local; switch (expression.operator) { @@ -1631,7 +1629,7 @@ export class Compiler extends DiagnosticEmitter { } compileAssignment(expression: Expression, valueExpression: Expression, contextualType: Type): ExpressionRef { - let element: Element | null = null; + var element: Element | null = null; switch (expression.kind) { case NodeKind.IDENTIFIER: @@ -1648,7 +1646,7 @@ export class Compiler extends DiagnosticEmitter { if (!element) return this.module.createUnreachable(); - let type: Type | null = null; + var type: Type | null = null; switch (element.kind) { case ElementKind.LOCAL: @@ -1661,16 +1659,16 @@ export class Compiler extends DiagnosticEmitter { break; case ElementKind.PROPERTY: - const setterPrototype = (element).setterPrototype; + var setterPrototype = (element).setterPrototype; if (setterPrototype) { - const setterInstance = setterPrototype.resolve(); // reports + var setterInstance = setterPrototype.resolve(); // reports if (setterInstance) { if (contextualType == Type.void) { // just set if dropped anyway return this.compileCall(setterInstance, [ valueExpression ], expression); } else { // otherwise do a set followed by a get - const getterPrototype = (element).getterPrototype; + var getterPrototype = (element).getterPrototype; if (getterPrototype) { - const getterInstance = getterPrototype.resolve(); // reports + var getterInstance = getterPrototype.resolve(); // reports if (getterInstance) { return this.module.createBlock(null, [ this.compileCall(setterInstance, [ valueExpression ], expression), @@ -1696,7 +1694,7 @@ export class Compiler extends DiagnosticEmitter { } compileAssignmentWithValue(expression: Expression, valueWithCorrectType: ExpressionRef, tee: bool = false): ExpressionRef { - let element: Element | null = null; + var element: Element | null = null; switch (expression.kind) { case NodeKind.IDENTIFIER: @@ -1731,7 +1729,7 @@ export class Compiler extends DiagnosticEmitter { return this.module.createUnreachable(); } if (tee) { - const globalNativeType: NativeType = ((element).type).toNativeType(); + var globalNativeType = ((element).type).toNativeType(); return this.module.createBlock(null, [ // teeGlobal this.module.createSetGlobal((element).internalName, valueWithCorrectType), this.module.createGetGlobal((element).internalName, globalNativeType) @@ -1747,7 +1745,7 @@ export class Compiler extends DiagnosticEmitter { } compileCallExpression(expression: CallExpression, contextualType: Type): ExpressionRef { - let element: Element | null = null; + var element: Element | null = null; switch (expression.expression.kind) { // case NodeKind.SUPER: @@ -1766,14 +1764,14 @@ export class Compiler extends DiagnosticEmitter { return this.module.createUnreachable(); if (element.kind == ElementKind.FUNCTION_PROTOTYPE) { - const functionPrototype = element; - let functionInstance: Function | null = null; + var functionPrototype = element; + var functionInstance: Function | null = null; if (functionPrototype.isBuiltIn) { - const k = expression.typeArguments.length; - const resolvedTypeArguments = new Array(k); + var k = expression.typeArguments.length; + var resolvedTypeArguments = new Array(k); sb.length = 0; - for (let i = 0; i < k; ++i) { - let resolvedType = this.program.resolveType(expression.typeArguments[i], this.currentFunction.contextualTypeArguments, true); // reports + for (var i = 0; i < k; ++i) { + var resolvedType = this.program.resolveType(expression.typeArguments[i], this.currentFunction.contextualTypeArguments, true); // reports if (!resolvedType) return this.module.createUnreachable(); resolvedTypeArguments[i] = resolvedType; @@ -1783,7 +1781,7 @@ export class Compiler extends DiagnosticEmitter { functionInstance = functionPrototype.instances.get(sb.join(",")); if (!functionInstance) { this.currentType = contextualType; - let expr = compileBuiltinCall(this, functionPrototype, resolvedTypeArguments, expression.arguments, expression); + var expr = compileBuiltinCall(this, functionPrototype, resolvedTypeArguments, expression.arguments, expression); if (!expr) { this.error(DiagnosticCode.Operation_not_supported, expression.range); return this.module.createUnreachable(); @@ -1806,9 +1804,9 @@ export class Compiler extends DiagnosticEmitter { compileCall(functionInstance: Function, argumentExpressions: Expression[], reportNode: Node): ExpressionRef { // validate and compile arguments - const parameters = functionInstance.parameters; - const parameterCount = parameters.length; - const argumentCount = argumentExpressions.length; + var parameters = functionInstance.parameters; + var parameterCount = parameters.length; + var argumentCount = argumentExpressions.length; if (argumentExpressions.length > parameterCount) { // too many arguments this.error(DiagnosticCode.Expected_0_arguments_but_got_1, reportNode.range, (functionInstance.isInstance ? parameterCount - 1 : parameterCount).toString(10), @@ -1816,12 +1814,12 @@ export class Compiler extends DiagnosticEmitter { ); return this.module.createUnreachable(); } - const operands = new Array(parameterCount); - for (let i = 0; i < parameterCount; ++i) { + var operands = new Array(parameterCount); + for (var i = 0; i < parameterCount; ++i) { if (argumentExpressions.length > i) { operands[i] = this.compileExpression(argumentExpressions[i], parameters[i].type); } else { - const initializer = parameters[i].initializer; + var initializer = parameters[i].initializer; if (initializer) { // omitted, uses initializer // FIXME: here, the initializer is compiled in the caller's scope. // a solution could be to use a stub for each possible overload, calling the @@ -1851,7 +1849,7 @@ export class Compiler extends DiagnosticEmitter { } compileElementAccessExpression(expression: ElementAccessExpression, contextualType: Type): ExpressionRef { - const element = this.program.resolveElement(expression.expression, this.currentFunction); // reports + var element = this.program.resolveElement(expression.expression, this.currentFunction); // reports if (!element) return this.module.createUnreachable(); throw new Error("not implemented"); @@ -1892,7 +1890,7 @@ export class Compiler extends DiagnosticEmitter { return this.module.createUnreachable(); } - const element = this.program.resolveElement(expression, this.currentFunction); // reports + var element = this.program.resolveElement(expression, this.currentFunction); // reports if (!element) return this.module.createUnreachable(); @@ -1907,7 +1905,7 @@ export class Compiler extends DiagnosticEmitter { if (element.isBuiltIn) return compileBuiltinGetConstant(this, element); - const global = element; + var global = element; if (!this.compileGlobal(global)) // reports return this.module.createUnreachable(); assert(global.type != null); @@ -1936,7 +1934,7 @@ export class Compiler extends DiagnosticEmitter { // case LiteralKind.ARRAY: case LiteralKind.FLOAT: { - const floatValue = (expression).value; + var floatValue = (expression).value; if (contextualType == Type.f32) return this.module.createF32(floatValue); this.currentType = Type.f64; @@ -1944,7 +1942,7 @@ export class Compiler extends DiagnosticEmitter { } case LiteralKind.INTEGER: { - const intValue = (expression).value; + var intValue = (expression).value; if (contextualType == Type.bool && (intValue.isZero || intValue.isOne)) return this.module.createI32(intValue.isZero ? 0 : 1); if (contextualType == Type.f64) @@ -1980,11 +1978,11 @@ export class Compiler extends DiagnosticEmitter { } compilePropertyAccessExpression(propertyAccess: PropertyAccessExpression, contextualType: Type): ExpressionRef { - const expression = propertyAccess.expression; - const propertyName = propertyAccess.property.name; + var expression = propertyAccess.expression; + var propertyName = propertyAccess.property.name; // the lhs expression is either 'this', 'super', an identifier or another property access - let target: Element | null; + var target: Element | null; switch (expression.kind) { case NodeKind.THIS: @@ -2018,7 +2016,7 @@ export class Compiler extends DiagnosticEmitter { return this.module.createUnreachable(); // look up the property within the target to obtain the actual element - let element: Element | null; + var element: Element | null; switch (target.kind) { case ElementKind.LOCAL: @@ -2053,7 +2051,7 @@ export class Compiler extends DiagnosticEmitter { if (element.kind == ElementKind.ENUMVALUE) { this.currentType = Type.i32; if ((element).hasConstantValue) - return this.module.createI32((element).constantValue) + return this.module.createI32((element).constantValue); this.compileEnum((element).enum); return this.module.createGetGlobal((element).internalName, NativeType.I32); } @@ -2084,9 +2082,9 @@ export class Compiler extends DiagnosticEmitter { return this.module.createGetGlobal((element).internalName, this.currentType.toNativeType()); case ElementKind.PROPERTY: // getter - const getterPrototype = (element).getterPrototype; + var getterPrototype = (element).getterPrototype; if (getterPrototype) { - const getterInstance = getterPrototype.resolve([], this.currentFunction.contextualTypeArguments); + var getterInstance = getterPrototype.resolve([], this.currentFunction.contextualTypeArguments); if (getterInstance) { return this.compileCall(getterInstance, [], propertyAccess); } else { @@ -2102,24 +2100,24 @@ export class Compiler extends DiagnosticEmitter { } compileTernaryExpression(expression: TernaryExpression, contextualType: Type): ExpressionRef { - const condition = this.compileExpression(expression.condition, Type.i32); - const ifThen = this.compileExpression(expression.ifThen, contextualType); - const ifElse = this.compileExpression(expression.ifElse, contextualType); + var condition = this.compileExpression(expression.condition, Type.i32); + var ifThen = this.compileExpression(expression.ifThen, contextualType); + var ifElse = this.compileExpression(expression.ifElse, contextualType); return this.module.createIf(condition, ifThen, ifElse); } compileUnaryPostfixExpression(expression: UnaryPostfixExpression, contextualType: Type): ExpressionRef { - const operator = expression.operator; + var operator = expression.operator; // make a getter for the expression (also obtains the type) - const getValue = this.compileExpression(expression.operand, contextualType, contextualType == Type.void ? ConversionKind.NONE : ConversionKind.IMPLICIT); + var getValue = this.compileExpression(expression.operand, contextualType, contextualType == Type.void ? ConversionKind.NONE : ConversionKind.IMPLICIT); // use a temp local for the intermediate value - const tempLocal = this.currentFunction.getTempLocal(this.currentType); + var tempLocal = this.currentFunction.getTempLocal(this.currentType); - let op: BinaryOp; - let nativeType: NativeType; - let nativeOne: ExpressionRef; + var op: BinaryOp; + var nativeType: NativeType; + var nativeOne: ExpressionRef; if (tempLocal.type == Type.f32) { op = operator == Token.PLUS_PLUS ? BinaryOp.AddF32 : BinaryOp.SubF32; @@ -2143,7 +2141,7 @@ export class Compiler extends DiagnosticEmitter { } // make a setter that sets the new value (temp value +/- 1) - const setValue = this.compileAssignmentWithValue(expression.operand, + var setValue = this.compileAssignmentWithValue(expression.operand, this.module.createBinary(op, this.module.createGetLocal(tempLocal.index, nativeType), nativeOne @@ -2162,10 +2160,10 @@ export class Compiler extends DiagnosticEmitter { } compileUnaryPrefixExpression(expression: UnaryPrefixExpression, contextualType: Type): ExpressionRef { - const operandExpression = expression.operand; + var operandExpression = expression.operand; - let operand: ExpressionRef; - let op: UnaryOp; + var operand: ExpressionRef; + var op: UnaryOp; switch (expression.operator) { @@ -2240,7 +2238,7 @@ function isModuleExport(element: Element, declaration: DeclarationStatement): bo return false; if (declaration.range.source.isEntry) return true; - let parentNode = declaration.parent; + var parentNode = declaration.parent; if (!parentNode) return false; if (parentNode.kind == NodeKind.VARIABLE) @@ -2248,7 +2246,7 @@ function isModuleExport(element: Element, declaration: DeclarationStatement): bo return false; if (parentNode.kind != NodeKind.NAMESPACE && parentNode.kind != NodeKind.CLASS) return false; - let parent = element.program.elements.get((parentNode).internalName); + var parent = element.program.elements.get((parentNode).internalName); if (!parent) return false; return isModuleExport(parent, parentNode); diff --git a/src/decompiler.ts b/src/decompiler.ts index 8905e484..15d7b4bf 100644 --- a/src/decompiler.ts +++ b/src/decompiler.ts @@ -20,7 +20,7 @@ import { export class Decompiler { static decompile(module: Module): string { - const decompiler: Decompiler = new Decompiler(); + var decompiler = new Decompiler(); decompiler.decompile(module); return decompiler.finish(); } @@ -39,13 +39,12 @@ export class Decompiler { } decompileFunction(func: FunctionRef): void { - const name: string = readString(_BinaryenFunctionGetName(func)) || "$" + this.functionId.toString(10); - const body: ExpressionRef = _BinaryenFunctionGetBody(func); + var name = readString(_BinaryenFunctionGetName(func)) || "$" + this.functionId.toString(10); + var body = _BinaryenFunctionGetBody(func); this.push("function "); this.push(name); this.push("("); - let k: Index = _BinaryenFunctionGetNumParams(func); - for (let i: Index = 0; i < k; ++i) { + for (var i: Index = 0, k: Index = _BinaryenFunctionGetNumParams(func); i < k; ++i) { if (i > 0) this.push(", "); this.push("$"); @@ -67,12 +66,12 @@ export class Decompiler { } decompileExpression(expr: ExpressionRef): void { - const id: ExpressionId = _BinaryenExpressionGetId(expr); - const type: NativeType = _BinaryenExpressionGetType(expr); + var id = _BinaryenExpressionGetId(expr); + var type = _BinaryenExpressionGetType(expr); - let nested: ExpressionRef; - let string: string | null; - let i: Index, k: Index; + var nested: ExpressionRef; + var string: string | null; + var i: Index, k: Index; switch (id) { @@ -829,7 +828,7 @@ export class Decompiler { } finish(): string { - const ret: string = this.text.join(""); + var ret = this.text.join(""); this.text = []; return ret; } diff --git a/src/diagnostics.ts b/src/diagnostics.ts index b3e808f2..304d7fa8 100644 --- a/src/diagnostics.ts +++ b/src/diagnostics.ts @@ -64,7 +64,7 @@ export class DiagnosticMessage { } static create(code: DiagnosticCode, category: DiagnosticCategory, arg0: string | null = null, arg1: string | null = null): DiagnosticMessage { - let message: string = diagnosticCodeToString(code); + var message = diagnosticCodeToString(code); if (arg0 != null) message = message.replace("{0}", arg0); if (arg1 != null) @@ -98,7 +98,7 @@ export class DiagnosticMessage { export function formatDiagnosticMessage(message: DiagnosticMessage, useColors: bool = false, showContext: bool = false): string { // format context first (uses same string builder) - let context: string = ""; + var context = ""; if (message.range && showContext) context = formatDiagnosticContext(message.range, useColors); @@ -114,16 +114,16 @@ export function formatDiagnosticMessage(message: DiagnosticMessage, useColors: b // range information if available if (message.range) { - const range: Range = message.range; - const text: string = range.source.text; + var range = message.range; + var text = range.source.text; if (showContext) { sb.push("\n"); sb.push(context); } sb.push("\n"); - let pos: i32 = range.start; - let line: i32 = 1; - let column: i32 = 1; + var pos = range.start; + var line = 1; + var column = 1; while (pos-- > 0) if (text.charCodeAt(pos) == CharCode.LINEFEED) line++; @@ -141,10 +141,10 @@ export function formatDiagnosticMessage(message: DiagnosticMessage, useColors: b } export function formatDiagnosticContext(range: Range, useColors: bool = false): string { - const text: string = range.source.text; - const len: i32 = text.length; - let start: i32 = range.start; - let end: i32 = range.end; + var text = range.source.text; + var len = text.length; + var start = range.start; + var end = range.end; while (start > 0 && !isLineBreak(text.charCodeAt(start - 1))) start--; while (end < len && !isLineBreak(text.charCodeAt(end))) @@ -176,7 +176,7 @@ export abstract class DiagnosticEmitter { } emitDiagnostic(code: DiagnosticCode, category: DiagnosticCategory, range: Range, arg0: string | null = null, arg1: string | null = null) { - const message: DiagnosticMessage = DiagnosticMessage.create(code, category, arg0, arg1).withRange(range); + var message = DiagnosticMessage.create(code, category, arg0, arg1).withRange(range); this.diagnostics.push(message); if (!this.silentDiagnostics) { console.log(formatDiagnosticMessage(message, true, true) + "\n"); // temporary diff --git a/src/index.ts b/src/index.ts index ad5894f1..54c4c638 100644 --- a/src/index.ts +++ b/src/index.ts @@ -67,7 +67,7 @@ export function nextFile(parser: Parser): string | null { /** Obtains the next diagnostic message. Returns `null` once there are no more messages. */ export function nextDiagnostic(parser: Parser): DiagnosticMessage | null { - const program: Program = parser.program; + var program = parser.program; if (program.diagnosticsOffset < program.diagnostics.length) return program.diagnostics[program.diagnosticsOffset++]; return null; @@ -120,14 +120,14 @@ export function setNoMemory(options: Options, noMemory: bool): void { /** Compiles the sources computed by the parser to a module. */ export function compile(parser: Parser, options: Options | null = null): Module { - const program: Program = parser.finish(); - const compiler: Compiler = new Compiler(program, options); + var program = parser.finish(); + var compiler = new Compiler(program, options); return compiler.compile(); } /** Decompiles a module to its (low level) source. */ export function decompile(module: Module): string { - const decompiler: Decompiler = new Decompiler(); + var decompiler = new Decompiler(); decompiler.decompile(module); return decompiler.finish(); } diff --git a/src/module.ts b/src/module.ts index e802088b..445f8058 100644 --- a/src/module.ts +++ b/src/module.ts @@ -246,7 +246,7 @@ export class MemorySegment { offset: U64; static create(buffer: Uint8Array, offset: U64) { - const segment: MemorySegment = new MemorySegment(); + var segment = new MemorySegment(); segment.buffer = buffer; segment.offset = offset; return segment; @@ -262,7 +262,7 @@ export class Module { static MAX_MEMORY_WASM32: Index = 0xffff; static create(): Module { - const module: Module = new Module(); + var module = new Module(); module.ref = _BinaryenModuleCreate(); module.lit = changetype(Heap.allocate(16)); module.noEmit = false; @@ -270,9 +270,9 @@ export class Module { } static createFrom(buffer: Uint8Array): Module { - const cArr: usize = allocU8Array(buffer); + var cArr = allocU8Array(buffer); try { - const module: Module = new Module(); + var module = new Module(); module.ref = _BinaryenModuleRead(cArr, buffer.length); module.lit = changetype(Heap.allocate(16)); module.noEmit = false; @@ -283,7 +283,7 @@ export class Module { } static createStub(): Module { - const module: Module = new Module(); + var module = new Module(); module.ref = 0; module.lit = changetype(0); module.noEmit = true; @@ -296,8 +296,8 @@ export class Module { addFunctionType(name: string, result: NativeType, paramTypes: NativeType[]): FunctionRef { if (this.noEmit) return 0; - const cStr: usize = allocString(name); - const cArr: usize = allocI32Array(paramTypes); + var cStr = allocString(name); + var cArr = allocI32Array(paramTypes); try { return _BinaryenAddFunctionType(this.ref, cStr, result, cArr, paramTypes.length); } finally { @@ -308,7 +308,7 @@ export class Module { getFunctionTypeBySignature(result: NativeType, paramTypes: NativeType[]): FunctionTypeRef { if (this.noEmit) return 0; - const cArr: usize = allocI32Array(paramTypes); + var cArr = allocI32Array(paramTypes); try { return _BinaryenGetFunctionTypeBySignature(this.ref, result, cArr, paramTypes.length); } finally { @@ -354,8 +354,8 @@ export class Module { createHost(op: HostOp, name: string | null = null, operands: ExpressionRef[] | null = null): ExpressionRef { if (this.noEmit) return 0; - const cStr: usize = allocString(name); - const cArr: usize = allocI32Array(operands); + var cStr = allocString(name); + var cArr = allocI32Array(operands); try { return _BinaryenHost(this.ref, op, cStr, cArr, operands ? (operands).length : 0); } finally { @@ -376,7 +376,7 @@ export class Module { createGetGlobal(name: string, type: NativeType): ExpressionRef { if (this.noEmit) return 0; - const cStr: usize = allocString(name); + var cStr = allocString(name); try { return _BinaryenGetGlobal(this.ref, cStr, type); } finally { @@ -433,7 +433,7 @@ export class Module { createSetGlobal(name: string, value: ExpressionRef): ExpressionRef { if (this.noEmit) return 0; - const cStr: usize = allocString(name); + var cStr = allocString(name); try { return _BinaryenSetGlobal(this.ref, cStr, value); } finally { @@ -443,8 +443,8 @@ export class Module { createBlock(label: string | null, children: ExpressionRef[], type: NativeType = NativeType.Auto): ExpressionRef { if (this.noEmit) return 0; - const cStr: usize = allocString(label); - const cArr: usize = allocI32Array(children); + var cStr = allocString(label); + var cArr = allocI32Array(children); try { return _BinaryenBlock(this.ref, cStr, cArr, children.length, type); } finally { @@ -455,7 +455,7 @@ export class Module { createBreak(label: string | null, condition: ExpressionRef = 0, value: ExpressionRef = 0): ExpressionRef { if (this.noEmit) return 0; - const cStr: usize = allocString(label); + var cStr = allocString(label); try { return _BinaryenBreak(this.ref, cStr, condition, value); } finally { @@ -470,7 +470,7 @@ export class Module { createLoop(label: string | null, body: ExpressionRef): ExpressionRef { if (this.noEmit) return 0; - const cStr: usize = allocString(label); + var cStr = allocString(label); try { return _BinaryenLoop(this.ref, cStr, body); } finally { @@ -500,11 +500,11 @@ export class Module { createSwitch(names: string[], defaultName: string | null, condition: ExpressionRef, value: ExpressionRef = 0): ExpressionRef { if (this.noEmit) return 0; - const strs: usize[] = new Array(names.length); - let i: i32, k: i32 = names.length; - for (i = 0; i < k; ++i) strs[i] = allocString(names[i]); - const cArr: usize = allocI32Array(strs); - const cStr: usize = allocString(defaultName); + var strs = new Array(names.length); + for (var i = 0, k: i32 = names.length; i < k; ++i) + strs[i] = allocString(names[i]); + var cArr = allocI32Array(strs); + var cStr = allocString(defaultName); try { return _BinaryenSwitch(this.ref, cArr, k, cStr, condition, value); } finally { @@ -516,8 +516,8 @@ export class Module { createCall(target: string, operands: ExpressionRef[], returnType: NativeType): ExpressionRef { if (this.noEmit) return 0; - const cStr: usize = allocString(target); - const cArr: usize = allocI32Array(operands); + var cStr = allocString(target); + var cArr = allocI32Array(operands); try { return _BinaryenCall(this.ref, cStr, cArr, operands.length, returnType); } finally { @@ -528,8 +528,8 @@ export class Module { createCallImport(target: string, operands: ExpressionRef[], returnType: NativeType): ExpressionRef { if (this.noEmit) return 0; - const cStr: usize = allocString(target); - const cArr: usize = allocI32Array(operands); + var cStr = allocString(target); + var cArr = allocI32Array(operands); try { return _BinaryenCallImport(this.ref, cStr, cArr, operands.length, returnType); } finally { @@ -547,7 +547,7 @@ export class Module { addGlobal(name: string, type: NativeType, mutable: bool, initializer: ExpressionRef): GlobalRef { if (this.noEmit) return 0; - const cStr: usize = allocString(name); + var cStr = allocString(name); try { return _BinaryenAddGlobal(this.ref, cStr, type, mutable ? 1 : 0, initializer); } finally { @@ -557,8 +557,8 @@ export class Module { addFunction(name: string, type: FunctionTypeRef, varTypes: NativeType[], body: ExpressionRef): FunctionRef { if (this.noEmit) return 0; - const cStr: usize = allocString(name); - const cArr: usize = allocI32Array(varTypes); + var cStr = allocString(name); + var cArr = allocI32Array(varTypes); try { return _BinaryenAddFunction(this.ref, cStr, type, cArr, varTypes.length, body); } finally { @@ -568,7 +568,7 @@ export class Module { } removeFunction(name: string): void { - const cStr: usize = allocString(name); + var cStr = allocString(name); try { _BinaryenRemoveFunction(this.ref, cStr); } finally { @@ -578,8 +578,8 @@ export class Module { addFunctionExport(internalName: string, externalName: string): ExportRef { if (this.noEmit) return 0; - const cStr1: usize = allocString(internalName); - const cStr2: usize = allocString(externalName); + var cStr1 = allocString(internalName); + var cStr2 = allocString(externalName); try { return _BinaryenAddFunctionExport(this.ref, cStr1, cStr2); } finally { @@ -590,8 +590,8 @@ export class Module { addTableExport(internalName: string, externalName: string): ExportRef { if (this.noEmit) return 0; - const cStr1: usize = allocString(internalName); - const cStr2: usize = allocString(externalName); + var cStr1 = allocString(internalName); + var cStr2 = allocString(externalName); try { return _BinaryenAddTableExport(this.ref, cStr1, cStr2); } finally { @@ -602,8 +602,8 @@ export class Module { addMemoryExport(internalName: string, externalName: string): ExportRef { if (this.noEmit) return 0; - const cStr1: usize = allocString(internalName); - const cStr2: usize = allocString(externalName); + var cStr1 = allocString(internalName); + var cStr2 = allocString(externalName); try { return _BinaryenAddMemoryExport(this.ref, cStr1, cStr2); } finally { @@ -614,8 +614,8 @@ export class Module { addGlobalExport(internalName: string, externalName: string): ExportRef { if (this.noEmit) return 0; - const cStr1: usize = allocString(internalName); - const cStr2: usize = allocString(externalName); + var cStr1 = allocString(internalName); + var cStr2 = allocString(externalName); try { return _BinaryenAddGlobalExport(this.ref, cStr1, cStr2); } finally { @@ -626,7 +626,7 @@ export class Module { removeExport(externalName: string): void { if (this.noEmit) return; - const cStr = allocString(externalName); + var cStr = allocString(externalName); try { _BinaryenRemoveExport(this.ref, cStr); } finally { @@ -636,9 +636,9 @@ export class Module { addFunctionImport(internalName: string, externalModuleName: string, externalBaseName: string, functionType: FunctionTypeRef): ImportRef { if (this.noEmit) return 0; - const cStr1: usize = allocString(internalName); - const cStr2: usize = allocString(externalModuleName); - const cStr3: usize = allocString(externalBaseName); + var cStr1 = allocString(internalName); + var cStr2 = allocString(externalModuleName); + var cStr3 = allocString(externalBaseName); try { return _BinaryenAddFunctionImport(this.ref, cStr1, cStr2, cStr3, functionType); } finally { @@ -650,9 +650,9 @@ export class Module { addTableImport(internalName: string, externalModuleName: string, externalBaseName: string): ImportRef { if (this.noEmit) return 0; - const cStr1: usize = allocString(internalName); - const cStr2: usize = allocString(externalModuleName); - const cStr3: usize = allocString(externalBaseName); + var cStr1 = allocString(internalName); + var cStr2 = allocString(externalModuleName); + var cStr3 = allocString(externalBaseName); try { return _BinaryenAddTableImport(this.ref, cStr1, cStr2, cStr3); } finally { @@ -664,9 +664,9 @@ export class Module { addMemoryImport(internalName: string, externalModuleName: string, externalBaseName: string): ImportRef { if (this.noEmit) return 0; - const cStr1: usize = allocString(internalName); - const cStr2: usize = allocString(externalModuleName); - const cStr3: usize = allocString(externalBaseName); + var cStr1 = allocString(internalName); + var cStr2 = allocString(externalModuleName); + var cStr3 = allocString(externalBaseName); try { return _BinaryenAddMemoryImport(this.ref, cStr1, cStr2, cStr3); } finally { @@ -678,9 +678,9 @@ export class Module { addGlobalImport(internalName: string, externalModuleName: string, externalBaseName: string, globalType: NativeType): ImportRef { if (this.noEmit) return 0; - const cStr1: usize = allocString(internalName); - const cStr2: usize = allocString(externalModuleName); - const cStr3: usize = allocString(externalBaseName); + var cStr1 = allocString(internalName); + var cStr2 = allocString(externalModuleName); + var cStr3 = allocString(externalBaseName); try { return _BinaryenAddGlobalImport(this.ref, cStr1, cStr2, cStr3, globalType); } finally { @@ -692,7 +692,7 @@ export class Module { removeImport(internalName: string): void { if (this.noEmit) return; - const cStr: usize = allocString(internalName); + var cStr = allocString(internalName); try { _BinaryenRemoveImport(this.ref, cStr); } finally { @@ -702,23 +702,23 @@ export class Module { setMemory(initial: Index, maximum: Index, segments: MemorySegment[], target: Target, exportName: string | null = null): void { if (this.noEmit) return; - const cStr: usize = allocString(exportName); - let i: i32, k: i32 = segments.length; - const segs: usize[] = new Array(k); - const offs: ExpressionRef[] = new Array(k); - const sizs: Index[] = new Array(k); - for (i = 0; i < k; ++i) { - const buffer: Uint8Array = segments[i].buffer; - const offset: U64 = segments[i].offset; + var cStr = allocString(exportName); + var k = segments.length; + var segs = new Array(k); + var offs = new Array(k); + var sizs = new Array(k); + for (var i = 0; i < k; ++i) { + var buffer = segments[i].buffer; + var offset = segments[i].offset; segs[i] = allocU8Array(buffer); offs[i] = target == Target.WASM64 ? this.createI64(offset.lo, offset.hi) : this.createI32(offset.toI32()); sizs[i] = buffer.length; } - const cArr1: usize = allocI32Array(segs); - const cArr2: usize = allocI32Array(offs); - const cArr3: usize = allocI32Array(sizs); + var cArr1 = allocI32Array(segs); + var cArr2 = allocI32Array(offs); + var cArr3 = allocI32Array(sizs); try { _BinaryenSetMemory(this.ref, initial, maximum, cStr, cArr1, cArr2, cArr3, k); } finally { @@ -732,7 +732,7 @@ export class Module { setFunctionTable(funcs: FunctionRef[]): void { if (this.noEmit) return; - const cArr: usize = allocI32Array(funcs); + var cArr = allocI32Array(funcs); try { _BinaryenSetFunctionTable(this.ref, cArr, funcs.length); } finally { @@ -756,10 +756,11 @@ export class Module { } runPasses(passes: string[], func: FunctionRef = 0): void { - let i: i32, k: i32 = passes.length; - const names: usize[] = new Array(k); - for (i = 0; i < k; ++i) names[i] = allocString(passes[i]); - const cArr: usize = allocI32Array(names); + var k = passes.length; + var names = new Array(k); + for (var i = 0; i < k; ++i) + names[i] = allocString(passes[i]); + var cArr = allocI32Array(names); try { if (func) _BinaryenFunctionRunPasses(func, this.ref, cArr, k); @@ -816,7 +817,7 @@ export class Module { cloneExpression(expr: ExpressionRef, noSideEffects: bool = false, maxDepth: i32 = 0x7fffffff): ExpressionRef { if (this.noEmit || maxDepth < 0) return 0; - let nested1: ExpressionRef, + var nested1: ExpressionRef, nested2: ExpressionRef; switch (_BinaryenExpressionGetId(expr)) { @@ -862,7 +863,7 @@ export class Relooper { noEmit: bool; static create(module: Module): Relooper { - const relooper: Relooper = new Relooper(); + var relooper = new Relooper(); relooper.module = module; relooper.ref = _RelooperCreate(); relooper.noEmit = false; @@ -870,7 +871,7 @@ export class Relooper { } static createStub(module: Module): Relooper { - const relooper: Relooper = new Relooper(); + var relooper = new Relooper(); relooper.module = module; relooper.ref = 0; relooper.noEmit = true; @@ -896,7 +897,7 @@ export class Relooper { addBranchForSwitch(from: RelooperBlockRef, to: RelooperBlockRef, indexes: i32[], code: ExpressionRef = 0): void { if (this.noEmit) return; - const cArr: usize = allocI32Array(indexes); + var cArr = allocI32Array(indexes); try { _RelooperAddBranchForSwitch(from, to, cArr, indexes.length, code); } finally { @@ -919,19 +920,19 @@ export function setAPITracing(on: bool): void { function allocU8Array(u8s: Uint8Array | null): usize { if (!u8s) return 0; - const ptr: usize = Heap.allocate((u8s).length); - let idx: usize = ptr; - for (let i: i32 = 0, k: i32 = (u8s).length; i < k; ++i) + var ptr = Heap.allocate((u8s).length); + var idx = ptr; + for (var i = 0, k = (u8s).length; i < k; ++i) store(idx++, (u8s)[i]); return ptr; } function allocI32Array(i32s: i32[] | null): usize { if (!i32s) return 0; - const ptr: usize = Heap.allocate((i32s).length << 2); - let idx: usize = ptr; - for (let i: i32 = 0, k: i32 = (i32s).length; i < k; ++i) { - let val: i32 = (i32s)[i]; + var ptr = Heap.allocate((i32s).length << 2); + var idx = ptr; + for (var i = 0, k = (i32s).length; i < k; ++i) { + var val = (i32s)[i]; // store(idx, val) is not portable store(idx , ( val & 0xff) as u8); store(idx + 1, ((val >> 8) & 0xff) as u8); @@ -943,9 +944,9 @@ function allocI32Array(i32s: i32[] | null): usize { } function stringLengthUTF8(str: string): usize { - let len: i32 = 0; - for (let i: i32 = 0, k: i32 = str.length; i < k; ++i) { - let u: i32 = str.charCodeAt(i); + var len = 0; + for (var i = 0, k = str.length; i < k; ++i) { + var u = str.charCodeAt(i); if (u >= 0xD800 && u <= 0xDFFF && i + 1 < k) u = 0x10000 + ((u & 0x3FF) << 10) | (str.charCodeAt(++i) & 0x3FF); if (u <= 0x7F) @@ -966,10 +967,10 @@ function stringLengthUTF8(str: string): usize { function allocString(str: string | null): usize { if (str == null) return 0; - const ptr: usize = Heap.allocate(stringLengthUTF8((str)) + 1); - let idx: usize = ptr; - for (let i: i32 = 0, k: i32 = (str).length; i < k; ++i) { - let u: i32 = (str).charCodeAt(i); + var ptr = Heap.allocate(stringLengthUTF8((str)) + 1); + var idx = ptr; + for (var i = 0, k = (str).length; i < k; ++i) { + var u = (str).charCodeAt(i); if (u >= 0xD800 && u <= 0xDFFF && i + 1 < k) u = 0x10000 + ((u & 0x3FF) << 10) | ((str).charCodeAt(++i) & 0x3FF); if (u <= 0x7F) @@ -1007,10 +1008,10 @@ function allocString(str: string | null): usize { export function readString(ptr: usize): string | null { if (!ptr) return null; - const arr: i32[] = []; + var arr = new Array(); // the following is based on Emscripten's UTF8ArrayToString - let cp: u32; - let u1: u32, u2: u32, u3: u32, u4: u32, u5: u32; + var cp: u32; + var u1: u32, u2: u32, u3: u32, u4: u32, u5: u32; while (cp = load(ptr++)) { if (!(cp & 0x80)) { arr.push(cp); diff --git a/src/parser.ts b/src/parser.ts index 6c03fa6c..ac2d5534 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -98,20 +98,20 @@ export class Parser extends DiagnosticEmitter { /** 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) + var normalizedPath = normalizePath(path); + for (var i = 0, k = this.program.sources.length; i < k; ++i) if (this.program.sources[i].normalizedPath == normalizedPath) return; // already parsed this.seenlog.add(normalizedPath); - const source: Source = new Source(path, text, isEntry); + var source = new Source(path, text, isEntry); this.program.sources.push(source); - const tn: Tokenizer = new Tokenizer(source, this.program.diagnostics); + var tn = new Tokenizer(source, this.program.diagnostics); source.tokenizer = tn; while (!tn.skip(Token.ENDOFFILE)) { - const statement: Statement | null = this.parseTopLevelStatement(tn); + var statement = this.parseTopLevelStatement(tn); if (statement) { statement.parent = source; source.statements.push(statement); @@ -120,10 +120,10 @@ export class Parser extends DiagnosticEmitter { } parseTopLevelStatement(tn: Tokenizer, isNamespaceMember: bool = false): Statement | null { - let decorators: Decorator[] | null = null; + var decorators: Decorator[] | null = null; while (tn.skip(Token.AT)) { - const decorator: Decorator | null = this.parseDecorator(tn); + var decorator = this.parseDecorator(tn); if (!decorator) break; if (!decorators) @@ -131,7 +131,7 @@ export class Parser extends DiagnosticEmitter { (decorators).push(decorator); } - let modifiers: Modifier[] | null = null; + var modifiers: Modifier[] | null = null; if (tn.skip(Token.EXPORT)) modifiers = addModifier(Node.createModifier(ModifierKind.EXPORT, tn.range()), modifiers); @@ -145,8 +145,8 @@ export class Parser extends DiagnosticEmitter { tn.mark(); - let statement: Statement | null = null; - let modifier: Modifier | null; + var statement: Statement | null = null; + var modifier: Modifier | null; switch (tn.next()) { case Token.CONST: @@ -224,7 +224,7 @@ export class Parser extends DiagnosticEmitter { } if (decorators /* not consumed */) - for (let i: i32 = 0, k: i32 = (decorators).length; i < k; ++i) + for (var i = 0, k = (decorators).length; i < k; ++i) this.error(DiagnosticCode.Decorators_are_not_valid_here, (decorators)[i].range); return statement; @@ -233,8 +233,8 @@ export class Parser extends DiagnosticEmitter { /** Obtains the next file to parse. */ nextFile(): string | null { if (this.backlog.length) { - const filename: string = this.backlog[0]; - for (let i: i32 = 0, k: i32 = this.backlog.length - 1; i < k; ++i) + var filename = this.backlog[0]; + for (var i = 0, k = this.backlog.length - 1; i < k; ++i) this.backlog[i] = this.backlog[i + 1]; this.backlog.length--; return filename; @@ -253,18 +253,18 @@ export class Parser extends DiagnosticEmitter { parseType(tn: Tokenizer, acceptParenthesized: bool = true, suppressErrors: bool = false): TypeNode | null { // not TypeScript-compatible - const token: Token = tn.next(); - const startPos: i32 = tn.tokenPos; + var token = tn.next(); + var startPos = tn.tokenPos; // void if (token == Token.VOID) return Node.createType(Node.createIdentifier("void", tn.range()), [], false, tn.range(startPos, tn.pos)); - let type: TypeNode; + var type: TypeNode; // ( ... ) if (acceptParenthesized && token == Token.OPENPAREN) { - const innerType: TypeNode | null = this.parseType(tn, false, suppressErrors); + var innerType = this.parseType(tn, false, suppressErrors); if (!innerType) return null; if (!tn.skip(Token.CLOSEPAREN)) { @@ -291,14 +291,14 @@ export class Parser extends DiagnosticEmitter { // Name } else if (token == Token.IDENTIFIER) { - const identifier: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); - const parameters: TypeNode[] = new Array(); - let nullable: bool = false; + var identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var parameters = new Array(); + var nullable = false; // Name if (tn.skip(Token.LESSTHAN)) { do { - const parameter: TypeNode | null = this.parseType(tn, true, suppressErrors); + var parameter = this.parseType(tn, true, suppressErrors); if (!parameter) return null; parameters.push(parameter); @@ -328,16 +328,16 @@ export class Parser extends DiagnosticEmitter { } // ... [][] while (tn.skip(Token.OPENBRACKET)) { - let bracketStart: i32 = tn.tokenPos; + var bracketStart = tn.tokenPos; if (!tn.skip(Token.CLOSEBRACKET)) { if (!suppressErrors) this.error(DiagnosticCode._0_expected, tn.range(), "]"); return null; } - const bracketRange = tn.range(bracketStart, tn.pos); + var bracketRange = tn.range(bracketStart, tn.pos); // ...[] | null - let nullable: bool = false; + nullable = false; if (tn.skip(Token.BAR)) { if (tn.skip(Token.NULL)) { nullable = true; @@ -359,10 +359,10 @@ export class Parser extends DiagnosticEmitter { parseDecorator(tn: Tokenizer): Decorator | null { // at '@': Identifier ('.' Identifier)* '(' Arguments - const startPos: i32 = tn.tokenPos; + var startPos = tn.tokenPos; if (tn.skip(Token.IDENTIFIER)) { - let name: string = tn.readIdentifier(); - let expression: Expression = Node.createIdentifier(name, tn.range(startPos, tn.pos)); + var name = tn.readIdentifier(); + var expression: Expression = Node.createIdentifier(name, tn.range(startPos, tn.pos)); while (tn.skip(Token.DOT)) { if (tn.skip(Token.IDENTIFIER)) { name = tn.readIdentifier(); @@ -373,7 +373,7 @@ export class Parser extends DiagnosticEmitter { } } if (tn.skip(Token.OPENPAREN)) { - const args: Expression[] | null = this.parseArguments(tn); + var args = this.parseArguments(tn); if (args) return Node.createDecorator(expression, args, tn.range(startPos, tn.pos)); } else @@ -385,17 +385,17 @@ export class Parser extends DiagnosticEmitter { parseVariable(tn: Tokenizer, modifiers: Modifier[] | null, decorators: Decorator[] | null): VariableStatement | null { // at ('const' | 'let' | 'var'): VariableDeclaration (',' VariableDeclaration)* ';'? - const startPos: i32 = modifiers && modifiers.length ? modifiers[0].range.start : tn.tokenPos; - const members: VariableDeclaration[] = new Array(); - const isDeclare = hasModifier(ModifierKind.DECLARE, modifiers); + var startPos = modifiers && modifiers.length ? modifiers[0].range.start : tn.tokenPos; + var members = new Array(); + var isDeclare = hasModifier(ModifierKind.DECLARE, modifiers); do { - const member: VariableDeclaration | null = this.parseVariableDeclaration(tn, isDeclare, modifiers, decorators); + var member = this.parseVariableDeclaration(tn, isDeclare, modifiers, decorators); if (!member) return null; members.push(member); } while (tn.skip(Token.COMMA)); - const ret: VariableStatement = Node.createVariable(members, modifiers, decorators, tn.range(startPos, tn.pos)); + var ret = Node.createVariable(members, modifiers, decorators, tn.range(startPos, tn.pos)); tn.skip(Token.SEMICOLON); return ret; } @@ -406,13 +406,13 @@ export class Parser extends DiagnosticEmitter { this.error(DiagnosticCode.Identifier_expected, tn.range()); return null; } - const identifier: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); - let type: TypeNode | null = null; + var type: TypeNode | null = null; if (tn.skip(Token.COLON)) type = this.parseType(tn); - let initializer: Expression | null = null; + var initializer: Expression | null = null; if (tn.skip(Token.EQUALS)) { if (isDeclare) this.error(DiagnosticCode.Initializers_are_not_allowed_in_ambient_contexts, tn.range()); // recoverable @@ -431,20 +431,20 @@ export class Parser extends DiagnosticEmitter { parseEnum(tn: Tokenizer, modifiers: Modifier[] | null, decorators: Decorator[] | null): EnumDeclaration | null { // at 'enum': Identifier '{' (EnumValueDeclaration (',' EnumValueDeclaration )*)? '}' ';'? - const startPos: i32 = modifiers && modifiers.length ? modifiers[0].range.start : tn.tokenPos; + var startPos = modifiers && modifiers.length ? modifiers[0].range.start : tn.tokenPos; if (tn.next() != Token.IDENTIFIER) { this.error(DiagnosticCode.Identifier_expected, tn.range()); return null; } - const identifier: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); if (tn.next() != Token.OPENBRACE) { this.error(DiagnosticCode._0_expected, tn.range(), "{"); return null; } - const members: EnumValueDeclaration[] = new Array(); + var members = new Array(); if (!tn.skip(Token.CLOSEBRACE)) { do { - const member: EnumValueDeclaration | null = this.parseEnumValue(tn); + var member = this.parseEnumValue(tn); if (!member) return null; members.push(member); @@ -454,7 +454,7 @@ export class Parser extends DiagnosticEmitter { return null; } } - const ret: EnumDeclaration = Node.createEnum(identifier, members, modifiers, decorators, tn.range(startPos, tn.pos)); + var ret = Node.createEnum(identifier, members, modifiers, decorators, tn.range(startPos, tn.pos)); tn.skip(Token.SEMICOLON); return ret; } @@ -465,8 +465,8 @@ export class Parser extends DiagnosticEmitter { this.error(DiagnosticCode.Identifier_expected, tn.range()); return null; } - const identifier: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); - let value: Expression | null = null; + var identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var value: Expression | null = null; if (tn.skip(Token.EQUALS)) { value = this.parseExpression(tn, Precedence.COMMA + 1); if (!value) @@ -477,23 +477,23 @@ export class Parser extends DiagnosticEmitter { parseReturn(tn: Tokenizer): ReturnStatement | null { // at 'return': Expression | (';' | '}' | ...'\n') - let expr: Expression | null = null; + var expr: Expression | null = null; if (tn.peek(true) != Token.SEMICOLON && tn.nextToken != Token.CLOSEBRACE && !tn.nextTokenOnNewLine) { expr = this.parseExpression(tn); if (!expr) return null; } - const ret: ReturnStatement = Node.createReturn(expr, tn.range()); + var ret = Node.createReturn(expr, tn.range()); tn.skip(Token.SEMICOLON); return ret; } parseTypeParameters(tn: Tokenizer): TypeParameter[] | null { // at '<': TypeParameter (',' TypeParameter)* '>' - const typeParameters: TypeParameter[] = new Array(); + var typeParameters = new Array(); if (!tn.skip(Token.GREATERTHAN)) { do { - const typeParameter: TypeParameter | null = this.parseTypeParameter(tn); + var typeParameter = this.parseTypeParameter(tn); if (!typeParameter) return null; typeParameters.push(typeParameter); @@ -510,8 +510,8 @@ export class Parser extends DiagnosticEmitter { parseTypeParameter(tn: Tokenizer): TypeParameter | null { // Identifier ('extends' Type)? if (tn.next() == Token.IDENTIFIER) { - const identifier: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); - let extendsType: TypeNode | null = null; + var identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var extendsType: TypeNode | null = null; if (tn.skip(Token.EXTENDS)) { extendsType = this.parseType(tn); if (!extendsType) @@ -525,10 +525,10 @@ export class Parser extends DiagnosticEmitter { parseParameters(tn: Tokenizer): Parameter[] | null { // at '(': (Parameter (',' Parameter)*)? ')' - const parameters: Parameter[] = new Array(); + var parameters = new Array(); if (tn.peek() != Token.CLOSEPAREN) { do { - const param: Parameter | null = this.parseParameter(tn); + var param = this.parseParameter(tn); if (!param) return null; parameters.push(param); @@ -543,29 +543,29 @@ export class Parser extends DiagnosticEmitter { parseParameter(tn: Tokenizer): Parameter | null { // '...'? Identifier (':' Type)? ('=' Expression)? - let multiple: bool = false; - let startRange: Range | null = null; + var isRest = false; + var startRange: Range | null = null; if (tn.skip(Token.DOT_DOT_DOT)) { - multiple = true; + isRest = true; startRange = tn.range(); } if (tn.skip(Token.IDENTIFIER)) { - if (!multiple) + if (!isRest) startRange = tn.range(); - const identifier: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); - let type: TypeNode | null = null; + var identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var type: TypeNode | null = null; if (tn.skip(Token.COLON)) { type = this.parseType(tn); if (!type) return null; } - let initializer: Expression | null = null; + var initializer: Expression | null = null; if (tn.skip(Token.EQUALS)) { initializer = this.parseExpression(tn); if (!initializer) return null; } - return Node.createParameter(identifier, type, initializer, multiple, Range.join(startRange, tn.range())); + return Node.createParameter(identifier, type, initializer, isRest, Range.join(startRange, tn.range())); } else this.error(DiagnosticCode.Identifier_expected, tn.range()); return null; @@ -573,14 +573,14 @@ export class Parser extends DiagnosticEmitter { parseFunction(tn: Tokenizer, modifiers: Modifier[] | null, decorators: Decorator[] | null): FunctionDeclaration | null { // at 'function': Identifier ('<' TypeParameters)? '(' Parameters (':' Type)? '{' Statement* '}' ';'? - const startPos: i32 = modifiers && modifiers.length ? modifiers[0].range.start : tn.tokenPos; + var startPos = modifiers && modifiers.length ? modifiers[0].range.start : tn.tokenPos; if (!tn.skip(Token.IDENTIFIER)) { this.error(DiagnosticCode.Identifier_expected, tn.range(tn.pos)); return null; } - const identifier: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); - let typeParameters: TypeParameter[] | null = null; + var identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var typeParameters: TypeParameter[] | null = null; if (tn.skip(Token.LESSTHAN)) { typeParameters = this.parseTypeParameters(tn); if (!typeParameters) @@ -591,20 +591,20 @@ export class Parser extends DiagnosticEmitter { this.error(DiagnosticCode._0_expected, tn.range(tn.pos), "("); return null; } - const parameters: Parameter[] | null = this.parseParameters(tn); + var parameters = this.parseParameters(tn); if (!parameters) return null; - let isSetter: bool = hasModifier(ModifierKind.SET, modifiers); + var isSetter = hasModifier(ModifierKind.SET, modifiers); if (isSetter) { if (parameters.length != 1) this.error(DiagnosticCode.A_set_accessor_must_have_exactly_one_parameter, identifier.range); // recoverable if (parameters.length && parameters[0].initializer) this.error(DiagnosticCode.A_set_accessor_parameter_cannot_have_an_initializer, identifier.range); // recoverable } - let isGetter: bool = hasModifier(ModifierKind.GET, modifiers); + var isGetter = hasModifier(ModifierKind.GET, modifiers); if (isGetter && parameters.length) this.error(DiagnosticCode.A_get_accessor_cannot_have_parameters, identifier.range); // recoverable - let returnType: TypeNode | null = null; + var returnType: TypeNode | null = null; if (tn.skip(Token.COLON)) { returnType = this.parseType(tn, isSetter); if (!returnType) @@ -612,36 +612,36 @@ export class Parser extends DiagnosticEmitter { } else if (!isSetter) { this.error(DiagnosticCode.Type_expected, tn.range(tn.pos)); // recoverable } - const isDeclare: bool = hasModifier(ModifierKind.DECLARE, modifiers); - let statements: Statement[] | null = null; + var isDeclare = hasModifier(ModifierKind.DECLARE, modifiers); + var statements: Statement[] | null = null; if (tn.skip(Token.OPENBRACE)) { statements = new Array(); if (isDeclare) this.error(DiagnosticCode.An_implementation_cannot_be_declared_in_ambient_contexts, tn.range()); // recoverable while (!tn.skip(Token.CLOSEBRACE)) { - const statement: Statement | null = this.parseStatement(tn); + var statement = this.parseStatement(tn); if (!statement) return null; statements.push(statement); } } else if (!isDeclare) this.error(DiagnosticCode.Function_implementation_is_missing_or_not_immediately_following_the_declaration, tn.range(tn.pos)); - const ret: FunctionDeclaration = Node.createFunction(identifier, typeParameters, parameters, returnType, statements, modifiers, decorators, tn.range(startPos, tn.pos)); + var ret = Node.createFunction(identifier, typeParameters, parameters, returnType, statements, modifiers, decorators, tn.range(startPos, tn.pos)); tn.skip(Token.SEMICOLON); return ret; } parseClass(tn: Tokenizer, modifiers: Modifier[] | null, decorators: Decorator[] | null): ClassDeclaration | null { // at 'class': Identifier ('<' TypeParameters)? ('extends' Type)? ('implements' Type (',' Type)*)? '{' ClassMember* '}' - const startPos: i32 = decorators && decorators.length + var startPos = decorators && decorators.length ? decorators[0].range.start : modifiers && modifiers.length ? modifiers[0].range.start : tn.tokenPos; if (tn.skip(Token.IDENTIFIER)) { - const identifier: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); - let typeParameters: TypeParameter[] | null; + var identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var typeParameters: TypeParameter[] | null; if (tn.skip(Token.LESSTHAN)) { typeParameters = this.parseTypeParameters(tn); @@ -650,17 +650,17 @@ export class Parser extends DiagnosticEmitter { } else typeParameters = []; - let extendsType: TypeNode | null = null; + var extendsType: TypeNode | null = null; if (tn.skip(Token.EXTENDS)) { extendsType = this.parseType(tn); if (!extendsType) return null; } - let implementsTypes: TypeNode[] = new Array(); + var implementsTypes = new Array(); if (tn.skip(Token.IMPLEMENTS)) { do { - const type: TypeNode | null = this.parseType(tn); + var type = this.parseType(tn); if (!type) return null; implementsTypes.push(type); @@ -668,11 +668,11 @@ export class Parser extends DiagnosticEmitter { } if (tn.skip(Token.OPENBRACE)) { - const members: DeclarationStatement[] = new Array(); + var members = new Array(); if (!tn.skip(Token.CLOSEBRACE)) { - const isDeclare = hasModifier(ModifierKind.DECLARE, modifiers); + var isDeclare = hasModifier(ModifierKind.DECLARE, modifiers); do { - const member: DeclarationStatement | null = this.parseClassMember(tn, isDeclare); + var member = this.parseClassMember(tn, isDeclare); if (!member) return null; members.push(member); @@ -688,18 +688,18 @@ export class Parser extends DiagnosticEmitter { parseClassMember(tn: Tokenizer, parentIsDeclare: bool): DeclarationStatement | null { // ('public' | 'private' | 'protected')? ('static' | 'abstract')? ('get' | 'set')? Identifier ... - const startRange: Range = tn.range(); + var startPos = tn.pos; - let decorators: Decorator[] = new Array(); + var decorators = new Array(); while (tn.skip(Token.AT)) { - const decorator: Decorator | null = this.parseDecorator(tn); + var decorator = this.parseDecorator(tn); if (!decorator) break; decorators.push(decorator); } - let modifiers: Modifier[] | null = null; + var modifiers: Modifier[] | null = null; if (tn.skip(Token.PUBLIC)) modifiers = addModifier(Node.createModifier(ModifierKind.PUBLIC, tn.range()), modifiers); @@ -716,18 +716,18 @@ export class Parser extends DiagnosticEmitter { if (tn.skip(Token.READONLY)) modifiers = addModifier(Node.createModifier(ModifierKind.READONLY, tn.range()), modifiers); - let isGetter: bool = false; - let isSetter: bool = false; + var isGetter = false; + var isSetter = false; if (isGetter = tn.skip(Token.GET)) modifiers = addModifier(Node.createModifier(ModifierKind.GET, tn.range()), modifiers); else if (isSetter = tn.skip(Token.SET)) // can't be both modifiers = addModifier(Node.createModifier(ModifierKind.SET, tn.range()), modifiers); if (tn.skip(Token.CONSTRUCTOR) || tn.skip(Token.IDENTIFIER)) { // order is important - const identifier: IdentifierExpression = tn.token == Token.CONSTRUCTOR + var identifier: IdentifierExpression = tn.token == Token.CONSTRUCTOR ? Node.createConstructor(tn.range()) : Node.createIdentifier(tn.readIdentifier(), tn.range()); - let typeParameters: TypeParameter[] | null; + var typeParameters: TypeParameter[] | null; if (tn.skip(Token.LESSTHAN)) { if (identifier.kind == NodeKind.CONSTRUCTOR) this.error(DiagnosticCode.Type_parameters_cannot_appear_on_a_constructor_declaration, tn.range()); // recoverable @@ -742,7 +742,7 @@ export class Parser extends DiagnosticEmitter { // method: '(' Parameters (':' Type)? '{' Statement* '}' ';'? if (tn.skip(Token.OPENPAREN)) { - let parameters = this.parseParameters(tn); + var parameters = this.parseParameters(tn); if (!parameters) return null; if (isGetter && parameters.length) @@ -753,7 +753,7 @@ export class Parser extends DiagnosticEmitter { if (parameters.length && parameters[0].initializer) this.error(DiagnosticCode.A_set_accessor_parameter_cannot_have_an_initializer, identifier.range); } - let returnType: TypeNode | null = null; + var returnType: TypeNode | null = null; if (tn.skip(Token.COLON)) { if (identifier.kind == NodeKind.CONSTRUCTOR) this.error(DiagnosticCode.Type_annotation_cannot_appear_on_a_constructor_declaration, tn.range()); @@ -764,13 +764,13 @@ export class Parser extends DiagnosticEmitter { return null; } else if (!isSetter && identifier.kind != NodeKind.CONSTRUCTOR) this.error(DiagnosticCode.Type_expected, tn.range()); // recoverable - let statements: Statement[] | null = null; + var statements: Statement[] | null = null; if (tn.skip(Token.OPENBRACE)) { if (parentIsDeclare) this.error(DiagnosticCode.An_implementation_cannot_be_declared_in_ambient_contexts, tn.range()); // recoverable statements = new Array(); while (!tn.skip(Token.CLOSEBRACE)) { - const statement: Statement | null = this.parseStatement(tn); + var statement = this.parseStatement(tn); if (!statement) return null; statements.push(statement); @@ -780,35 +780,35 @@ export class Parser extends DiagnosticEmitter { this.error(DiagnosticCode.Function_implementation_is_missing_or_not_immediately_following_the_declaration, tn.range()); // recoverable } - const ret: MethodDeclaration = Node.createMethod(identifier, typeParameters, parameters, returnType, statements, modifiers, decorators, Range.join(startRange, tn.range())); + var retMethod = Node.createMethod(identifier, typeParameters, parameters, returnType, statements, modifiers, decorators, tn.range(startPos, tn.pos)); tn.skip(Token.SEMICOLON); - return ret; + return retMethod; // field: (':' Type)? ('=' Expression)? ';'? } else { - let modifier: Modifier | null; + var 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; + var type: TypeNode | null = null; if (tn.skip(Token.COLON)) { type = this.parseType(tn); if (!type) return null; } else this.error(DiagnosticCode.Type_expected, tn.range()); // recoverable - let initializer: Expression | null = null; + var initializer: Expression | null = null; if (tn.skip(Token.EQUALS)) { initializer = this.parseExpression(tn); if (!initializer) return null; } - const ret: FieldDeclaration = Node.createField(identifier, type, initializer, modifiers, decorators, Range.join(startRange, tn.range())); + var retField = Node.createField(identifier, type, initializer, modifiers, decorators, tn.range(startPos, tn.pos)); tn.skip(Token.SEMICOLON); - return ret; + return retField; } } else this.error(DiagnosticCode.Identifier_expected, tn.range()); @@ -817,18 +817,18 @@ export class Parser extends DiagnosticEmitter { parseNamespace(tn: Tokenizer, modifiers: Modifier[] | null, decorators: Decorator[] | null): NamespaceDeclaration | null { // at 'namespace': Identifier '{' (Variable | Function)* '}' - const startRange: Range = modifiers && modifiers.length ? modifiers[0].range : tn.range(); + var startPos = modifiers && modifiers.length ? modifiers[0].range.start : tn.tokenPos; if (tn.skip(Token.IDENTIFIER)) { - const identifier: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); if (tn.skip(Token.OPENBRACE)) { - const members: Statement[] = new Array(); + var members = new Array(); while (!tn.skip(Token.CLOSEBRACE)) { - const member: Statement | null = this.parseTopLevelStatement(tn, true); + var member = this.parseTopLevelStatement(tn, true); if (!member) return null; members.push(member); } - const ret: NamespaceDeclaration = Node.createNamespace(identifier, members, modifiers, decorators, Range.join(startRange, tn.range())); + var ret = Node.createNamespace(identifier, members, modifiers, decorators, tn.range(startPos, tn.pos)); tn.skip(Token.SEMICOLON); return ret; } else @@ -840,12 +840,12 @@ export class Parser extends DiagnosticEmitter { parseExport(tn: Tokenizer, modifiers: Modifier[] | null): ExportStatement | null { // at 'export': '{' ExportMember (',' ExportMember)* }' ('from' StringLiteral)? ';'? - const startRange: Range = modifiers && modifiers.length ? modifiers[0].range : tn.range(); + var startPos = modifiers && modifiers.length ? modifiers[0].range.start : tn.tokenPos; if (tn.skip(Token.OPENBRACE)) { - const members: ExportMember[] = new Array(); + var members = new Array(); if (!tn.skip(Token.CLOSEBRACE)) { do { - const member: ExportMember | null = this.parseExportMember(tn); + var member = this.parseExportMember(tn); if (!member) return null; members.push(member); @@ -855,7 +855,7 @@ export class Parser extends DiagnosticEmitter { return null; } } - let path: StringLiteralExpression | null = null; + var path: StringLiteralExpression | null = null; if (tn.skip(Token.FROM)) { if (tn.skip(Token.STRINGLITERAL)) path = Node.createStringLiteral(tn.readString(), tn.range()); @@ -864,7 +864,7 @@ export class Parser extends DiagnosticEmitter { return null; } } - const ret: ExportStatement = Node.createExport(members, path, modifiers, Range.join(startRange, tn.range())); + var ret = Node.createExport(members, path, modifiers, tn.range(startPos, tn.pos)); if (ret.normalizedPath && !this.seenlog.has(ret.normalizedPath)) { this.backlog.push(ret.normalizedPath); this.seenlog.add(ret.normalizedPath); @@ -879,8 +879,8 @@ export class Parser extends DiagnosticEmitter { parseExportMember(tn: Tokenizer): ExportMember | null { // Identifier ('as' Identifier)? if (tn.skip(Token.IDENTIFIER)) { - const identifier: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); - let asIdentifier: IdentifierExpression | null = null; + var identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var asIdentifier: IdentifierExpression | null = null; if (tn.skip(Token.AS)) { if (tn.skip(Token.IDENTIFIER)) asIdentifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); @@ -897,14 +897,14 @@ export class Parser extends DiagnosticEmitter { parseImport(tn: Tokenizer): ImportStatement | null { // at 'import': ('{' (ImportMember (',' ImportMember)*)? '}' | '*' 'as' Identifier) 'from' StringLiteral ';'? - const startPos: i32 = tn.tokenPos; - let members: ImportDeclaration[] | null = null; - let namespaceName: IdentifierExpression | null = null; + var startPos = tn.tokenPos; + var members: ImportDeclaration[] | null = null; + var namespaceName: IdentifierExpression | null = null; if (tn.skip(Token.OPENBRACE)) { members = new Array(); if (!tn.skip(Token.CLOSEBRACE)) { do { - const member: ImportDeclaration | null = this.parseImportDeclaration(tn); + var member = this.parseImportDeclaration(tn); if (!member) return null; members.push(member); @@ -932,8 +932,8 @@ export class Parser extends DiagnosticEmitter { } if (tn.skip(Token.FROM)) { if (tn.skip(Token.STRINGLITERAL)) { - const path: StringLiteralExpression = Node.createStringLiteral(tn.readString(), tn.range()); - let ret: ImportStatement; + var path = Node.createStringLiteral(tn.readString(), tn.range()); + var ret: ImportStatement; if (members) { if (!namespaceName) ret = Node.createImport(members, path, tn.range(startPos, tn.pos)); @@ -963,8 +963,8 @@ export class Parser extends DiagnosticEmitter { parseImportDeclaration(tn: Tokenizer): ImportDeclaration | null { // Identifier ('as' Identifier)? if (tn.skip(Token.IDENTIFIER)) { - const identifier: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); - let asIdentifier: IdentifierExpression | null = null; + var identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var asIdentifier: IdentifierExpression | null = null; if (tn.skip(Token.AS)) { if (tn.skip(Token.IDENTIFIER)) asIdentifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); @@ -982,11 +982,11 @@ export class Parser extends DiagnosticEmitter { parseExportImport(tn: Tokenizer, startRange: Range): ExportImportStatement | null { // at 'export' 'import': Identifier ('=' Identifier)? ';'? if (tn.skip(Token.IDENTIFIER)) { - const asIdentifier: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var asIdentifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); if (tn.skip(Token.EQUALS)) { if (tn.skip(Token.IDENTIFIER)) { - const identifier: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); - const ret: ExportImportStatement = Node.createExportImport(identifier, asIdentifier, Range.join(startRange, tn.range())); + var identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var ret = Node.createExportImport(identifier, asIdentifier, Range.join(startRange, tn.range())); tn.skip(Token.SEMICOLON); return ret; } else @@ -1001,7 +1001,7 @@ export class Parser extends DiagnosticEmitter { parseStatement(tn: Tokenizer, topLevel: bool = false): Statement | null { // at previous token tn.mark(); - const token: Token = tn.next(); + var token = tn.next(); switch (token) { case Token.BREAK: @@ -1060,56 +1060,56 @@ export class Parser extends DiagnosticEmitter { parseBlockStatement(tn: Tokenizer, topLevel: bool): BlockStatement | null { // at '{': Statement* '}' ';'? - const startPos: i32 = tn.tokenPos; - const statements: Statement[] = new Array(); + var startPos = tn.tokenPos; + var statements = new Array(); while (!tn.skip(Token.CLOSEBRACE)) { - const statement: Statement | null = this.parseStatement(tn, topLevel); + var statement = this.parseStatement(tn, topLevel); if (!statement) return null; statements.push(statement); } - const ret: BlockStatement = Node.createBlock(statements, tn.range(startPos, tn.pos)); + var ret = Node.createBlock(statements, tn.range(startPos, tn.pos)); tn.skip(Token.SEMICOLON); return ret; } parseBreak(tn: Tokenizer): BreakStatement | null { // at 'break': Identifier? ';'? - let identifier: IdentifierExpression | null = null; + var identifier: IdentifierExpression | null = null; if (tn.peek(true) == Token.IDENTIFIER && !tn.nextTokenOnNewLine) { tn.next(true); identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); } - const ret: ContinueStatement = Node.createBreak(identifier, tn.range()); + var ret = Node.createBreak(identifier, tn.range()); tn.skip(Token.SEMICOLON); return ret; } parseContinue(tn: Tokenizer): ContinueStatement | null { // at 'continue': Identifier? ';'? - let identifier: IdentifierExpression | null = null; + var identifier: IdentifierExpression | null = null; if (tn.peek(true) == Token.IDENTIFIER && !tn.nextTokenOnNewLine) { tn.next(true); identifier = Node.createIdentifier(tn.readIdentifier(), tn.range()); } - const ret: ContinueStatement = Node.createContinue(identifier, tn.range()); + var ret = Node.createContinue(identifier, tn.range()); tn.skip(Token.SEMICOLON); return ret; } parseDoStatement(tn: Tokenizer): DoStatement | null { // at 'do': Statement 'while' '(' Expression ')' ';'? - const startPos: i32 = tn.tokenPos; - const statement: Statement | null = this.parseStatement(tn); + var startPos = tn.tokenPos; + var statement = this.parseStatement(tn); if (!statement) return null; if (tn.skip(Token.WHILE)) { if (tn.skip(Token.OPENPAREN)) { - const condition: Expression | null = this.parseExpression(tn); + var condition = this.parseExpression(tn); if (!condition) return null; if (tn.skip(Token.CLOSEPAREN)) { - const ret: DoStatement = Node.createDo(statement, condition, tn.range(startPos, tn.pos)); + var ret = Node.createDo(statement, condition, tn.range(startPos, tn.pos)); tn.skip(Token.SEMICOLON); return ret; } @@ -1123,19 +1123,19 @@ export class Parser extends DiagnosticEmitter { parseExpressionStatement(tn: Tokenizer): ExpressionStatement | null { // at previous token - const expr: Expression | null = this.parseExpression(tn); + var expr = this.parseExpression(tn); if (!expr) return null; - const ret: ExpressionStatement = Node.createExpression(expr); + var ret = Node.createExpression(expr); tn.skip(Token.SEMICOLON); return ret; } parseForStatement(tn: Tokenizer): ForStatement | null { // at 'for': '(' Statement? Expression? ';' Expression? ')' Statement - const startPos: i32 = tn.tokenPos; + var startPos = tn.tokenPos; if (tn.skip(Token.OPENPAREN)) { - let initializer: Statement | null = null; + var initializer: Statement | null = null; if (tn.skip(Token.LET) || tn.skip(Token.CONST) || tn.skip(Token.VAR)) { initializer = this.parseVariable(tn, null, null); } else if (!tn.skip(Token.SEMICOLON)) { @@ -1144,14 +1144,14 @@ export class Parser extends DiagnosticEmitter { return null; } if (tn.token == Token.SEMICOLON) { - let condition: ExpressionStatement | null = null; + var condition: ExpressionStatement | null = null; if (!tn.skip(Token.SEMICOLON)) { condition = this.parseExpressionStatement(tn); if (!condition) return null; } if (tn.token == Token.SEMICOLON) { - let incrementor: Expression | null = null; + var incrementor: Expression | null = null; if (!tn.skip(Token.CLOSEPAREN)) { incrementor = this.parseExpression(tn); if (!incrementor) @@ -1161,7 +1161,7 @@ export class Parser extends DiagnosticEmitter { return null; } } - const statement: Statement | null = this.parseStatement(tn); + var statement = this.parseStatement(tn); if (!statement) return null; return Node.createFor(initializer, condition ? condition.expression : null, incrementor, statement, tn.range(startPos, tn.pos)); @@ -1176,22 +1176,22 @@ export class Parser extends DiagnosticEmitter { parseIfStatement(tn: Tokenizer): IfStatement | null { // at 'if': '(' Expression ')' Statement ('else' Statement)? - const startRange: Range = tn.range(); + var startPos = tn.tokenPos; if (tn.skip(Token.OPENPAREN)) { - const condition: Expression | null = this.parseExpression(tn); + var condition = this.parseExpression(tn); if (!condition) return null; if (tn.skip(Token.CLOSEPAREN)) { - const statement: Statement | null = this.parseStatement(tn); + var statement = this.parseStatement(tn); if (!statement) return null; - let elseStatement: Statement | null = null; + var elseStatement: Statement | null = null; if (tn.skip(Token.ELSE)) { elseStatement = this.parseStatement(tn); if (!elseStatement) return null; } - return Node.createIf(condition, statement, elseStatement, Range.join(startRange, tn.range())); + return Node.createIf(condition, statement, elseStatement, tn.range(startPos, tn.pos)); } this.error(DiagnosticCode._0_expected, tn.range(), ")"); } else @@ -1201,21 +1201,21 @@ export class Parser extends DiagnosticEmitter { parseSwitchStatement(tn: Tokenizer): SwitchStatement | null { // at 'switch': '(' Expression ')' '{' SwitchCase* '}' ';'? - const startPos: i32 = tn.tokenPos; + var startPos = tn.tokenPos; if (tn.skip(Token.OPENPAREN)) { - const condition: Expression | null = this.parseExpression(tn); + var condition = this.parseExpression(tn); if (!condition) return null; if (tn.skip(Token.CLOSEPAREN)) { if (tn.skip(Token.OPENBRACE)) { - const cases: SwitchCase[] = []; + var cases = new Array(); while (!tn.skip(Token.CLOSEBRACE)) { - const case_: SwitchCase | null = this.parseSwitchCase(tn); + var case_ = this.parseSwitchCase(tn); if (!case_) return null; cases.push(case_); } - const ret: SwitchStatement = Node.createSwitch(condition, cases, tn.range(startPos, tn.pos)); + var ret = Node.createSwitch(condition, cases, tn.range(startPos, tn.pos)); tn.skip(Token.SEMICOLON); return ret; } else @@ -1228,17 +1228,19 @@ export class Parser extends DiagnosticEmitter { } parseSwitchCase(tn: Tokenizer): SwitchCase | null { - const startPos: i32 = tn.tokenPos; + var startPos = tn.tokenPos; + var statements: Statement[], + statement: Statement | null; // 'case' Expression ':' Statement* if (tn.skip(Token.CASE)) { - const label: Expression | null = this.parseExpression(tn); + var label = this.parseExpression(tn); if (!label) return null; if (tn.skip(Token.COLON)) { - const statements: Statement[] = new Array(); + statements = new Array(); while (tn.peek() != Token.CASE && tn.nextToken != Token.DEFAULT && tn.nextToken != Token.CLOSEBRACE) { - const statement: Statement | null = this.parseStatement(tn); + statement = this.parseStatement(tn); if (!statement) return null; statements.push(statement); @@ -1250,12 +1252,12 @@ export class Parser extends DiagnosticEmitter { // 'default' ':' Statement* } else if (tn.skip(Token.DEFAULT)) { if (tn.skip(Token.COLON)) { - const statements: Statement[] = new Array(); + statements = new Array(); while (tn.peek() != Token.CASE && tn.nextToken != Token.DEFAULT && tn.nextToken != Token.CLOSEBRACE) { - const statement: Statement | null = this.parseStatement(tn); + statement = this.parseStatement(tn); if (!statement) return null; - statements.push(statement); + statements.push(statement); } return Node.createSwitchCase(null, statements, tn.range(startPos, tn.pos)); } else @@ -1269,29 +1271,30 @@ export class Parser extends DiagnosticEmitter { parseThrowStatement(tn: Tokenizer): ThrowStatement | null { // at 'throw': Expression ';'? - const startPos: i32 = tn.tokenPos; - const expression: Expression | null = this.parseExpression(tn); + var startPos = tn.tokenPos; + var expression = this.parseExpression(tn); if (!expression) return null; - const ret: ThrowStatement = Node.createThrow(expression, tn.range(startPos, tn.pos)); + var ret = Node.createThrow(expression, tn.range(startPos, tn.pos)); tn.skip(Token.SEMICOLON); return ret; } parseTryStatement(tn: Tokenizer): TryStatement | null { // at 'try': '{' Statement* '}' ('catch' '(' VariableMember ')' '{' Statement* '}')? ('finally' '{' Statement* '}'? ';'? - const startRange: Range = tn.range(); + var startPos = tn.tokenPos; + var stmt: Statement | null; if (tn.skip(Token.OPENBRACE)) { - const statements: Statement[] = new Array(); + var statements = new Array(); while (!tn.skip(Token.CLOSEBRACE)) { - const stmt: Statement | null = this.parseStatement(tn); + stmt = this.parseStatement(tn); if (!stmt) return null; statements.push(stmt); } - let catchVariable: IdentifierExpression | null = null; - let catchStatements: Statement[] | null = null; - let finallyStatements: Statement[] | null = null; + var catchVariable: IdentifierExpression | null = null; + var catchStatements: Statement[] | null = null; + var finallyStatements: Statement[] | null = null; if (tn.skip(Token.CATCH)) { if (!tn.skip(Token.OPENPAREN)) { this.error(DiagnosticCode._0_expected, tn.range(), "("); @@ -1312,7 +1315,7 @@ export class Parser extends DiagnosticEmitter { } catchStatements = new Array(); while (!tn.skip(Token.CLOSEBRACE)) { - const stmt: Statement | null = this.parseStatement(tn); + var stmt = this.parseStatement(tn); if (!stmt) return null; catchStatements.push(stmt); @@ -1325,7 +1328,7 @@ export class Parser extends DiagnosticEmitter { } finallyStatements = new Array(); while (!tn.skip(Token.CLOSEBRACE)) { - const stmt: Statement | null = this.parseStatement(tn); + stmt = this.parseStatement(tn); if (!stmt) return null; finallyStatements.push(stmt); @@ -1335,7 +1338,7 @@ export class Parser extends DiagnosticEmitter { this.error(DiagnosticCode._0_expected, tn.range(), "catch"); return null; } - const ret: TryStatement = Node.createTry(statements, catchVariable, catchStatements, finallyStatements, Range.join(startRange, tn.range())); + var ret = Node.createTry(statements, catchVariable, catchStatements, finallyStatements, tn.range(startPos, tn.pos)); tn.skip(Token.SEMICOLON); return ret; } else @@ -1345,16 +1348,16 @@ export class Parser extends DiagnosticEmitter { parseTypeDeclaration(tn: Tokenizer, modifiers: Modifier[] | null = null, decorators: Decorator[] | null = null): TypeDeclaration | null { // at 'type': Identifier '=' Type ';'? - const startPos: i32 = decorators && decorators.length ? decorators[0].range.start - : modifiers && modifiers.length ? modifiers[0].range.start - : tn.tokenPos; + var startPos = decorators && decorators.length ? decorators[0].range.start + : modifiers && modifiers.length ? modifiers[0].range.start + : tn.tokenPos; if (tn.skip(Token.IDENTIFIER)) { - const name: IdentifierExpression = Node.createIdentifier(tn.readIdentifier(), tn.range()); + var name = Node.createIdentifier(tn.readIdentifier(), tn.range()); if (tn.skip(Token.EQUALS)) { - const type: TypeNode | null = this.parseType(tn); + var type = this.parseType(tn); if (!type) return null; - const ret: TypeDeclaration = Node.createTypeDeclaration(name, type, modifiers, decorators, tn.range(startPos, tn.pos)); + var ret = Node.createTypeDeclaration(name, type, modifiers, decorators, tn.range(startPos, tn.pos)); tn.skip(Token.SEMICOLON); return ret; } else @@ -1366,16 +1369,16 @@ export class Parser extends DiagnosticEmitter { parseWhileStatement(tn: Tokenizer): WhileStatement | null { // at 'while': '(' Expression ')' Statement ';'? - const startRange: Range = tn.range(); + var startPos = tn.tokenPos; if (tn.skip(Token.OPENPAREN)) { - const expression: Expression | null = this.parseExpression(tn); + var expression = this.parseExpression(tn); if (!expression) return null; if (tn.skip(Token.CLOSEPAREN)) { - const statement: Statement | null = this.parseStatement(tn); + var statement = this.parseStatement(tn); if (!statement) return null; - const ret: WhileStatement = Node.createWhile(expression, statement, Range.join(startRange, tn.range())); + var ret = Node.createWhile(expression, statement, tn.range(startPos, tn.pos)); tn.skip(Token.SEMICOLON); return ret; } else @@ -1389,9 +1392,9 @@ export class Parser extends DiagnosticEmitter { // see: http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm#climbing parseExpressionStart(tn: Tokenizer): Expression | null { - const token: Token = tn.next(true); - const startPos: i32 = tn.tokenPos; - let expr: Expression | null = null; + var token = tn.next(true); + var startPos = tn.tokenPos; + var expr: Expression | null = null; if (token == Token.NULL) return Node.createNull(tn.range()); @@ -1400,9 +1403,9 @@ export class Parser extends DiagnosticEmitter { if (token == Token.FALSE) return Node.createFalse(tn.range()); - let p: Precedence = determinePrecedenceStart(token); + var p = determinePrecedenceStart(token); if (p != Precedence.INVALID) { - let operand: Expression | null; + var operand: Expression | null; // TODO: SpreadExpression, YieldExpression (currently become unsupported UnaryPrefixExpressions) @@ -1441,7 +1444,7 @@ export class Parser extends DiagnosticEmitter { // ArrayLiteralExpression case Token.OPENBRACKET: { - const elementExpressions: (Expression | null)[] = new Array(); + var elementExpressions = new Array(); if (!tn.skip(Token.CLOSEBRACKET)) { do { if (tn.peek() == Token.COMMA || tn.peek() == Token.CLOSEBRACKET) @@ -1463,7 +1466,7 @@ export class Parser extends DiagnosticEmitter { // AssertionExpression (unary prefix) case Token.LESSTHAN: { - const toType: TypeNode | null = this.parseType(tn); + var toType = this.parseType(tn); if (!toType) return null; if (!tn.skip(Token.GREATERTHAN)) { @@ -1517,9 +1520,9 @@ export class Parser extends DiagnosticEmitter { if (!tn.skip(Token.LESSTHAN)) return null; - const typeArguments: TypeNode[] = []; + var typeArguments = new Array(); do { - const type: TypeNode | null = this.parseType(tn, true, true); + var type = this.parseType(tn, true, true); if (!type) { tn.reset(); return null; @@ -1535,10 +1538,10 @@ export class Parser extends DiagnosticEmitter { parseArguments(tn: Tokenizer): Expression[] | null { // at '(': (Expression (',' Expression)*)? ')' - const args: Expression[] = new Array(); + var args = new Array(); if (!tn.skip(Token.CLOSEPAREN)) { do { - const expr: Expression | null = this.parseExpression(tn, Precedence.COMMA + 1); + var expr = this.parseExpression(tn, Precedence.COMMA + 1); if (!expr) return null; args.push(expr); @@ -1552,32 +1555,32 @@ export class Parser extends DiagnosticEmitter { } parseExpression(tn: Tokenizer, precedence: Precedence = 0): Expression | null { - let expr: Expression | null = this.parseExpressionStart(tn); + var expr = this.parseExpressionStart(tn); if (!expr) return null; - const startPos: i32 = expr.range.start; + var startPos = expr.range.start; // CallExpression - const typeArguments: TypeNode[] | null = this.tryParseTypeArgumentsBeforeArguments(tn); + var typeArguments = this.tryParseTypeArgumentsBeforeArguments(tn); // there might be better ways to distinguish a LESSTHAN from a CALL with type arguments if (typeArguments || tn.skip(Token.OPENPAREN)) { - const args: Expression[] | null = this.parseArguments(tn); + var args = this.parseArguments(tn); if (!args) return null; expr = Node.createCall(expr, (typeArguments ? typeArguments : []), args, tn.range(startPos, tn.pos)); } - let token: Token; - let next: Expression | null = null; - let nextPrecedence: Precedence; + var token: Token; + var next: Expression | null = null; + var nextPrecedence: Precedence; while ((nextPrecedence = determinePrecedence(token = tn.peek())) >= precedence) { // precedence climbing tn.next(); // AssertionExpression if (token == Token.AS) { - const toType: TypeNode | null = this.parseType(tn); + var toType = this.parseType(tn); if (!toType) return null; expr = Node.createAssertion(AssertionKind.AS, expr, toType, tn.range(startPos, tn.pos)); @@ -1603,11 +1606,11 @@ export class Parser extends DiagnosticEmitter { // TernaryExpression } else if (token == Token.QUESTION) { - const ifThen: Expression | null = this.parseExpression(tn); + var ifThen = this.parseExpression(tn); if (!ifThen) return null; if (tn.skip(Token.COLON)) { - const ifElse: Expression | null = this.parseExpression(tn); + var ifElse = this.parseExpression(tn); if (!ifElse) return null; expr = Node.createTernary(expr, ifThen, ifElse, tn.range(startPos, tn.pos)); @@ -1626,7 +1629,7 @@ export class Parser extends DiagnosticEmitter { if (next.kind == NodeKind.IDENTIFIER) { expr = Node.createPropertyAccess(expr, next, tn.range(startPos, tn.pos)); } else if (next.kind == NodeKind.CALL) { // amend - let propertyCall: CallExpression = next; + var propertyCall = next; if (propertyCall.expression.kind == NodeKind.IDENTIFIER) { propertyCall.expression = Node.createPropertyAccess(expr, propertyCall.expression, tn.range(startPos, tn.pos)); } else diff --git a/src/program.ts b/src/program.ts index 24ff1e00..ce29b1d5 100644 --- a/src/program.ts +++ b/src/program.ts @@ -84,7 +84,7 @@ class QueuedImport { declaration: ImportDeclaration; } -const noTypesYet: Map = new Map(); +const noTypesYet = new Map(); /** Represents an AssemblyScript program. */ export class Program extends DiagnosticEmitter { @@ -135,15 +135,15 @@ export class Program extends DiagnosticEmitter { initializeBuiltins(this); - const queuedExports = new Map(); - const queuedImports = new Array(); + var queuedExports = new Map(); + var queuedImports = new Array(); // build initial lookup maps of internal names to declarations - for (let i = 0, k = this.sources.length; i < k; ++i) { - const source = this.sources[i]; - const statements = source.statements; - for (let j = 0, l = statements.length; j < l; ++j) { - const statement = statements[j]; + for (var i = 0, k = this.sources.length; i < k; ++i) { + var source = this.sources[i]; + var statements = source.statements; + for (var j = 0, l = statements.length; j < l; ++j) { + var statement = statements[j]; switch (statement.kind) { case NodeKind.CLASS: @@ -185,24 +185,24 @@ export class Program extends DiagnosticEmitter { } } - let element: Element | null; + var element: Element | null; // queued imports should be resolvable now through traversing exports and queued exports - for (let j = 0; j < queuedImports.length;) { - const queuedImport = queuedImports[j]; + for (i = 0; i < queuedImports.length;) { + var queuedImport = queuedImports[i]; element = this.tryResolveImport(queuedImport.referencedName, queuedExports); if (element) { this.elements.set(queuedImport.internalName, element); - queuedImports.splice(j, 1); + queuedImports.splice(i, 1); } else { this.error(DiagnosticCode.Module_0_has_no_exported_member_1, queuedImport.declaration.range, (queuedImport.declaration.parent).path.value, queuedImport.declaration.externalIdentifier.name); - ++j; + ++i; } } // queued exports should be resolvable now that imports are finalized - for (let [exportName, queuedExport] of queuedExports) { - let currentExport: QueuedExport | null = queuedExport; + for (var [exportName, queuedExport] of queuedExports) { + var currentExport: QueuedExport | null = queuedExport; // nullable below do { if (currentExport.isReExport) { element = this.exports.get(currentExport.referencedName); @@ -227,12 +227,12 @@ export class Program extends DiagnosticEmitter { /** Tries to resolve an import by traversing exports and queued exports. */ private tryResolveImport(referencedName: string, queuedExports: Map): Element | null { - let element: Element | null; + var element: Element | null; do { element = this.exports.get(referencedName); if (element) return element; - const queuedExport = queuedExports.get(referencedName); + var queuedExport = queuedExports.get(referencedName); if (!queuedExport) return null; if (queuedExport.isReExport) { @@ -244,12 +244,12 @@ export class Program extends DiagnosticEmitter { } private initializeClass(declaration: ClassDeclaration, namespace: Element | null = null): void { - const internalName = declaration.internalName; + var internalName = declaration.internalName; if (this.elements.has(internalName)) { this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName); return; } - const prototype = new ClassPrototype(this, declaration.name.name, internalName, declaration); + var prototype = new ClassPrototype(this, declaration.name.name, internalName, declaration); prototype.namespace = namespace; this.elements.set(internalName, prototype); @@ -282,8 +282,8 @@ export class Program extends DiagnosticEmitter { } // initialize members - const memberDeclarations = declaration.members; - for (let i = 0, k = memberDeclarations.length; i < k; ++i) { + var memberDeclarations = declaration.members; + for (var i = 0, k = memberDeclarations.length; i < k; ++i) { switch (memberDeclarations[i].kind) { case NodeKind.FIELD: @@ -301,8 +301,8 @@ export class Program extends DiagnosticEmitter { } private initializeField(declaration: FieldDeclaration, classPrototype: ClassPrototype): void { - const name = declaration.name.name; - const internalName = declaration.internalName; + var name = declaration.name.name; + var internalName = declaration.internalName; // static fields become global variables if (hasModifier(ModifierKind.STATIC, declaration.modifiers)) { @@ -317,7 +317,7 @@ export class Program extends DiagnosticEmitter { } } else classPrototype.members = new Map(); - const staticField = new Global(this, name, internalName, declaration, null); + var staticField = new Global(this, name, internalName, declaration, null); classPrototype.members.set(name, staticField); this.elements.set(internalName, staticField); @@ -330,19 +330,19 @@ export class Program extends DiagnosticEmitter { } } else classPrototype.instanceMembers = new Map(); - const instanceField = new FieldPrototype(classPrototype, name, internalName, declaration); + var instanceField = new FieldPrototype(classPrototype, name, internalName, declaration); classPrototype.instanceMembers.set(name, instanceField); } } private initializeMethod(declaration: MethodDeclaration, classPrototype: ClassPrototype): void { - let isGetter = false; + var isGetter = false; if ((isGetter = hasModifier(ModifierKind.GET, declaration.modifiers)) || hasModifier(ModifierKind.SET, declaration.modifiers)) { this.initializeAccessor(declaration, classPrototype, isGetter); return; } - const name = declaration.name.name; - const internalName = declaration.internalName; + var name = declaration.name.name; + var internalName = declaration.internalName; // static methods become global functions if (hasModifier(ModifierKind.STATIC, declaration.modifiers)) { @@ -358,7 +358,7 @@ export class Program extends DiagnosticEmitter { } } else classPrototype.members = new Map(); - const staticPrototype: FunctionPrototype = new FunctionPrototype(this, name, internalName, declaration, null); + var staticPrototype = new FunctionPrototype(this, name, internalName, declaration, null); classPrototype.members.set(name, staticPrototype); this.elements.set(internalName, staticPrototype); @@ -371,16 +371,16 @@ export class Program extends DiagnosticEmitter { } } else classPrototype.instanceMembers = new Map(); - const instancePrototype: FunctionPrototype = new FunctionPrototype(this, name, internalName, declaration, classPrototype); + var instancePrototype = new FunctionPrototype(this, name, internalName, declaration, classPrototype); classPrototype.instanceMembers.set(name, instancePrototype); } } private initializeAccessor(declaration: MethodDeclaration, classPrototype: ClassPrototype, isGetter: bool): void { - const propertyName = declaration.name.name; - const internalPropertyName = declaration.internalName; + var propertyName = declaration.name.name; + var internalPropertyName = declaration.internalName; - let propertyElement = this.elements.get(internalPropertyName); + var propertyElement = this.elements.get(internalPropertyName); if (propertyElement) { if (propertyElement.kind != ElementKind.PROPERTY || (isGetter ? (propertyElement).getterPrototype : (propertyElement).setterPrototype)) { this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalPropertyName); @@ -389,16 +389,16 @@ export class Program extends DiagnosticEmitter { } else propertyElement = new Property(this, propertyName, internalPropertyName, classPrototype); - let name = (isGetter ? GETTER_PREFIX : SETTER_PREFIX) + propertyName; + var name = (isGetter ? GETTER_PREFIX : SETTER_PREFIX) + propertyName; // static accessors become global functions if (hasModifier(ModifierKind.STATIC, declaration.modifiers)) { - const internalStaticName = classPrototype.internalName + STATIC_DELIMITER + name; + var internalStaticName = classPrototype.internalName + STATIC_DELIMITER + name; if (this.elements.has(internalStaticName)) { this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalStaticName); return; } - const staticPrototype = new FunctionPrototype(this, name, internalStaticName, declaration, null); + var staticPrototype = new FunctionPrototype(this, name, internalStaticName, declaration, null); if (isGetter) (propertyElement).getterPrototype = staticPrototype; else @@ -410,7 +410,7 @@ export class Program extends DiagnosticEmitter { // instance accessors are remembered until resolved } else { - const internalInstanceName = classPrototype.internalName + INSTANCE_DELIMITER + name; + var internalInstanceName = classPrototype.internalName + INSTANCE_DELIMITER + name; if (classPrototype.instanceMembers) { if (classPrototype.instanceMembers.has(name)) { this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, declaration.internalName); @@ -418,7 +418,7 @@ export class Program extends DiagnosticEmitter { } } else classPrototype.instanceMembers = new Map(); - const instancePrototype = new FunctionPrototype(this, name, internalInstanceName, declaration, classPrototype); + var instancePrototype = new FunctionPrototype(this, name, internalInstanceName, declaration, classPrototype); if (isGetter) (propertyElement).getterPrototype = instancePrototype; else @@ -428,12 +428,12 @@ export class Program extends DiagnosticEmitter { } private initializeEnum(declaration: EnumDeclaration, namespace: Element | null = null): void { - const internalName = declaration.internalName; + var internalName = declaration.internalName; if (this.elements.has(internalName)) { this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName); return; } - const enm = new Enum(this, declaration.name.name, internalName, declaration); + var enm = new Enum(this, declaration.name.name, internalName, declaration); enm.namespace = namespace; this.elements.set(internalName, enm); @@ -454,14 +454,14 @@ export class Program extends DiagnosticEmitter { this.exports.set(internalName, enm); } - const values = declaration.values; - for (let i: i32 = 0, k: i32 = values.length; i < k; ++i) + var values = declaration.values; + for (var i = 0, k = values.length; i < k; ++i) this.initializeEnumValue(values[i], enm); } private initializeEnumValue(declaration: EnumValueDeclaration, enm: Enum): void { - const name = declaration.name.name; - const internalName = declaration.internalName; + var name = declaration.name.name; + var internalName = declaration.internalName; if (enm.members) { if (enm.members.has(name)) { this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName); @@ -469,23 +469,24 @@ export class Program extends DiagnosticEmitter { } } else enm.members = new Map(); - const value = new EnumValue(enm, this, name, internalName, declaration); + var value = new EnumValue(enm, this, name, internalName, declaration); enm.members.set(name, value); } private initializeExports(statement: ExportStatement, queuedExports: Map): void { - const members = statement.members; - for (let i = 0, k = members.length; i < k; ++i) + var members = statement.members; + for (var i = 0, k = members.length; i < k; ++i) this.initializeExport(members[i], statement.internalPath, queuedExports); } private initializeExport(member: ExportMember, internalPath: string | null, queuedExports: Map): void { - const externalName: string = member.range.source.internalPath + PATH_DELIMITER + member.externalIdentifier.name; + var externalName = member.range.source.internalPath + PATH_DELIMITER + member.externalIdentifier.name; if (this.exports.has(externalName)) { this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, member.externalIdentifier.range, externalName); return; } - let referencedName: string; + var referencedName: string; + var queuedExport: QueuedExport | null; // export local element if (internalPath == null) { @@ -502,7 +503,7 @@ export class Program extends DiagnosticEmitter { this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, member.externalIdentifier.range, externalName); return; } - const queuedExport = new QueuedExport(); + queuedExport = new QueuedExport(); queuedExport.isReExport = false; queuedExport.referencedName = referencedName; // -> internal name queuedExport.member = member; @@ -519,8 +520,7 @@ export class Program extends DiagnosticEmitter { } // walk already known queued exports - const seen = new Set(); - let queuedExport: QueuedExport | null; + var seen = new Set(); while (queuedExport = queuedExports.get(referencedName)) { if (queuedExport.isReExport) { if (this.exports.has(queuedExport.referencedName)) { @@ -545,21 +545,21 @@ export class Program extends DiagnosticEmitter { this.error(DiagnosticCode.Export_declaration_conflicts_with_exported_declaration_of_0, member.externalIdentifier.range, externalName); return; } - const queuedReExport = new QueuedExport(); - queuedReExport.isReExport = true; - queuedReExport.referencedName = referencedName; // -> export name - queuedReExport.member = member; - queuedExports.set(externalName, queuedReExport); + queuedExport = new QueuedExport(); + queuedExport.isReExport = true; + queuedExport.referencedName = referencedName; // -> export name + queuedExport.member = member; + queuedExports.set(externalName, queuedExport); } } private initializeFunction(declaration: FunctionDeclaration, namespace: Element | null = null): void { - const internalName = declaration.internalName; + var internalName = declaration.internalName; if (this.elements.has(internalName)) { this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName); return; } - const prototype = new FunctionPrototype(this, declaration.name.name, internalName, declaration, null); + var prototype = new FunctionPrototype(this, declaration.name.name, internalName, declaration, null); prototype.namespace = namespace; this.elements.set(internalName, prototype); @@ -589,12 +589,12 @@ export class Program extends DiagnosticEmitter { } private initializeImports(statement: ImportStatement, queuedExports: Map, queuedImports: QueuedImport[]): void { - const declarations = statement.declarations; + var declarations = statement.declarations; if (declarations) { - for (let i = 0, k = declarations.length; i < k; ++i) + for (var i = 0, k = declarations.length; i < k; ++i) this.initializeImport(declarations[i], statement.internalPath, queuedExports, queuedImports); } else if (statement.namespaceName) { - const internalName = statement.range.source.internalPath + "/" + statement.namespaceName.name; + var internalName = statement.range.source.internalPath + "/" + statement.namespaceName.name; if (this.elements.has(internalName)) { this.error(DiagnosticCode.Duplicate_identifier_0, statement.namespaceName.range, internalName); return; @@ -605,13 +605,13 @@ export class Program extends DiagnosticEmitter { } private initializeImport(declaration: ImportDeclaration, internalPath: string, queuedExports: Map, queuedImports: QueuedImport[]): void { - const internalName = declaration.internalName; + var internalName = declaration.internalName; if (this.elements.has(internalName)) { this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName); return; } - let referencedName = internalPath + PATH_DELIMITER + declaration.externalIdentifier.name; + var referencedName = internalPath + PATH_DELIMITER + declaration.externalIdentifier.name; // resolve right away if the export exists if (this.exports.has(referencedName)) { @@ -620,8 +620,8 @@ export class Program extends DiagnosticEmitter { } // walk already known queued exports - const seen = new Set(); - let queuedExport: QueuedExport | null; + var seen = new Set(); + var queuedExport: QueuedExport | null; while (queuedExport = queuedExports.get(referencedName)) { if (queuedExport.isReExport) { if (this.exports.has(queuedExport.referencedName)) { @@ -642,7 +642,7 @@ export class Program extends DiagnosticEmitter { } // otherwise queue it - const queuedImport = new QueuedImport(); + var queuedImport = new QueuedImport(); queuedImport.internalName = internalName; queuedImport.referencedName = referencedName; queuedImport.declaration = declaration; @@ -650,12 +650,12 @@ export class Program extends DiagnosticEmitter { } private initializeInterface(declaration: InterfaceDeclaration, namespace: Element | null = null): void { - const internalName = declaration.internalName; + var internalName = declaration.internalName; if (this.elements.has(internalName)) { this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName); return; } - const prototype = new InterfacePrototype(this, declaration.name.name, internalName, declaration); + var prototype = new InterfacePrototype(this, declaration.name.name, internalName, declaration); prototype.namespace = namespace; this.elements.set(internalName, prototype); @@ -676,8 +676,8 @@ export class Program extends DiagnosticEmitter { this.exports.set(internalName, prototype); } - const memberDeclarations = declaration.members; - for (let i = 0, k = memberDeclarations.length; i < k; ++i) { + var memberDeclarations = declaration.members; + for (var i = 0, k = memberDeclarations.length; i < k; ++i) { switch (memberDeclarations[i].kind) { case NodeKind.FIELD: @@ -695,9 +695,9 @@ export class Program extends DiagnosticEmitter { } private initializeNamespace(declaration: NamespaceDeclaration, parentNamespace: Element | null = null): void { - const internalName = declaration.internalName; + var internalName = declaration.internalName; - let namespace = this.elements.get(internalName); + var namespace = this.elements.get(internalName); if (!namespace) { namespace = new Namespace(this, declaration.name.name, internalName, declaration); namespace.namespace = parentNamespace; @@ -721,8 +721,8 @@ export class Program extends DiagnosticEmitter { this.exports.set(internalName, namespace); } - const members = declaration.members; - for (let i = 0, k = members.length; i < k; ++i) { + var members = declaration.members; + for (var i = 0, k = members.length; i < k; ++i) { switch (members[i].kind) { case NodeKind.CLASS: @@ -762,7 +762,7 @@ export class Program extends DiagnosticEmitter { private initializeType(declaration: TypeDeclaration, namespace: Element | null = null): void { // type aliases are program globals // TODO: what about namespaced types? - const name = declaration.name.name; + var name = declaration.name.name; if (this.types.has(name) || this.typeAliases.has(name)) { this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, name); return; @@ -771,16 +771,16 @@ export class Program extends DiagnosticEmitter { } private initializeVariables(statement: VariableStatement, namespace: Element | null = null): void { - const declarations = statement.declarations; - for (let i = 0, k = declarations.length; i < k; ++i) { - const declaration = declarations[i]; - const internalName = declaration.internalName; + var declarations = statement.declarations; + for (var i = 0, k = declarations.length; i < k; ++i) { + var declaration = declarations[i]; + var internalName = declaration.internalName; if (this.elements.has(internalName)) { this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName); continue; } - const global = new Global(this, declaration.name.name, internalName, declaration, null); + var global = new Global(this, declaration.name.name, internalName, declaration, null); global.namespace = namespace; this.elements.set(internalName, global); @@ -813,25 +813,25 @@ export class Program extends DiagnosticEmitter { resolveType(node: TypeNode, contextualTypeArguments: Map | null = null, reportNotFound: bool = true): Type | null { // resolve parameters - const k = node.typeArguments.length; - const paramTypes = new Array(k); - for (let i = 0; i < k; ++i) { - const paramType = this.resolveType(node.typeArguments[i], contextualTypeArguments, reportNotFound); + var k = node.typeArguments.length; + var paramTypes = new Array(k); + for (var i = 0; i < k; ++i) { + var paramType = this.resolveType(node.typeArguments[i], contextualTypeArguments, reportNotFound); if (!paramType) return null; paramTypes[i] = paramType; } - let globalName = node.identifier.name; + var globalName = node.identifier.name; if (k) // can't be a placeholder if it has parameters globalName += typesToString(paramTypes); else if (contextualTypeArguments) { - const placeholderType = contextualTypeArguments.get(globalName); + var placeholderType = contextualTypeArguments.get(globalName); if (placeholderType) return placeholderType; } - let type: Type | null; + var type: Type | null; // check file-global type if (type = this.types.get(node.range.source.internalPath + PATH_DELIMITER + globalName)) @@ -842,7 +842,7 @@ export class Program extends DiagnosticEmitter { return type; // check type alias - let alias = this.typeAliases.get(globalName); + var alias = this.typeAliases.get(globalName); if (alias && (type = this.resolveType(alias, null, reportNotFound))) return type; @@ -854,8 +854,8 @@ export class Program extends DiagnosticEmitter { /** Resolves an array of type parameters to concrete types. */ resolveTypeArguments(typeParameters: TypeParameter[], typeArgumentNodes: TypeNode[] | null, contextualTypeArguments: Map | null = null, alternativeReportNode: Node | null = null): Type[] | null { - const parameterCount = typeParameters.length; - const argumentCount = typeArgumentNodes ? typeArgumentNodes.length : 0; + var parameterCount = typeParameters.length; + var argumentCount = typeArgumentNodes ? typeArgumentNodes.length : 0; if (parameterCount != argumentCount) { if (argumentCount) this.error(DiagnosticCode.Expected_0_type_arguments_but_got_1, Range.join((typeArgumentNodes)[0].range, (typeArgumentNodes)[argumentCount - 1].range), parameterCount.toString(10), argumentCount.toString(10)); @@ -863,9 +863,9 @@ export class Program extends DiagnosticEmitter { this.error(DiagnosticCode.Expected_0_type_arguments_but_got_1, alternativeReportNode.range.atEnd, parameterCount.toString(10), "0"); return null; } - const typeArguments = new Array(parameterCount); - for (let i = 0; i < parameterCount; ++i) { - const type = this.resolveType((typeArgumentNodes)[i], contextualTypeArguments, true); // reports + var typeArguments = new Array(parameterCount); + for (var i = 0; i < parameterCount; ++i) { + var type = this.resolveType((typeArgumentNodes)[i], contextualTypeArguments, true); // reports if (!type) return null; // TODO: check extendsType @@ -876,13 +876,13 @@ export class Program extends DiagnosticEmitter { /** Resolves an identifier to the element it refers to. */ resolveIdentifier(identifier: IdentifierExpression, contextualFunction: Function): Element | null { - const name = identifier.name; - const local = contextualFunction.locals.get(name); + var name = identifier.name; + var local = contextualFunction.locals.get(name); if (local) return local; - let element: Element | null; - let namespace: Element | null; + var element: Element | null; + var namespace: Element | null; // search parent namespaces if applicable if (contextualFunction && (namespace = contextualFunction.prototype.namespace)) { @@ -906,8 +906,8 @@ export class Program extends DiagnosticEmitter { /** Resolves a property access the element it refers to. */ resolvePropertyAccess(propertyAccess: PropertyAccessExpression, contextualFunction: Function): Element | null { - const expression = propertyAccess.expression; - let target: Element | null = null; + var expression = propertyAccess.expression; + var target: Element | null = null; if (expression.kind == NodeKind.IDENTIFIER) { target = this.resolveIdentifier(expression, contextualFunction); } else if (expression.kind == NodeKind.PROPERTYACCESS) { @@ -916,9 +916,9 @@ export class Program extends DiagnosticEmitter { throw new Error("unexpected target kind"); if (!target) return null; - const propertyName = propertyAccess.property.name; + var propertyName = propertyAccess.property.name; if (target.members) { - const member = target.members.get(propertyName); + var member = target.members.get(propertyName); if (member) return member; } @@ -955,9 +955,9 @@ export class Program extends DiagnosticEmitter { function hasDecorator(name: string, decorators: Decorator[] | null): bool { if (decorators) - for (let i = 0, k = decorators.length; i < k; ++i) { - const decorator = decorators[i]; - const expression = decorator.name; + for (var i = 0, k = decorators.length; i < k; ++i) { + var decorator = decorators[i]; + var expression = decorator.name; if (expression.kind == NodeKind.IDENTIFIER && decorator.arguments.length <= 1 && (expression).name == name) return true; } @@ -1117,7 +1117,7 @@ export class Namespace extends Element { constructor(program: Program, simpleName: string, internalName: string, declaration: NamespaceDeclaration | null = null) { super(program, simpleName, internalName); if ((this.declaration = declaration) && this.declaration.modifiers) { - for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) { + for (var i = 0, k = this.declaration.modifiers.length; i < k; ++i) { switch (this.declaration.modifiers[i].modifierKind) { case ModifierKind.IMPORT: this.isImported = true; break; case ModifierKind.EXPORT: this.isExported = true; break; @@ -1141,7 +1141,7 @@ export class Enum extends Element { constructor(program: Program, simpleName: string, internalName: string, declaration: EnumDeclaration | null = null) { super(program, simpleName, internalName); if ((this.declaration = declaration) && this.declaration.modifiers) { - for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) { + for (var i = 0, k = this.declaration.modifiers.length; i < k; ++i) { switch (this.declaration.modifiers[i].modifierKind) { case ModifierKind.EXPORT: this.isExported = true; break; case ModifierKind.IMPORT: this.isImported = true; break; @@ -1191,7 +1191,7 @@ export class Global extends Element { super(program, simpleName, internalName); if (this.declaration = declaration) { if (this.declaration.modifiers) { - for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) { + for (var i = 0, k = this.declaration.modifiers.length; i < k; ++i) { switch (this.declaration.modifiers[i].modifierKind) { case ModifierKind.IMPORT: this.isImported = true; break; case ModifierKind.EXPORT: this.isExported = true; break; @@ -1277,7 +1277,7 @@ export class FunctionPrototype extends Element { super(program, simpleName, internalName); if (this.declaration = declaration) { if (this.declaration.modifiers) - for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) { + for (var i = 0, k = this.declaration.modifiers.length; i < k; ++i) { switch (this.declaration.modifiers[i].modifierKind) { case ModifierKind.IMPORT: this.isImported = true; break; case ModifierKind.EXPORT: this.isExported = true; break; @@ -1312,37 +1312,37 @@ export class FunctionPrototype extends Element { get isAccessor(): bool { return (this.flags & (ElementFlags.GETTER | ElementFlags.SETTER)) != 0; } resolve(typeArguments: Type[] | null = null, contextualTypeArguments: Map | null = null): Function | null { - const instanceKey = typeArguments ? typesToString(typeArguments, "", "") : ""; - let instance = this.instances.get(instanceKey); + var instanceKey = typeArguments ? typesToString(typeArguments, "", "") : ""; + var instance = this.instances.get(instanceKey); if (instance) return instance; - const declaration = this.declaration; + var declaration = this.declaration; if (!declaration) throw new Error("declaration expected"); // cannot resolve built-ins // override call specific contextual type arguments - let i: i32, k: i32; + var i: i32, k: i32; if (typeArguments && (k = typeArguments.length)) { - const inheritedTypeArguments: Map | null = contextualTypeArguments; + var inheritedTypeArguments = contextualTypeArguments; contextualTypeArguments = new Map(); if (inheritedTypeArguments) - for (let [name, type] of inheritedTypeArguments) - contextualTypeArguments.set(name, type); + for (var [inheritedName, inheritedType] of inheritedTypeArguments) + contextualTypeArguments.set(inheritedName, inheritedType); for (i = 0; i < k; ++i) contextualTypeArguments.set(declaration.typeParameters[i].identifier.name, typeArguments[i]); } // resolve parameters k = declaration.parameters.length; - const parameters = new Array(k); - const parameterTypes = new Array(k); - let typeNode: TypeNode | null; + var parameters = new Array(k); + var parameterTypes = new Array(k); + var typeNode: TypeNode | null; for (i = 0; i < k; ++i) { if (typeNode = declaration.parameters[i].type) { - const type = this.program.resolveType(typeNode, contextualTypeArguments, true); // reports - if (type) { - parameters[i] = new Parameter(declaration.parameters[i].name.name, type); - parameterTypes[i] = type; + var parameterType = this.program.resolveType(typeNode, contextualTypeArguments, true); // reports + if (parameterType) { + parameters[i] = new Parameter(declaration.parameters[i].name.name, parameterType); + parameterTypes[i] = parameterType; } else return null; } else @@ -1350,12 +1350,12 @@ export class FunctionPrototype extends Element { } // resolve return type - let returnType: Type; + var returnType: Type; if (this.isSetter) { returnType = Type.void; // not annotated } else { if (typeNode = declaration.returnType) { - const type = this.program.resolveType(typeNode, contextualTypeArguments, true); // reports + var type = this.program.resolveType(typeNode, contextualTypeArguments, true); // reports if (type) returnType = type; else @@ -1364,7 +1364,7 @@ export class FunctionPrototype extends Element { return null; } - let internalName = this.internalName; + var internalName = this.internalName; if (instanceKey.length) internalName += "<" + instanceKey + ">"; instance = new Function(this, internalName, typeArguments, parameters, returnType, null); // TODO: class @@ -1373,7 +1373,7 @@ export class FunctionPrototype extends Element { } resolveInclTypeArguments(typeArgumentNodes: TypeNode[] | null, contextualTypeArguments: Map | null, alternativeReportNode: Node | null): Function | null { - let resolvedTypeArguments: Type[] | null; + var resolvedTypeArguments: Type[] | null; if (this.isGeneric) { assert(typeArgumentNodes != null && typeArgumentNodes.length != 0); if (!this.declaration) @@ -1427,19 +1427,19 @@ export class Function extends Element { this.returnType = returnType; this.instanceMethodOf = instanceMethodOf; this.flags = prototype.flags; - let localIndex = 0; + var localIndex = 0; if (instanceMethodOf) { assert(this.isInstance); this.locals.set("this", new Local(prototype.program, "this", localIndex++, instanceMethodOf.type)); if (instanceMethodOf.contextualTypeArguments) { if (!this.contextualTypeArguments) this.contextualTypeArguments = new Map(); - for (let [name, type] of instanceMethodOf.contextualTypeArguments) + for (var [name, type] of instanceMethodOf.contextualTypeArguments) this.contextualTypeArguments.set(name, type); } } else assert(!this.isInstance); - for (let i = 0, k = parameters.length; i < k; ++i) { - const parameter = parameters[i]; + for (var i = 0, k = parameters.length; i < k; ++i) { + var parameter = parameters[i]; this.locals.set(parameter.name, new Local(prototype.program, parameter.name, localIndex++, parameter.type)); } } @@ -1447,9 +1447,9 @@ export class Function extends Element { /** Adds a local of the specified type, with an optional name. */ addLocal(type: Type, name: string | null = null): Local { // if it has a name, check previously as this method will throw otherwise - let localIndex = this.parameters.length + this.additionalLocals.length; + var localIndex = this.parameters.length + this.additionalLocals.length; if (this.isInstance) localIndex++; // plus 'this' - const local = new Local(this.prototype.program, name ? name : "anonymous$" + localIndex.toString(10), localIndex, type); + var local = new Local(this.prototype.program, name ? name : "anonymous$" + localIndex.toString(10), localIndex, type); if (name) { if (this.locals.has(name)) throw new Error("unexpected duplicate local name"); @@ -1466,7 +1466,7 @@ export class Function extends Element { /** Gets a free temporary local of the specified type. */ getTempLocal(type: Type): Local { - let temps: Local[] | null; + var temps: Local[] | null; switch (type.toNativeType()) { case NativeType.I32: temps = this.tempI32s; break; case NativeType.I64: temps = this.tempI64s; break; @@ -1481,7 +1481,7 @@ export class Function extends Element { /** Frees the temporary local for reuse. */ freeTempLocal(local: Local): void { - let temps: Local[]; + var temps: Local[]; switch (local.type.toNativeType()) { case NativeType.I32: temps = this.tempI32s || (this.tempI32s = []); break; case NativeType.I64: temps = this.tempI64s || (this.tempI64s = []); break; @@ -1494,7 +1494,7 @@ export class Function extends Element { /** Gets and immediately frees a temporary local of the specified type. */ getAndFreeTempLocal(type: Type): Local { - let temps: Local[]; + var temps: Local[]; switch (type.toNativeType()) { case NativeType.I32: temps = this.tempI32s || (this.tempI32s = []); break; case NativeType.I64: temps = this.tempI64s || (this.tempI64s = []); break; @@ -1504,14 +1504,14 @@ export class Function extends Element { } if (temps.length > 0) return temps[temps.length - 1]; - let local: Local = this.addLocal(type); + var local: Local = this.addLocal(type); temps.push(local); return local; } /** Enters a(nother) break context. */ enterBreakContext(): string { - const id = this.nextBreakId++; + var id = this.nextBreakId++; if (!this.breakStack) this.breakStack = [ id ]; else @@ -1522,7 +1522,7 @@ export class Function extends Element { /** Leaves the current break context. */ leaveBreakContext(): void { assert(this.breakStack != null); - const length = (this.breakStack).length; + var length = (this.breakStack).length; assert(length > 0); (this.breakStack).pop(); if (length > 1) { @@ -1563,7 +1563,7 @@ export class FieldPrototype extends Element { super(classPrototype.program, simpleName, internalName); this.classPrototype = classPrototype; if ((this.declaration = declaration) && this.declaration.modifiers) { - for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) { + for (var i = 0, k = this.declaration.modifiers.length; i < k; ++i) { switch (this.declaration.modifiers[i].modifierKind) { case ModifierKind.EXPORT: this.isExported = true; break; case ModifierKind.READONLY: this.isReadonly = true; break; @@ -1639,7 +1639,7 @@ export class ClassPrototype extends Element { super(program, simpleName, internalName); if (this.declaration = declaration) { if (this.declaration.modifiers) { - for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) { + for (var i = 0, k = this.declaration.modifiers.length; i < k; ++i) { switch (this.declaration.modifiers[i].modifierKind) { case ModifierKind.IMPORT: this.isImported = true; break; case ModifierKind.EXPORT: this.isExported = true; break; @@ -1654,29 +1654,29 @@ export class ClassPrototype extends Element { } resolve(typeArguments: Type[], contextualTypeArguments: Map | null): Class { - const instanceKey = typesToString(typeArguments, "", ""); - let instance = this.instances.get(instanceKey); + var instanceKey = typesToString(typeArguments, "", ""); + var instance = this.instances.get(instanceKey); if (instance) return instance; - const declaration = this.declaration; + var declaration = this.declaration; if (!declaration) throw new Error("declaration expected"); // cannot resolve built-ins // override call specific contextual type arguments - let i: i32, k = typeArguments.length; + var i: i32, k = typeArguments.length; if (k) { - const inheritedTypeArguments: Map | null = contextualTypeArguments; + var inheritedTypeArguments = contextualTypeArguments; contextualTypeArguments = new Map(); if (inheritedTypeArguments) - for (let [name, type] of inheritedTypeArguments) - contextualTypeArguments.set(name, type); + for (var [inheritedName, inheritedType] of inheritedTypeArguments) + contextualTypeArguments.set(inheritedName, inheritedType); for (i = 0; i < k; ++i) contextualTypeArguments.set(declaration.typeParameters[i].identifier.name, typeArguments[i]); } // TODO: set up instance fields and methods if (this.instanceMembers) - for (let [key, member] of this.instanceMembers) { + for (var member of this.instanceMembers.values()) { switch (member.kind) { case ElementKind.FIELD_PROTOTYPE: break; case ElementKind.FUNCTION_PROTOTYPE: break; @@ -1684,7 +1684,7 @@ export class ClassPrototype extends Element { } } - let internalName = this.internalName; + var internalName = this.internalName; if (instanceKey.length) internalName += "<" + instanceKey + ">"; instance = new Class(this, internalName, typeArguments, null); // TODO: base class @@ -1694,7 +1694,7 @@ export class ClassPrototype extends Element { } resolveInclTypeArguments(typeArgumentNodes: TypeNode[] | null, contextualTypeArguments: Map | null, alternativeReportNode: Node | null): Class | null { - let resolvedTypeArguments: Type[] | null; + var resolvedTypeArguments: Type[] | null; if (this.isGeneric) { assert(typeArgumentNodes != null && typeArgumentNodes.length != 0); if (!this.declaration) @@ -1740,21 +1740,21 @@ export class Class extends Element { // inherit contextual type arguments from base class if (base && base.contextualTypeArguments) { if (!this.contextualTypeArguments) this.contextualTypeArguments = new Map(); - for (let [name, type] of base.contextualTypeArguments) - this.contextualTypeArguments.set(name, type); + for (var [baseName, baseType] of base.contextualTypeArguments) + this.contextualTypeArguments.set(baseName, baseType); } // apply instance-specific contextual type arguments - const declaration: ClassDeclaration | null = this.prototype.declaration; + var declaration = this.prototype.declaration; if (declaration) { // irrelevant for built-ins - const typeParameters: TypeParameter[] = declaration.typeParameters; + var typeParameters = declaration.typeParameters; if (typeParameters.length != typeArguments.length) throw new Error("unexpected type argument count mismatch"); - const k = typeArguments.length; + var k = typeArguments.length; if (k) { if (!this.contextualTypeArguments) this.contextualTypeArguments = new Map(); - for (let i = 0; i < k; ++i) + for (var i = 0; i < k; ++i) this.contextualTypeArguments.set(typeParameters[i].identifier.name, typeArguments[i]); } } diff --git a/src/tokenizer.ts b/src/tokenizer.ts index 5987db22..9e0c3389 100644 --- a/src/tokenizer.ts +++ b/src/tokenizer.ts @@ -376,7 +376,7 @@ export class Tokenizer extends DiagnosticEmitter { this.end = source.text.length; this.diagnostics = diagnostics ? diagnostics : new Array(); - const text: string = source.text; + var text = source.text; // skip bom if (this.pos < this.end && text.charCodeAt(this.pos) == CharCode.BYTEORDERMARK) @@ -397,13 +397,13 @@ export class Tokenizer extends DiagnosticEmitter { } private unsafeNext(preferIdentifier: bool = false): Token { - const text: string = this.source.text; + var text = this.source.text; while (true) { if (this.pos >= this.end) return Token.ENDOFFILE; this.tokenPos = this.pos; - let c: i32 = text.charCodeAt(this.pos); + var c = text.charCodeAt(this.pos); switch (c) { case CharCode.CARRIAGERETURN: @@ -534,7 +534,7 @@ export class Tokenizer extends DiagnosticEmitter { continue; } if (text.charCodeAt(this.pos) == CharCode.ASTERISK) { // multi-line comment - let closed: bool = false; + var closed = false; while (++this.pos < this.end) { c = text.charCodeAt(this.pos); if (c == CharCode.ASTERISK && this.pos + 1 < this.end && text.charCodeAt(this.pos + 1) == CharCode.SLASH) { @@ -687,18 +687,18 @@ export class Tokenizer extends DiagnosticEmitter { this.pos++; return Token.AT; - default: { + default: if (isIdentifierStart(c)) { if (isKeywordCharacter(c)) { - const posBefore: i32 = this.pos; + var posBefore = this.pos; while (++this.pos < this.end && isIdentifierPart(c = text.charCodeAt(this.pos))) { if (!isKeywordCharacter(c)) { this.pos = posBefore; return Token.IDENTIFIER; } } - const keywordText: string = text.substring(posBefore, this.pos); - const keywordToken: Token = Token.fromKeyword(keywordText); + var keywordText = text.substring(posBefore, this.pos); + var keywordToken = Token.fromKeyword(keywordText); if (keywordToken != Token.INVALID && !(preferIdentifier && Token.isAlsoIdentifier(keywordToken))) return keywordToken; this.pos = posBefore; @@ -711,17 +711,16 @@ export class Tokenizer extends DiagnosticEmitter { this.error(DiagnosticCode.Invalid_character, this.range(this.pos, this.pos + 1)); this.pos++; return Token.INVALID; - } } } } peek(checkOnNewLine: bool = false): Token { - const text: string = this.source.text; + var text = this.source.text; if (this.nextToken < 0) { - const posBefore: i32 = this.pos; - const tokenBefore: Token = this.token; - const tokenPosBefore: i32 = this.tokenPos; + var posBefore = this.pos; + var tokenBefore = this.token; + var tokenPosBefore = this.tokenPos; this.nextToken = this.unsafeNext(); if (checkOnNewLine) { this.nextTokenOnNewLine = false; @@ -740,9 +739,9 @@ export class Tokenizer extends DiagnosticEmitter { } skip(token: Token): bool { - const posBefore: i32 = this.pos; - const tokenBefore: Token = this.token; - const tokenPosBefore: i32 = this.tokenPos; + var posBefore = this.pos; + var tokenBefore = this.token; + var tokenPosBefore = this.tokenPos; if ((this.token = this.unsafeNext(token == Token.IDENTIFIER)) == token) { this.nextToken = -1; return true; @@ -755,7 +754,7 @@ export class Tokenizer extends DiagnosticEmitter { } // skipUntil(token1: Token, token2: Token = -1): bool { - // let next: Token; + // var next: Token; // do { // if ((next = this.peek()) == Token.ENDOFFILE) // return false; @@ -788,24 +787,24 @@ export class Tokenizer extends DiagnosticEmitter { } readIdentifier(): string { - const text: string = this.source.text; - const start: i32 = this.pos; + var text = this.source.text; + var start = this.pos; while (++this.pos < this.end && isIdentifierPart(text.charCodeAt(this.pos))); return text.substring(start, this.pos); } readString(): string { - const text: string = this.source.text; - const quote: i32 = text.charCodeAt(this.pos++); - let start: i32 = this.pos; - let result: string = ""; + var text = this.source.text; + var quote = text.charCodeAt(this.pos++); + var start = this.pos; + var result = ""; while (true) { if (this.pos >= this.end) { result += text.substring(start, this.pos); this.error(DiagnosticCode.Unterminated_string_literal, this.range(start - 1, this.end)); break; } - const c: i32 = text.charCodeAt(this.pos); + var c = text.charCodeAt(this.pos); if (c == quote) { result += text.substring(start, this.pos++); break; @@ -832,8 +831,8 @@ export class Tokenizer extends DiagnosticEmitter { return ""; } - const text: string = this.source.text; - const c: i32 = text.charCodeAt(this.pos++); + var text = this.source.text; + var c = text.charCodeAt(this.pos++); switch (c) { case CharCode._0: @@ -891,10 +890,10 @@ export class Tokenizer extends DiagnosticEmitter { } readRegexp(): string { - let result: string = ""; - let start: i32 = this.pos; - let escaped: bool = false; - const text: string = this.source.text; + var text = this.source.text; + var start = this.pos; + var result = ""; + var escaped = false; while (true) { if (this.pos >= this.end) { result += text.substring(start, this.pos); @@ -906,7 +905,7 @@ export class Tokenizer extends DiagnosticEmitter { escaped = true; continue; } - const c: i32 = text.charCodeAt(this.pos); + var c = text.charCodeAt(this.pos); if (c == CharCode.SLASH) { result += text.substring(start, this.pos); this.pos++; @@ -923,7 +922,7 @@ export class Tokenizer extends DiagnosticEmitter { } testInteger(): bool { - const text: string = this.source.text; + var text = this.source.text; if (text.charCodeAt(this.pos) == CharCode._0 && this.pos + 1 < this.end) { switch (text.charCodeAt(this.pos + 2)) { case CharCode.X: @@ -935,9 +934,9 @@ export class Tokenizer extends DiagnosticEmitter { return true; } } - let pos: i32 = this.pos; + var pos = this.pos; while (pos < this.end) { - const c: i32 = text.charCodeAt(pos); + var c = text.charCodeAt(pos); if (c == CharCode.DOT || c == CharCode.E || c == CharCode.e) return false; if (c < CharCode._0 || c > CharCode._9) @@ -948,7 +947,7 @@ export class Tokenizer extends DiagnosticEmitter { } readInteger(): I64 { - const text: string = this.source.text; + var text = this.source.text; if (text.charCodeAt(this.pos) == CharCode._0 && this.pos + 2 < this.end) { switch (text.charCodeAt(this.pos + 1)) { case CharCode.X: @@ -965,9 +964,9 @@ export class Tokenizer extends DiagnosticEmitter { return this.readOctalInteger(); } if (isOctalDigit(text.charCodeAt(this.pos + 1))) { - const start: i32 = this.pos; + var start = this.pos; this.pos++; - const value: I64 = this.readOctalInteger(); + var value = this.readOctalInteger(); this.error(DiagnosticCode.Octal_literals_are_not_allowed_in_strict_mode, this.range(start, this.pos)); return value; } @@ -976,11 +975,11 @@ export class Tokenizer extends DiagnosticEmitter { } readHexInteger(): I64 { - const text: string = this.source.text; - const start: i32 = this.pos; - let value: I64 = new I64(0, 0); + var text = this.source.text; + var start = this.pos; + var value = new I64(0, 0); while (this.pos < this.end) { - const c: i32 = text.charCodeAt(this.pos); + var c = text.charCodeAt(this.pos); if (c >= CharCode._0 && c <= CharCode._9) { // value = value * 16 + c - CharCode._0; value.mul32(16); @@ -1003,11 +1002,11 @@ export class Tokenizer extends DiagnosticEmitter { } readDecimalInteger(): I64 { - const text: string = this.source.text; - const start: i32 = this.pos; - let value: I64 = new I64(0, 0); + var text = this.source.text; + var start = this.pos; + var value = new I64(0, 0); while (this.pos < this.end) { - const c: i32 = text.charCodeAt(this.pos); + var c = text.charCodeAt(this.pos); if (c >= CharCode._0 && c <= CharCode._9) { // value = value * 10 + c - CharCode._0; value.mul32(10); @@ -1022,11 +1021,11 @@ export class Tokenizer extends DiagnosticEmitter { } readOctalInteger(): I64 { - const text: string = this.source.text; - const start: i32 = this.pos; - let value: I64 = new I64(0, 0); + var text = this.source.text; + var start = this.pos; + var value = new I64(0, 0); while (this.pos < this.end) { - const c: i32 = text.charCodeAt(this.pos); + var c = text.charCodeAt(this.pos); if (c >= CharCode._0 && c <= CharCode._7) { // value = value * 8 + c - CharCode._0; value.mul32(8); @@ -1041,11 +1040,11 @@ export class Tokenizer extends DiagnosticEmitter { } readBinaryInteger(): I64 { - const text: string = this.source.text; - const start: i32 = this.pos; - let value: I64 = new I64(); + var text = this.source.text; + var start = this.pos; + var value = new I64(); while (this.pos < this.end) { - const c: i32 = text.charCodeAt(this.pos); + var c = text.charCodeAt(this.pos); if (c == CharCode._0) { // value = value * 2; value.mul32(2); @@ -1064,8 +1063,8 @@ export class Tokenizer extends DiagnosticEmitter { } readFloat(): f64 { - let start: i32 = this.pos; - const text: string = this.source.text; + var start = this.pos; + var text = this.source.text; while (this.pos < this.end && isDecimalDigit(text.charCodeAt(this.pos))) this.pos++; if (this.pos < this.end && text.charCodeAt(this.pos) == CharCode.DOT) { @@ -1074,7 +1073,7 @@ export class Tokenizer extends DiagnosticEmitter { this.pos++; } if (this.pos < this.end) { - const c: i32 = text.charCodeAt(this.pos); + var c = text.charCodeAt(this.pos); if (c == CharCode.E || c == CharCode.e) { if (++this.pos < this.end && text.charCodeAt(this.pos) == CharCode.MINUS) this.pos++; @@ -1086,11 +1085,11 @@ export class Tokenizer extends DiagnosticEmitter { } readUnicodeEscape(): string { - let remain: i32 = 4; - let value: i32 = 0; - const text: string = this.source.text; + var remain = 4; + var value = 0; + var text = this.source.text; while (this.pos < this.end) { - const c: i32 = text.charCodeAt(this.pos++); + var c = text.charCodeAt(this.pos++); if (c >= CharCode._0 && c <= CharCode._9) value = value * 16 + c - CharCode._0; else if (c >= CharCode.A && c <= CharCode.F) @@ -1112,17 +1111,17 @@ export class Tokenizer extends DiagnosticEmitter { } private readExtendedUnicodeEscape(): string { - const start: i32 = this.pos; - const value: I64 = this.readHexInteger(); - let invalid: bool = false; + var start = this.pos; + var value = this.readHexInteger(); + var invalid = false; if (value.gt32(0x10FFFF)) { this.error(DiagnosticCode.An_extended_Unicode_escape_value_must_be_between_0x0_and_0x10FFFF_inclusive, this.range(start, this.pos)); invalid = true; } - const value32: i32 = value.toI32(); - const text: string = this.source.text; + var value32 = value.toI32(); + var text = this.source.text; if (this.pos >= this.end) { this.error(DiagnosticCode.Unexpected_end_of_text, this.range(start, this.end)); invalid = true; diff --git a/src/types.ts b/src/types.ts index c4ad3be7..cdead5dd 100644 --- a/src/types.ts +++ b/src/types.ts @@ -91,7 +91,7 @@ export class Type { /** Composes a class type from this type and a class. */ asClass(classType: Class): Type { assert(this.kind == TypeKind.USIZE); - const ret: Type = new Type(this.kind, this.size); + var ret = new Type(this.kind, this.size); ret.classType = classType; return ret; } @@ -99,7 +99,7 @@ export class Type { /** Composes a function type from this type and a function. */ asFunction(functionType: Function): Type { assert(this.kind == TypeKind.USIZE); - const ret: Type = new Type(this.kind, this.size); + var ret = new Type(this.kind, this.size); ret.functionType = functionType; return ret; } @@ -224,20 +224,20 @@ export class Type { /** Converts an array of types to an array of native types. */ export function typesToNativeTypes(types: Type[]): NativeType[] { - const k: i32 = types.length; - const ret: NativeType[] = new Array(k); - for (let i: i32 = 0; i < k; ++i) + var k = types.length; + var ret = new Array(k); + for (var i = 0; i < k; ++i) ret[i] = types[i].toNativeType(); return ret; } /** Converts an array of types to its combined string representation. Usually type arguments. */ export function typesToString(types: Type[], prefix: string = "<", postfix: string = ">"): string { - const k: i32 = types.length; + var k = types.length; if (!k) return ""; sb.length = 0; - for (let i: i32 = 0; i < k; ++i) + for (var i = 0; i < k; ++i) sb[i] = types[i].toString(); return prefix + sb.join(", ") + postfix; } diff --git a/src/util/charcode.ts b/src/util/charcode.ts index e63b80d5..8234c4da 100644 --- a/src/util/charcode.ts +++ b/src/util/charcode.ts @@ -208,9 +208,9 @@ function lookupInUnicodeMap(code: u16, map: u16[]): bool { if (code < map[0]) return false; - let lo: i32 = 0; - let hi: i32 = map.length; - let mid: i32; + var lo = 0; + var hi = map.length; + var mid: i32; while (lo + 1 < hi) { mid = lo + (hi - lo) / 2; diff --git a/src/util/i64.ts b/src/util/i64.ts index feb51530..197967d1 100644 --- a/src/util/i64.ts +++ b/src/util/i64.ts @@ -240,8 +240,8 @@ export class I64 { } // |other| >= 2, so |this/other| < |MIN_VALUE| - let tempLo: i32 = this.lo; - let tempHi: i32 = this.hi; + var tempLo = this.lo; + var tempHi = this.hi; this.shr32(1, 0); this.div32(lo, hi); this.shl32(1, 0); @@ -308,12 +308,12 @@ export class I64 { } mod32(lo: i32, hi: i32 = 0): void { - const thisLo: i32 = this.lo; - const thisHi: i32 = this.hi; + var thisLo = this.lo; + var thisHi = this.hi; this.div32(lo, hi); this.mul32(lo, hi); - const resLo: i32 = this.lo; - const resHi: i32 = this.hi; + var resLo = this.lo; + var resHi = this.hi; this.lo = thisLo; this.hi = thisHi; this.sub32(resLo, resHi); @@ -407,7 +407,7 @@ export class I64 { } toString(): string { - let negative: bool = false; + var negative = false; if (this.hi < 0) { i64_add_internal(~this.lo, ~this.hi, 1, 0); negative = true; @@ -417,7 +417,7 @@ export class I64 { } if (i64_hi) { - let lo: string = (i64_lo as u32 >>> 0).toString(16); + var lo = (i64_lo as u32 >>> 0).toString(16); while (lo.length < 8) lo = "0" + lo; return (negative ? "-0x" : "0x") + (i64_hi as u32 >>> 0).toString(16) + lo; @@ -426,21 +426,21 @@ export class I64 { } } -let i64_lo: i32 = 0; -let i64_hi: i32 = 0; +var i64_lo = 0; +var i64_hi = 0; function i64_add_internal(lo: i32, hi: i32, otherLo: i32, otherHi: i32): void { - let a48: i32 = hi >>> 16; - let a32: i32 = hi & 0xFFFF; - let a16: i32 = lo >>> 16; - let a00: i32 = lo & 0xFFFF; + var a48 = hi >>> 16; + var a32 = hi & 0xFFFF; + var a16 = lo >>> 16; + var a00 = lo & 0xFFFF; - let b48: i32 = otherHi >>> 16; - let b32: i32 = otherHi & 0xFFFF; - let b16: i32 = otherLo >>> 16; - let b00: i32 = otherLo & 0xFFFF; + var b48 = otherHi >>> 16; + var b32 = otherHi & 0xFFFF; + var b16 = otherLo >>> 16; + var b00 = otherLo & 0xFFFF; - let c48: i32 = 0, c32: i32 = 0, c16: i32 = 0, c00: i32 = 0; + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; c00 += a00 + b00; c16 += c00 >>> 16; c00 &= 0xFFFF; @@ -458,15 +458,15 @@ function i64_add_internal(lo: i32, hi: i32, otherLo: i32, otherHi: i32): void { } function i64_mul_internal(lo: i32, hi: i32, otherLo: i32, otherHi: i32): void { - let a48: i32 = hi >>> 16; - let a32: i32 = hi & 0xFFFF; - let a16: i32 = lo >>> 16; - let a00: i32 = lo & 0xFFFF; + var a48 = hi >>> 16; + var a32 = hi & 0xFFFF; + var a16 = lo >>> 16; + var a00 = lo & 0xFFFF; - let b48: i32 = otherHi >>> 16; - let b32: i32 = otherHi & 0xFFFF; - let b16: i32 = otherLo >>> 16; - let b00: i32 = otherLo & 0xFFFF; + var b48 = otherHi >>> 16; + var b32 = otherHi & 0xFFFF; + var b16 = otherLo >>> 16; + var b00 = otherLo & 0xFFFF; var c48 = 0, c32 = 0, c16 = 0, c00 = 0; c00 += a00 * b00; diff --git a/src/util/path.ts b/src/util/path.ts index 9ed82c18..3e150b1b 100644 --- a/src/util/path.ts +++ b/src/util/path.ts @@ -6,8 +6,8 @@ import { export function normalize(path: string, trimExtension: bool = false, separator: CharCode = CharCode.SLASH): string { // expects a relative path - let pos: i32 = 0; - let len: i32 = path.length; + var pos = 0; + var len = path.length; // trim leading './' while (pos + 1 < len && path.charCodeAt(pos) == CharCode.DOT && path.charCodeAt(pos + 1) == separator) @@ -23,7 +23,7 @@ export function normalize(path: string, trimExtension: bool = false, separator: pos = 0; } - let atEnd: bool; + var atEnd: bool; while (pos + 1 < len) { atEnd = false; @@ -51,7 +51,7 @@ export function normalize(path: string, trimExtension: bool = false, separator: ) { // find preceeding '/' - let ipos: i32 = pos; + var ipos = pos; while (--ipos >= 0) { if (path.charCodeAt(ipos) == separator) { if (pos - ipos != 3 || path.charCodeAt(ipos + 1) != CharCode.DOT || path.charCodeAt(ipos + 2) != CharCode.DOT) { // exclude '..' itself @@ -87,7 +87,7 @@ export function resolve(normalizedPath: string, normalizedOrigin: string, separa /** Obtains the directory portion of a normalized path. */ export function dirname(normalizedPath: string, separator: CharCode = CharCode.SLASH): string { - let pos: i32 = normalizedPath.length; + var pos = normalizedPath.length; while (--pos > 0) if (normalizedPath.charCodeAt(pos) == separator) return normalizedPath.substring(0, pos); diff --git a/src/util/sb.ts b/src/util/sb.ts index c55ed97a..19eedb15 100644 --- a/src/util/sb.ts +++ b/src/util/sb.ts @@ -1,2 +1,2 @@ /** A shared string builder utilized to reduce overall array allocations. */ -export const sb: string[] = []; +export const sb = new Array(); diff --git a/std/portable.d.ts b/std/portable.d.ts index 303c8ec7..e7b20cd6 100644 --- a/std/portable.d.ts +++ b/std/portable.d.ts @@ -157,6 +157,7 @@ declare class String { private constructor(); indexOf(subject: string): i32; lastIndexOf(subject: string): i32; + charAt(index: i32): string; charCodeAt(index: i32): i32; substring(from: i32, to?: i32): string; startsWith(subject: string): bool; @@ -205,16 +206,18 @@ declare class Map { has(key: K): bool; get(key: K): V | null; clear(): void; - [Symbol.iterator](): Iterator<[K, V]>; + entries(): Iterable<[K, V]>; + keys(): Iterable; + values(): Iterable; + [Symbol.iterator](): Iterator<[K,V]>; +} + +interface Iterable { + [Symbol.iterator](): Iterator; } interface Iterator {} -declare namespace JSON { - /** @deprecated */ - function stringify(subject: any): string; -} - declare namespace console { /** @deprecated */ function log(message: string): void;