Skip to content

Commit 35033ad

Browse files
committed
Feature(Backend\LLVM): Introduce SymbolTable
1 parent a25183f commit 35033ad

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

src/Backend/LLVM/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
CallExpression,
1212
SpreadElement,
1313
JSXNamespacedName,
14-
FunctionDeclaration, Identifier, TSTypeAnnotation, ReturnStatement,
14+
FunctionDeclaration,
15+
Identifier,
16+
ReturnStatement,
1517
} from '@babel/types';
1618

1719
export function passBlockStatement(parent: BlockStatement, ctx: Context, builder: llvm.IRBuilder) {
@@ -97,6 +99,11 @@ function buildFromCallExpression(
9799
}
98100

99101
function buildFromIdentifier(block: Identifier, ctx: Context, builder: llvm.IRBuilder): llvm.Value {
102+
const variable = ctx.variables.get(block.name);
103+
if (variable) {
104+
return variable;
105+
}
106+
100107
return ctx.llvmModule.getFunction(block.name);
101108
}
102109

@@ -125,6 +132,10 @@ export function passVariableDeclaration(block: VariableDeclaration, ctx: Context
125132
if (declaration.init) {
126133
const right = buildFromExpression(declaration.init, ctx, builder);
127134

135+
if (declaration.id.type === 'Identifier') {
136+
ctx.variables.set(declaration.id.name, right);
137+
}
138+
128139
return;
129140
}
130141

@@ -154,9 +165,14 @@ export function passStatement(stmt: Statement, ctx: Context, builder: llvm.IRBui
154165
}
155166
}
156167

168+
class SymbolTable extends Map<string, llvm.Value> {
169+
170+
}
171+
157172
class Context {
158173
public llvmContext: llvm.LLVMContext;
159174
public llvmModule: llvm.Module;
175+
public variables: SymbolTable = new SymbolTable();
160176

161177
public constructor() {
162178
this.llvmContext = new llvm.LLVMContext();

src/Frontend/TypeScript/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import {parse} from '@babel/parser'
22

33
const example = `
44
{
5-
function returnVoid(): void {
6-
return;
7-
}
5+
const a = 1;
6+
const b = 2;
7+
const c = a + b;
88
9-
returnVoid();
10-
11-
puts("Hello World!");
9+
puts(c);
1210
}
1311
`;
1412

0 commit comments

Comments
 (0)