Explorar o código

real names and more to make sure generated identifiers are unique, unfinished, ++

Johan Rong hai 3 semanas
pai
achega
caf8721431

+ 2 - 2
src/zelkelc/__main__.py

@@ -11,8 +11,8 @@ struct MyStruct {
 
 //test
 class MyClass {
-    val immutable_value: i64 = 12
-    var mutable_value: i64 = "sjdhfjsd"
+    val immutable_value: i64
+    var mutable_value: i64
     
     fn function(test: i64, test2: i64) -> void {
         return 0

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

@@ -14,7 +14,7 @@ class ClassDeclaration:
 @dataclass
 class StructDeclaration:
     name: str
-    members: list[ValueDeclaration]
+    members: list[MemberDeclaration]
     real_name: str
 
 @dataclass
@@ -22,7 +22,6 @@ class MemberDeclaration:
     name: str
     mutable: bool
     typ: values.Value
-    real_name: str
     real_idx: int
 
 @dataclass

+ 43 - 54
src/zelkelc/ast/parser.py

@@ -38,7 +38,7 @@ class Parser:
     def parse_type(self, is_return_type: bool) -> values.Value:
         identifier = self.expect_type(token.Identifier)
         match identifier.value:
-            case "String":
+            case "str":
                 return values.String
             case "i64":
                 return values.Integer64
@@ -58,11 +58,11 @@ class Parser:
                 case token.Identifier():
                     match t.value:
                         case "fn":
-                            ast.append(self.parse_function_declaration())
+                            ast.append(self.parse_function_declaration("_main.zk_")) # TODO: replace with actual filepath to the file
                         case "struct":
-                            ast.append(self.parse_struct_declaration())
+                            ast.append(self.parse_struct_declaration("_main.zk_"))
                         case "class":
-                            ast.append(self.parse_class_declaration())
+                            ast.append(self.parse_class_declaration("_main.zk_"))
                         case _:
                             print(f"Invalid keyword in global {t.value}")
                             exit(1)
@@ -73,22 +73,11 @@ class Parser:
         
         return ast
 
-    def parse_struct_declaration(self) -> nodes.StructDeclaration:
+    def parse_struct_declaration(self, path: str) -> nodes.StructDeclaration:
         self.expect(token.Identifier("struct"))
         name = self.expect_type(token.Identifier).value
         self.expect(token.LBrace())
         
-        members = self.parse_struct_declaration_body(name)
-        
-        self.expect(token.RBrace())
-        
-        return nodes.StructDeclaration(
-            name,
-            members,
-            real_name = f"s_{name}"
-        )
-        
-    def parse_struct_declaration_body(self, struct_name: str) -> list[nodes.MemberDeclaration]:
         members: list[nodes.MemberDeclaration] = []
                 
         while self.cursor < len(self.tokens) and not self.tokens[self.cursor] == token.RBrace():
@@ -98,19 +87,25 @@ class Parser:
                 case token.Identifier():
                     match t.value:
                         case "val":
-                            members.append(self.parse_member_declaration(False, struct_name, len(members)))
+                            members.append(self.parse_member_declaration(False, len(members)))
                         case "var":
-                            members.append(self.parse_member_declaration(True, struct_name, len(members)))
+                            members.append(self.parse_member_declaration(True, len(members)))
                         case _:
                             print(f"Invalid keyword in struct body {t.value}")
                             exit(1)
                 case _:
                     print(f"Invalid token found while parsing class declaration: {t}")
                     exit(1)
-                            
-        return members
+        
+        self.expect(token.RBrace())
+        
+        return nodes.StructDeclaration(
+            name,
+            members,
+            real_name = f"{path}.s_{name}"
+        )
     
-    def parse_member_declaration(self, mutable: bool, struct_name: str, member_idx) -> nodes.MemberDeclaration:
+    def parse_member_declaration(self, mutable: bool, member_idx: int) -> nodes.MemberDeclaration:
         if mutable:
             self.expect(token.Identifier("var"))
         else:
@@ -125,51 +120,46 @@ class Parser:
             name,
             mutable,
             typ,
-            real_name = f"s_m_{struct_name}_{member_idx}_{name}",
-            real_idx=member_idx
+            real_idx = member_idx
         )
     
-    def parse_class_declaration(self) -> nodes.ClassDeclaration:
+    def parse_class_declaration(self, path: str) -> nodes.ClassDeclaration:
         self.expect(token.Identifier("class"))
         name = self.expect_type(token.Identifier).value
         self.expect(token.LBrace())
         
-        members, methods = self.parse_class_declaration_body()
-        
-        self.expect(token.RBrace())
-        
-        return nodes.ClassDeclaration(
-            name,
-            members,
-            methods,
-        )
-    
-    def parse_class_declaration_body(self) -> tuple[list[nodes.ValueDeclaration], list[nodes.FunctionDeclaration]]:
         members: list[nodes.ValueDeclaration] = []
         methods: list[nodes.FunctionDeclaration] = []
         
         while self.cursor < len(self.tokens) and not self.tokens[self.cursor] == token.RBrace():
             t = self.tokens[self.cursor]
-            
+
             match t:
                 case token.Identifier():
                     match t.value:
                         case "fn":
-                            methods.append(self.parse_function_declaration())
+                            methods.append(self.parse_function_declaration(f"{path}.c_{name}"))
                         case "val":
-                            members.append(self.parse_value_declaration(False))
+                            members.append(self.parse_member_declaration(False, len(members)))
                         case "var":
-                            members.append(self.parse_value_declaration(True))
+                            members.append(self.parse_member_declaration(True, len(members)))
                         case _:
                             print(f"Invalid keyword in class body {t.value}")
                             exit(1)
                 case _:
                     print(f"Invalid token found while parsing class declaration: {t}")
                     exit(1)
-                            
-        return members, methods
+        
+        self.expect(token.RBrace())
+        
+        return nodes.ClassDeclaration(
+            name,
+            members,
+            methods,
+            real_name = f"{path}.c_{name}"
+        )
     
-    def parse_function_declaration(self) -> nodes.FunctionDeclaration:
+    def parse_function_declaration(self, path: str) -> nodes.FunctionDeclaration:
         self.expect(token.Identifier("fn"))
         name = self.expect_type(token.Identifier).value
         self.expect(token.LParen())
@@ -196,17 +186,7 @@ class Parser:
         typ = self.parse_type(True)
         
         self.expect(token.LBrace())
-        body = self.parse_function_declaration_body()
-        self.expect(token.RBrace())
-        
-        return nodes.FunctionDeclaration(
-            name = name,
-            typ = typ,
-            args = args,
-            body = body,
-        )
         
-    def parse_function_declaration_body(self) -> list[nodes.Node]:
         body: list[nodes.Node] = []
         hasReturn: bool = False # TODO: should be replaced with real return tree checking, this is not a good system
         
@@ -217,7 +197,7 @@ class Parser:
                 case token.Identifier():
                     match t.value:
                         case "fn":
-                            body.append(self.parse_function_declaration())
+                            body.append(self.parse_function_declaration(f"{path}.f_{name}"))
                         case "val":
                             body.append(self.parse_value_declaration(False))
                         case "var":
@@ -237,7 +217,15 @@ class Parser:
             print("Function must have return")
             exit(1)
         
-        return body
+        self.expect(token.RBrace())
+        
+        return nodes.FunctionDeclaration(
+            name = name,
+            typ = typ,
+            args = args,
+            body = body,
+            real_name = f"{path}.f_{name}_{typ}"
+        )
     
     def parse_return(self) -> nodes.ReturnStatement:
         self.expect(token.Identifier("return"))
@@ -269,6 +257,7 @@ class Parser:
             mutable,
             typ,
             expr,
+            real_name = f"{self.cursor}_{name}"
         )
     
     def parse_expression(self) -> nodes.Expression:

+ 8 - 9
src/zelkelc/ast/scope.py

@@ -1,30 +1,29 @@
 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:
+class ClassSignature:
     name: str
-    members: list[ValueInfo]
-    methods: list[FunctionInfo]
+    members: list[ValueSignature]
+    methods: list[FunctionSignature]
 
 @dataclass 
-class FunctionInfo:
+class FunctionSignature:
     name: str
     static: bool
     typ: values.Value
     args: dict[str, values.Value]
     
 @dataclass
-class ValueInfo:
+class ValueSignature:
     name: str
     mutable: bool
     typ: values.Value
 
 @dataclass
 class Scope:
-    classes: nodes.ClassDeclaration # burde ikkje bruke node, fordi den har masse unødvendig info, trenger berre navn, real_name, real_idx, ..., args, static,mutable, type, osv
-    functions: nodes.FunctionDeclaration # burde ikkje bruke node, fordi den har masse unødvendig info, trenger berre navn, real_name, real_idx, ..., args, static,mutable, type, osv
-    variables: nodes.ValueDeclaration # burde ikkje bruke node, fordi den har masse unødvendig info, trenger berre navn, real_name, real_idx, ..., args, static,mutable, type, osv
+    classes: ClassSignature
+    functions: FunctionSignature
+    variables: ValueSignature

+ 6 - 4
src/zelkelc/codegen/codegen.py

@@ -1,13 +1,15 @@
-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
     global_functions: str
+    
+    def __init__(self):
+        self.structs = ""
+        self.struct_functions = ""
+        self.global_functions = ""
 
 class Codegen:
     ast: list[nodes.FunctionDeclaration | nodes.ClassDeclaration]
@@ -15,7 +17,7 @@ class Codegen:
     
     def __init__(self, ast: list[nodes.FunctionDeclaration | nodes.ClassDeclaration]):
         self.ast = ast
-        self.ir = IRTable("", "", "")
+        self.ir = IRTable()
         
     def generate(self):
         for node in self.ast: