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
---
.idea/misc.xml | 2 +-
.idea/pythonProject.iml | 2 +-
ExpBuilder.py | 35 ++++++++++++++++++++++++++++++-----
requirements.txt | 2 ++
4 files changed, 34 insertions(+), 7 deletions(-)
create mode 100644 requirements.txt
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 716a352..6f66444 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -19,5 +19,5 @@
-
+
\ No newline at end of file
diff --git a/.idea/pythonProject.iml b/.idea/pythonProject.iml
index 74d515a..2b5f780 100644
--- a/.idea/pythonProject.iml
+++ b/.idea/pythonProject.iml
@@ -4,7 +4,7 @@
-
+
\ No newline at end of file
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())
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..fc66bdd
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,2 @@
+antlr4-python3-runtime==4.13.1
+llvmlite==0.41.1
--
cgit 1.4.1-2-gfad0