commit a1be864841356b237cbbee42c163384d69995555
parent 258121cfb53b6b002dd0d2f68b91ce5234011620
Author: Erik Oosting <crazazy@tilde.cafe>
Date: Wed, 13 Dec 2023 16:56:15 +0100
add more visitor rules
Diffstat:
4 files changed, 34 insertions(+), 7 deletions(-)
diff --git a/.idea/misc.xml b/.idea/misc.xml
@@ -19,5 +19,5 @@
<component name="Black">
<option name="sdkName" value="Python 3.11 (pythonProject)" />
</component>
- <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (pythonProject)" project-jdk-type="Python SDK" />
+ <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (fp-il)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
diff --git a/.idea/pythonProject.iml b/.idea/pythonProject.iml
@@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
- <orderEntry type="inheritedJdk" />
+ <orderEntry type="jdk" jdkName="Python 3.11 (fp-il)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
diff --git 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
@@ -0,0 +1,2 @@
+antlr4-python3-runtime==4.13.1
+llvmlite==0.41.1