commit 40b307d719e3afa4f04ca96eef6d8c84025b4e65
parent f45ce5bf66fface5afc9e570a14239b9582e323c
Author: citbl <citbl@citbl.org>
Date: Sun, 14 Jun 2026 13:03:34 +1000
refac binder
Diffstat:
4 files changed, 94 insertions(+), 88 deletions(-)
diff --git a/src/binder.adb b/src/binder.adb
@@ -12,10 +12,10 @@ package body Binder is
function Init (AST : Declaration_Vectors.Vector) return Sem is
Symbols : Symbols_Map.Map;
- Sc : Scope_Ref;
+ Sc : AST_Scope_Ref;
S : Sem;
begin
- Sc := new Scope'(Parent => null, Symbols => Symbols);
+ Sc := new AST_Scope'(Parent => null, Symbols => Symbols);
S := (Scope => Sc, AST => AST);
Load_STD_Symbols (S);
return S;
@@ -26,17 +26,17 @@ package body Binder is
-----------------------
procedure Load_STD_Symbols (S : in out Sem) is
- Info : Symbol_Info;
- Int_Type : constant Type_Info_Ref := new Type_Info'(Kind => TY_INT);
- String_Type : constant Type_Info_Ref := new Type_Info'(Kind => TY_STRING);
- Bool_Type : constant Type_Info_Ref := new Type_Info'(Kind => TY_BOOL);
- Void_Type : constant Type_Info_Ref := new Type_Info'(Kind => TY_VOID);
- Error_Type : constant Type_Info_Ref := new Type_Info'(Kind => TY_ERROR);
- Print_Type : constant Type_Info_Ref :=
- new Type_Info'
+ 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);
+ Bool_Type : constant AST_Type_Info_Ref := new AST_Type_Info'(Kind => TY_BOOL);
+ Void_Type : constant AST_Type_Info_Ref := new AST_Type_Info'(Kind => TY_VOID);
+ Error_Type : constant AST_Type_Info_Ref := new AST_Type_Info'(Kind => TY_ERROR);
+ Print_Type : constant AST_Type_Info_Ref :=
+ new AST_Type_Info'
(Kind => TY_FUNC,
Func =>
- (Return_Type => Void_Type, Params => new Type_List'(1 => String_Type)));
+ (Return_Type => Void_Type, Params => new AST_Type_List'(1 => String_Type)));
begin
Put_Line ("load std symbols");
Info :=
@@ -50,9 +50,9 @@ package body Binder is
procedure New_Scope_From_Scope (S : in out Sem) is
Symbols : Symbols_Map.Map;
- Sc : Scope_Ref;
+ Sc : AST_Scope_Ref;
begin
- Sc := new Scope'(Parent => S.Scope, Symbols => Symbols);
+ Sc := new AST_Scope'(Parent => S.Scope, Symbols => Symbols);
S.Scope := Sc;
end;
@@ -61,9 +61,9 @@ package body Binder is
-----------------------------
procedure Scope_Contains_Or_Error
- (Sc : Scope_Ref; Name : Unbounded_String; Kind : Symbol_Kind_Enum)
+ (Sc : AST_Scope_Ref; Name : Unbounded_String; Kind : AST_Symbol_Kind_Enum)
is
- Info : Symbol_Info;
+ Info : AST_Symbol_Info;
begin
if Sc.Symbols.Contains (To_String (Name)) then
Info := Sc.Symbols.Element (To_String (Name));
@@ -72,16 +72,16 @@ package body Binder is
("Found "
& To_String (Name)
& " of kind "
- & Symbol_Kind_Enum'Image (Kind)
+ & AST_Symbol_Kind_Enum'Image (Kind)
& " in scope.");
return;
else
raise Program_Error
with
"Symbol found in scope was not of expected type "
- & Symbol_Kind_Enum'Image (Kind)
+ & AST_Symbol_Kind_Enum'Image (Kind)
& " but is in fact "
- & Symbol_Kind_Enum'Image (Info.Kind);
+ & AST_Symbol_Kind_Enum'Image (Info.Kind);
end if;
elsif Sc.Parent /= null then
Scope_Contains_Or_Error (Sc.Parent, Name, Kind);
diff --git a/src/binder.ads b/src/binder.ads
@@ -1,80 +1,16 @@
-with Types; use Types;
-
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Hash;
+with Types; use Types;
package Binder is
- type Type_Info;
- type Function_Info;
-
- type Type_Info_Ref is access all Type_Info;
- 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_List is array (Positive range <>) of Type_Info_Ref;
- type Type_List_Ref is access all Type_List;
-
- type Function_Info is record
- Params : Type_List_Ref;
- Return_Type : Type_Info_Ref;
- end record;
-
- type Type_Info (Kind : Type_Kind_Enum := TY_ERROR) is record
- case Kind is
- when TY_FUNC =>
- Func : Function_Info;
-
- when others =>
- null;
- end case;
- end record;
-
- --------------------------------
- -- symbol info -----------------
- -- made of kind and type info --
- --------------------------------
-
- type Symbol_Info is record
- Name : Unbounded_String;
- Kind : Symbol_Kind_Enum;
- Typ : Type_Info_Ref;
- end record;
-
- -------------------------------------
- -- hashmap of String : Symbol_Info --
- -------------------------------------
-
- package Symbols_Map is new
- Ada.Containers.Indefinite_Hashed_Maps
- (Key_Type => String,
- Element_Type => Symbol_Info, -- symbol's info
- Hash => Ada.Strings.Hash,
- Equivalent_Keys => "=");
-
- -------------
- --- scope ---
- -------------
-
- type Scope;
-
- type Scope_Ref is access Scope;
-
- type Scope is record
- Symbols : Symbols_Map.Map;
- Parent : Scope_Ref;
- end record;
-
---------
-- sem --
---------
type Sem is record
- Scope : Scope_Ref;
+ Scope : AST_Scope_Ref;
AST : Declaration_Vectors.Vector;
end record;
diff --git a/src/parser.adb b/src/parser.adb
@@ -144,7 +144,7 @@ package body Parser is
T := Match (P, R_Paren);
T := Match (P, Separator);
CS := Call_Stmt'(Name => Name, Params => Expressions);
- return Stmt'(Kind => Stmt_Call, Call => CS);
+ return Stmt'(Kind => Stmt_Call, Call => CS, Scope => null);
end;
----------------
diff --git a/src/types.ads b/src/types.ads
@@ -1,6 +1,8 @@
+with Ada.Strings.Hash;
with Ada.Containers.Vectors;
with Ada.Containers.Indefinite_Vectors;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
+with Ada.Containers.Indefinite_Hashed_Maps;
package Types is
@@ -38,7 +40,13 @@ package Types is
type Expr;
type Expr_Ref is access Expr;
- ---------
+ --------------------------
+ -- Parser related types --
+ --------------------------
+
+ type AST_Scope;
+
+ type AST_Scope_Ref is access AST_Scope;
type Param is record
Name : Unbounded_String;
@@ -71,11 +79,10 @@ package Types is
-- when Stmt_Expr =>
-- Expr : Expr_Stmt;
+ Scope : AST_Scope_Ref;
end case;
end record;
- -- type Stmt_Ref is access Stmt;
-
type Call_Expr is record
Callee : Unbounded_String;
Params : Expr_Vectors.Vector;
@@ -125,4 +132,67 @@ package Types is
package Declaration_Vectors is new
Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Decl);
+ ---------------------------
+ -- Binding related types --
+ ---------------------------
+
+ type AST_Type_Info;
+ type AST_Function_Info;
+
+ type AST_Type_Info_Ref is access all AST_Type_Info;
+ type AST_Function_Info_Ref is access all AST_Function_Info;
+
+ type AST_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 AST_Type_List is array (Positive range <>) of AST_Type_Info_Ref;
+ type AST_Type_List_Ref is access all AST_Type_List;
+
+ type AST_Function_Info is record
+ Params : AST_Type_List_Ref;
+ Return_Type : AST_Type_Info_Ref;
+ end record;
+
+ type AST_Type_Info (Kind : Type_Kind_Enum := TY_ERROR) is record
+ case Kind is
+ when TY_FUNC =>
+ Func : AST_Function_Info;
+
+ when others =>
+ null;
+ end case;
+ end record;
+
+ --------------------------------
+ -- symbol info -----------------
+ -- made of kind and type info --
+ --------------------------------
+
+ type AST_Symbol_Info is record
+ Name : Unbounded_String;
+ Kind : AST_Symbol_Kind_Enum;
+ Typ : AST_Type_Info_Ref;
+ end record;
+
+ -------------------------------------
+ -- hashmap of String : Symbol_Info --
+ -------------------------------------
+
+ package Symbols_Map is new
+ Ada.Containers.Indefinite_Hashed_Maps
+ (Key_Type => String,
+ Element_Type => AST_Symbol_Info, -- symbol's info
+ Hash => Ada.Strings.Hash,
+ Equivalent_Keys => "=");
+
+ -------------
+ --- scope ---
+ -------------
+
+ type AST_Scope is record
+ Symbols : Symbols_Map.Map;
+ Parent : AST_Scope_Ref;
+ end record;
+
end Types;