@@ -23,6 +23,12 @@ class AST_Semantic_Parser(ast.NodeVisitor):
2323 """
2424 A "Rosetta Stone" that translates Python AST nodes into
2525 DIVE-V2 conceptual keywords.
26+
27+ This parser walks through Python's Abstract Syntax Tree and categorizes
28+ code constructs into semantic dimensions (Love, Justice, Power, Wisdom).
29+
30+ Note: Visitor methods don't "visit" in the semantic sense - they record
31+ and categorize AST nodes into semantic concepts for later analysis.
2632 """
2733
2834 def __init__ (self , vocabulary : Set [str ]):
@@ -109,9 +115,7 @@ def _map_word_to_concept(self, word: str) -> Optional[str]:
109115 return concept
110116 return None
111117
112- def get_intent_concepts (
113- self , function_name : str , docstring : Optional [str ]
114- ) -> List [str ]:
118+ def get_intent_concepts (self , function_name : str , docstring : Optional [str ]) -> List [str ]:
115119 """
116120 Parses the function's name and docstring to find its "Stated Purpose" (Intent).
117121 """
@@ -131,9 +135,7 @@ def get_intent_concepts(
131135 return [word for word in name_words if word in self .known_vocabulary ]
132136 return list (concepts )
133137
134- def get_execution_map (
135- self , body : List [ast .AST ]
136- ) -> Tuple [Dict [ast .AST , str ], List [str ]]:
138+ def get_execution_map (self , body : List [ast .AST ]) -> Tuple [Dict [ast .AST , str ], List [str ]]:
137139 """
138140 Parses the function's body to map each AST node to a semantic dimension
139141 and return the list of concepts found.
@@ -150,7 +152,13 @@ def _add_concept(self, node: ast.AST, concept: str):
150152 self ._node_map [node ] = concept
151153 self ._concepts_found .add (concept )
152154
153- def visit_Call (self , node : ast .Call ):
155+ def visit_Call (self , node : ast .Call ) -> None :
156+ """
157+ Records function/method calls and categorizes them semantically.
158+
159+ Maps method names to semantic dimensions (e.g., 'execute' -> Power,
160+ 'validate' -> Justice, 'get' -> Wisdom).
161+ """
154162 concept = None
155163 if isinstance (node .func , ast .Attribute ):
156164 method_name = node .func .attr
@@ -171,36 +179,83 @@ def visit_Call(self, node: ast.Call):
171179 self ._add_concept (node , concept )
172180 self .generic_visit (node )
173181
174- def visit_If (self , node : ast .If ):
182+ def visit_If (self , node : ast .If ) -> None :
183+ """
184+ Records If statements as Justice concepts (control flow/decision-making).
185+
186+ If statements enforce conditions and control execution flow, which
187+ aligns with Justice (rules, structure, enforcement).
188+ """
175189 self ._add_concept (node , "justice" )
176190 self .generic_visit (node )
177191
178- def visit_Assert (self , node : ast .Assert ):
192+ def visit_Assert (self , node : ast .Assert ) -> None :
193+ """
194+ Records Assert statements as Justice concepts (validation/enforcement).
195+
196+ Assertions enforce invariants and preconditions, directly representing
197+ Justice principles of validation and rule enforcement.
198+ """
179199 self ._add_concept (node , "justice" )
180200 self .generic_visit (node )
181201
182- def visit_Try (self , node : ast .Try ):
202+ def visit_Try (self , node : ast .Try ) -> None :
203+ """
204+ Records Try-Except blocks with dual semantics.
205+
206+ Try blocks represent Justice (structural error handling), while
207+ exception handlers represent Love (mercy, graceful recovery).
208+ """
183209 self ._add_concept (node , "justice" )
184210 if node .handlers :
185211 self ._add_concept (node .handlers [0 ], "love" )
186212 self .generic_visit (node )
187213
188- def visit_Raise (self , node : ast .Raise ):
214+ def visit_Raise (self , node : ast .Raise ) -> None :
215+ """
216+ Records Raise statements as Power concepts (forceful action).
217+
218+ Raising exceptions is an active, forceful interruption of normal
219+ flow, representing Power (control, force, action).
220+ """
189221 self ._add_concept (node , "power" )
190222 self .generic_visit (node )
191223
192- def visit_For (self , node : ast .For ):
224+ def visit_For (self , node : ast .For ) -> None :
225+ """
226+ Records For loops as Justice concepts (structured iteration).
227+
228+ For loops impose structure and order on iteration, representing
229+ Justice (rules, patterns, systematic processing).
230+ """
193231 self ._add_concept (node , "justice" )
194232 self .generic_visit (node )
195233
196- def visit_While (self , node : ast .While ):
234+ def visit_While (self , node : ast .While ) -> None :
235+ """
236+ Records While loops as Justice concepts (conditional iteration).
237+
238+ While loops enforce conditions for continued iteration, representing
239+ Justice (rules, enforcement, conditional control).
240+ """
197241 self ._add_concept (node , "justice" )
198242 self .generic_visit (node )
199243
200- def visit_Return (self , node : ast .Return ):
244+ def visit_Return (self , node : ast .Return ) -> None :
245+ """
246+ Records Return statements as Wisdom concepts (providing results).
247+
248+ Return statements deliver computed results or knowledge back to
249+ callers, representing Wisdom (information, knowledge transfer).
250+ """
201251 self ._add_concept (node , "wisdom" )
202252 self .generic_visit (node )
203253
204- def generic_visit (self , node : ast .AST ):
205- """This is the default visitor that just continues the walk."""
254+ def generic_visit (self , node : ast .AST ) -> None :
255+ """
256+ Default visitor that continues traversing the AST.
257+
258+ This method is called for AST node types that don't have
259+ specific visitor methods defined.
260+ """
206261 super ().generic_visit (node )
0 commit comments