Some thoughts on an initial stdlib to get things going

This commit is contained in:
dcodeIO
2017-12-07 04:37:14 +01:00
parent 325ecf5165
commit 59dafc8d22
25 changed files with 196 additions and 280 deletions

View File

@ -1624,59 +1624,54 @@ export class Compiler extends DiagnosticEmitter {
}
compileIdentifierExpression(expression: IdentifierExpression, contextualType: Type): ExpressionRef {
switch (expression.kind) {
// null
if (expression.kind == NodeKind.NULL) {
if (contextualType.classType) // keep contextualType
return this.options.target == Target.WASM64 ? this.module.createI64(0, 0) : this.module.createI32(0);
if (this.options.target == Target.WASM64) {
this.currentType = Type.u64;
return this.module.createI64(0, 0);
} else {
this.currentType = Type.u32;
case NodeKind.NULL:
if (this.options.target == Target.WASM64) {
if (!contextualType.classType) {
assert(contextualType.kind == TypeKind.USIZE);
this.currentType = Type.usize64;
}
return this.module.createI64(0, 0);
}
if (!contextualType.classType) {
assert(contextualType.kind == TypeKind.USIZE);
this.currentType = Type.usize32;
}
return this.module.createI32(0);
}
// true
} else if (expression.kind == NodeKind.TRUE) {
this.currentType = Type.bool;
return this.module.createI32(1);
case NodeKind.TRUE:
this.currentType = Type.bool;
return this.module.createI32(1);
// false
} else if (expression.kind == NodeKind.FALSE) {
this.currentType = Type.bool;
return this.module.createI32(0);
case NodeKind.FALSE:
this.currentType = Type.bool;
return this.module.createI32(0);
// this
} else if (expression.kind == NodeKind.THIS) {
if (this.currentFunction.instanceMethodOf) {
this.currentType = this.currentFunction.instanceMethodOf.type;
return this.module.createGetLocal(0, this.options.target == Target.WASM64 ? NativeType.I64 : NativeType.I32);
}
this.error(DiagnosticCode._this_cannot_be_referenced_in_current_location, expression.range);
this.currentType = this.options.target == Target.WASM64 ? Type.u64 : Type.u32;
return this.module.createUnreachable();
}
case NodeKind.THIS:
if (this.currentFunction.instanceMethodOf) {
this.currentType = this.currentFunction.instanceMethodOf.type;
return this.module.createGetLocal(0, this.options.target == Target.WASM64 ? NativeType.I64 : NativeType.I32);
}
this.error(DiagnosticCode._this_cannot_be_referenced_in_current_location, expression.range);
this.currentType = this.options.target == Target.WASM64 ? Type.u64 : Type.u32;
return this.module.createUnreachable();
if (expression.kind == NodeKind.IDENTIFIER) {
// NaN
if ((<IdentifierExpression>expression).name == "NaN")
if (this.currentType.kind == TypeKind.F32)
return this.module.createF32(NaN);
else {
case NodeKind.IDENTIFIER:
// TODO: some sort of resolveIdentifier maybe
if ((<IdentifierExpression>expression).name == "NaN") {
if (this.currentType == Type.f32)
return this.module.createF32(NaN);
this.currentType = Type.f64;
return this.module.createF64(NaN);
}
// Infinity
if ((<IdentifierExpression>expression).name == "Infinity")
if (this.currentType.kind == TypeKind.F32)
return this.module.createF32(Infinity);
else {
if ((<IdentifierExpression>expression).name == "Infinity") {
if (this.currentType == Type.f32)
return this.module.createF32(Infinity);
this.currentType = Type.f64;
return this.module.createF64(Infinity);
}
break;
}
const element: Element | null = this.program.resolveElement(expression, this.currentFunction); // reports

1
src/glue/js.d.ts vendored
View File

@ -14,6 +14,7 @@ declare type bool = boolean;
// Raw memory access (here: Binaryen memory)
declare function store<T = u8>(ptr: usize, val: T): void;
declare function load<T = u8>(ptr: usize): T;
declare function assert(isTrue: bool): void;
// Other things that might or might not be useful
declare function select<T>(ifTrue: T, ifFalse: T, condition: bool): T;