Skip to content

Commit

Permalink
More cleanup and a possible fix for #29
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Feb 16, 2018
1 parent f729444 commit 3d7e8b2
Show file tree
Hide file tree
Showing 17 changed files with 613 additions and 255 deletions.
4 changes: 2 additions & 2 deletions dist/asc.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/assemblyscript.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/assemblyscript.js.map

Large diffs are not rendered by default.

56 changes: 42 additions & 14 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,9 @@ export class WhileStatement extends Statement {
/** Cached unused modifiers for reuse. */
var reusableModifiers: Modifier[] | null = null;

export function setReusableModifiers(modifiers: Modifier[]) {
export function setReusableModifiers(
modifiers: Modifier[]
): void {
reusableModifiers = modifiers;
}

Expand All @@ -1660,30 +1662,42 @@ export function createModifiers(): Modifier[] {
return ret;
}

/** Adds a modifier to a modifiers array. Creates and returns a new array if `null`. */
export function addModifier(modifier: Modifier, modifiers: Modifier[] | null): Modifier[] {
/** Adds a modifier to a set of modifiers. Creates a new set if `null`. */
export function addModifier(
modifier: Modifier,
modifiers: Modifier[] | null
): Modifier[] {
if (modifiers == null)
modifiers = createModifiers();
modifiers.push(modifier);
return modifiers;
}

/** Gets a specific modifier from the specified array of modifiers. */
export function getModifier(kind: ModifierKind, modifiers: Modifier[] | null): Modifier | null {
/** Gets a specific modifier from the specified set of modifiers. */
export function getModifier(
kind: ModifierKind,
modifiers: Modifier[] | null
): Modifier | null {
if (modifiers)
for (var i = 0, k = modifiers.length; i < k; ++i)
if (modifiers[i].modifierKind == kind)
return modifiers[i];
return null;
}

/** Tests whether a specific modifier exists in the specified array of modifiers. */
export function hasModifier(kind: ModifierKind, modifiers: Modifier[] | null): bool {
/** Tests whether a modifier exists in the specified set of modifiers. */
export function hasModifier(
kind: ModifierKind,
modifiers: Modifier[] | null
): bool {
return getModifier(kind, modifiers) != null;
}

/** Gets a specific decorator within the specified decorators, if present. */
export function getFirstDecorator(name: string, decorators: Decorator[] | null): Decorator | null {
/** Gets the first decorator by name within at set of decorators, if present. */
export function getFirstDecorator(
name: string,
decorators: Decorator[] | null
): Decorator | null {
if (decorators)
for (var i = 0, k = decorators.length; i < k; ++i) {
var decorator = decorators[i];
Expand All @@ -1695,12 +1709,18 @@ export function getFirstDecorator(name: string, decorators: Decorator[] | null):
}

/** Tests if a specific decorator is present within the specified decorators. */
export function hasDecorator(name: string, decorators: Decorator[] | null): bool {
export function hasDecorator(
name: string,
decorators: Decorator[] | null
): bool {
return getFirstDecorator(name, decorators) != null;
}

/** Mangles a declaration's name to an internal name. */
export function mangleInternalName(declaration: DeclarationStatement, asGlobal: bool = false): string {
export function mangleInternalName(
declaration: DeclarationStatement,
asGlobal: bool = false
): string {
var name = declaration.name.text;
var parent = declaration.parent;
if (!parent)
Expand All @@ -1720,18 +1740,26 @@ export function mangleInternalName(declaration: DeclarationStatement, asGlobal:
}

/** Mangles an external to an internal path. */
export function mangleInternalPath(path: string): string {
export function mangleInternalPath(
path: string
): string {
if (path.endsWith(".ts"))
path = path.substring(0, path.length - 3);
return path;
}

function setParent(nodes: Node[], parent: Node): void {
function setParent(
nodes: Node[],
parent: Node
): void {
for (var i = 0, k = nodes.length; i < k; ++i)
nodes[i].parent = parent;
}

function setParentOpt(nodes: (Node | null)[], parent: Node): void {
function setParentOpt(
nodes: (Node | null)[],
parent: Node
): void {
for (var i = 0, k = nodes.length; i < k; ++i) {
var node = nodes[i];
if (node)
Expand Down
4 changes: 0 additions & 4 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ import {
typesToNativeTypes
} from "./types";

import {
sb
} from "./util/sb";

/** Compilation target. */
export enum Target {
/** WebAssembly with 32-bit pointers. */
Expand Down
15 changes: 6 additions & 9 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import {
isLineBreak
} from "./util/charcode";

import {
sb
} from "./util/sb";

export {
DiagnosticCode,
diagnosticCodeToString
Expand Down Expand Up @@ -103,7 +99,7 @@ export function formatDiagnosticMessage(message: DiagnosticMessage, useColors: b
context = formatDiagnosticContext(message.range, useColors);

// general information
sb.length = 0;
var sb: string[] = [];
if (useColors) sb.push(diagnosticCategoryToColor(message.category));
sb.push(diagnosticCategoryToString(message.category));
if (useColors) sb.push(colorReset);
Expand Down Expand Up @@ -141,10 +137,11 @@ export function formatDiagnosticContext(range: Range, useColors: bool = false):
start--;
while (end < len && !isLineBreak(text.charCodeAt(end)))
end++;
sb.length = 0;
sb.push("\n ");
sb.push(text.substring(start, end));
sb.push("\n ");
var sb: string[] = [
"\n ",
text.substring(start, end),
"\n "
];
while (start < range.start) {
sb.push(" ");
start++;
Expand Down

0 comments on commit 3d7e8b2

Please sign in to comment.