Skip to content

Commit 453e9de

Browse files
Refactor: Extract CoordinateUtils and improve AST parser
Co-authored-by: taurekaw <taurekaw@gmail.com>
1 parent 974b703 commit 453e9de

18 files changed

+815
-58
lines changed

IMPROVEMENTS_IMPLEMENTED.md

Lines changed: 435 additions & 0 deletions
Large diffs are not rendered by default.

harmonizer/ast_semantic_parser.py

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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]):
@@ -150,7 +156,13 @@ def _add_concept(self, node: ast.AST, concept: str):
150156
self._node_map[node] = concept
151157
self._concepts_found.add(concept)
152158

153-
def visit_Call(self, node: ast.Call):
159+
def visit_Call(self, node: ast.Call) -> None:
160+
"""
161+
Records function/method calls and categorizes them semantically.
162+
163+
Maps method names to semantic dimensions (e.g., 'execute' -> Power,
164+
'validate' -> Justice, 'get' -> Wisdom).
165+
"""
154166
concept = None
155167
if isinstance(node.func, ast.Attribute):
156168
method_name = node.func.attr
@@ -171,36 +183,83 @@ def visit_Call(self, node: ast.Call):
171183
self._add_concept(node, concept)
172184
self.generic_visit(node)
173185

174-
def visit_If(self, node: ast.If):
186+
def visit_If(self, node: ast.If) -> None:
187+
"""
188+
Records If statements as Justice concepts (control flow/decision-making).
189+
190+
If statements enforce conditions and control execution flow, which
191+
aligns with Justice (rules, structure, enforcement).
192+
"""
175193
self._add_concept(node, "justice")
176194
self.generic_visit(node)
177195

178-
def visit_Assert(self, node: ast.Assert):
196+
def visit_Assert(self, node: ast.Assert) -> None:
197+
"""
198+
Records Assert statements as Justice concepts (validation/enforcement).
199+
200+
Assertions enforce invariants and preconditions, directly representing
201+
Justice principles of validation and rule enforcement.
202+
"""
179203
self._add_concept(node, "justice")
180204
self.generic_visit(node)
181205

182-
def visit_Try(self, node: ast.Try):
206+
def visit_Try(self, node: ast.Try) -> None:
207+
"""
208+
Records Try-Except blocks with dual semantics.
209+
210+
Try blocks represent Justice (structural error handling), while
211+
exception handlers represent Love (mercy, graceful recovery).
212+
"""
183213
self._add_concept(node, "justice")
184214
if node.handlers:
185215
self._add_concept(node.handlers[0], "love")
186216
self.generic_visit(node)
187217

188-
def visit_Raise(self, node: ast.Raise):
218+
def visit_Raise(self, node: ast.Raise) -> None:
219+
"""
220+
Records Raise statements as Power concepts (forceful action).
221+
222+
Raising exceptions is an active, forceful interruption of normal
223+
flow, representing Power (control, force, action).
224+
"""
189225
self._add_concept(node, "power")
190226
self.generic_visit(node)
191227

192-
def visit_For(self, node: ast.For):
228+
def visit_For(self, node: ast.For) -> None:
229+
"""
230+
Records For loops as Justice concepts (structured iteration).
231+
232+
For loops impose structure and order on iteration, representing
233+
Justice (rules, patterns, systematic processing).
234+
"""
193235
self._add_concept(node, "justice")
194236
self.generic_visit(node)
195237

196-
def visit_While(self, node: ast.While):
238+
def visit_While(self, node: ast.While) -> None:
239+
"""
240+
Records While loops as Justice concepts (conditional iteration).
241+
242+
While loops enforce conditions for continued iteration, representing
243+
Justice (rules, enforcement, conditional control).
244+
"""
197245
self._add_concept(node, "justice")
198246
self.generic_visit(node)
199247

200-
def visit_Return(self, node: ast.Return):
248+
def visit_Return(self, node: ast.Return) -> None:
249+
"""
250+
Records Return statements as Wisdom concepts (providing results).
251+
252+
Return statements deliver computed results or knowledge back to
253+
callers, representing Wisdom (information, knowledge transfer).
254+
"""
201255
self._add_concept(node, "wisdom")
202256
self.generic_visit(node)
203257

204-
def generic_visit(self, node: ast.AST):
205-
"""This is the default visitor that just continues the walk."""
258+
def generic_visit(self, node: ast.AST) -> None:
259+
"""
260+
Default visitor that continues traversing the AST.
261+
262+
This method is called for AST node types that don't have
263+
specific visitor methods defined.
264+
"""
206265
super().generic_visit(node)

harmonizer/ast_semantic_parser_v2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
from typing import Dict, List, Optional, Set, Tuple
2222

2323
from harmonizer.programming_constructs_vocabulary import (
24-
PROGRAMMING_VERBS,
2524
COMPOUND_PATTERNS,
25+
PROGRAMMING_VERBS,
2626
)
2727

2828

harmonizer/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import os
77
from dataclasses import dataclass, field
8-
from typing import List, Dict, Any
8+
from typing import Any, Dict, List
99

1010
# Try to import tomli for TOML parsing
1111
try:

0 commit comments

Comments
 (0)