Ver código fonte

some basic non working blueprint code generation

Johan Rong 4 semanas atrás
pai
commit
afc6d827a3

+ 5 - 0
README.md

@@ -3,3 +3,8 @@
 ## Goals 
 * [ ] Self-hosted
 * [ ] Reference-counting garbage collector
+
+## TODO
+* [ ] Scope checking and stuff in parser
+* [ ] Parser should generate all names (unique), struct indexes, etc for codegen, this should also be in the scope
+    * [ ] Multi pass parser: first scope/symbol collection (what i've been doing now), then check if it makes sense :)

+ 1 - 1
src/zelkelc/__main__.py

@@ -2,7 +2,7 @@ from src.zelkelc.lexer.tokenizer import lex
 from src.zelkelc.ast.parser import Parser
 from src.zelkelc.codegen.codegen import Codegen
 
-import rich # type: ignore
+import rich
 
 source = """
 //test

+ 1 - 0
src/zelkelc/ast/nodes.py

@@ -2,6 +2,7 @@ from __future__ import annotations
 
 from dataclasses import dataclass
 import src.zelkelc.ast.values as values
+import src.zelkelc.ast.scope as scope
 
 @dataclass
 class ClassDeclaration:

+ 31 - 0
src/zelkelc/ast/scope.py

@@ -0,0 +1,31 @@
+from __future__ import annotations
+
+import src.zelkelc.ast.nodes as nodes
+import src.zelkelc.ast.values as values
+from dataclasses import dataclass
+
+@dataclass
+class ClassInfo:
+    name: str
+    members: list[ValueInfo]
+    methods: list[FunctionInfo]
+
+@dataclass 
+class FunctionInfo:
+    name: str
+    static: bool
+    typ: values.Value
+    args: dict[str, values.Value]
+    
+@dataclass
+class ValueInfo:
+    name: str
+    mutable: bool
+    typ: values.Value
+
+@dataclass
+class Scope:
+    classes: nodes.ClassDeclaration
+    functions: nodes.FunctionDeclaration    
+    variables: nodes.ValueDeclaration
+    child: Scope | None

+ 26 - 5
src/zelkelc/codegen/codegen.py

@@ -1,16 +1,37 @@
+from dataclasses import dataclass
+
 import src.zelkelc.ast.nodes as nodes
 import src.zelkelc.ast.values as values
 
+@dataclass
+class IRTable:
+    structs: str
+    struct_functions: str
+    functions: str
+
 class Codegen:
     ast: list[nodes.FunctionDeclaration | nodes.ClassDeclaration]
+    ir: IRTable
     
     def __init__(self, ast: list[nodes.FunctionDeclaration | nodes.ClassDeclaration]):
         self.ast = ast
+        self.ir = IRTable("", "", "")
         
-    def generate(self) -> str:
-        ir = ""
+    def generate(self):        
+        for node in self.ast:
+            match node:
+                case nodes.FunctionDeclaration():
+                    print("TODO: functiondeclaration codegen unimplemented")
+                    continue
+                case nodes.ClassDeclaration():
+                    self.gen_class_declaration(node)
         
-        return ir
+        return self.ir.structs + "\n" + self.ir.struct_functions + "\n" + self.ir.functions
 
-    def gen_class_declaration(self) -> str:
-        
+    def gen_class_declaration(self, node: nodes.ClassDeclaration):
+        member_types: list[str] = []
+        for m in node.members:
+            member_types.append(str(m.typ))
+        member_str = ", ".join(member_types)
+        
+        self.ir.structs += f"\n%{node.name} = type { {member_str} }"

+ 0 - 0
src/zelkelc/evaluator/evaluator.py