' AND ( Keyboard shortcut: 4 )\r
' NOT ( Keyboard shortcut: 5 )\r
\r
-DECLARE SUB rmslg (x1!, x3!, l!)\r
+DECLARE SUB removeRedundancies (startIndex!, endIndex!, removalCount!)\r
DECLARE SUB getP (a!, b!)\r
DECLARE SUB movM (x1!, n!)\r
DECLARE SUB lihts (x1, x2, l)\r
END SUB\r
\r
SUB lihts (x1, x2, l)\r
-' Removes redundant parentheses from the logical expression\r
-rmslg x1, x2, l1\r
-l = l1\r
-'BEEP\r
-prnp = prnp + 1\r
-FOR a = x1 TO x2 - l\r
- prn a, 0, 13, 1, CHR$(tehe(a))\r
-NEXT a\r
+ ' Removes redundant parentheses from the logical expression and marks how many were removed\r
+ removeRedundancies x1, x2, l1\r
+ l = l1\r
+ 'BEEP\r
+ prnp = prnp + 1\r
+ FOR a = x1 TO x2 - l\r
+ prn a, 0, 13, 1, CHR$(tehe(a))\r
+ NEXT a\r
END SUB\r
\r
SUB mov (x1, n)\r
END SUB\r
\r
SUB prn (x, y, c, c1, a$)\r
-' Prints characters to the screen in a specific location and color\r
-x1 = x * 8\r
-y1 = (y + prnp) * 8\r
-\r
-FOR b = 1 TO LEN(a$)\r
- LINE (x1, y1)-(x1 + 7, y1 + 7), c1, BF\r
- d = ASC(RIGHT$(LEFT$(a$, b), 1))\r
- IF d > 122 THEN GOTO 22\r
- FOR y2 = 0 TO 7\r
- FOR x2 = 0 TO 7\r
- c2 = font(x2, y2, d)\r
- IF c2 > 0 THEN PSET (x1 + x2, y1 + y2), c\r
- NEXT x2\r
- NEXT y2\r
- 22\r
- x1 = x1 + 8\r
-NEXT b\r
+ ' Prints characters to the screen at location (x,y) with color c, background c1\r
+ x1 = x * 8\r
+ y1 = (y + prnp) * 8\r
+\r
+ FOR b = 1 TO LEN(a$)\r
+ LINE (x1, y1)-(x1 + 7, y1 + 7), c1, BF\r
+ d = ASC(RIGHT$(LEFT$(a$, b), 1))\r
+ IF d > 122 THEN GOTO 22\r
+ FOR y2 = 0 TO 7\r
+ FOR x2 = 0 TO 7\r
+ c2 = font(x2, y2, d)\r
+ IF c2 > 0 THEN PSET (x1 + x2, y1 + y2), c\r
+ NEXT x2\r
+ NEXT y2\r
+22 x1 = x1 + 8\r
+ NEXT b\r
\r
END SUB\r
\r
-SUB rmslg (x1, x3, l)\r
-' Removes redundant parentheses from the logical expression\r
-x2 = x3\r
-l2 = 0\r
+SUB removeRedundancies (startIndex, endIndex, removalCount)\r
+ ' This procedure scans for parentheses that can be safely removed\r
+ ' without changing the logic of the expression, then removes them.\r
\r
-a = x1\r
-26\r
-IF tehe(a) = 40 THEN\r
- IF a = x1 THEN p1 = 100 ELSE getP tehe(a - 1), p1\r
- c = a\r
- d = 1\r
- p2 = 0\r
- 25\r
- c = c + 1\r
- IF tehe(c) = 40 THEN d = d + 1\r
- IF tehe(c) = 41 THEN d = d - 1\r
- IF d = 1 THEN\r
- IF (tehe(c) > 0) AND (tehe(c) <= 5) THEN\r
- getP tehe(c), b\r
- IF b > p2 THEN p2 = b\r
+ DIM currentEnd, parenthesesCount\r
+ currentEnd = endIndex\r
+ parenthesesCount = 0\r
+\r
+ a = startIndex\r
+26 IF tehe(a) = 40 THEN\r
+ ' We found an opening parenthesis. Now let's see if it can be removed.\r
+ IF a = startIndex THEN p1 = 100 ELSE getP tehe(a - 1), p1\r
+\r
+ c = a\r
+ d = 1\r
+ p2 = 0\r
+\r
+25 c = c + 1\r
+ IF tehe(c) = 40 THEN d = d + 1\r
+ IF tehe(c) = 41 THEN d = d - 1\r
+\r
+ ' Once d returns to 1, we are back to one level of parentheses,\r
+ ' meaning we can check operator priority inside.\r
+ IF d = 1 THEN\r
+ IF (tehe(c) > 0) AND (tehe(c) <= 5) THEN\r
+ getP tehe(c), b\r
+ IF b > p2 THEN p2 = b\r
+ END IF\r
+ END IF\r
+\r
+ IF d > 0 THEN GOTO 25\r
+\r
+ IF c + 1 > currentEnd THEN p3 = 100 ELSE getP tehe(c + 1), p3\r
+\r
+ ' If the operator outside is higher priority than what's inside,\r
+ ' we can safely remove the parentheses.\r
+ IF (p1 > p2) AND (p3 >= p2) THEN\r
+ movM c, 1\r
+ movM a, 1\r
+ parenthesesCount = parenthesesCount + 2\r
+ currentEnd = currentEnd - 2\r
+ a = a - 1\r
END IF\r
END IF\r
- IF d > 0 THEN GOTO 25\r
- IF c + 1 > x2 THEN p3 = 100 ELSE getP tehe(c + 1), p3\r
-\r
- IF (p1 > p2) AND (p3 >= p2) THEN\r
- movM c, 1\r
- movM a, 1\r
- l2 = l2 + 2\r
- x2 = x2 - 2\r
- a = a - 1\r
- END IF\r
-END IF\r
-a = a + 1\r
-IF a <= x2 THEN GOTO 26\r
-l = l2\r
+\r
+ a = a + 1\r
+ IF a <= currentEnd THEN GOTO 26\r
+\r
+ removalCount = parenthesesCount\r
END SUB\r
\r
SUB sist\r
NEXT a\r
\r
GOTO 9\r
+\r
mkneg:\r
-FOR c = 1 TO tehl\r
- d = opr(oprm, c)\r
- IF d = ASC("t") THEN d = ASC("v") ELSE d = ASC("t")\r
- prn xloc(ngx), c, 4, 0, CHR$(d)\r
- opr(oprm, c) = d\r
-NEXT c\r
-ng = 0\r
+ ' NOT operation (negation) is applied to the current operand\r
+ FOR c = 1 TO tehl\r
+ d = opr(oprm, c)\r
+ IF d = ASC("t") THEN d = ASC("v") ELSE d = ASC("t")\r
+ prn xloc(ngx), c, 4, 0, CHR$(d)\r
+ opr(oprm, c) = d\r
+ NEXT c\r
+ ng = 0\r
RETURN\r
9\r
\r