commit f45ce5bf66fface5afc9e570a14239b9582e323c
parent ac598df694907701d3aeda4b5acfd1458aad5aaf
Author: citbl <citbl@citbl.org>
Date: Sat, 13 Jun 2026 22:19:03 +1000
refactor types to generic package
Diffstat:
7 files changed, 148 insertions(+), 138 deletions(-)
diff --git a/src/binder.adb b/src/binder.adb
@@ -39,7 +39,8 @@ package body Binder is
(Return_Type => Void_Type, Params => new Type_List'(1 => String_Type)));
begin
Put_Line ("load std symbols");
- Info := (Name => To_Unbounded_String("print"), Kind => SYM_FUNC, Typ => Print_Type);
+ Info :=
+ (Name => To_Unbounded_String ("print"), Kind => SYM_FUNC, Typ => Print_Type);
S.Scope.Symbols.Include ("print", Info);
end;
diff --git a/src/binder.ads b/src/binder.ads
@@ -1,4 +1,4 @@
-with Parser; use Parser;
+with Types; use Types;
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
@@ -13,7 +13,8 @@ package Binder is
type Function_Info_Ref is access all Function_Info;
type Symbol_Kind_Enum is (SYM_FUNC, SYM_VAR, SYM_LET);
- type Type_Kind_Enum is (TY_STRING, TY_INT, TY_FLOAT, TY_BOOL, TY_VOID, TY_FUNC, TY_ERROR);
+ type Type_Kind_Enum is
+ (TY_STRING, TY_INT, TY_FLOAT, TY_BOOL, TY_VOID, TY_FUNC, TY_ERROR);
type Type_List is array (Positive range <>) of Type_Info_Ref;
type Type_List_Ref is access all Type_List;
@@ -33,7 +34,6 @@ package Binder is
end case;
end record;
-
--------------------------------
-- symbol info -----------------
-- made of kind and type info --
diff --git a/src/lexer.ads b/src/lexer.ads
@@ -1,40 +1,9 @@
with Ada.Containers.Vectors;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
+with Types; use Types;
package Lexer is
- type Source_Ref is not null access constant String;
- type File_Name_Ref is not null access constant String;
-
- type Token_Kind is
- (Ident,
- Float_Literal,
- Int_Literal,
- Str_Literal,
- Bool_Literal,
- L_Paren,
- R_Paren,
- L_Bracket,
- R_Bracket,
- L_Brace,
- R_Brace,
- ColonColon,
- Colon,
- Comma,
- Separator,
- Keyword,
- Unknown,
- EOF);
-
- type Token is record
- Kind : Token_Kind;
- Lexeme : Unbounded_String;
- Line, Col : Positive;
- end record;
-
- package Token_Vector is new
- Ada.Containers.Vectors (Index_Type => Natural, Element_Type => Token);
-
type Lexer is record
Source : Source_Ref;
File_Name : File_Name_Ref;
diff --git a/src/nightshade.adb b/src/nightshade.adb
@@ -1,6 +1,7 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
with Utils;
+with Types;
with Lexer;
with Parser;
with Binder;
@@ -15,8 +16,8 @@ begin
declare
File_Name : constant String := Argument (1);
Buffer : constant String := Utils.Load_Contents (File_Name);
- Src_Ref : constant Lexer.Source_Ref := new String'(Buffer);
- File_Name_Ref : constant Lexer.File_Name_Ref := new String'(File_Name);
+ Src_Ref : constant Types.Source_Ref := new String'(Buffer);
+ File_Name_Ref : constant Types.File_Name_Ref := new String'(File_Name);
L : Lexer.Lexer := Lexer.Init (File_Name_Ref, Src_Ref);
begin
Lexer.Lex (L);
@@ -28,10 +29,10 @@ begin
Parser.Parse (P);
Parser.Print_AST (P);
- declare
- S : Binder.Sem := Binder.Init(P.Declarations);
+ declare
+ S : Binder.Sem := Binder.Init (P.Declarations);
begin
- Binder.Analysis(S);
+ Binder.Analysis (S);
end;
end;
end;
diff --git a/src/parser.adb b/src/parser.adb
@@ -1,6 +1,7 @@
-with Ada.Text_IO; use Ada.Text_IO;
-with Ada.Strings; use Ada.Strings;
-with Ada.Strings.Fixed; use 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;
+with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package body Parser is
diff --git a/src/parser.ads b/src/parser.ads
@@ -1,108 +1,18 @@
-with Ada.Containers.Vectors;
-with Ada.Containers.Indefinite_Vectors;
-with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
-with Lexer; use Lexer;
+with Lexer; use Lexer;
+with Types; use Types;
package Parser is
- type Expr;
- type Expr_Ref is access Expr;
-
- type Param is record
- Name : Unbounded_String;
- Kind : Unbounded_String;
- end record;
-
- package Param_Vectors is new
- Ada.Containers.Vectors (Index_Type => Natural, Element_Type => Param);
-
- package Expr_Vectors is new
- Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Expr_Ref);
-
- type Stmt_Kind is (Stmt_Call);
-
- type Call_Stmt is record
- Name : Unbounded_String;
- Params : Expr_Vectors.Vector;
- end record;
-
- type Stmt (Kind : Stmt_Kind) is record
- case Kind is
- when Stmt_Call =>
- Call : Call_Stmt;
-
- -- when Stmt_If =>
- -- Iff : If_Stmt;
-
- -- when Stmt_While =>
- -- Whilee : While_Stmt;
-
- -- when Stmt_Expr =>
- -- Expr : Expr_Stmt;
- end case;
- end record;
-
- -- type Stmt_Ref is access Stmt;
-
- type Call_Expr is record
- Callee : Unbounded_String;
- Params : Expr_Vectors.Vector;
- end record;
-
- package Stmt_Vectors is new
- 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
- case Kind is
- when Expr_Call =>
- Call : Call_Expr;
-
- when String_Literal =>
- Lit_Str : Unbounded_String;
-
- File_Name : File_Name_Ref;
- Line : Positive;
- Col : Positive;
- end case;
- end record;
-
- type Func_Decl is record
- Name : Unbounded_String;
- Params : Param_Vectors.Vector;
- Return_Kind : Unbounded_String;
- Stmts : Stmt_Vectors.Vector;
- Public : Boolean;
- File_Name : File_Name_Ref;
- Line : Positive;
- Col : Positive;
- end record;
-
- type Decl_Kind is (Function_Kind);
-
- type Decl (Kind : Decl_Kind) is record
- case Kind is
- when Function_Kind =>
- Func : Func_Decl;
-
- -- when Struct_Kind =>
- -- Struct : Struct_Decl;
- end case;
- end record;
-
- package Declaration_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;
+ Tokens : Types.Token_Vector.Vector;
Declarations : Declaration_Vectors.Vector;
Pos : Positive;
end record;
function Init
- (File_Name : File_Name_Ref; Source : Source_Ref; Tokens : Lexer.Token_Vector.Vector)
+ (File_Name : File_Name_Ref; Source : Source_Ref; Tokens : Types.Token_Vector.Vector)
return Parser;
procedure Parse (P : in out Parser);
procedure Print_AST (P : Parser);
diff --git a/src/types.ads b/src/types.ads
@@ -0,0 +1,128 @@
+with Ada.Containers.Vectors;
+with Ada.Containers.Indefinite_Vectors;
+with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
+
+package Types is
+
+ type Token_Kind is
+ (Ident,
+ Float_Literal,
+ Int_Literal,
+ Str_Literal,
+ Bool_Literal,
+ L_Paren,
+ R_Paren,
+ L_Bracket,
+ R_Bracket,
+ L_Brace,
+ R_Brace,
+ ColonColon,
+ Colon,
+ Comma,
+ Separator,
+ Keyword,
+ Unknown,
+ EOF);
+
+ type Source_Ref is not null access constant String;
+ type File_Name_Ref is not null access constant String;
+
+ type Token is record
+ Kind : Token_Kind;
+ Lexeme : Unbounded_String;
+ Line, Col : Positive;
+ end record;
+
+ package Token_Vector is new
+ Ada.Containers.Vectors (Index_Type => Natural, Element_Type => Token);
+ type Expr;
+ type Expr_Ref is access Expr;
+
+ ---------
+
+ type Param is record
+ Name : Unbounded_String;
+ Kind : Unbounded_String;
+ end record;
+
+ package Param_Vectors is new
+ Ada.Containers.Vectors (Index_Type => Natural, Element_Type => Param);
+
+ package Expr_Vectors is new
+ Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Expr_Ref);
+
+ type Stmt_Kind is (Stmt_Call);
+
+ type Call_Stmt is record
+ Name : Unbounded_String;
+ Params : Expr_Vectors.Vector;
+ end record;
+
+ type Stmt (Kind : Stmt_Kind) is record
+ case Kind is
+ when Stmt_Call =>
+ Call : Call_Stmt;
+
+ -- when Stmt_If =>
+ -- Iff : If_Stmt;
+
+ -- when Stmt_While =>
+ -- Whilee : While_Stmt;
+
+ -- when Stmt_Expr =>
+ -- Expr : Expr_Stmt;
+ end case;
+ end record;
+
+ -- type Stmt_Ref is access Stmt;
+
+ type Call_Expr is record
+ Callee : Unbounded_String;
+ Params : Expr_Vectors.Vector;
+ end record;
+
+ package Stmt_Vectors is new
+ 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
+ case Kind is
+ when Expr_Call =>
+ Call : Call_Expr;
+
+ when String_Literal =>
+ Lit_Str : Unbounded_String;
+
+ File_Name : File_Name_Ref;
+ Line : Positive;
+ Col : Positive;
+ end case;
+ end record;
+
+ type Func_Decl is record
+ Name : Unbounded_String;
+ Params : Param_Vectors.Vector;
+ Return_Kind : Unbounded_String;
+ Stmts : Stmt_Vectors.Vector;
+ Public : Boolean;
+ File_Name : File_Name_Ref;
+ Line : Positive;
+ Col : Positive;
+ end record;
+
+ type Decl_Kind is (Function_Kind);
+
+ type Decl (Kind : Decl_Kind) is record
+ case Kind is
+ when Function_Kind =>
+ Func : Func_Decl;
+
+ -- when Struct_Kind =>
+ -- Struct : Struct_Decl;
+ end case;
+ end record;
+
+ package Declaration_Vectors is new
+ Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Decl);
+
+end Types;