nightshade

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

commit 6292fbc58d352a8dd6c49411ec04ef21bba9ad27
parent 0fab61cb9153f6579016a7b4d8609c982fb4b6d7
Author: citbl <citbl@citbl.org>
Date:   Sun, 14 Jun 2026 19:56:19 +1000

refac AST stuff

Diffstat:
Msrc/nightshade.adb | 11++++++++---
Msrc/parser.adb | 48------------------------------------------------
Msrc/parser.ads | 1-
Msrc/scope.adb | 22+++++++++++-----------
Msrc/scope.ads | 10+++-------
Msrc/typing.adb | 10++++++++++
Msrc/typing.ads | 2++
Msrc/utils.adb | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/utils.ads | 3+++
9 files changed, 87 insertions(+), 70 deletions(-)

diff --git a/src/nightshade.adb b/src/nightshade.adb @@ -28,13 +28,18 @@ begin P : Parser.Parser := Parser.Init (File_Name_Ref, Src_Ref, L.Tokens); begin Parser.Parse (P); - Parser.Print_AST (P.Declarations); + Utils.Print_AST (P.Declarations); declare - BoundAST : Scope.Sem := Scope.Init (P.Declarations); + BoundAST : Scope.Scoper := Scope.Init (P.Declarations); begin Scope.Analysis (BoundAST); - Parser.Print_AST (BoundAST.AST); + Utils.Print_AST (BoundAST.AST); + declare + TypedAST : Typing.Typer := Typing.Init(P.Declarations); + begin + Typing.Type_Check(TypedAST); + end; end; end; end; diff --git a/src/parser.adb b/src/parser.adb @@ -269,53 +269,5 @@ package body Parser is 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; - Put_Line ("Scope: " & AST_Scope_Ref'Image (S.Scope)); - 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 (Decls : Declaration_Vectors.Vector) is - begin - Put_Line - ("------------------------------ Print AST: ------------------------------------"); - for Decl of Decls loop - if Decl.Kind = Function_Kind then - Put_Line ("Function: (at scope " & AST_Scope_Ref'Image (Decl.Scope) & " )"); - Print_Func_decl (Decl.Func, 1); - end if; - end loop; - Put_Line - ("-------------------------------------------------------------------------------"); - end Print_AST; end Parser; diff --git a/src/parser.ads b/src/parser.ads @@ -15,5 +15,4 @@ package Parser is (File_Name : File_Name_Ref; Source : Source_Ref; Tokens : Types.Token_Vector.Vector) return Parser; procedure Parse (P : in out Parser); - procedure Print_AST (Decls : Declaration_Vectors.Vector); end Parser; diff --git a/src/scope.adb b/src/scope.adb @@ -4,16 +4,16 @@ with Ada.Text_IO; use Ada.Text_IO; package body Scope is - procedure Load_STD_Symbols (S : in out Sem); + procedure Load_STD_Symbols (S : in out Scoper); ---------- -- init -- ---------- - function Init (AST : Declaration_Vectors.Vector) return Sem is + function Init (AST : Declaration_Vectors.Vector) return Scoper is Symbols : Symbols_Map.Map; Sc : AST_Scope_Ref; - S : Sem; + S : Scoper; begin Sc := new AST_Scope'(Parent => null, Symbols => Symbols); S := (Scope => Sc, AST => AST); @@ -25,7 +25,7 @@ package body Scope is -- load std in scope -- ----------------------- - procedure Load_STD_Symbols (S : in out Sem) is + procedure Load_STD_Symbols (S : in out Scoper) is Info : AST_Symbol_Info; Int_Type : constant AST_Type_Info_Ref := new AST_Type_Info'(Kind => TY_INT); String_Type : constant AST_Type_Info_Ref := new AST_Type_Info'(Kind => TY_STRING); @@ -48,7 +48,7 @@ package body Scope is -- new scope from scope -- -------------------------- - procedure New_Scope_From_Scope (S : in out Sem) is + procedure New_Scope_From_Scope (S : in out Scoper) is Symbols : Symbols_Map.Map; Sc : AST_Scope_Ref; begin @@ -97,9 +97,9 @@ package body Scope is -- scope call param -- ---------------------- - procedure Scope_Call (S : in out Sem; Func_Call : in Call_Expr); + procedure Scope_Call (S : in out Scoper; Func_Call : in Call_Expr); - procedure Scope_Expr_Ref (S : in out Sem; Expr : Expr_Ref) is + procedure Scope_Expr_Ref (S : in out Scoper; Expr : Expr_Ref) is begin case Expr.Kind is when String_Literal => @@ -118,7 +118,7 @@ package body Scope is -- scope func call -- --------------------- - procedure Scope_Call (S : in out Sem; Func_Call : in Call_Expr) is + procedure Scope_Call (S : in out Scoper; Func_Call : in Call_Expr) is begin -- determine whether the callee exist in scope or bail with error Put_Line ("scoping call"); @@ -136,7 +136,7 @@ package body Scope is -- scope var decl -- -------------------- - procedure Scope_Var_Decl (S : in out Sem) is + procedure Scope_Var_Decl (S : in out Scoper) is begin -- TODO we do not yet add to scope, but we do not currently have @@ -149,7 +149,7 @@ package body Scope is -- scope func -- ---------------- - procedure Scope_Func (S : in out Sem; Fn : in out Func_Decl) is + procedure Scope_Func (S : in out Scoper; Fn : in out Func_Decl) is Found_Scope : AST_Scope_Ref; begin -- entering function block scope @@ -168,7 +168,7 @@ package body Scope is -- analysis -- -------------- - procedure Analysis (S : in out Sem) is + procedure Analysis (S : in out Scoper) is begin for Declaration of S.AST loop if Declaration.Kind = Function_Kind then diff --git a/src/scope.ads b/src/scope.ads @@ -5,11 +5,7 @@ with Types; use Types; package Scope is - --------- - -- sem -- - --------- - - type Sem is record + type Scoper is record Scope : AST_Scope_Ref; AST : Declaration_Vectors.Vector; end record; @@ -18,12 +14,12 @@ package Scope is -- init -- ---------- - function Init (AST : Declaration_Vectors.Vector) return Sem; + function Init (AST : Declaration_Vectors.Vector) return Scoper; -------------- -- analysis -- -------------- - procedure Analysis (S : in out Sem); + procedure Analysis (S : in out Scoper); end Scope; diff --git a/src/typing.adb b/src/typing.adb @@ -5,4 +5,14 @@ package body Typing is return Typer'(AST => AST); end; + + procedure Type_Check(TT : in out Typer) is + begin + for Declaration of TT.AST loop + if Declaration.Kind = Function_Kind then + null; + end if; + end loop; + end; + end Typing; diff --git a/src/typing.ads b/src/typing.ads @@ -8,4 +8,6 @@ package Typing is function Init (AST : Declaration_Vectors.Vector) return Typer; + procedure Type_Check(TT : in out Typer); + end Typing; diff --git a/src/utils.adb b/src/utils.adb @@ -25,4 +25,54 @@ package body Utils is return To_String (Content); end Load_Contents; + + ----------- + -- 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; + Put_Line ("Scope: " & AST_Scope_Ref'Image (S.Scope)); + 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 (Decls : Declaration_Vectors.Vector) is + begin + Put_Line + ("------------------------------ Print AST: ------------------------------------"); + for Decl of Decls loop + if Decl.Kind = Function_Kind then + Put_Line ("Function: (at scope " & AST_Scope_Ref'Image (Decl.Scope) & " )"); + Print_Func_decl (Decl.Func, 1); + end if; + end loop; + Put_Line + ("-------------------------------------------------------------------------------"); + end Print_AST; + end Utils; diff --git a/src/utils.ads b/src/utils.ads @@ -1,3 +1,6 @@ +with Types; use Types; + package Utils is function Load_Contents (File_Name : String) return String; + procedure Print_AST (Decls : Declaration_Vectors.Vector); end Utils;