Jelajahi Sumber

fix irname stuff, ++

Johan Rong 3 hari lalu
induk
melakukan
a7d73096f8

+ 13 - 5
examples/testing/src/main.zk

@@ -2,20 +2,28 @@
     #"@int.fmt = private unnamed_addr constant [5 x i8] c"%ld\0A\00""#
 )
 
+#llvm(
+    #"@float.fmt = private unnamed_addr constant [5 x i8] c"%lf\0A\00""#
+)
+
 #llvm(
     "declare i32 @printf(ptr, ...)"
 )
 
+fn test() -> i64 {
+    return 12 + 2
+}
+
 fn main() -> void {
-    val test: i64 = 32894732 + 1
+    val test: i64 = 123 * 2
+    val test2: f64 = 5.0 * 0.5 + 2.0
     
     #llvm(
-        "%call = call i32 (ptr, ...) @printf(ptr @int.fmt, i64 %{:irname})"
-
+        "%call = call i32 (ptr, ...) @printf(ptr @int.fmt, i64 %v_30_test)"
     )
-    
+
     #llvm(
-        "ret void"
+        "%call.2 = call i32 (ptr, ...) @printf(ptr @float.fmt, double %v_40_test2)"
     )
     
     return

+ 17 - 3
src/zelkelc/ast/parser.py

@@ -32,6 +32,21 @@ class Parser:
     def current_type(self, tok_type: token.Token) -> bool:
         return type(self.tokens[self.cursor]) is tok_type
 
+    @staticmethod
+    def type_to_str(typ: types.Value, is_return_type: bool) -> types.Value:
+        match typ:
+            case types.String:
+                return "str"
+            case types.Integer64:
+                return "i64"
+            case types.Float64:
+                return "f64"
+            case types.Void if is_return_type:
+                return "void"
+            case _:
+                print(f"Invalid type found in type_to_str: {typ}")
+                sys.exit(1)
+
     def parse_type(self, is_return_type: bool) -> types.Value:
         identifier = self.expect_type(token.Identifier)
         match identifier.value:
@@ -44,7 +59,7 @@ class Parser:
             case "void" if is_return_type:
                 return types.Void
             case _:
-                print("Invalid type found in parse_type")
+                print(f"Invalid type found in parse_type: {identifier.value}")
                 sys.exit(1)
 
     def parse(self):
@@ -208,7 +223,6 @@ class Parser:
         self.expect(token.LBrace())
 
         body: list[types.Node] = []
-        hasReturn: bool = False  # TODO: should be replaced with real return tree checking, this is not a good system
 
         while (
             self.cursor < len(self.tokens)
@@ -249,7 +263,7 @@ class Parser:
 
         self.expect(token.RBrace())
 
-        irname = f"{path}.f_{name}_{typ}"
+        irname = f"{path}.f_{name}_{self.type_to_str(typ, True)}"
         if name == "main":
             irname = "zkmain"
 

+ 1 - 1
src/zelkelc/ast/types.py

@@ -107,7 +107,7 @@ class Float64:
     value: int
 
     def to_llvm() -> str:
-        return "f64"
+        return "double"
 
     def __eq__(self, other):
         if not isinstance(other, Float64):

+ 22 - 10
src/zelkelc/codegen/codegen.py

@@ -96,10 +96,7 @@ class Codegen:
                 case types.MacroCall():
                     ir += "\n" + rf"{child_node.body}"
                 case types.ReturnStatement():
-                    print(
-                        f"gen_function_declaration() {type(child_node)} is unimplemented"
-                    )
-                    continue
+                    ir += self.gen_return_statement(child_node)
                 case _:
                     print(
                         f"gen_function_declaration() {type(child_node)} is unimplemented"
@@ -123,15 +120,30 @@ class Codegen:
                     ir += f"\n%{node.irname} = add i64 %{count}, 0"
             case types.Float64:
                 if node.mutable:
-                    ir += f"\n%{node.irname} = alloca f64, align 8"
-                    ir += f"\nstore f64 %{count}, f64* %{node.irname}, align 8"
+                    ir += f"\n%{node.irname} = alloca double, align 8"
+                    ir += f"\nstore double %{count}, double* %{node.irname}, align 8"
                 else:
-                    ir += f"\n%{node.irname} = fadd f64 %{count}, 0"
+                    ir += f"\n%{node.irname} = fadd double %{count}, 0.0"
             case _:
                 print(f"gen_value_declaration() for {node.typ} is unimplemented")
                 sys.exit(1)
 
         return ir
+    
+    def gen_return_statement(self, node: types.ReturnStatement) -> str:
+        print(node)
+        print(node.expr)
+        
+        ir = ""
+        
+        if node.expr:
+            count, expr = self.gen_expression(node.expr)
+            ir += expr
+            ir += f"\nret {node.expr.typ.to_llvm()} %{count}"
+        else:
+            ir += "\nret void"
+            
+        return ir
 
     def make_i64_op(self, sign: str) -> str:
         match sign:
@@ -174,7 +186,7 @@ class Codegen:
                         ir += f"\n%{self.temp_counter} = {self.make_i64_op(node.op)} i64 %{left_irname}, %{right_irname}"
                         self.temp_counter += 1
                     case types.Float64:
-                        ir += f"\n%{self.temp_counter} = {self.make_f64_op(node.op)} f64 %{left_irname}, %{right_irname}"
+                        ir += f"\n%{self.temp_counter} = {self.make_f64_op(node.op)} double %{left_irname}, %{right_irname}"
                         self.temp_counter += 1
                     case types.VariableRef:
                         match node.left.value.typ:
@@ -182,7 +194,7 @@ class Codegen:
                                 ir += f"\n%{self.temp_counter} = {self.make_i64_op(node.op)} i64 %{left_irname}, %{right_irname}"
                                 self.temp_counter += 1
                             case types.Float64:
-                                ir += f"\n%{self.temp_counter} = {self.make_f64_op(node.op)} f64 %{left_irname}, %{right_irname}"
+                                ir += f"\n%{self.temp_counter} = {self.make_f64_op(node.op)} double %{left_irname}, %{right_irname}"
                                 self.temp_counter += 1
                             case _:
                                 print(
@@ -203,7 +215,7 @@ class Codegen:
                         ir += f"\n%{self.temp_counter} = add i64 {node.value.value}, 0"
                         self.temp_counter += 1
                     case types.Float64:
-                        ir += f"\n%{self.temp_counter} = fadd f64 {node.value.value}, 0"
+                        ir += f"\n%{self.temp_counter} = fadd double {node.value.value}, 0.0"
                         self.temp_counter += 1
                     case types.VariableRef:
                         return node.value.irname, ir