Skip to content

Commit 9632777

Browse files
committed
Type declaration parsing; Future feature references
1 parent aa563a9 commit 9632777

18 files changed

+364
-172
lines changed

package-lock.json

Lines changed: 60 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"url": "https://github.com/AssemblyScript/next/issues"
1212
},
1313
"dependencies": {
14-
"@types/node": "^8.0.58",
14+
"@types/node": "^8.5.1",
1515
"binaryen": "40.0.0-nightly.20171209",
1616
"glob": "^7.1.2",
1717
"minimist": "^1.2.0",
@@ -20,14 +20,14 @@
2020
"devDependencies": {
2121
"@types/chalk": "^2.2.0",
2222
"@types/diff": "^3.2.2",
23-
"@types/glob": "^5.0.33",
23+
"@types/glob": "^5.0.34",
2424
"@types/long": "^3.0.32",
2525
"@types/minimist": "^1.2.0",
2626
"chalk": "^2.3.0",
2727
"diff": "^3.4.0",
2828
"long": "^3.2.0",
2929
"ts-loader": "^3.2.0",
30-
"ts-node": "^3.3.0",
30+
"ts-node": "^4.0.2",
3131
"typescript": "^2.6.2",
3232
"webpack": "^3.10.0"
3333
},

src/ast.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export enum NodeKind {
6262
SWITCH,
6363
THROW,
6464
TRY,
65+
TYPEDECLARATION,
6566
VARIABLE, // wraps declarations
6667
VARIABLEDECLARATION,
6768
WHILE,
@@ -542,6 +543,17 @@ export abstract class Node {
542543
return stmt;
543544
}
544545

546+
static createTypeDeclaration(identifier: IdentifierExpression, alias: TypeNode, modifiers: Modifier[] | null, decorators: Decorator[] | null, range: Range): TypeDeclaration {
547+
const stmt: TypeDeclaration = new TypeDeclaration();
548+
stmt.range = range;
549+
(stmt.name = identifier).parent = stmt;
550+
(stmt.alias = alias).parent = stmt;
551+
let i: i32, k: i32;
552+
if (stmt.modifiers = modifiers) for (i = 0, k = (<Modifier[]>modifiers).length; i < k; ++i) (<Modifier[]>modifiers)[i].parent = stmt;
553+
if (stmt.decorators = decorators) for (i = 0, k = (<Decorator[]>decorators).length; i < k; ++i) (<Decorator[]>decorators)[i].parent = stmt;
554+
return stmt;
555+
}
556+
545557
static createVariable(declarations: VariableDeclaration[], modifiers: Modifier[] | null, decorators: Decorator[] | null, range: Range): VariableStatement {
546558
const stmt: VariableStatement = new VariableStatement();
547559
stmt.range = range;
@@ -552,10 +564,10 @@ export abstract class Node {
552564
return stmt;
553565
}
554566

555-
static createVariableDeclaration(identifier: IdentifierExpression, type: TypeNode | null, initializer: Expression | null, modifiers: Modifier[] | null, decorators: Decorator[] | null, range: Range): VariableDeclaration {
567+
static createVariableDeclaration(name: IdentifierExpression, type: TypeNode | null, initializer: Expression | null, modifiers: Modifier[] | null, decorators: Decorator[] | null, range: Range): VariableDeclaration {
556568
const elem: VariableDeclaration = new VariableDeclaration();
557569
elem.range = range;
558-
(elem.name = identifier).parent = elem;
570+
(elem.name = name).parent = elem;
559571
if (elem.type = type) (<TypeNode>type).parent = elem;
560572
if (elem.initializer = initializer) (<Expression>initializer).parent = elem;
561573
elem.modifiers = modifiers;
@@ -1864,6 +1876,33 @@ export class TryStatement extends Statement {
18641876
}
18651877
}
18661878

1879+
/** Represents a `type` declaration. */
1880+
export class TypeDeclaration extends DeclarationStatement {
1881+
1882+
kind = NodeKind.TYPEDECLARATION;
1883+
1884+
/** Type being aliased. */
1885+
alias: TypeNode;
1886+
1887+
serialize(sb: string[]): void {
1888+
let i: i32, k: i32;
1889+
if (this.decorators)
1890+
for (i = 0, k = this.decorators.length; i < k; ++i) {
1891+
this.decorators[i].serialize(sb);
1892+
sb.push("\n");
1893+
}
1894+
if (this.modifiers)
1895+
for (i = 0, k = (<Modifier[]>this.modifiers).length; i < k; ++i) {
1896+
(<Modifier[]>this.modifiers)[i].serialize(sb);
1897+
sb.push(" ");
1898+
}
1899+
sb.push("type ");
1900+
this.name.serialize(sb);
1901+
sb.push(" = ");
1902+
this.alias.serialize(sb);
1903+
}
1904+
}
1905+
18671906
/** Represents a single variable declaration within a {@link VariableStatement}. */
18681907
export class VariableDeclaration extends VariableLikeDeclarationStatement {
18691908

src/builtins.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export function initialize(program: Program): void {
4242
// host operations
4343
addFunction(program, "current_memory");
4444
addFunction(program, "grow_memory");
45+
// addFunction(program, "move_memory");
46+
// addFunction(program, "set_memory");
4547

4648
// imported
4749
addFunction(program, "parseInt");
@@ -589,6 +591,29 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
589591
arg0 = compiler.compileExpression(operands[0], Type.i32);
590592
return module.createHost(HostOp.GrowMemory, null, [ arg0 ]);
591593

594+
// see: https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md
595+
/*
596+
case "move_memory": // move_memory(dest: usize, src: usize: n: usize) -> void
597+
compiler.currentType = Type.void;
598+
if (!validateCall(compiler, typeArguments, 0, operands, 3, reportNode))
599+
return module.createUnreachable();
600+
arg0 = compiler.compileExpression(operands[0], usizeType);
601+
arg1 = compiler.compileExpression(operands[1], usizeType);
602+
arg2 = compiler.compileExpression(operands[2], usizeType);
603+
compiler.currentType = Type.void;
604+
return module.createHost(HostOp.MoveMemory, null, [ arg0, arg1, arg2 ]);
605+
606+
case "set_memory": // set_memory(dest: usize, value: u32, n: usize) -> void
607+
compiler.currentType = Type.void;
608+
if (!validateCall(compiler, typeArguments, 0, operands, 3, reportNode))
609+
return module.createUnreachable();
610+
arg0 = compiler.compileExpression(operands[0], usizeType);
611+
arg1 = compiler.compileExpression(operands[1], Type.u32);
612+
arg2 = compiler.compileExpression(operands[2], usizeType);
613+
compiler.currentType = Type.void;
614+
return module.createHost(HostOp.SetMemory, null, [ arg0, arg1, arg2 ]);
615+
*/
616+
592617
// imported
593618

594619
case "parseInt": // takes a pointer to the string

0 commit comments

Comments
 (0)