Skip to content

Commit 188b4e4

Browse files
committed
Try parsing signatures only if node is callable, see AssemblyScript#149; Minor refactoring
1 parent 7a8995b commit 188b4e4

File tree

14 files changed

+929
-809
lines changed

14 files changed

+929
-809
lines changed

dist/assemblyscript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/n-body/build/index.asm.js

Lines changed: 239 additions & 109 deletions
Large diffs are not rendered by default.

examples/n-body/build/optimized.wasm

-13 Bytes
Binary file not shown.

examples/n-body/build/optimized.wat

Lines changed: 286 additions & 295 deletions
Large diffs are not rendered by default.

examples/n-body/build/untouched.wat

Lines changed: 228 additions & 278 deletions
Large diffs are not rendered by default.

src/ast.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
STATIC_DELIMITER,
1010
INSTANCE_DELIMITER,
1111
LIBRARY_PREFIX
12-
} from "./program";
12+
} from "./common";
1313

1414
import {
1515
Token,
@@ -110,6 +110,26 @@ export function nodeIsConstantValue(kind: NodeKind): bool {
110110
return false;
111111
}
112112

113+
/** Checks if a node might be callable. */
114+
export function nodeIsCallable(kind: NodeKind): bool {
115+
switch (kind) {
116+
case NodeKind.IDENTIFIER:
117+
case NodeKind.CALL:
118+
case NodeKind.ELEMENTACCESS:
119+
case NodeKind.PROPERTYACCESS: return true;
120+
}
121+
return false;
122+
}
123+
124+
/** Checks if a node might be callable with generic arguments. */
125+
export function nodeIsGenericCallable(kind: NodeKind): bool {
126+
switch (kind) {
127+
case NodeKind.IDENTIFIER:
128+
case NodeKind.PROPERTYACCESS: return true;
129+
}
130+
return false;
131+
}
132+
113133
/** Base class of all nodes. */
114134
export abstract class Node {
115135

src/common.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Common constants.
3+
* @module common
4+
*//***/
5+
6+
/** Indicates traits of a {@link Node} or {@link Element}. */
7+
export enum CommonFlags {
8+
/** No flags set. */
9+
NONE = 0,
10+
11+
// Basic modifiers
12+
13+
/** Has an `import` modifier. */
14+
IMPORT = 1 << 0,
15+
/** Has an `export` modifier. */
16+
EXPORT = 1 << 1,
17+
/** Has a `declare` modifier. */
18+
DECLARE = 1 << 2,
19+
/** Has a `const` modifier. */
20+
CONST = 1 << 3,
21+
/** Has a `let` modifier. */
22+
LET = 1 << 4,
23+
/** Has a `static` modifier. */
24+
STATIC = 1 << 5,
25+
/** Has a `readonly` modifier. */
26+
READONLY = 1 << 6,
27+
/** Has an `abstract` modifier. */
28+
ABSTRACT = 1 << 7,
29+
/** Has a `public` modifier. */
30+
PUBLIC = 1 << 8,
31+
/** Has a `private` modifier. */
32+
PRIVATE = 1 << 9,
33+
/** Has a `protected` modifier. */
34+
PROTECTED = 1 << 10,
35+
/** Has a `get` modifier. */
36+
GET = 1 << 11,
37+
/** Has a `set` modifier. */
38+
SET = 1 << 12,
39+
40+
// Extended modifiers usually derived from basic modifiers
41+
42+
/** Is ambient, that is either declared or nested in a declared element. */
43+
AMBIENT = 1 << 13,
44+
/** Is generic. */
45+
GENERIC = 1 << 14,
46+
/** Is part of a generic context. */
47+
GENERIC_CONTEXT = 1 << 15,
48+
/** Is an instance member. */
49+
INSTANCE = 1 << 16,
50+
/** Is a constructor. */
51+
CONSTRUCTOR = 1 << 17,
52+
/** Is an arrow function. */
53+
ARROW = 1 << 18,
54+
/** Is a module export. */
55+
MODULE_EXPORT = 1 << 19,
56+
/** Is a module import. */
57+
MODULE_IMPORT = 1 << 20,
58+
59+
// Compilation states
60+
61+
/** Is a builtin. */
62+
BUILTIN = 1 << 21,
63+
/** Is compiled. */
64+
COMPILED = 1 << 22,
65+
/** Has a constant value and is therefore inlined. */
66+
INLINED = 1 << 23,
67+
/** Is scoped. */
68+
SCOPED = 1 << 24,
69+
/** Is a trampoline. */
70+
TRAMPOLINE = 1 << 25,
71+
/** Is a virtual method. */
72+
VIRTUAL = 1 << 26
73+
}
74+
75+
/** Path delimiter inserted between file system levels. */
76+
export const PATH_DELIMITER = "/";
77+
/** Substitution used to indicate the parent directory. */
78+
export const PARENT_SUBST = "..";
79+
/** Function name prefix used for getters. */
80+
export const GETTER_PREFIX = "get:";
81+
/** Function name prefix used for setters. */
82+
export const SETTER_PREFIX = "set:";
83+
/** Delimiter used between class names and instance members. */
84+
export const INSTANCE_DELIMITER = "#";
85+
/** Delimiter used between class and namespace names and static members. */
86+
export const STATIC_DELIMITER = ".";
87+
/** Delimiter used between a function and its inner elements. */
88+
export const INNER_DELIMITER = "~";
89+
/** Substitution used to indicate a library directory. */
90+
export const LIBRARY_SUBST = "~lib";
91+
/** Library directory prefix. */
92+
export const LIBRARY_PREFIX = LIBRARY_SUBST + PATH_DELIMITER;
93+
/** Prefix used to indicate a filespace element. */
94+
export const FILESPACE_PREFIX = "file:";

src/compiler.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ import {
3636
getGetLocalIndex
3737
} from "./module";
3838

39+
import {
40+
CommonFlags,
41+
PATH_DELIMITER,
42+
INNER_DELIMITER,
43+
INSTANCE_DELIMITER,
44+
STATIC_DELIMITER,
45+
GETTER_PREFIX,
46+
SETTER_PREFIX
47+
} from "./common";
48+
3949
import {
4050
Program,
4151
ClassPrototype,
@@ -54,18 +64,10 @@ import {
5464
Property,
5565
VariableLikeElement,
5666
FlowFlags,
57-
CommonFlags,
5867
ConstantValueKind,
5968
Flow,
6069
OperatorKind,
61-
DecoratorFlags,
62-
63-
PATH_DELIMITER,
64-
INNER_DELIMITER,
65-
INSTANCE_DELIMITER,
66-
STATIC_DELIMITER,
67-
GETTER_PREFIX,
68-
SETTER_PREFIX
70+
DecoratorFlags
6971
} from "./program";
7072

7173
import {
@@ -1487,17 +1489,11 @@ export class Compiler extends DiagnosticEmitter {
14871489
this.currentFunction.flow = blockFlow;
14881490

14891491
var stmts = this.compileStatements(statements);
1490-
var lastType: NativeType;
14911492
var stmt = stmts.length == 0
14921493
? this.module.createNop()
14931494
: stmts.length == 1
14941495
? stmts[0]
1495-
: this.module.createBlock(null, stmts,
1496-
// if the last expression is a value, annotate the block's return value
1497-
(lastType = getExpressionType(stmts[stmts.length - 1])) == NativeType.None
1498-
? NativeType.None
1499-
: lastType
1500-
);
1496+
: this.module.createBlock(null, stmts,getExpressionType(stmts[stmts.length - 1]));
15011497

15021498
// Switch back to the parent flow
15031499
var parentFlow = blockFlow.leaveBranchOrScope();

src/definitions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
* @module definitions
44
*//***/
55

6+
import {
7+
CommonFlags
8+
} from "./common";
9+
610
import {
711
Program,
812
Element,
9-
CommonFlags,
1013
ElementKind,
1114
Global,
1215
Enum,

src/extra/ast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ import {
9292

9393
import {
9494
CommonFlags
95-
} from "../program";
95+
} from "../common";
9696

9797
/** An AST builder. */
9898
export class ASTBuilder {

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ import {
3434
} from "./parser";
3535

3636
import {
37-
Program,
38-
LIBRARY_PREFIX
37+
Program
3938
} from "./program";
4039

4140
/** Parses a source file. If `parser` has been omitted a new one is created. */
@@ -168,4 +167,4 @@ export function buildTSD(program: Program): string {
168167
}
169168

170169
/** Prefix indicating a library file. */
171-
export { LIBRARY_PREFIX };
170+
export { LIBRARY_PREFIX } from "./common";

src/parser.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
*//***/
55

66
import {
7-
Program,
87
CommonFlags,
98
LIBRARY_PREFIX,
109
PATH_DELIMITER
10+
} from "./common";
11+
12+
import {
13+
Program
1114
} from "./program";
1215

1316
import {
@@ -77,7 +80,9 @@ import {
7780
VoidStatement,
7881
WhileStatement,
7982

80-
mangleInternalPath
83+
mangleInternalPath,
84+
nodeIsCallable,
85+
nodeIsGenericCallable
8186
} from "./ast";
8287

8388
const builtinsFile = LIBRARY_PREFIX + "builtins.ts";
@@ -3113,16 +3118,18 @@ export class Parser extends DiagnosticEmitter {
31133118
if (!expr) return null;
31143119
var startPos = expr.range.start;
31153120

3116-
// CallExpression with type arguments
3117-
var typeArguments: CommonTypeNode[] | null;
3118-
while (
3119-
// there might be better ways to distinguish a LESSTHAN from a CALL with type arguments
3120-
(typeArguments = this.tryParseTypeArgumentsBeforeArguments(tn)) ||
3121-
tn.skip(Token.OPENPAREN)
3122-
) {
3123-
let args = this.parseArguments(tn);
3124-
if (!args) return null;
3125-
expr = Node.createCallExpression(expr, typeArguments, args, tn.range(startPos, tn.pos));
3121+
// CallExpression?
3122+
if (nodeIsCallable(expr.kind)) {
3123+
let typeArguments: CommonTypeNode[] | null = null;
3124+
while (
3125+
tn.skip(Token.OPENPAREN)
3126+
||
3127+
nodeIsGenericCallable(expr.kind) && (typeArguments = this.tryParseTypeArgumentsBeforeArguments(tn)) !== null
3128+
) {
3129+
let args = this.parseArguments(tn);
3130+
if (!args) return null;
3131+
expr = Node.createCallExpression(expr, typeArguments, args, tn.range(startPos, tn.pos)); // is again callable
3132+
}
31263133
}
31273134

31283135
var token: Token;

0 commit comments

Comments
 (0)