Skip to content

Commit bbf5334

Browse files
committed
[SB05-065] lkql: Add the introspection builtin "get_symbols"
Will be used by the documentation generator to get the symbols of the stdlib/an imported module, in order to be able to get the doc for them. Change-Id: I6d6696ad387fe55754868a679394c6de7a481acc
1 parent 4e70ec9 commit bbf5334

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

lkql/extensions/src/lkql-builtin_functions.adb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ package body LKQL.Builtin_Functions is
6767
function Eval_Profile
6868
(Ctx : Eval_Context; Args : Primitive_Array) return Primitive;
6969

70+
function Eval_Get_Symbols
71+
(Ctx : Eval_Context; Args : Primitive_Array) return Primitive;
72+
7073
function Eval_Help
7174
(Ctx : Eval_Context; Args : Primitive_Array) return Primitive;
7275

@@ -356,6 +359,53 @@ package body LKQL.Builtin_Functions is
356359
return Make_Unit_Primitive;
357360
end Eval_Help;
358361

362+
----------------------------
363+
-- Eval_Get_Local_Symbols --
364+
----------------------------
365+
366+
function Eval_Get_Symbols
367+
(Ctx : Eval_Context; Args : Primitive_Array) return Primitive
368+
is
369+
procedure Get_Symbols_In_Frame
370+
(C : Eval_Contexts.Environment_Access; Recurse : Boolean);
371+
372+
S : Symbol_Set;
373+
374+
procedure Get_Symbols_In_Frame
375+
(C : Eval_Contexts.Environment_Access; Recurse : Boolean)
376+
is
377+
begin
378+
if C = null then
379+
return;
380+
end if;
381+
for I in Get_Env_Map (C).Iterate loop
382+
S.Include (String_Value_Maps.Key (I));
383+
end loop;
384+
385+
if Recurse then
386+
Get_Symbols_In_Frame (Get_Parent (C), True);
387+
end if;
388+
end Get_Symbols_In_Frame;
389+
390+
Pkg : constant Primitive := Args (1);
391+
Ret : constant Primitive := Make_Empty_List;
392+
begin
393+
394+
if Booleanize (Pkg) then
395+
Get_Symbols_In_Frame
396+
(Eval_Contexts.Environment_Access (Pkg.Unchecked_Get.Namespace),
397+
Recurse => False);
398+
else
399+
Get_Symbols_In_Frame (Ctx.Frames, Recurse => True);
400+
end if;
401+
402+
for El of S loop
403+
Ret.Get.List_Val.Elements.Append (To_Primitive (El.all));
404+
end loop;
405+
return Ret;
406+
407+
end Eval_Get_Symbols;
408+
359409
-----------------------
360410
-- Builtin_Functions --
361411
-----------------------
@@ -424,6 +474,13 @@ package body LKQL.Builtin_Functions is
424474
Eval_Profile'Access,
425475
"Given any object, if it is a callable, return its profile as text"),
426476

477+
Create
478+
("get_symbols",
479+
(1 => Param ("package", Kind_Namespace, Make_Unit_Primitive)),
480+
Eval_Get_Symbols'Access,
481+
"Given a module, return the symbols stored in it. If given no module"
482+
& ", return the local symbols"),
483+
427484
Create
428485
("help",
429486
(1 => Param ("obj")),

lkql/extensions/src/lkql-eval_contexts.adb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,22 @@ package body LKQL.Eval_Contexts is
498498
Ctx.Kernel.LKQL_Path_List.Append (To_Unbounded_String (Path));
499499
end Add_LKQL_Path;
500500

501+
-----------------
502+
-- Get_Env_Map --
503+
-----------------
504+
505+
function Get_Env_Map (Self : Environment_Access) return Environment_Map is
506+
begin
507+
return Self.Local_Bindings;
508+
end Get_Env_Map;
509+
510+
----------------
511+
-- Get_Parent --
512+
----------------
513+
514+
function Get_Parent (Self : Environment_Access) return Environment_Access is
515+
begin
516+
return Self.Parent;
517+
end Get_Parent;
518+
501519
end LKQL.Eval_Contexts;

lkql/extensions/src/lkql-eval_contexts.ads

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ package LKQL.Eval_Contexts is
7979
procedure Dec_Ref (Self : in out Environment_Access);
8080
function Env_Map_Image (Self : Environment_Map) return String;
8181

82+
function Get_Env_Map (Self : Environment_Access) return Environment_Map;
83+
-- Get the env map for this env.
84+
85+
function Get_Parent (Self : Environment_Access) return Environment_Access;
86+
-- Get the parent env for this env.
87+
8288
function Env_Image (Env : Environment_Access) return String;
8389
-- Return a structured debug image of the env passed in parameter.
8490

0 commit comments

Comments
 (0)