소스 검색

return without value, return void

Johan Rong 3 주 전
부모
커밋
849f87f60f
4개의 변경된 파일과 18개의 추가작업 그리고 47개의 파일을 삭제
  1. 6 1
      src/zelkelc/__main__.py
  2. 10 4
      src/zelkelc/ast/parser.py
  3. 2 1
      src/zelkelc/ast/types.py
  4. 0 41
      src/zelkelc/ast/values.py

+ 6 - 1
src/zelkelc/__main__.py

@@ -6,8 +6,13 @@ from src.zelkelc.codegen.codegen import Codegen
 import rich
 
 source = """
+class String {
+    value: str
+}
+
 fn main() -> i64 {
-    val test: i64 = 10 + (5 * 5)
+    val a: i64 = 1
+    val test: i64 = 74 + 123 * (2+3 * 4)
     return 0
 }
 """

+ 10 - 4
src/zelkelc/ast/parser.py

@@ -1,6 +1,8 @@
 import src.zelkelc.lexer.token as token
 import src.zelkelc.ast.types as types
 
+from typing import Optional
+
 class Parser:
     tokens: list[token.Token]
     cursor: int
@@ -229,7 +231,12 @@ class Parser:
         self.expect(token.Identifier("return"))
         
         # TODO: if there is an expression then check for add it?
-        expr = self.parse_expression()
+    
+        expr: Optional[types.Expression] = None    
+        if not self.current(token.RBrace()):
+            expr = self.parse_expression()
+
+        print(expr)
         
         return types.ReturnStatement(
             expr
@@ -292,7 +299,7 @@ class Parser:
 
         return self.parse_primary_expression()
 
-    def parse_primary_expression(self) -> types.Expression:
+    def parse_primary_expression(self) -> Optional[types.Expression]:
         t = self.tokens[self.cursor]
 
         match t:
@@ -327,8 +334,7 @@ class Parser:
                 return expr
 
             case _:
-                print(f"Unexpected token in primary expression: {t}")
-                exit(1)
+                return None
 
     def _operator_precedence(self, op: str) -> int:
         table = {

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

@@ -1,6 +1,7 @@
 from __future__ import annotations
 
 from dataclasses import dataclass
+from typing import Optional
 
 @dataclass
 class ClassDeclaration:
@@ -40,7 +41,7 @@ class FunctionDeclaration:
     
 @dataclass
 class ReturnStatement:
-    expr: Expression | None
+    expr: Optional[Expression]
 
 Node = ClassDeclaration | ValueDeclaration | FunctionDeclaration | StructDeclaration
 

+ 0 - 41
src/zelkelc/ast/values.py

@@ -1,41 +0,0 @@
-from dataclasses import dataclass
-
-@dataclass
-class Integer64:
-    value: int
-    
-    def to_llvm() -> str:
-        return "i64"
-
-@dataclass
-class String:
-    value: str
-    
-    def to_llvm() -> str:
-        print("Cannot convert String to llvm ir")
-        exit(1)
-
-@dataclass
-class Void:
-    pass
-
-    def to_llvm() -> str:
-        return "void"
-
-@dataclass
-class Variable:
-    name: str
-    
-    def to_llvm() -> str:
-        print("Cannot convert Variable to llvm ir")
-        exit(1)
-
-@dataclass
-class Ptr[T]:
-    pass
-
-@dataclass
-class Val[T]:
-    pass
-
-Value = Integer64 | String | Void | Variable