Skip to content

Commit

Permalink
Add better bracket validation for Brain****; fixes #625 (#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
Carson-Tang authored Mar 2, 2020
1 parent 0368058 commit fb89bd7
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion dmoj/executors/BF.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Executor(CExecutor):
test_program = ',+[-.,+]'

def __init__(self, problem_id, source_code, **kwargs):
if source_code.count(b'[') != source_code.count(b']'):
if self._has_invalid_brackets(source_code):
raise CompileError(b'Unmatched brackets\n')
code = template.replace(b'{code}', b''.join(map(trans.get, source_code, itertools.repeat(b''))))
super().__init__(problem_id, code, **kwargs)
Expand All @@ -58,6 +58,18 @@ def launch(self, *args, **kwargs):
kwargs['memory'] += 8192
return super().launch(str(memory * 1024), **kwargs)

def _has_invalid_brackets(self, source_code) -> bool:
open_brackets = 0
for c in source_code:
if c == b'['[0]:
open_brackets += 1
elif c == b']'[0]:
if open_brackets == 0:
return True
else:
open_brackets -= 1
return open_brackets != 0

@classmethod
def get_runtime_versions(cls):
return ('bf', (1, 33, 7)),

0 comments on commit fb89bd7

Please sign in to comment.