nightshade

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

commit 6ece05c4f7c38cb172215f3dd9d3fc4c94bb3011
parent adfe90e9356cbe9be7be652aa43959e9b822d0c6
Author: citbl <citbl@citbl.org>
Date:   Sat, 13 Jun 2026 18:15:22 +1000

sem wip 2

Diffstat:
Msrc/sem.adb | 45++++++++++++++++++++++++++++++++++++++-------
Msrc/sem.ads | 26++++++++++++++++++--------
2 files changed, 56 insertions(+), 15 deletions(-)

diff --git a/src/sem.adb b/src/sem.adb @@ -9,17 +9,43 @@ package body Sem is function Init (AST : Declaration_Vectors.Vector) return Sem is Symbols : Symbols_Map.Map; - S : constant Sem := (Symbols => Symbols, AST => AST); + Sc : Scope_Ref; + S : Sem; begin + Sc := new Scope'(Parent => null, Symbols => Symbols); + S := (Scope => Sc, AST => AST); return S; end Init; + ----------------------- + -- load std in scope -- + ----------------------- + + procedure Load_STD_Symbols (S : in out Sem) is + begin + S.Scope.Symbols.Include ("print", SYM_CALL); + end; + + -------------------------- + -- new scope from scope -- + -------------------------- + + procedure New_Scope_From_Scope (S : in out Sem) is + Symbols : Symbols_Map.Map; + Sc : Scope_Ref; + begin + Sc := new Scope'(Parent => S.Scope, Symbols => Symbols); + S.Scope := Sc; + end; + --------------------- -- scope func call -- --------------------- - procedure Scope_Call (S : in out Sem) is + procedure Scope_Call (S : in out Sem; Callee : in Call_Stmt) is begin + -- determine whether the callee exist in scope or bail with error + null; end; @@ -36,9 +62,14 @@ package body Sem is -- scope func -- ---------------- - procedure Scope_Func (S : in out Sem; Func : in Func_Decl) is + procedure Scope_Func (S : in out Sem; Fn : in Func_Decl) is begin - null; + -- go through statements and +scope var_decl and -scope calls + for Statement of Fn.Stmts loop + if Statement.Kind = Stmt_Call then + Scope_Call (S, Statement.Call); + end if; + end loop; end; -------------- @@ -47,9 +78,9 @@ package body Sem is procedure Analysis (S : in out Sem) is begin - for X of S.AST loop - if X.Kind = Function_Kind then - Scope_Func(S, X.Func); + for Declaration of S.AST loop + if Declaration.Kind = Function_Kind then + Scope_Func (S, Declaration.Func); end if; null; end loop; diff --git a/src/sem.ads b/src/sem.ads @@ -6,10 +6,7 @@ with Ada.Strings.Hash; package Sem is - type Symbol_Info is record - Name : Unbounded_String; - Kind : Unbounded_String; - end record; + type Symbol_Kind is (SYM_CALL, SYM_VAR, SYM_LET); ---------------------------------------- -- dictionary of String : Symbol_Info -- @@ -18,17 +15,30 @@ package Sem is package Symbols_Map is new Ada.Containers.Indefinite_Hashed_Maps (Key_Type => String, - Element_Type => Symbol_Info, + Element_Type => Symbol_Kind, 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 - Symbols : Symbols_Map.Map; - AST : Declaration_Vectors.Vector; + Scope : Scope_Ref; + AST : Declaration_Vectors.Vector; end record; ---------- @@ -41,6 +51,6 @@ package Sem is -- analysis -- -------------- - procedure Analysis(S: in out Sem); + procedure Analysis (S : in out Sem); end Sem;