From a1be864841356b237cbbee42c163384d69995555 Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Wed, 13 Dec 2023 16:56:15 +0100 Subject: add more visitor rules --- ExpBuilder.py | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'ExpBuilder.py') 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()) -- cgit 1.4.1-2-gfad0