Skip to content

Commit

Permalink
Code reviews and fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Aug 17, 2023
1 parent 5f4e870 commit 66311f4
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 142 deletions.
12 changes: 8 additions & 4 deletions src/ast.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
BasicBlock,
Constant,
IRBuilder,
Module
Expand All @@ -14,22 +13,27 @@ interface ASTResolveResults {
}

abstract class ASTNode {
public abstract resolve(results: ASTResolveResults): void;
public abstract resolve(
results: ASTResolveResults
): void;

public abstract marker(): Token;
}

abstract class ExpressionAST extends ASTNode {
public abstract visit(
builder: IRBuilder,
module: Module): Constant;
module: Module
): Constant;

public abstract type(): DataType;
}

abstract class StatementAST extends ASTNode {
public abstract visit(
builder: IRBuilder,
module: Module): void;
module: Module
): void;
}

export {
Expand Down
50 changes: 37 additions & 13 deletions src/ast_expr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
BasicBlock,
Constant,
ConstantFP,
ConstantInt,
Expand All @@ -21,7 +20,10 @@ class ExprASTBool implements ExpressionAST {
private value: boolean;
public mark: Token;

public constructor(mark: Token, value: boolean) {
public constructor(
mark: Token,
value: boolean
) {
this.mark = mark;
this.value = value;
}
Expand All @@ -30,6 +32,7 @@ class ExprASTBool implements ExpressionAST {
builder: IRBuilder,
module: Module
): Constant {

return ConstantInt.get(
Type.getIntNTy(LLVMGlobalContext, 1),
this.value ? 1 : 0,
Expand All @@ -41,7 +44,9 @@ class ExprASTBool implements ExpressionAST {
return DataType.BOOL;
}

public resolve(results: ASTResolveResults): void { }
public resolve(
results: ASTResolveResults
): void { }

public marker(): Token {
return this.mark;
Expand All @@ -52,7 +57,10 @@ class ExprASTString implements ExpressionAST {
private value: string;
private mark: Token;

public constructor(mark: Token, value: string) {
public constructor(
mark: Token,
value: string
) {
this.mark = mark;
this.value = value;
}
Expand All @@ -61,19 +69,21 @@ class ExprASTString implements ExpressionAST {
builder: IRBuilder,
module: Module
): Constant {

return builder.CreateGlobalStringPtr(
this.value,
YttriaUtil.generateHash(this.value),
0,
module
0, module
);
}

public type(): DataType {
return DataType.STRING;
}

public resolve(results: ASTResolveResults): void { }
public resolve(
results: ASTResolveResults
): void { }

public marker(): Token {
return this.mark;
Expand All @@ -85,7 +95,11 @@ class ExprASTInt implements ExpressionAST {
private bit: number;
private mark: Token;

public constructor(mark: Token, value: BigInt, bit: number) {
public constructor(
mark: Token,
value: BigInt,
bit: number
) {
this.mark = mark;
this.value = value;
this.bit = bit;
Expand All @@ -95,6 +109,7 @@ class ExprASTInt implements ExpressionAST {
builder: IRBuilder,
module: Module
): Constant {

return ConstantInt.get(
LLVMDataType.getIntType(this.bit),
Number(this.value),
Expand All @@ -112,15 +127,18 @@ class ExprASTInt implements ExpressionAST {
type: string,
results: ASTResolveResults
): void {

if(this.value < min)
results.errors.set(
this.marker(),
'Underflow value for ' + type + ' type: ' + this.value.toString()
'Underflow value for ' + type +
' type: ' + this.value.toString()
);
else if(this.value > max)
results.errors.set(
this.marker(),
'Overflow value for ' + type + ' type: ' + this.value.toString()
'Overflow value for ' + type +
' type: ' + this.value.toString()
);
}

Expand Down Expand Up @@ -179,7 +197,11 @@ class ExprASTFloat implements ExpressionAST {
private bit: number;
private mark: Token;

public constructor(mark: Token, value: number, bit: number) {
public constructor(
mark: Token,
value: number,
bit: number
) {
this.mark = mark;
this.value = value;
this.bit = bit;
Expand All @@ -189,6 +211,7 @@ class ExprASTFloat implements ExpressionAST {
builder: IRBuilder,
module: Module
): Constant {

return ConstantFP.get(
this.type().getLLVMType(),
this.value
Expand All @@ -214,8 +237,8 @@ class ExprASTUnary implements ExpressionAST {
public constructor(
mark: Token,
operator:
string, expr: ExpressionAST) {

string, expr: ExpressionAST
) {
this.mark = mark;
this.operator = operator;
this.expr = expr;
Expand All @@ -225,6 +248,7 @@ class ExprASTUnary implements ExpressionAST {
builder: IRBuilder,
module: Module
): Constant {

const visited: Constant =
this.expr.visit(builder, module);

Expand Down
41 changes: 34 additions & 7 deletions src/ast_stmt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { IRBuilder, Function, Module, BasicBlock, Constant, FunctionType, ConstantInt } from "llvm-bindings";
import { ASTResolveResults, ExpressionAST, StatementAST } from "./ast";
import {
IRBuilder,
Function,
Module,
BasicBlock,
Constant,
FunctionType,
ConstantInt
} from "llvm-bindings";

import {
ASTResolveResults,
ExpressionAST,
StatementAST
} from "./ast";

import { Token } from "./token";
import { DataType } from "./data_type";
import { ExprASTString } from "./ast_expr";
Expand All @@ -11,7 +25,10 @@ class StmtASTMain implements StatementAST {
private mark: Token;
private body: StatementAST;

public constructor(mark: Token, body: StatementAST) {
public constructor(
mark: Token,
body: StatementAST
) {
this.mark = mark;
this.body = body;
}
Expand All @@ -20,6 +37,7 @@ class StmtASTMain implements StatementAST {
builder: IRBuilder,
module: Module
): void {

const main: BasicBlock = BasicBlock.Create(
LLVMGlobalContext,
'entry',
Expand All @@ -43,8 +61,9 @@ class StmtASTMain implements StatementAST {
);
}

public resolve(results: ASTResolveResults): void {
}
public resolve(
results: ASTResolveResults
): void { }

public marker(): Token {
return this.mark;
Expand All @@ -55,7 +74,10 @@ class StmtASTRender implements StatementAST {
private mark: Token;
private expr: ExpressionAST;

public constructor(mark: Token, expr: ExpressionAST) {
public constructor(
mark: Token,
expr: ExpressionAST
) {
this.mark = mark;
this.expr = expr;
}
Expand All @@ -66,10 +88,12 @@ class StmtASTRender implements StatementAST {
): void {
let formatter: string = "";
let formatted: Constant = this.expr.visit(builder, module);

const dataType: DataType = this.expr.type();

if(DataType.isOfIntType(dataType) ||
dataType == DataType.BOOL) {

if(dataType == DataType.I64)
formatter = '%llu';
else formatter = '%d';
Expand Down Expand Up @@ -98,4 +122,7 @@ class StmtASTRender implements StatementAST {
}
}

export { StmtASTMain, StmtASTRender };
export {
StmtASTMain,
StmtASTRender
};
47 changes: 26 additions & 21 deletions src/data_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ class DataType {
private bit: number;
private llvmType: Type;

public constructor(name: string, bit: number, llvmType: Type | undefined) {
public constructor(
name: string,
bit: number,
llvmType: Type | undefined
) {
this.name = name;
this.bit = bit;
this.llvmType = llvmType as Type;
Expand Down Expand Up @@ -47,7 +51,9 @@ class DataType {
public static F64: DataType =
new DataType("f64", 64, Type.getDoubleTy(LLVMGlobalContext));

public static isOfIntType(type: DataType): boolean {
public static isOfIntType(
type: DataType
): boolean {
switch(type) {
case DataType.I4:
case DataType.I8:
Expand All @@ -60,14 +66,11 @@ class DataType {
return false;
}

public static isOfFloatType(type: DataType): boolean {
switch(type) {
case DataType.F32:
case DataType.F64:
return true;
}

return false;
public static isOfFloatType(
type: DataType
): boolean {
return type == DataType.F32 ||
type == DataType.F64;
}
}

Expand All @@ -87,22 +90,21 @@ class LLVMDataType {
[64, [Type.getInt64Ty(LLVMGlobalContext),DataType.I64]],
]);

public static getFloatType(bit: number): Type {
public static getFloatType(
bit: number
): Type {
if(LLVMDataType.floatTypeMap.has(bit))
return LLVMDataType.floatTypeMap.get(bit)![0];

return Type.getFloatTy(LLVMGlobalContext);
}

public static getFloatDataType(bit: number): DataType {
switch(bit) {
case 32:
return DataType.F32;
case 64:
return DataType.F64;
}
public static getFloatDataType(
bit: number
): DataType {

return DataType.UNKNOWN;
return bit == 32 ?
DataType.F32 : DataType.F64;
}

public static getIntType(bit: number): Type {
Expand All @@ -126,8 +128,11 @@ class LLVMDataType {
return DataType.I64;
}

return DataType.UNKNOWN
return DataType.UNKNOWN;
}
}

export { DataType, LLVMDataType };
export {
DataType,
LLVMDataType
};
3 changes: 2 additions & 1 deletion src/llvm_context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LLVMContext } from "llvm-bindings"

const LLVMGlobalContext: LLVMContext = new LLVMContext();
const LLVMGlobalContext: LLVMContext =
new LLVMContext();

export default LLVMGlobalContext;
Loading

0 comments on commit 66311f4

Please sign in to comment.