nightshade

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

commit 3a9b88fddeeb3588a6a6d7983e9b07ec6b44e9eb
parent 13b736df6bd78a6cedf711f8002fddefa5ad97bf
Author: citbl <citbl@citbl.org>
Date:   Thu,  4 Jun 2026 22:11:11 +1000

parsing wip

Diffstat:
Msrc/nightshade.adb | 4++--
Msrc/parser.adb | 48+++++++++++++++++++++++++++++++++++++-----------
Msrc/parser.ads | 54++++++++++++++++++++++++++++++++++++++++++++++++++++--
3 files changed, 91 insertions(+), 15 deletions(-)

diff --git a/src/nightshade.adb b/src/nightshade.adb @@ -25,9 +25,9 @@ begin Put_Line ("done lexing"); declare - P : Parser.Parser := Parser.Init (File_Name_Ref, Src_Ref); + P : Parser.Parser := Parser.Init (File_Name_Ref, Src_Ref, L.Tokens); begin - Parser.Parse(P, L.Tokens); + Parser.Parse(P); Parser.Print_AST (P); end; end; diff --git a/src/parser.adb b/src/parser.adb @@ -1,28 +1,54 @@ with Lexer; use Lexer; package body Parser is + function Init - (File_Name : File_Name_Ref; Source : Source_Ref) - return Parser + (File_Name : File_Name_Ref; Source : Source_Ref; Tokens : Token_Vector.Vector) return Parser is P : Parser := - (Source => Source, File_Name => File_Name, Pos => 1); + (Source => Source, File_Name => File_Name, Tokens => Tokens, Pos => 1, AST => null); begin return P; end Init; - procedure Parse_Program (P : in out Parser; Next_Token : Token) is + function Parse_Program (P : in out Parser) return Node_Ref is + N : Node_Ref; begin - if Next_Token = + -- if Next_Token = + + -- 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; end Parse_Program; - procedure Parse (P : in out Parser; Tokens : Token_Vector.Vector) is - Next_Token : Token := Tokens(P.Pos); + function Peek (P : in Parser) return Token is + T : Token; begin - while Next_Token /= EOF loop - Parse_Program(P, Next_Token); - end loop; - null; + if P.Pos < P.Tokens.Last_Index then + T := P.Tokens (P.Pos + 1); + end if; + return Token'(Kind => EOF, Lexeme => To_Unbounded_String(""), Line => 1, Col => 1); + end Peek; + + -- parse + + procedure Parse (P : in out Parser) is + N : Node_Ref; + begin + N := Parse_Program (P); + P.AST := N; end Parse; procedure Print_AST (P : Parser) is diff --git a/src/parser.ads b/src/parser.ads @@ -3,14 +3,64 @@ 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, + NODE_FUNCTION_DECL, + NODE_STRUCT_DECL, + NODE_STRUCT_IMPL, + NODE_PARAM, + NODE_VAR_DECL, + NODE_VAR_ASSIGN, + NODE_FIELD_DECL, + NODE_FIELD_IMPL, + NODE_METHOD_DECL, + NODE_BLOCK, + NODE_CALL_EXPR, + NODE_RETURN, + NODE_BREAK, + NODE_CONTINUE, + NODE_INT_LITERAL, + NODE_FLOAT_LITERAL, + NODE_STRING_LITERAL, + NODE_BOOL_LITERAL, + NODE_IDENT, + NODE_TYPE, + NODE_BINARY_EXPR, + NODE_UNARY_EXPR, + NODE_EXPR_STATEMENT, + NODE_SUBSCRIPT_EXPR, + NODE_IF, + NODE_WHILE, + NODE_FOR, + NODE_EMPTY_STATEMENT, + NODE_UNKNOWN); + + package Nodes_Vector is new + Ada.Containers.Vectors (Index_Type => Natural, Element_Type => Node_Ref); + + type Node is record + Kind : Node_Kind; + Children : Nodes_Vector.Vector; + File_Name : File_Name_Ref; + Line : Positive; + Col : Positive; + end record; + type Parser is record Source : Source_Ref; File_Name : File_Name_Ref; + Tokens : Lexer.Token_Vector.Vector; + AST : Node_Ref; Pos : Positive; end record; function Init - (File_Name : File_Name_Ref; Source : Source_Ref) return Parser; - procedure Parse (P : in out Parser; Tokens : Lexer.Token_Vector.Vector); + (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;