commit 6a1a19fc3ffeb64f93eedfa1a1198ee51657ed76
parent 1dfe3c590a36aa11a01e912656ce0069374384a9
Author: citbl <citbl@citbl.org>
Date: Fri, 5 Jun 2026 21:24:19 +1000
parser wip
Diffstat:
| M | src/parser.adb | | | 90 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | src/parser.ads | | | 66 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- |
| M | test.mty | | | 2 | -- |
3 files changed, 153 insertions(+), 5 deletions(-)
diff --git a/src/parser.adb b/src/parser.adb
@@ -77,13 +77,103 @@ package body Parser is
raise Program_Error with "Consume: Token out of bounds";
end Consume;
+ function Parse_Expr(P: in out Parser) return Expr is
+ T: Token;
+ begin
+
+ end Parse_Param;
+
+ -- TODO parse func call statement
+ function Parse_Call_Statement(P : in out Parser) return Call_Statement is
+ T : Token;
+ Name : Unbounded_String;
+ begin
+
+ T := Expect(P, Ident);
+ Name := T.Lexeme;
+ T := Expect(P, L_Paren);
+
+ loop
+ T := Peek(P);
+ exit when T.Kind = EOF or T.Kind = R_Paren;
+
+
+ end loop;
+
+ end Parse_Call_Statement;
+
+
+ -- parse stmt
+
+ function Parse_Statement (P : in out Parser) return Statement is
+ T : Token;
+ begin
+
+ loop
+ T := Peek(P);
+ exit when T.Kind = EOF or (T.Kind = Keyword and T.Lexeme = "end");
+
+ if T.Kind = Ident and Peek2(P).Kind = L_Paren then
+ -- func call
+ Parse_Call_Statement(P);
+ end if; -- TODO support other statement kinds
+
+ end loop;
+
+
+
+ end Parse_Statement;
+
+ -- parse func decl
+
+ function Parse_Func_Decl (P : in out Parser) return Node_Ref is
+ T : Token;
+ Name_Token : Token;
+ Public : Boolean := false;
+ Func : Func_Decl;
+ begin
+ T := Consume (P); -- fx
+ if T.Lexeme = fx then
+ Public := true;
+ end if;
+ Name_Token := Match (P, Ident);
+ Match (P, L_Paren); -- (
+ loop
+ -- params
+ T := Peek (P);
+ exit when T.Kind = EOF or T.Kind = R_Paren;
+ if T.Kind = Comma then
+ Consume (P);
+ goto continue;
+ end if;
+ Expect (P, Ident); -- type of param
+ Expect (P, Ident); -- name of param
+
+ <<continue>>
+ end loop;
+
+ Expect (P, R_Paren);
+ Expect (P, ColonColon);
+
+ -- parse statements
+
+ end Parse_Funcion_Decl;
+
-- parse program
function Parse_Program (P : in out Parser) return Node_Ref is
N : Node_Ref;
+ T : Token;
begin
-- if Next_Token =
+ while Peek (P).Kind /= EOF loop
+ T := Peek (P);
+ if T.Kind = Keyword and (T.Lexeme = "fx" or T.Lexeme = "fn") then
+ return Parse_Func_Decl (P);
+ end if;
+ end loop;
+
-- type Node is record
-- Kind : Node_Kind;
-- Children : Nodes_Vector.Vector;
diff --git a/src/parser.ads b/src/parser.ads
@@ -41,6 +41,67 @@ package Parser is
package Nodes_Vector is new
Ada.Containers.Vectors (Index_Type => Natural, Element_Type => Node_Ref);
+ type Decl_Kind is (Function_Decl, Struct_Decl);
+
+ type Decl (Kind : Decl_Kind := Function_Decl) is record
+ case Kind is
+ when Function_Kind =>
+ Func : Function_Decl;
+
+ when Struct_Kind =>
+ Struct : Struct_Decl;
+ end case;
+ end record;
+
+ type Expr_Kind is (Expr_Call, String_Literal);
+ type Expr (Kind : Expr_Kind := Call_Expr) is record
+ case Kind is
+ when Expr_Call =>
+ Call_Expr : Call_Expr;
+
+ when String_Literal =>
+ Lit_String : Lit_String;
+ end case;
+ end record;
+
+ type Param is record
+ Name : Unbounded_String;
+ Kind : Unbounded_String;
+ end record;
+
+ package Param_Vectors is new
+ Ada.Containers.Vectors (Index_Type => Natural, Element_Type => Param);
+
+ type Func_Decl is record
+ Name : Unbounded_String;
+ Params : Param_Vectors.Vector;
+ Return_Kind : Unbounded_String;
+ Block : Block;
+ end record;
+
+ type Call_Stmt is record
+ Name : Unbounded_String;
+ Params : Param_Vectors.Vector;
+ end record;
+
+ type Statement_Kind is (Stmt_Call, Stmt_If, Stmt_While, Stmt_Expr);
+
+ type Statement (Kind : Statement_Kind := Stmt_call) is record
+ case Kind is
+ when Stmt_Call =>
+ Call_Stmt : Call_Stmt;
+
+ when Stmt_If =>
+ If_Stmt : If_Stmt;
+
+ when Stmt_While =>
+ While_Stmt : While_Stmt;
+
+ when Stmt_Expr =>
+ Expr_Stmt : Expr_Stmt;
+ end case;
+ end record;
+
type Node is record
Kind : Node_Kind;
Children : Nodes_Vector.Vector;
@@ -58,9 +119,8 @@ package Parser is
end record;
function Init
- (File_Name : File_Name_Ref;
- Source : Source_Ref;
- Tokens : Lexer.Token_Vector.Vector) return Parser;
+ (File_Name : File_Name_Ref; Source : Source_Ref; Tokens : Lexer.Token_Vector.Vector)
+ return Parser;
procedure Parse (P : in out Parser);
procedure Print_AST (P : Parser);
end Parser;
diff --git a/test.mty b/test.mty
@@ -1,5 +1,3 @@
-use io
-
// this is a test program
fx main() ::