Code cleanup in /scripts/circuits.py with more descriptive temporary variables
authorNick Downing <nick@ndcode.org>
Mon, 21 Jul 2025 08:14:14 +0000 (18:14 +1000)
committerNick Downing <nick@ndcode.org>
Mon, 21 Jul 2025 08:48:36 +0000 (18:48 +1000)
scripts/circuits.py

index 1141632..6371a71 100755 (executable)
@@ -351,10 +351,10 @@ for circuit_find, circuit_replace, circuit_internal in CIRCUITS:
   def match(level, stack):
     while True:
       if level == 0: # match symbols (top level)
-        i, template = stack
-        if i >= len(template):
-          # template unifies -- before raising a match we need to check that
-          # all nets flagged as internal are not connected to another symbol
+        i, symbols_template = stack
+        if i >= len(symbols_template):
+          # all unified -- but before raising a match we need to check that
+          # the nets flagged as internal are not connected to another symbol
           symbols_internal = set(circuit_symbols)
           for j in range(len(circuit_internal)):
             gj = len(global_nets) + j
@@ -366,18 +366,24 @@ for circuit_find, circuit_replace, circuit_internal in CIRCUITS:
               return
           #print('match')
           raise Match()
-        t = template[i]
-        assert isinstance(t[1], tuple)
-        stack = (i + 1, template)
-        candidates = candidate_symbols(t[1])
+        symbol_template = symbols_template[i]
+        stack = (i + 1, symbols_template)
+
+        tt = symbol_template[0] # template type
+        t = symbol_template[1] # template nets
+        assert isinstance(t, tuple)
+
+        candidates = candidate_symbols(t)
         #print('candidates', candidates)
         for j in range(len(symbols)) if candidates is None else candidates:
-          a = symbols[j]
-          assert isinstance(a[2], tuple)
-          if t[0] == a[0] and len(t[1]) == len(a[2]):
+          symbol_actual = symbols[j]
+          at = symbol_actual[0] # actual type
+          a = symbol_actual[2] # actual nets
+          assert isinstance(a, tuple)
+          if tt == at and len(t) == len(a):
             #print('trying symbol', i, j)
             circuit_symbols[i] = j
-            match(1, (0, t[1], a[2], stack))
+            match(1, (0, t, a, stack))
             #print('failed symbol', i, j)
             circuit_symbols[i] = -1 # not really necessary
         return
@@ -448,17 +454,23 @@ for circuit_find, circuit_replace, circuit_internal in CIRCUITS:
   # so we can restart from after that symbol after doing a replacement,
   # it also resets the trail after each match so that the backtracking
   # search does not need to untrail everything when a match is raised
-  t = circuit_find[0]
-  assert isinstance(t[1], tuple)
+  symbol_template = circuit_find[0]
   stack = (1, circuit_find)
+
+  tt = symbol_template[0] # template type
+  t = symbol_template[1] # template nets
+  assert isinstance(t, tuple)
+
   for i in range(len(symbols)):
-    a = symbols[i]
-    assert isinstance(a[2], tuple)
-    if t[0] == a[0] and len(t[1]) == len(a[2]):
+    symbol_actual = symbols[i]
+    at = symbol_actual[0] # actual type
+    a = symbol_actual[2] # actual nets
+    assert isinstance(a, tuple)
+    if tt == at and len(t) == len(a):
       #print('trying symbol', 0, i)
       circuit_symbols[0] = i
       try:
-        match(1, (0, t[1], symbols[i][2], stack))
+        match(1, (0, t, a, stack))
         #print('failed symbol', 0, i)
         circuit_symbols[0] = -1 # not really necessary
       except Match: