summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Oosting2023-12-13 16:56:15 +0100
committerErik Oosting2023-12-13 16:56:15 +0100
commita1be864841356b237cbbee42c163384d69995555 (patch)
treeacfe4743193d6f0f2992d8c1f1049b0315fe090b
parent258121cfb53b6b002dd0d2f68b91ce5234011620 (diff)
add more visitor rules
-rw-r--r--.idea/misc.xml2
-rw-r--r--.idea/pythonProject.iml2
-rw-r--r--ExpBuilder.py35
-rw-r--r--requirements.txt2
4 files changed, 34 insertions, 7 deletions
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 @@
   <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
index 74d515a..2b5f780 100644
--- 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
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