diff options
author | Erik Oosting | 2023-12-13 16:56:15 +0100 |
---|---|---|
committer | Erik Oosting | 2023-12-13 16:56:15 +0100 |
commit | a1be864841356b237cbbee42c163384d69995555 (patch) | |
tree | acfe4743193d6f0f2992d8c1f1049b0315fe090b /ExpBuilder.py | |
parent | 258121cfb53b6b002dd0d2f68b91ce5234011620 (diff) |
add more visitor rules
Diffstat (limited to 'ExpBuilder.py')
-rw-r--r-- | ExpBuilder.py | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/ExpBuilder.py b/ExpBuilder.py index 1c9699a..a78cb53 100644 --- a/ExpBuilder.py +++ b/ExpBuilder.py @@ -1,19 +1,44 @@ +from typing import Dict + from antlr4 import TerminalNode +from llvmlite import ir from gen import ANFVisitor from gen.ANFParser import ANFParser -from llvmlite.ir import * class ExpBuilder(ANFVisitor): - module = Module("Program") + module = ir.Module("Program") + i_type = ir.IntType(64) def __init__(self): - self.currentFunc: Function | None = None + self.var_env: Dict[ir.Value] = dict() + self.currentFunc: ir.Function | None = None def visitDef(self, ctx: ANFParser.DefContext): args = [i.getText() for i in ctx.IDENT()][1:] - i_type = IntType(64) - self.currentFunc = Function(self.module, FunctionType(i_type, [i_type] * len(args)), name=ctx.IDENT(0)) + self.currentFunc = ir.Function(self.module, + ir.FunctionType(self.i_type, [self.i_type] * len(args)), + name=ctx.IDENT(0)) self.visit(ctx.cexp()) + + def visitTrue(self, ctx:ANFParser.TrueContext): + return ir.Constant(self.i_type, True) + + def visitFalse(self, ctx: ANFParser.FalseContext): + return ir.Constant(self.i_type, False) + + def visitVar(self, ctx:ANFParser.VarContext): + var_name = ctx.IDENT().getText() + return self.var_env[var_name] + + def visitNum(self, ctx:ANFParser.NumContext): + number = int(ctx.NUMBER().getText()) + + + def visitLet(self, ctx:ANFParser.LetContext): + var_name = ctx.IDENT().getText() + self.var_env[var_name] = self.visit(ctx.funcall()) + self.currentFunc.append_basic_block(self.var_env[var_name]) + return self.visit(ctx.cexp()) |