Skip to content

Commit dc723d4

Browse files
committed
Feature(compiler): Initial support for Array<T>
1 parent d4902b1 commit dc723d4

3 files changed

Lines changed: 67 additions & 2 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
import * as ts from "typescript";
3+
import * as llvm from 'llvm-node';
4+
import {NodeGenerateInterface} from "../node-generate.interface";
5+
import {Context} from "../context";
6+
import {ArrayReference} from "../value";
7+
8+
export class ArrayLiteralExpressionCodeGenerator implements NodeGenerateInterface<ts.ArrayLiteralExpression, ArrayReference> {
9+
generate(node: ts.ArrayLiteralExpression, ctx: Context, builder: llvm.IRBuilder): ArrayReference {
10+
// const type = ctx.typeChecker.getTypeAtLocation(node);
11+
// const elementType = NativeTypeResolver.getType(type, ctx);
12+
13+
// @todo Fix this hardcore...
14+
const elementType = llvm.Type.getDoublePtrTy(ctx.llvmContext);
15+
16+
const structType = llvm.StructType.create(ctx.llvmContext, 'array<int>');
17+
structType.setBody([
18+
elementType,
19+
llvm.Type.getInt32Ty(ctx.llvmContext),
20+
]);
21+
22+
const allocate = builder.createAlloca(
23+
structType
24+
);
25+
26+
return new ArrayReference(
27+
elementType,
28+
allocate
29+
);
30+
}
31+
}

src/backend/llvm/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ import {RUNTIME_DEFINITION_FILE} from "@static-script/runtime";
99
import {LANGUAGE_DEFINITION_FILE} from "../../constants";
1010
import {CMangler} from "./c.mangler";
1111
import {ManglerInterface} from "./mangler.interface";
12-
import {ClassReference, FunctionReference, ObjectReference, Primitive, Value, ValueTypeEnum} from "./value";
12+
import {
13+
ArrayReference,
14+
ClassReference,
15+
FunctionReference,
16+
ObjectReference,
17+
Primitive,
18+
Value,
19+
ValueTypeEnum
20+
} from "./value";
1321
import {BinaryExpressionCodeGenerator} from "./code-generation/binary-expression";
1422
import {ReturnStatementCodeGenerator} from "./code-generation/return-statement";
1523
import {ForStatementGenerator} from "./code-generation/for-statement";
@@ -19,6 +27,8 @@ import {BreakStatementGenerator} from "./code-generation/break-statement";
1927
import {ContinueStatementGenerator} from "./code-generation/continue-statement";
2028
import {ClassDeclarationGenerator} from "./code-generation/class-statement";
2129
import {NewExpressionGenerator} from "./code-generation/new-expression";
30+
import {ArrayLiteralExpressionCodeGenerator} from "./code-generation/array-literal-expression";
31+
import {ArrayLiteralExpression} from "typescript";
2232

2333
export function passIfStatement(parent: ts.IfStatement, ctx: Context, builder: llvm.IRBuilder) {
2434
const positiveBlock = llvm.BasicBlock.create(ctx.llvmContext, "if.true");
@@ -408,6 +418,8 @@ export function buildFromExpression(block: ts.Expression, ctx: Context, builder:
408418
return buildFromIdentifier(<any>block, ctx, builder);
409419
case ts.SyntaxKind.NumericLiteral:
410420
return buildFromNumericLiteral(<any>block, ctx, builder, nativeType);
421+
case ts.SyntaxKind.ArrayLiteralExpression:
422+
return new ArrayLiteralExpressionCodeGenerator().generate(block as ArrayLiteralExpression, ctx, builder);
411423
case ts.SyntaxKind.StringLiteral:
412424
return buildFromStringValue(<any>block, ctx, builder);
413425
case ts.SyntaxKind.TrueKeyword:
@@ -443,7 +455,7 @@ export function passVariableDeclaration(block: ts.VariableDeclaration, ctx: Cont
443455
let allocate: llvm.AllocaInst;
444456

445457
const defaultValue = buildFromExpression(block.initializer, ctx, builder, nativeTypeForDefaultValue);
446-
if (defaultValue instanceof ObjectReference) {
458+
if (defaultValue instanceof ObjectReference || defaultValue instanceof ArrayReference) {
447459
allocate = defaultValue.getValue();
448460
} else {
449461
allocate = builder.createAlloca(

src/backend/llvm/value.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ export class FunctionReference implements Value {
5555
}
5656
}
5757

58+
export class ArrayReference implements Value {
59+
public elementType: llvm.Type;
60+
public llvmValue: llvm.AllocaInst;
61+
62+
constructor(elementType: llvm.Type, llvmValue: llvm.AllocaInst) {
63+
this.elementType = elementType;
64+
this.llvmValue = llvmValue;
65+
}
66+
67+
getValue(): llvm.AllocaInst {
68+
return this.llvmValue;
69+
}
70+
71+
public toBoolean(ctx: Context, builder: llvm.IRBuilder, node: ts.Node): Value {
72+
throw new UnsupportedError(node, 'Cannot cast ClassReference to boolean');
73+
}
74+
75+
public isString(): boolean {
76+
return false;
77+
}
78+
}
79+
5880
export class ObjectReference implements Value {
5981
public classReference: ClassReference;
6082
public llvmValue: llvm.AllocaInst;

0 commit comments

Comments
 (0)