nightshade

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

commit ac9861924ca783bcdefbf33f95d65b0efd893548
parent fea965dc915d55b08e0893bc2291bba2c85dabb4
Author: citbl <citbl@citbl.org>
Date:   Wed, 10 Jun 2026 20:56:00 +1000

print ast

Diffstat:
Msrc/parser.adb | 71+++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
Msrc/parser.ads | 4++--
2 files changed, 53 insertions(+), 22 deletions(-)

diff --git a/src/parser.adb b/src/parser.adb @@ -1,6 +1,6 @@ -with Ada.Text_IO; use Ada.Text_IO; -with Ada.Strings; use Ada.Strings; -with Ada.Strings.Fixed; +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 @@ -113,13 +113,12 @@ package body Parser is -- parse func call -- --------------------- - function Parse_Call_Statement (P : in out Parser) return Stmt_Ref is + function Parse_Call_Statement (P : in out Parser) return Stmt is T : Token; Name : Unbounded_String; E : Expr_Ref; Expressions : Expr_Vectors.Vector; CS : Call_Stmt; - ST : Stmt_Ref; begin T := Match (P, Ident); @@ -138,15 +137,14 @@ package body Parser is 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; + return Stmt'(Kind => Stmt_Call, Call => CS); end; ---------------- -- parse stmt -- ---------------- - function Parse_Statement (P : in out Parser) return Stmt_Ref is + function Parse_Statement (P : in out Parser) return Stmt is T : Token; begin T := Peek (P); @@ -154,7 +152,6 @@ package body Parser is T := Peek (P); Skip_New_Lines (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; @@ -175,10 +172,7 @@ package body Parser is 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; @@ -186,7 +180,6 @@ package body Parser is Name := Match (P, Ident).Lexeme; Put_Line ("name: " & To_String (Name)); T := Peek (P); - Put_Line (Token'Image (T)); T := Match (P, L_Paren); -- ( loop @@ -209,8 +202,7 @@ package body Parser is -- func decl statements T := Peek (P); exit when T.Kind = EOF or (T.Kind = Keyword and T.Lexeme = "end"); - ST := Parse_Statement (P); - Stmts.Append (ST); + Stmts.Append (Parse_Statement (P)); end loop; return @@ -231,8 +223,8 @@ package body Parser is ------------------- procedure Parse_Program (P : in out Parser) is - TL : Top_Level_Vectors.Vector; - T : Token; + TL : Top_Level_Vectors.Vector; + T : Token; Counter : Positive := 1; begin Put_Line ("parsing"); @@ -241,7 +233,6 @@ package body Parser is Counter := Counter + 1; 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)); end if; @@ -250,6 +241,7 @@ package body Parser is P.Top_Program := TL; + Put_Line ("done parsing"); end; ----------- @@ -259,12 +251,51 @@ package body Parser is procedure Parse (P : in out Parser) is begin Parse_Program (P); - Put_Line("done parsing"); + Put_Line ("done parsing"); end Parse; + ----------- + -- print -- + ----------- + + procedure Print_Statement (S : in Stmt; N : Natural) is + begin + Put (N * " "); + if S.Kind = Stmt_Call then + Put ("Call: " & To_String (S.Call.Name) & "() with Params: "); + for P of S.Call.Params loop + if P.Kind = String_Literal then + Put (To_String (P.Lit_Str) & " "); + end if; + end loop; + end if; + end; + + procedure Print_Func_decl (F : in Func_Decl; N : Natural) is + begin + Put (N * " "); + Put_Line ("Name: " & To_String (F.Name)); + for P of F.Params loop + Put (N * " "); + Put_Line (To_String (P.Name) & " : " & To_String (P.Kind)); + end loop; + Put ((N * " ") & "Returns: " & To_String (F.Return_Kind)); + Put_Line (""); + Put (N * " "); + Put_Line ("Stmts:"); + for S of F.Stmts loop + Print_Statement (S, N + 1); + end loop; + end; + procedure Print_AST (P : Parser) is begin - null; + for Decl of P.Top_Program loop + if Decl.Kind = Function_Kind then + Put_Line ("Function:"); + Print_Func_decl (Decl.Func, 1); + end if; + end loop; end Print_AST; end Parser; diff --git a/src/parser.ads b/src/parser.ads @@ -80,10 +80,10 @@ package Parser is end case; end record; - type Stmt_Ref is access Stmt; + -- type Stmt_Ref is access Stmt; package Stmt_Vectors is new - Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Stmt_Ref); + Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Stmt); type Expr_Kind is (Expr_Call, String_Literal); type Expr (Kind : Expr_Kind := Expr_Call) is record