nightshade

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

commit fa78956b1f887e5aab7a00245401c6f29401fdf4
parent 6292fbc58d352a8dd6c49411ec04ef21bba9ad27
Author: citbl <citbl@citbl.org>
Date:   Sun, 14 Jun 2026 21:15:09 +1000

matching to base types in the scoping

Diffstat:
Msrc/parser.adb | 14+++++++-------
Msrc/scope.adb | 38+++++++++++++++++++++-----------------
Msrc/scope.ads | 6++++++
Msrc/types.ads | 24+++++++++++++-----------
4 files changed, 47 insertions(+), 35 deletions(-)

diff --git a/src/parser.adb b/src/parser.adb @@ -108,11 +108,12 @@ package body Parser is T := Match (P, Str_Literal); E := new Expr' - (Kind => String_Literal, - Lit_Str => T.Lexeme, - File_Name => P.File_Name, - Line => T.Line, - Col => T.Col); + (Entity_Type => null, + Kind => String_Literal, + Lit_Str => T.Lexeme, + File_Name => P.File_Name, + Line => T.Line, + Col => T.Col); return E; end Parse_Expr; @@ -144,7 +145,7 @@ package body Parser is T := Match (P, R_Paren); T := Match (P, Separator); - CS := Call_Expr'(Name => Name, Params => Expressions); + CS := Call_Expr'(Name => Name, Params => Expressions, Entity_Type => null); return Stmt'(Kind => Stmt_Call, Call => CS, Scope => null); end; @@ -269,5 +270,4 @@ package body Parser is Put_Line ("done parsing"); end Parse; - end Parser; diff --git a/src/scope.adb b/src/scope.adb @@ -6,6 +6,12 @@ package body Scope is procedure Load_STD_Symbols (S : in out Scoper); + -- 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); + ---------- -- init -- ---------- @@ -26,13 +32,8 @@ package body Scope 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); - 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 := + Info : AST_Symbol_Info; + Print_Type : constant AST_Type_Info_Ref := new AST_Type_Info' (Kind => TY_FUNC, Func => @@ -40,7 +41,9 @@ package body Scope is begin Put_Line ("load std symbols"); Info := - (Name => To_Unbounded_String ("print"), Kind => SYM_FUNC, Typ => Print_Type); + (Name => To_Unbounded_String ("print"), + Kind => SYM_FUNC, + Entity_Type => Print_Type); S.Scope.Symbols.Include ("print", Info); end; @@ -61,8 +64,9 @@ package body Scope is ----------------------------- -- recursive - procedure Scope_Contains_Or_Error + function Scope_Contains_Or_Error (Sc : AST_Scope_Ref; Name : Unbounded_String; Kind : AST_Symbol_Kind_Enum) + return AST_Type_Info_Ref is Info : AST_Symbol_Info; begin @@ -75,7 +79,7 @@ package body Scope is & " of kind " & AST_Symbol_Kind_Enum'Image (Kind) & " in scope."); - return; + return Info.Entity_Type; else raise Program_Error with @@ -86,7 +90,7 @@ package body Scope is end if; elsif Sc.Parent /= null then Put_Line ("Not found in current scope, looking in parent..."); - Scope_Contains_Or_Error (Sc.Parent, Name, Kind); + return Scope_Contains_Or_Error (Sc.Parent, Name, Kind); else raise Program_Error with "Could not find symbol " & To_String (Name) & " in scope."; @@ -97,12 +101,13 @@ package body Scope is -- scope call param -- ---------------------- - procedure Scope_Call (S : in out Scoper; Func_Call : in Call_Expr); + procedure Scope_Call (S : in out Scoper; Func_Call : in out Call_Expr); procedure Scope_Expr_Ref (S : in out Scoper; Expr : Expr_Ref) is begin case Expr.Kind is when String_Literal => + Expr.Entity_Type := String_Type; Put_Line ("scoping string literal has no effect"); null; -- no need to scope literals @@ -118,18 +123,17 @@ package body Scope is -- scope func call -- --------------------- - procedure Scope_Call (S : in out Scoper; Func_Call : in Call_Expr) is + procedure Scope_Call (S : in out Scoper; Func_Call : in out Call_Expr) is + Found_Entity_Type : AST_Type_Info_Ref; begin -- determine whether the callee exist in scope or bail with error Put_Line ("scoping call"); - Scope_Contains_Or_Error (S.Scope, Func_Call.Name, SYM_FUNC); + Found_Entity_Type := Scope_Contains_Or_Error (S.Scope, Func_Call.Name, SYM_FUNC); + Func_Call.Entity_Type := Found_Entity_Type; for Param of Func_Call.Params loop Scope_Expr_Ref (S, Param); end loop; - -- Func_Call.Params - - -- TODO scope the parameter's expression as part of the scope_call end; -------------------- diff --git a/src/scope.ads b/src/scope.ads @@ -10,6 +10,12 @@ package Scope is AST : Declaration_Vectors.Vector; end record; + 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); + ---------- -- init -- ---------- diff --git a/src/types.ads b/src/types.ads @@ -48,6 +48,12 @@ package Types is type AST_Scope_Ref is access AST_Scope; + 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 Param is record Name : Unbounded_String; Kind : Unbounded_String; @@ -62,8 +68,9 @@ package Types is type Stmt_Kind is (Stmt_Call); type Call_Expr is record - Name : Unbounded_String; - Params : Expr_Vectors.Vector; + Name : Unbounded_String; + Params : Expr_Vectors.Vector; + Entity_Type : AST_Type_Info_Ref; end record; type Stmt (Kind : Stmt_Kind) is record @@ -88,6 +95,7 @@ package Types is type Expr_Kind is (Expr_Call, String_Literal); type Expr (Kind : Expr_Kind := Expr_Call) is record + Entity_Type : AST_Type_Info_Ref; case Kind is when Expr_Call => Call : Call_Expr; @@ -132,12 +140,6 @@ package Types is -- 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); @@ -166,9 +168,9 @@ package Types is -------------------------------- type AST_Symbol_Info is record - Name : Unbounded_String; - Kind : AST_Symbol_Kind_Enum; - Typ : AST_Type_Info_Ref; + Name : Unbounded_String; + Kind : AST_Symbol_Kind_Enum; + Entity_Type : AST_Type_Info_Ref; end record; -------------------------------------