simplifying multilinear polynomials
This commit is contained in:
@ -4,14 +4,14 @@ from dataclasses import dataclass
|
||||
|
||||
|
||||
class OpKind(Enum):
|
||||
LEFT = '<'
|
||||
RIGHT = '>'
|
||||
INC = '+'
|
||||
DEC = '-'
|
||||
INPT = ','
|
||||
OUTP = '.'
|
||||
JMPZ = '['
|
||||
JMPNZ = ']'
|
||||
LEFT = "<"
|
||||
RIGHT = ">"
|
||||
INC = "+"
|
||||
DEC = "-"
|
||||
INPT = ","
|
||||
OUTP = "."
|
||||
JMPZ = "["
|
||||
JMPNZ = "]"
|
||||
|
||||
|
||||
@dataclass
|
||||
@ -33,7 +33,6 @@ def optimize(tokens):
|
||||
if len(new_tokens) <= 1:
|
||||
return new_tokens
|
||||
|
||||
|
||||
tokens = otpi_cancel_opposing(new_tokens)
|
||||
while len(new_tokens := otpi_cancel_opposing(tokens)) != len(tokens):
|
||||
tokens = new_tokens
|
||||
@ -49,7 +48,10 @@ def opti_collapse_tokens(tokens):
|
||||
new_tokens.append(next_token)
|
||||
continue
|
||||
current_token = new_tokens.pop()
|
||||
if current_token.kind == next_token.kind and str(current_token.kind.value) in "<>+-":
|
||||
if (
|
||||
current_token.kind == next_token.kind
|
||||
and str(current_token.kind.value) in "<>+-"
|
||||
):
|
||||
current_token.operand += 1
|
||||
new_tokens.append(current_token)
|
||||
else:
|
||||
@ -66,8 +68,11 @@ def otpi_cancel_opposing(tokens):
|
||||
new_tokens.append(next_token)
|
||||
continue
|
||||
current_token = new_tokens.pop()
|
||||
if (current_token.kind.value + next_token.kind.value in ("+-", "-+", "<>", "><", "[]")
|
||||
and current_token.operand == next_token.operand):
|
||||
if (
|
||||
current_token.kind.value + next_token.kind.value
|
||||
in ("+-", "-+", "<>", "><", "[]")
|
||||
and current_token.operand == next_token.operand
|
||||
):
|
||||
continue
|
||||
else:
|
||||
new_tokens.append(current_token)
|
||||
@ -81,18 +86,28 @@ def generate_code(tokens):
|
||||
# code generation
|
||||
for token in tokens:
|
||||
match token.kind:
|
||||
case OpKind.LEFT: out.append(f"{' ' * nest * 2}p -= {token.operand};\n")
|
||||
case OpKind.RIGHT: out.append(f"{' ' * nest * 2}p += {token.operand};\n")
|
||||
case OpKind.INC: out.append(f"{' ' * nest * 2}*p += {token.operand};\n")
|
||||
case OpKind.DEC: out.append(f"{' ' * nest * 2}*p -= {token.operand};\n")
|
||||
case OpKind.INPT: out.append(f"{' ' * nest * 2}*p = getchar();\n")
|
||||
case OpKind.OUTP: out.append(f"{' ' * nest * 2}putchar(*p);\n")
|
||||
case OpKind.JMPZ: out.append(f"{' ' * nest * 2}" + "if (*p) do {\n");nest += 1
|
||||
case OpKind.JMPNZ: nest -= 1;out.append(f"{' ' * nest * 2}" + "} while (*p);\n")
|
||||
|
||||
case OpKind.LEFT:
|
||||
out.append(f"{' ' * nest * 2}p -= {token.operand};\n")
|
||||
case OpKind.RIGHT:
|
||||
out.append(f"{' ' * nest * 2}p += {token.operand};\n")
|
||||
case OpKind.INC:
|
||||
out.append(f"{' ' * nest * 2}*p += {token.operand};\n")
|
||||
case OpKind.DEC:
|
||||
out.append(f"{' ' * nest * 2}*p -= {token.operand};\n")
|
||||
case OpKind.INPT:
|
||||
out.append(f"{' ' * nest * 2}*p = getchar();\n")
|
||||
case OpKind.OUTP:
|
||||
out.append(f"{' ' * nest * 2}putchar(*p);\n")
|
||||
case OpKind.JMPZ:
|
||||
out.append(f"{' ' * nest * 2}" + "if (*p) do {\n")
|
||||
nest += 1
|
||||
case OpKind.JMPNZ:
|
||||
nest -= 1
|
||||
out.append(f"{' ' * nest * 2}" + "} while (*p);\n")
|
||||
|
||||
if nest < 0:
|
||||
return "Error!"
|
||||
|
||||
|
||||
if nest != 0:
|
||||
return "Error!"
|
||||
|
||||
|
Reference in New Issue
Block a user