Skip to content

Commit c00912d

Browse files
committed
Feature(compiler): TypeOf - initial support
1 parent cab32de commit c00912d

4 files changed

Lines changed: 70 additions & 6 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as ts from "typescript";
2+
import * as llvm from 'llvm-node';
3+
import {NodeGenerateInterface} from "../node-generate.interface";
4+
import {Context} from "../context";
5+
import {Value, ValueTypeEnum} from "../value";
6+
import {NativeType} from "../native-type";
7+
import {buildFromExpression, buildFromString} from "../index";
8+
import UnsupportedError from "../../error";
9+
10+
export class TypeOfExpressionCodeGenerator implements NodeGenerateInterface<ts.TypeOfExpression, Value> {
11+
generate(node: ts.TypeOfExpression, ctx: Context, builder: llvm.IRBuilder, nativeType?: NativeType): Value {
12+
const right = buildFromExpression(node.expression, ctx, builder);
13+
switch (right.getType()) {
14+
case ValueTypeEnum.BOOLEAN:
15+
return buildFromString('boolean', ctx, builder);
16+
case ValueTypeEnum.STRING:
17+
return buildFromString('string', ctx, builder);
18+
case ValueTypeEnum.DOUBLE:
19+
return buildFromString('number', ctx, builder);
20+
case ValueTypeEnum.INT8:
21+
return buildFromString('int8', ctx, builder);
22+
case ValueTypeEnum.INT16:
23+
return buildFromString('int16', ctx, builder);
24+
case ValueTypeEnum.INT32:
25+
return buildFromString('int32', ctx, builder);
26+
case ValueTypeEnum.INT64:
27+
return buildFromString('int64', ctx, builder);
28+
case ValueTypeEnum.INT128:
29+
return buildFromString('int128', ctx, builder);
30+
default:
31+
throw new UnsupportedError(
32+
node,
33+
`Unsupported typeof call`,
34+
)
35+
}
36+
}
37+
}

src/backend/llvm/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {PropertyAccessExpressionCodeGenerator} from "./code-generation/property-
3535
import {TryStatementGenerator} from "./code-generation/try-statement";
3636
import {PostfixUnaryExpressionCodeGenerator} from "./code-generation/postfix-unary-expression";
3737
import {FunctionDeclarationCodeGenerator} from "./code-generation/function-declaration";
38+
import {TypeOfExpressionCodeGenerator} from "./code-generation/typeof-expression";
3839

3940
export function emitCondition(
4041
condition: ts.Expression,
@@ -49,15 +50,17 @@ export function emitCondition(
4950
builder.createCondBr(conditionBoolValue.getValue(), positiveBlock, negativeBlock);
5051
}
5152

52-
export function buildFromStringValue(node: ts.StringLiteral, ctx: Context, builder: llvm.IRBuilder): Value {
53+
export function buildFromString(value: string, ctx: Context, builder: llvm.IRBuilder): Value {
5354
return new Primitive(
54-
builder.createGlobalStringPtr(
55-
node.text,
56-
),
55+
builder.createGlobalStringPtr(value),
5756
ValueTypeEnum.STRING
5857
);
5958
}
6059

60+
export function buildFromStringValue(node: ts.StringLiteral, ctx: Context, builder: llvm.IRBuilder): Value {
61+
return buildFromString(node.text, ctx, builder);
62+
}
63+
6164
export function buildFromTrueKeyword(node: ts.BooleanLiteral, ctx: Context, builder: llvm.IRBuilder): Value {
6265
return new Primitive(
6366
llvm.ConstantInt.get(
@@ -307,9 +310,10 @@ export function buildFromExpression(block: ts.Expression, ctx: Context, builder:
307310
return new CallExpressionCodeGenerator().generate(<any>block, ctx, builder);
308311
case ts.SyntaxKind.ExpressionStatement:
309312
return <any>buildFromExpression((<any>block).expression, ctx, builder);
310-
case ts.SyntaxKind.ParenthesizedExpression: {
313+
case ts.SyntaxKind.TypeOfExpression:
314+
return new TypeOfExpressionCodeGenerator().generate(block as ts.TypeOfExpression, ctx, builder);
315+
case ts.SyntaxKind.ParenthesizedExpression:
311316
return buildFromExpression((<ts.ParenthesizedExpression>block).expression, ctx, builder);
312-
}
313317
default:
314318
throw new UnsupportedError(
315319
block,

tests/snapshots/general/typeof.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
{
3+
function callTypeOfOnStaticTypes(): void {
4+
console_log("callTypeOfOnStaticTypes");
5+
6+
console_log(typeof true);
7+
console_log(typeof false);
8+
console_log(typeof 1);
9+
console_log(typeof 1.0);
10+
console_log(typeof "hey");
11+
12+
console_log("");
13+
}
14+
15+
callTypeOfOnStaticTypes();
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
callTypeOfOnStaticTypes
2+
boolean
3+
boolean
4+
number
5+
number
6+
string
7+

0 commit comments

Comments
 (0)