Skip to content

[WIP] Switch to symbol tables #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
PATH_DELIMITER,
STATIC_DELIMITER,
INSTANCE_DELIMITER,
LIBRARY_PREFIX
LIBRARY_PREFIX,
Symbols
} from "./common";

import {
Expand Down Expand Up @@ -144,6 +145,8 @@ export abstract class Node {
parent: Node | null = null;
/** Common flags indicating specific traits. */
flags: CommonFlags = CommonFlags.NONE;
/** Underlying text. */
get text(): string { return this.range.source.text.substring(this.range.start, this.range.end); }

/** Tests if this node has the specified flag or flags. */
is(flag: CommonFlags): bool { return (this.flags & flag) == flag; }
Expand Down Expand Up @@ -172,7 +175,7 @@ export abstract class Node {
range: Range
): TypeNode {
return Node.createType(
Node.createIdentifierExpression("", range),
Node.createIdentifierExpression(Symbols.OMITTED, range),
null,
false,
range
Expand Down Expand Up @@ -241,26 +244,24 @@ export abstract class Node {
}

static createComment(
text: string,
kind: CommentKind,
range: Range
): CommentNode {
var node = new CommentNode();
node.range = range;
node.commentKind = kind;
node.text = text;
return node;
}

// expressions

static createIdentifierExpression(
name: string,
symbol: symbol,
range: Range
): IdentifierExpression {
var expr = new IdentifierExpression();
expr.range = range;
expr.text = name;
expr.symbol = symbol;
return expr;
}

Expand All @@ -269,7 +270,7 @@ export abstract class Node {
): IdentifierExpression {
var expr = new IdentifierExpression();
expr.range = range;
expr.text = "";
expr.symbol = Symbols.EMPTY;
return expr;
}

Expand Down Expand Up @@ -1236,8 +1237,6 @@ export class CommentNode extends Node {

/** Comment kind. */
commentKind: CommentKind;
/** Comment text. */
text: string;
}

// expressions
Expand All @@ -1249,8 +1248,8 @@ export abstract class Expression extends Node { }
export class IdentifierExpression extends Expression {
kind = NodeKind.IDENTIFIER;

/** Textual name. */
text: string;
/** Symbol. */
symbol: symbol;
}

/** Indicates the kind of a literal. */
Expand Down Expand Up @@ -1340,7 +1339,7 @@ export class CommaExpression extends Expression {
/** Represents a `constructor` expression. */
export class ConstructorExpression extends IdentifierExpression {
kind = NodeKind.CONSTRUCTOR;
text = "constructor";
symbol = Symbols.CONSTRUCTOR;
}

/** Represents an element access expression, e.g., array access. */
Expand Down Expand Up @@ -1395,7 +1394,7 @@ export class NewExpression extends CallExpression {
/** Represents a `null` expression. */
export class NullExpression extends IdentifierExpression {
kind = NodeKind.NULL;
text = "null";
symbol = Symbols.NULL;
}

/** Represents an object literal expression. */
Expand Down Expand Up @@ -1459,25 +1458,25 @@ export class StringLiteralExpression extends LiteralExpression {
/** Represents a `super` expression. */
export class SuperExpression extends IdentifierExpression {
kind = NodeKind.SUPER;
text = "super";
symbol = Symbols.SUPER;
}

/** Represents a `this` expression. */
export class ThisExpression extends IdentifierExpression {
kind = NodeKind.THIS;
text = "this";
symbol = Symbols.THIS;
}

/** Represents a `true` expression. */
export class TrueExpression extends IdentifierExpression {
kind = NodeKind.TRUE;
text = "true";
symbol = Symbols.TRUE;
}

/** Represents a `false` expression. */
export class FalseExpression extends IdentifierExpression {
kind = NodeKind.FALSE;
text = "false";
symbol = Symbols.FALSE;
}

/** Base class of all unary expressions. */
Expand Down Expand Up @@ -1544,7 +1543,7 @@ export class Source extends Node {
/** Contained statements. */
statements: Statement[];
/** Full source text. */
text: string;
private _text: string;
/** Tokenizer reference. */
tokenizer: Tokenizer | null = null;
/** Source map index. */
Expand All @@ -1563,13 +1562,15 @@ export class Source extends Node {
this.simplePath = pos >= 0 ? internalPath.substring(pos + 1) : internalPath;
this.statements = new Array();
this.range = new Range(this, 0, text.length);
this.text = text;
this._text = text;
}

/** Tests if this source is an entry file. */
get isEntry(): bool { return this.sourceKind == SourceKind.ENTRY; }
/** Tests if this source is a stdlib file. */
get isLibrary(): bool { return this.sourceKind == SourceKind.LIBRARY; }
/** Full source text of this source. */
get text(): string { return this._text; }
}

/** Base class of all declaration statements. */
Expand Down
28 changes: 28 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,31 @@ export const LIBRARY_SUBST = "~lib";
export const LIBRARY_PREFIX = LIBRARY_SUBST + PATH_DELIMITER;
/** Prefix used to indicate a filespace element. */
export const FILESPACE_PREFIX = "file:";

/** Common symbols. */
export namespace Symbols {
/** Symbol representing "void". */
export const VOID = Symbol.for("void");
/** Symbol representing "this". */
export const THIS = Symbol.for("this");
/** Symbol representing "super". */
export const SUPER = Symbol.for("super");
/** Symbol representing "true". */
export const TRUE = Symbol.for("true");
/** Symbol representing "false". */
export const FALSE = Symbol.for("false");
/** Symbol representing "null". */
export const NULL = Symbol.for("null");
/** Symbol representing "bool". */
export const BOOL = Symbol.for("bool");
/** Symbol representing "string". */
export const STRING = Symbol.for("string");
/** Symbol representing "Array". */
export const ARRAY = Symbol.for("Array");
/** Symbol representing "constructor". */
export const CONSTRUCTOR = Symbol.for("constructor");
/** Symbol representing "" (empty string). */
export const EMPTY = Symbol.for("");
/** Symbol representing omitted text. */
export const OMITTED = Symbol();
}
29 changes: 5 additions & 24 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,29 +332,16 @@ export class Compiler extends DiagnosticEmitter {
this.currentFunction = startFunctionInstance;

// add a mutable heap base dummy
if (options.isWasm64) {
module.addGlobal(
"HEAP_BASE",
NativeType.I64,
true,
module.createI64(0, 0)
);
} else {
module.addGlobal(
"HEAP_BASE",
NativeType.I32,
false,
module.createI32(0)
);
}
if (options.isWasm64) module.addGlobal("HEAP_BASE", NativeType.I64, true, module.createI64(0));
else module.addGlobal("HEAP_BASE", NativeType.I32, true, module.createI32(0));

// compile entry file(s) while traversing reachable elements
var sources = program.sources;
for (let i = 0, k = sources.length; i < k; ++i) {
if (sources[i].isEntry) this.compileSource(sources[i]);
}

// compile the start function if not empty or called by main
// compile the start function if not empty / utilized by an explicit main function
if (startFunctionBody.length || program.mainFunction !== null) {
let signature = startFunctionInstance.signature;
let funcRef = module.addFunction(
Expand All @@ -377,17 +364,11 @@ export class Compiler extends DiagnosticEmitter {
this.memoryOffset = memoryOffset;
module.removeGlobal("HEAP_BASE");
if (options.isWasm64) {
module.addGlobal(
"HEAP_BASE",
NativeType.I64,
false,
module.addGlobal("HEAP_BASE", NativeType.I64, false,
module.createI64(i64_low(memoryOffset), i64_high(memoryOffset))
);
} else {
module.addGlobal(
"HEAP_BASE",
NativeType.I32,
false,
module.addGlobal("HEAP_BASE", NativeType.I32, false,
module.createI32(i64_low(memoryOffset))
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ export abstract class DiagnosticEmitter {
): void {
var message = DiagnosticMessage.create(code, category, arg0, arg1, arg2).withRange(range);
this.diagnostics.push(message);
// console.log(formatDiagnosticMessage(message, true, true) + "\n"); // temporary
// console.log(<string>new Error("stack").stack);
console.log(formatDiagnosticMessage(message, true, true) + "\n"); // temporary
console.log(<string>new Error("stack").stack);
}

/** Emits an informatory diagnostic message. */
Expand Down
39 changes: 22 additions & 17 deletions src/extra/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import {
} from "../util";

import {
CommonFlags
CommonFlags, Symbols
} from "../common";

/** An AST builder. */
Expand Down Expand Up @@ -358,22 +358,28 @@ export class ASTBuilder {
return;
}
var typeNode = <TypeNode>node;
assert(typeNode.name.text.length);
this.visitIdentifierExpression(typeNode.name);
var typeArguments = typeNode.typeArguments;
if (typeArguments) {
let numTypeArguments = typeArguments.length;
let sb = this.sb;
if (numTypeArguments) {
sb.push("<");
this.visitTypeNode(typeArguments[0]);
for (let i = 1; i < numTypeArguments; ++i) {
sb.push(", ");
this.visitTypeNode(typeArguments[i]);
if (typeNode.name.symbol == Symbols.ARRAY && typeNode.name.text.startsWith("[")) {
let typeArguments = assert(typeNode.typeArguments);
assert(typeArguments.length == 1);
this.visitTypeNode(typeArguments[0]);
this.sb.push("[]");
} else {
this.visitIdentifierExpression(typeNode.name);
let typeArguments = typeNode.typeArguments;
if (typeArguments) {
let numTypeArguments = typeArguments.length;
let sb = this.sb;
if (numTypeArguments) {
sb.push("<");
this.visitTypeNode(typeArguments[0]);
for (let i = 1; i < numTypeArguments; ++i) {
sb.push(", ");
this.visitTypeNode(typeArguments[i]);
}
sb.push(">");
}
sb.push(">");
if (node.isNullable) sb.push(" | null");
}
if (node.isNullable) sb.push(" | null");
}
}

Expand Down Expand Up @@ -423,8 +429,7 @@ export class ASTBuilder {
// expressions

visitIdentifierExpression(node: IdentifierExpression): void {
if (node.is(CommonFlags.QUOTED)) this.visitStringLiteral(node.text);
else this.sb.push(node.text);
this.sb.push(node.text);
}

visitArrayLiteralExpression(node: ArrayLiteralExpression): void {
Expand Down
Loading