Sfoglia il codice sorgente

add argument oarsing in function declaration parser

Johan Rong 1 mese fa
parent
commit
22154fd445
3 ha cambiato i file con 24 aggiunte e 10 eliminazioni
  1. 1 1
      src/zelkelc/__main__.py
  2. 19 8
      src/zelkelc/ast/parser.py
  3. 4 1
      src/zelkelc/lexer/token.py

+ 1 - 1
src/zelkelc/__main__.py

@@ -7,7 +7,7 @@ class MyClass {
     val immutable_value: String = "sdf"
     var mutable_value: i64 = 128
     
-    fn function() -> void {
+    fn function(test: i64, test2: String) -> void {
         return
     }
 }

+ 19 - 8
src/zelkelc/ast/parser.py

@@ -23,12 +23,12 @@ class Parser:
         self.cursor += 1
         return self.tokens[self.cursor - 1]
     
-    def next(self, tok: token.Token): 
+    def current(self, tok: token.Token): 
         if not self.tokens[self.cursor] == tok:
             return False
         return True
 
-    def next_type(self, tok_type: token.Token) -> bool:
+    def current_type(self, tok_type: token.Token) -> bool:
         if not type(self.tokens[self.cursor]) == tok_type:
             return False
         return True
@@ -57,7 +57,7 @@ class Parser:
                     match t.value:
                         case "static":
                             self.expect(token.Identifier("static"))
-                            if self.next(token.Identifier("fn")):
+                            if self.current(token.Identifier("fn")):
                                 ast.append(self.parse_function_declaration(True))
                             else:
                                 print(f"Keyword {self.tokens[self.cursor]} does not exist or may not be static")
@@ -106,25 +106,36 @@ class Parser:
         name = self.expect_type(token.Identifier).value
         self.expect(token.LParen())
         
-        # parse declaration arguments
+        args: dict[str, values.Value] = {}
+        while self.cursor < len(self.tokens) and not self.current(token.RParen()):
+            name = self.expect_type(token.Identifier).value
+            if name in args:
+                print(f"Cannot define argument {name} twice")
+                exit(1)
+            
+            self.expect(token.Colon())
+            typ = self.parse_type(False)
+            args[name] = typ
+            
+            if not self.current(token.Comma()):
+                break
+            else:
+                self.expect(token.Comma())
         
         self.expect(token.RParen())
         
         self.expect(token.Arrow())
-        
         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,
             static = static,
-            args = {},
+            args = args,
             body = body,
         )
         

+ 4 - 1
src/zelkelc/lexer/token.py

@@ -64,5 +64,8 @@ class Star:
 class Slash:
     pass
 
+@dataclass
+class Ampersand:
+    pass
 
-Token = Identifier | String | Integer | LParen | RParen | LBrace | RBrace | Period | Comma | Arrow | Colon | Equals | Plus | Minus | Star | Slash
+Token = Identifier | String | Integer | LParen | RParen | LBrace | RBrace | Period | Comma | Arrow | Colon | Equals | Plus | Minus | Star | Slash | Ampersand