commit 808de209635bca9db8bb7139a330adb83282beba
parent c814dff4e1fad2e685828655dae911fbcabc62dc
Author: citbl <citbl@citbl.org>
Date: Mon, 8 Jun 2026 21:29:19 +1000
fix parser errors
Diffstat:
| M | src/parser.adb | | | 107 | +++++++++++++++++++++++++++++++++++++++++-------------------------------------- |
| M | src/parser.ads | | | 26 | ++++++++------------------ |
2 files changed, 63 insertions(+), 70 deletions(-)
diff --git a/src/parser.adb b/src/parser.adb
@@ -6,11 +6,15 @@ package body Parser is
(File_Name : File_Name_Ref; Source : Source_Ref; Tokens : Token_Vector.Vector) return Parser
is
P : Parser :=
- (Source => Source, File_Name => File_Name, Tokens => Tokens, Pos => 1, AST => null);
+ (Source => Source, File_Name => File_Name, Tokens => Tokens, Pos => 1, Top_Program => Top_Level_Vectors.Empty_Vector);
begin
return P;
end;
+ -----------
+ -- utils --
+ -----------
+
-- peek
function Peek (P : in Parser) return Token is
@@ -60,6 +64,10 @@ package body Parser is
raise Program_Error with "Consume: Token out of bounds";
end Consume;
+ ----------------
+ -- Parse Expr --
+ ----------------
+
function Parse_Expr (P : in out Parser) return Expr_Ref is
T : Token;
E : Expr_Ref;
@@ -70,7 +78,10 @@ package body Parser is
end Parse_Expr;
- -- TODO parse func call statement
+ ---------------------
+ -- parse func call --
+ ---------------------
+
function Parse_Call_Statement (P : in out Parser) return Stmt_Ref is
T : Token;
Name : Unbounded_String;
@@ -95,11 +106,13 @@ package body Parser is
T := Match (P, R_Paren);
CS := Call_Stmt'(Name => Name, Params => Expressions);
- ST := new Stmt'(Kind => Stmt_call, Call => CS);
+ ST := new Stmt'(Kind => Stmt_Call, Call => CS);
return ST;
end;
- -- parse stmt
+ ----------------
+ -- parse stmt --
+ ----------------
function Parse_Statement (P : in out Parser) return Stmt_Ref is
T : Token;
@@ -117,63 +130,66 @@ package body Parser is
end;
- -- parse func decl
+ ---------------------
+ -- parse func decl --
+ ---------------------
function Parse_Func_Decl (P : in out Parser) return Decl is
- T : Token;
- Name : Unbounded_String;
- Param_Name : Unbounded_String;
- Param_Kind : Unbounded_String;
- Public : Boolean := false;
- Func : Func_Decl;
- Stmts : Stmt_Vectors.Vector;
- Pa : Param;
- Params : Param_Vectors.Vector;
- S : Statement;
+ T : Token;
+ Name : Unbounded_String;
+ Param_Name : Unbounded_String;
+ Param_Kind : Unbounded_String;
+ Public : Boolean := false;
+ Func : Func_Decl;
+ Stmts : Stmt_Vectors.Vector;
+ Pa : Param;
+ Params : Param_Vectors.Vector;
+ ST : Stmt_Ref;
begin
T := Consume (P); -- fx
- if T.Lexeme = fx then
+ if T.Lexeme = "fx" then
Public := true;
end if;
Name := Match (P, Ident).Lexeme;
- Match (P, L_Paren); -- (
-
- -- func decl params
+ T := Match (P, L_Paren); -- (
loop
+ -- func decl params
T := Peek (P);
exit when T.Kind = EOF or T.Kind = R_Paren;
- Match (P, Ident); -- type of param
- Match (P, Ident); -- name of param
+ T := Match (P, Ident); -- type of param
+ T := Match (P, Ident); -- name of param
Pa := Param'(Name => Param_Name, Kind => Param_Kind);
Params.Append (Pa);
T := Peek (P);
exit when T.Kind /= Comma;
end loop;
- Match (P, R_Paren); -- TODO return kind
- Match (P, ColonColon);
-
- -- func decl statements
+ T := Match (P, R_Paren);
+ -- TODO func decl missing return kind parsing
+ T := Match (P, ColonColon);
loop
+ -- func decl statements
T := Peek (P);
exit when T.Kind = EOF or (T.Kind = Keyword and T.Lexeme = "end");
- S := Parse_Statement (P);
- Stmts.Append (S);
+ ST := Parse_Statement (P);
+ Stmts.Append (ST);
end loop;
return
- Decl
+ Decl'
(Kind => Function_Kind,
- Func => Func_Decl (Name => Name, Params => Params, Return_Kind => "", Stmts => Stmts));
+ Func => Func_Decl'(Name => Name, Params => Params, Return_Kind => To_Unbounded_String(""), Stmts => Stmts));
end Parse_Func_Decl;
- -- parse program
+ -------------------
+ -- parse program --
+ -------------------
- function Parse_Program (P : in out Parser) return Node_Ref is
- N : Node_Ref;
+ procedure Parse_Program (P : in out Parser) is
+ TL : Top_Level_Vectors.Vector;
T : Token;
begin
-- if Next_Token =
@@ -181,34 +197,21 @@ package body Parser is
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);
+ TL.Append(Parse_Func_Decl (P));
end if;
end loop;
- -- type Node is record
- -- Kind : Node_Kind;
- -- Children : Nodes_Vector.Vector;
- -- File_Name : File_Name_Ref;
- -- Line : Positive;
- -- Col : Positive;
- -- end record;
- N :=
- new Node'
- (Kind => NODE_PROGRAM,
- Children => Nodes_Vector.Empty_Vector,
- File_Name => P.File_Name,
- Line => 1,
- Col => 1);
- return N;
+ P.Top_Program := TL;
+
end;
- -- parse
+ -----------
+ -- parse --
+ -----------
procedure Parse (P : in out Parser) is
- N : Node_Ref;
begin
- N := Parse_Program (P);
- P.AST := N;
+ Parse_Program (P);
end Parse;
procedure Print_AST (P : Parser) is
diff --git a/src/parser.ads b/src/parser.ads
@@ -4,8 +4,6 @@ with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Lexer; use Lexer;
package Parser is
- type Node;
- type Node_Ref is access all Node;
type Node_Kind is
(NODE_PROGRAM,
@@ -39,9 +37,6 @@ package Parser is
NODE_EMPTY_Stmt,
NODE_UNKNOWN);
- package Nodes_Vector is new
- Ada.Containers.Vectors (Index_Type => Natural, Element_Type => Node_Ref);
-
type Param is record
Name : Unbounded_String;
Kind : Unbounded_String;
@@ -85,7 +80,6 @@ package Parser is
end case;
end record;
-
type Stmt_Ref is access Stmt;
package Stmt_Vectors is new
@@ -108,6 +102,7 @@ package Parser is
Return_Kind : Unbounded_String;
Stmts : Stmt_Vectors.Vector;
end record;
+
type Decl_Kind is (Function_Kind);
type Decl (Kind : Decl_Kind) is record
@@ -120,20 +115,15 @@ package Parser is
end case;
end record;
- type Node is record
- Kind : Node_Kind;
- Children : Nodes_Vector.Vector;
- File_Name : File_Name_Ref;
- Line : Positive;
- Col : Positive;
- end record;
+ package Top_Level_Vectors is new
+ Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Decl);
type Parser is record
- Source : Source_Ref;
- File_Name : File_Name_Ref;
- Tokens : Lexer.Token_Vector.Vector;
- AST : Node_Ref;
- Pos : Positive;
+ Source : Source_Ref;
+ File_Name : File_Name_Ref;
+ Tokens : Lexer.Token_Vector.Vector;
+ Top_Program : Top_Level_Vectors.Vector;
+ Pos : Positive;
end record;
function Init