nightshade

The nightshade programming language, compiler and tools (WIP)
Log | Files | Refs | README

commit e17ff4fba9795a95b163184709571a8225c7f1a8
parent c461dc7966bd44c0c326aa5f18818237e9fe0d26
Author: citbl <citbl@citbl.org>
Date:   Tue,  9 Jun 2026 21:33:59 +1000

parsin'

Diffstat:
Msrc/parser.adb | 88++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 67 insertions(+), 21 deletions(-)

diff --git a/src/parser.adb b/src/parser.adb @@ -1,4 +1,8 @@ -with Lexer; use Lexer; +with Lexer; use Lexer; +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Strings; use Ada.Strings; +with Ada.Strings.Fixed; +use Ada.Strings.Fixed; package body Parser is @@ -6,7 +10,11 @@ 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, Top_Program => Top_Level_Vectors.Empty_Vector); + (Source => Source, + File_Name => File_Name, + Tokens => Tokens, + Pos => 1, + Top_Program => Top_Level_Vectors.Empty_Vector); begin return P; end; @@ -37,15 +45,26 @@ package body Parser is -- match - function Match (P : in Parser; Kind : Token_Kind) return Token is + function Match (P : in out Parser; Kind : Token_Kind) return Token is T : Token; begin if P.Pos < P.Tokens.Last_Index then T := P.Tokens (P.Pos); if T.Kind = Kind then + P.Pos := P.Pos + 1; return T; else - raise Program_Error with "expected different token: " & Token_Kind'Image (Kind); + raise Program_Error + with + "Match: expected different token: " + & Token_Kind'Image (Kind) + & " but found: " + & Token_Kind'Image (T.Kind) + & " at L" + & Fixed.Trim(Positive'Image (T.Line), Left) + & ":" + & Fixed.Trim(Positive'Image (T.Col), Left); + end if; end if; raise Program_Error with "Match: Token out of bounds"; @@ -64,6 +83,19 @@ package body Parser is raise Program_Error with "Consume: Token out of bounds"; end Consume; + -------------------- + -- skip new lines -- + -------------------- + + procedure Skip_New_Lines (P : in out Parser) is + T : Token; + begin + T := Peek (P); + if T.Kind = Separator then + P.Pos := P.Pos + 1; + end if; + end; + ---------------- -- Parse Expr -- ---------------- @@ -105,6 +137,7 @@ package body Parser is end loop; T := Match (P, R_Paren); + T := Match (P, Separator); CS := Call_Stmt'(Name => Name, Params => Expressions); ST := new Stmt'(Kind => Stmt_Call, Call => CS); return ST; @@ -117,11 +150,11 @@ package body Parser is function Parse_Statement (P : in out Parser) return Stmt_Ref is T : Token; begin - loop + Skip_New_Lines(P); T := Peek (P); exit when T.Kind = EOF or (T.Kind = Keyword and T.Lexeme = "end"); - + Put_Line ("Parse_Statement"); if T.Kind = Ident and Peek2 (P).Kind = L_Paren then return Parse_Call_Statement (P); end if; @@ -135,23 +168,29 @@ package body Parser is --------------------- 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; - ST : Stmt_Ref; + 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 + + Put_Line ("Parse_Func_Decl"); T := Consume (P); -- fx if T.Lexeme = "fx" then Public := true; end if; Name := Match (P, Ident).Lexeme; + Put_Line("name: " & To_String(Name)); + T := Peek(P); + Put_Line(Token'Image(T)); T := Match (P, L_Paren); -- ( + Put_Line("woefijweoif"); loop -- func decl params @@ -178,9 +217,14 @@ package body Parser is end loop; return - Decl' + Decl' (Kind => Function_Kind, - Func => Func_Decl'(Name => Name, Params => Params, Return_Kind => To_Unbounded_String(""), Stmts => Stmts)); + Func => + Func_Decl' + (Name => Name, + Params => Params, + Return_Kind => To_Unbounded_String (""), + Stmts => Stmts)); end Parse_Func_Decl; @@ -190,14 +234,16 @@ package body Parser is procedure Parse_Program (P : in out Parser) is TL : Top_Level_Vectors.Vector; - T : Token; + T : Token; begin - -- if Next_Token = + Put_Line ("Parse_Program"); while Peek (P).Kind /= EOF loop + Skip_New_Lines (P); T := Peek (P); + Put_Line ("T:" & Token'Image (T)); if T.Kind = Keyword and (T.Lexeme = "fx" or T.Lexeme = "fn") then - TL.Append(Parse_Func_Decl (P)); + TL.Append (Parse_Func_Decl (P)); end if; end loop;