Implement isDefined and isConstant builtins

This commit is contained in:
dcodeIO
2018-07-14 16:42:00 +02:00
parent 10a9f407bf
commit 3b0fd9aac2
12 changed files with 611 additions and 479 deletions

View File

@ -51,6 +51,11 @@ import {
FlowFlags
} from "./program";
import {
ReportMode
} from "./resolver";
import { CommonFlags } from "./common";
/** Compiles a call to a built-in function. */
export function compileCall(
compiler: Compiler,
@ -128,6 +133,57 @@ export function compileCall(
? module.createI32(1)
: module.createI32(0);
}
case "isDefined": { // isDefined(expression) -> bool
compiler.currentType = Type.bool;
if (operands.length != 1) {
if (typeArguments) {
compiler.error(
DiagnosticCode.Type_0_is_not_generic,
reportNode.range, prototype.internalName
);
}
compiler.error(
DiagnosticCode.Expected_0_arguments_but_got_1,
reportNode.range, "1", operands.length.toString(10)
);
return module.createUnreachable();
}
if (typeArguments) {
compiler.error(
DiagnosticCode.Type_0_is_not_generic,
reportNode.range, prototype.internalName
);
return module.createUnreachable();
}
let element = compiler.resolver.resolveExpression(operands[0], compiler.currentFunction, ReportMode.SWALLOW);
return module.createI32(element ? 1 : 0);
}
case "isConstant": { // isConstant(expression) -> bool
compiler.currentType = Type.bool;
if (operands.length != 1) {
if (typeArguments) {
compiler.error(
DiagnosticCode.Type_0_is_not_generic,
reportNode.range, prototype.internalName
);
}
compiler.error(
DiagnosticCode.Expected_0_arguments_but_got_1,
reportNode.range, "1", operands.length.toString(10)
);
return module.createUnreachable();
}
if (typeArguments) {
compiler.error(
DiagnosticCode.Type_0_is_not_generic,
reportNode.range, prototype.internalName
);
return module.createUnreachable();
}
let expr = compiler.compileExpressionRetainType(operands[0], Type.i32, WrapMode.NONE);
compiler.currentType = Type.bool;
return module.createI32(getExpressionId(expr) == ExpressionId.Const ? 1 : 0);
}
// math