mirror of
https://github.com/open-thought/reasoning-gym.git
synced 2026-04-19 12:58:07 +00:00
lint
This commit is contained in:
parent
21c47db6c1
commit
ebb88e6c6a
24 changed files with 1215 additions and 814 deletions
|
|
@ -15,9 +15,13 @@ def optimize_once(tokens):
|
|||
# optimize arithmetic operations. E.g replace 1+2 with 3
|
||||
|
||||
# need to be careful not to optimize (1+2*3) to (3*3)
|
||||
if tokens[start_index+1].data in ["*", "/", "%"] or (start_index+3 >= len(tokens)) or (tokens[start_index+3].data not in ["*", "/", "%"]):
|
||||
num1, num2 = get_NUM_token_value(tokens[start_index]), get_NUM_token_value(tokens[start_index+2])
|
||||
op = tokens[start_index+1].data
|
||||
if (
|
||||
tokens[start_index + 1].data in ["*", "/", "%"]
|
||||
or (start_index + 3 >= len(tokens))
|
||||
or (tokens[start_index + 3].data not in ["*", "/", "%"])
|
||||
):
|
||||
num1, num2 = get_NUM_token_value(tokens[start_index]), get_NUM_token_value(tokens[start_index + 2])
|
||||
op = tokens[start_index + 1].data
|
||||
if op == "+":
|
||||
val = num1 + num2
|
||||
elif op == "-":
|
||||
|
|
@ -38,8 +42,13 @@ def optimize_once(tokens):
|
|||
raise NotImplementedError(op)
|
||||
|
||||
# remove the 3 old tokens and replace them with new one
|
||||
new_token = Token(Token.NUM, tokens[start_index].line, tokens[start_index].column, data=str(val),
|
||||
original_tokens=tokens[start_index:start_index+3])
|
||||
new_token = Token(
|
||||
Token.NUM,
|
||||
tokens[start_index].line,
|
||||
tokens[start_index].column,
|
||||
data=str(val),
|
||||
original_tokens=tokens[start_index : start_index + 3],
|
||||
)
|
||||
|
||||
for _ in range(3):
|
||||
tokens.pop(start_index)
|
||||
|
|
@ -52,16 +61,24 @@ def optimize_once(tokens):
|
|||
# replace printint(50) with print("50")
|
||||
# since printing strings compiles into less Brainfuck code than printing ints
|
||||
if tokens[start_index].data == "printint":
|
||||
tokens[start_index] = Token(Token.PRINT, tokens[start_index].line, tokens[start_index].column, original_tokens=[tokens[start_index]])
|
||||
tokens[start_index+2] = Token(Token.STRING, tokens[start_index].line, tokens[start_index].column,
|
||||
data=str(tokens[start_index+2].data), original_tokens=[tokens[start_index+2]])
|
||||
tokens[start_index] = Token(
|
||||
Token.PRINT, tokens[start_index].line, tokens[start_index].column, original_tokens=[tokens[start_index]]
|
||||
)
|
||||
tokens[start_index + 2] = Token(
|
||||
Token.STRING,
|
||||
tokens[start_index].line,
|
||||
tokens[start_index].column,
|
||||
data=str(tokens[start_index + 2].data),
|
||||
original_tokens=[tokens[start_index + 2]],
|
||||
)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
rules = [([Token.NUM, Token.BINOP, Token.NUM], optimize_binop), # arithmetic operations
|
||||
([Token.ID, Token.LPAREN, Token.NUM, Token.RPAREN], optimize_printint), # printint(50) to print("50")
|
||||
]
|
||||
rules = [
|
||||
([Token.NUM, Token.BINOP, Token.NUM], optimize_binop), # arithmetic operations
|
||||
([Token.ID, Token.LPAREN, Token.NUM, Token.RPAREN], optimize_printint), # printint(50) to print("50")
|
||||
]
|
||||
|
||||
# try to match one of the rules to the tokens in a "sliding window" style
|
||||
i = 0
|
||||
|
|
@ -69,7 +86,7 @@ def optimize_once(tokens):
|
|||
optimized = False
|
||||
for tokens_sequence, optimization_function in rules:
|
||||
if i + len(tokens_sequence) <= len(tokens):
|
||||
if all(tokens_sequence[n] == tokens[i+n].type for n in range(len(tokens_sequence))):
|
||||
if all(tokens_sequence[n] == tokens[i + n].type for n in range(len(tokens_sequence))):
|
||||
if optimization_function(tokens, i):
|
||||
optimized = True
|
||||
if optimized:
|
||||
|
|
@ -82,7 +99,7 @@ def optimize(tokens):
|
|||
prev_tokens = [token.type for token in tokens]
|
||||
while True:
|
||||
optimize_once(tokens)
|
||||
print(".", end='')
|
||||
print(".", end="")
|
||||
current_tokens = [token.type for token in tokens]
|
||||
if current_tokens == prev_tokens:
|
||||
break
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue