commit 7e2b7b6d1bf9ad265cccd908629e052ece71313c
parent 40b307d719e3afa4f04ca96eef6d8c84025b4e65
Author: citbl <citbl@citbl.org>
Date: Sun, 14 Jun 2026 13:45:57 +1000
binding scope in func decl
Diffstat:
5 files changed, 28 insertions(+), 17 deletions(-)
diff --git a/src/binder.adb b/src/binder.adb
@@ -60,8 +60,10 @@ package body Binder is
-- scope contains or error --
-----------------------------
- procedure Scope_Contains_Or_Error
+ -- recursive
+ function Scope_Contains_Or_Error
(Sc : AST_Scope_Ref; Name : Unbounded_String; Kind : AST_Symbol_Kind_Enum)
+ return AST_Scope_Ref
is
Info : AST_Symbol_Info;
begin
@@ -74,7 +76,7 @@ package body Binder is
& " of kind "
& AST_Symbol_Kind_Enum'Image (Kind)
& " in scope.");
- return;
+ return Sc;
else
raise Program_Error
with
@@ -84,7 +86,8 @@ package body Binder is
& AST_Symbol_Kind_Enum'Image (Info.Kind);
end if;
elsif Sc.Parent /= null then
- Scope_Contains_Or_Error (Sc.Parent, Name, Kind);
+ Put_Line ("Not found in current scope, looking in parent...");
+ return Scope_Contains_Or_Error (Sc.Parent, Name, Kind);
else
raise Program_Error
with "Could not find symbol " & To_String (Name) & " in scope.";
@@ -95,12 +98,13 @@ package body Binder is
-- scope func call --
---------------------
- procedure Scope_Call (S : in out Sem; Callee : in Call_Stmt) is
+ function Scope_Call (S : in out Sem; Callee : in Call_Stmt) return AST_Scope_Ref is
+ Found_Scope : AST_Scope_Ref;
begin
-- determine whether the callee exist in scope or bail with error
- Put_Line ("scoping call wip");
- Scope_Contains_Or_Error (S.Scope, Callee.Name, SYM_FUNC);
- null;
+ Put_Line ("scoping call");
+ Found_Scope := Scope_Contains_Or_Error (S.Scope, Callee.Name, SYM_FUNC);
+ return Found_Scope;
end;
--------------------
@@ -120,13 +124,17 @@ package body Binder is
-- scope func --
----------------
- procedure Scope_Func (S : in out Sem; Fn : in Func_Decl) is
+ procedure Scope_Func (S : in out Sem; Fn : in out Func_Decl) is
+ Found_Scope : AST_Scope_Ref;
begin
- -- go through statements and +scope var_decl and -scope calls
+ -- entering function block scope
+ New_Scope_From_Scope (S);
+
for Statement of Fn.Stmts loop
if Statement.Kind = Stmt_Call then
Put_Line ("scoping call");
- Scope_Call (S, Statement.Call);
+ Found_Scope := Scope_Call (S, Statement.Call);
+ Statement.Scope := Found_Scope;
end if;
end loop;
end;
diff --git a/src/nightshade.adb b/src/nightshade.adb
@@ -27,12 +27,13 @@ begin
P : Parser.Parser := Parser.Init (File_Name_Ref, Src_Ref, L.Tokens);
begin
Parser.Parse (P);
- Parser.Print_AST (P);
+ Parser.Print_AST (P.Declarations);
declare
- S : Binder.Sem := Binder.Init (P.Declarations);
+ BoundAST : Binder.Sem := Binder.Init (P.Declarations);
begin
- Binder.Analysis (S);
+ Binder.Analysis (BoundAST);
+ Parser.Print_AST (BoundAST.AST);
end;
end;
end;
diff --git a/src/parser.adb b/src/parser.adb
@@ -2,6 +2,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.Strings.Unbounded; use Ada.Strings.Unbounded;
+with Types; use Types;
package body Parser is
@@ -281,6 +282,7 @@ package body Parser is
Put (To_String (P.Lit_Str) & " ");
end if;
end loop;
+ Put_Line ("Scope: " & AST_Scope_Ref'Image (S.Scope));
end if;
end;
@@ -301,10 +303,10 @@ package body Parser is
end loop;
end;
- procedure Print_AST (P : Parser) is
+ procedure Print_AST (Decls : Declaration_Vectors.Vector) is
begin
Put_Line ("print AST:");
- for Decl of P.Declarations loop
+ for Decl of Decls loop
if Decl.Kind = Function_Kind then
Put_Line ("Function:");
Print_Func_decl (Decl.Func, 1);
diff --git a/src/parser.ads b/src/parser.ads
@@ -15,5 +15,5 @@ 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 (P : Parser);
+ procedure Print_AST (Decls : Declaration_Vectors.Vector);
end Parser;
diff --git a/src/types.ads b/src/types.ads
@@ -67,6 +67,7 @@ package Types is
end record;
type Stmt (Kind : Stmt_Kind) is record
+ Scope : AST_Scope_Ref;
case Kind is
when Stmt_Call =>
Call : Call_Stmt;
@@ -79,7 +80,6 @@ package Types is
-- when Stmt_Expr =>
-- Expr : Expr_Stmt;
- Scope : AST_Scope_Ref;
end case;
end record;