Skip to content

Commit 2d0f5f3

Browse files
committed
Comma expressions fwiw
1 parent 7e9b584 commit 2d0f5f3

11 files changed

Lines changed: 764 additions & 152 deletions

File tree

src/ast.ts

Lines changed: 74 additions & 48 deletions
Large diffs are not rendered by default.

src/builtins.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from "./ast";
1616

1717
import {
18-
Type
18+
Type, TypeKind
1919
} from "./types";
2020

2121
import {
@@ -169,8 +169,8 @@ function addFunction(program: Program, name: string, isGeneric: bool = false): F
169169
}
170170

171171
/** Compiles a get of a built-in global. */
172-
export function compileGetConstant(compiler: Compiler, global: Global): ExpressionRef {
173-
switch (global.internalName) {
172+
export function compileGetConstant(compiler: Compiler, global: Global, reportNode: Node): ExpressionRef {
173+
switch (global.internalName) { // switching on strings should become a compiler optimization eventually
174174

175175
case "NaN":
176176
if (compiler.currentType == Type.f32)
@@ -186,10 +186,9 @@ export function compileGetConstant(compiler: Compiler, global: Global): Expressi
186186

187187
case "HEAP_BASE": // constant, but never inlined
188188
return compiler.module.createGetGlobal("HEAP_BASE", (compiler.currentType = <Type>global.type).toNativeType());
189-
190-
default:
191-
throw new Error("not implemented: " + global.internalName);
192189
}
190+
compiler.error(DiagnosticCode.Operation_not_supported, reportNode.range);
191+
return compiler.module.createUnreachable();
193192
}
194193

195194
/** Compiles a call to a built-in function. */
@@ -283,7 +282,7 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
283282
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
284283
return module.createUnreachable();
285284
if ((compiler.currentType = (<Type[]>typeArguments)[0]).isAnyInteger) {
286-
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
285+
arg0 = compiler.compileExpression(operands[0], (<Type[]>typeArguments)[0]);
287286
return (compiler.currentType = (<Type[]>typeArguments)[0]).isLongInteger // sic
288287
? module.createUnary(UnaryOp.ClzI64, arg0)
289288
: (<Type[]>typeArguments)[0].isSmallInteger
@@ -797,7 +796,8 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
797796
return module.createUnreachable();
798797
return compiler.compileExpression(operands[0], Type.f64, ConversionKind.EXPLICIT);
799798
}
800-
return 0;
799+
compiler.error(DiagnosticCode.Operation_not_supported, reportNode.range);
800+
return module.createUnreachable();
801801
}
802802

803803
/** Pre-validates a call to a built-in function. */

src/compiler.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import {
8989
AssertionExpression,
9090
BinaryExpression,
9191
CallExpression,
92+
CommaExpression,
9293
ElementAccessExpression,
9394
FloatLiteralExpression,
9495
IdentifierExpression,
@@ -338,7 +339,7 @@ export class Compiler extends DiagnosticEmitter {
338339
}
339340

340341
compileGlobal(global: Global): bool {
341-
if (global.isCompiled || (global.isBuiltIn && compileBuiltinGetConstant(this, global)))
342+
if (global.isCompiled || global.isBuiltIn)
342343
return true;
343344

344345
var declaration = global.declaration;
@@ -1103,6 +1104,10 @@ export class Compiler extends DiagnosticEmitter {
11031104
expr = this.compileCallExpression(<CallExpression>expression, contextualType);
11041105
break;
11051106

1107+
case NodeKind.COMMA:
1108+
expr = this.compileCommaExpression(<CommaExpression>expression, contextualType);
1109+
break;
1110+
11061111
case NodeKind.ELEMENTACCESS:
11071112
expr = this.compileElementAccessExpression(<ElementAccessExpression>expression, contextualType);
11081113
break;
@@ -2123,8 +2128,13 @@ export class Compiler extends DiagnosticEmitter {
21232128
var elementType: Type;
21242129
switch (element.kind) {
21252130

2126-
case ElementKind.LOCAL:
21272131
case ElementKind.GLOBAL:
2132+
if (!this.compileGlobal(<Global>element)) // reports; not yet compiled if a static field compiled as a global
2133+
return this.module.createUnreachable();
2134+
assert((<Global>element).type != Type.void);
2135+
// fall-through
2136+
2137+
case ElementKind.LOCAL:
21282138
case ElementKind.FIELD:
21292139
elementType = (<VariableLikeElement>element).type;
21302140
break;
@@ -2356,6 +2366,16 @@ export class Compiler extends DiagnosticEmitter {
23562366
return this.module.createCall(functionInstance.internalName, operands, functionInstance.returnType.toNativeType());
23572367
}
23582368

2369+
compileCommaExpression(expression: CommaExpression, contextualType: Type): ExpressionRef {
2370+
var expressions = expression.expressions;
2371+
var k = expressions.length;
2372+
var exprs = new Array<ExpressionRef>(k--);
2373+
for (var i = 0; i < k; ++i)
2374+
exprs[i] = this.compileExpression(expressions[i], Type.void); // drop all
2375+
exprs[i] = this.compileExpression(expressions[i], contextualType); // except last
2376+
return this.module.createBlock(null, exprs, this.currentType.toNativeType());
2377+
}
2378+
23592379
compileElementAccessExpression(expression: ElementAccessExpression, contextualType: Type): ExpressionRef {
23602380
var resolved = this.program.resolveElementAccess(expression, this.currentFunction); // reports
23612381
if (!resolved)
@@ -2426,7 +2446,7 @@ export class Compiler extends DiagnosticEmitter {
24262446

24272447
case ElementKind.GLOBAL:
24282448
if (element.isBuiltIn)
2429-
return compileBuiltinGetConstant(this, <Global>element);
2449+
return compileBuiltinGetConstant(this, <Global>element, expression);
24302450
if (!this.compileGlobal(<Global>element)) // reports; not yet compiled if a static field compiled as a global
24312451
return this.module.createUnreachable();
24322452
assert((<Global>element).type != Type.void);
@@ -2500,6 +2520,8 @@ export class Compiler extends DiagnosticEmitter {
25002520
switch (element.kind) {
25012521

25022522
case ElementKind.GLOBAL: // static property
2523+
if (element.isBuiltIn)
2524+
return compileBuiltinGetConstant(this, <Global>element, propertyAccess);
25032525
if (!this.compileGlobal(<Global>element)) // reports; not yet compiled if a static field compiled as a global
25042526
return this.module.createUnreachable();
25052527
assert((<Global>element).type != Type.void);

0 commit comments

Comments
 (0)