Fix a bug in /scripts/circuits.py where it could use symbol again during search
authorNick Downing <nick@ndcode.org>
Mon, 21 Jul 2025 08:26:50 +0000 (18:26 +1000)
committerNick Downing <nick@ndcode.org>
Mon, 21 Jul 2025 08:48:36 +0000 (18:48 +1000)
scripts/circuits.py

index 6371a71..287c13b 100755 (executable)
@@ -383,8 +383,10 @@ for circuit_find, circuit_replace, circuit_internal in CIRCUITS:
           if tt == at and len(t) == len(a):
             #print('trying symbol', i, j)
             circuit_symbols[i] = j
+            symbol_actual[0] = -1 # delete symbol during search
             match(1, (0, t, a, stack))
             #print('failed symbol', i, j)
+            symbol_actual[0] = at # reinstate symbol on backtrack
             circuit_symbols[i] = -1 # not really necessary
         return
 
@@ -469,18 +471,16 @@ for circuit_find, circuit_replace, circuit_internal in CIRCUITS:
     if tt == at and len(t) == len(a):
       #print('trying symbol', 0, i)
       circuit_symbols[0] = i
+      symbol_actual[0] = -1 # delete symbol during search
       try:
         match(1, (0, t, a, stack))
         #print('failed symbol', 0, i)
+        symbol_actual[0] = at # reinstate symbol on backtrack
         circuit_symbols[0] = -1 # not really necessary
       except Match:
         # block is taken from last template symbol (often the output)
         block = symbols[circuit_symbols[-1]][1]
 
-        # delete old symbols
-        for j in circuit_symbols:
-          symbols[j][0] = -1
-
         # add new symbols
         for symbol_type, template in circuit_replace:
           symbols.append((symbol_type, block, substitute(template)))