Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LANGUAGE] Repairs undefined var exception #4887

Merged
merged 5 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions hedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3349,29 +3349,20 @@ def create_lookup_table(abstract_syntax_tree, level, lang, input_string):
def create_AST(input_string, level, lang="en"):
program_root = parse_input(input_string, level, lang)

try:
# checks whether any error production nodes are present in the parse tree
is_program_valid(program_root, input_string, level, lang)
abstract_syntax_tree = ExtractAST().transform(program_root)
is_program_complete(abstract_syntax_tree, level)
# checks whether any error production nodes are present in the parse tree
is_program_valid(program_root, input_string, level, lang)
abstract_syntax_tree = ExtractAST().transform(program_root)
is_program_complete(abstract_syntax_tree, level)

if not valid_echo(abstract_syntax_tree):
raise exceptions.LonelyEchoException()
if not valid_echo(abstract_syntax_tree):
raise exceptions.LonelyEchoException()

lookup_table = create_lookup_table(abstract_syntax_tree, level, lang, input_string)
commands = AllCommands(level).transform(program_root)
# FH, dec 2023. I don't love how AllCommands works on program root and not on AST,
# but his will do for now. One day we should really start to clean up our AST!
lookup_table = create_lookup_table(abstract_syntax_tree, level, lang, input_string)
commands = AllCommands(level).transform(program_root)
# FH, dec 2023. I don't love how AllCommands works on program root and not on AST,
# but his will do for now. One day we should really start to clean up our AST!

return abstract_syntax_tree, lookup_table, commands
except VisitError as E:
if isinstance(E, VisitError):
# Exceptions raised inside visitors are wrapped inside VisitError. Unwrap it if it is a
# HedyException to show the intended error message.
if isinstance(E.orig_exc, exceptions.HedyException):
raise E.orig_exc
else:
raise E
return abstract_syntax_tree, lookup_table, commands


def transpile_inner(input_string, level, lang="en", populate_source_map=False, is_debug=False):
Expand All @@ -3396,20 +3387,29 @@ def transpile_inner(input_string, level, lang="en", populate_source_map=False, i
else:
numerals_language = "Latin"

abstract_syntax_tree, lookup_table, commands = create_AST(input_string, level, lang)
try:
abstract_syntax_tree, lookup_table, commands = create_AST(input_string, level, lang)

# grab the right transpiler from the lookup
convertToPython = TRANSPILER_LOOKUP[level]
python = convertToPython(lookup_table, lang, numerals_language, is_debug).transform(abstract_syntax_tree)
# grab the right transpiler from the lookup
convertToPython = TRANSPILER_LOOKUP[level]
python = convertToPython(lookup_table, lang, numerals_language, is_debug).transform(abstract_syntax_tree)

has_clear = "clear" in commands
has_turtle = "forward" in commands or "turn" in commands or "color" in commands
has_pygame = "ifpressed" in commands or "ifpressed_else" in commands or "assign_button" in commands
has_clear = "clear" in commands
has_turtle = "forward" in commands or "turn" in commands or "color" in commands
has_pygame = "ifpressed" in commands or "ifpressed_else" in commands or "assign_button" in commands

if populate_source_map:
source_map.set_python_output(python)
if populate_source_map:
source_map.set_python_output(python)

return ParseResult(python, source_map, has_turtle, has_pygame, has_clear, commands)
return ParseResult(python, source_map, has_turtle, has_pygame, has_clear, commands)
except VisitError as E:
if isinstance(E, VisitError):
# Exceptions raised inside visitors are wrapped inside VisitError. Unwrap it if it is a
# HedyException to show the intended error message.
if isinstance(E.orig_exc, exceptions.HedyException):
raise E.orig_exc
else:
raise E


def execute(input_string, level):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_level/test_level_04.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,3 +954,13 @@ def test_source_map(self):

self.single_level_tester(code, expected=expected_code)
self.source_map_tester(code=code, expected_source_map=expected_source_map)

def test_undefined_list_access(self):
code = textwrap.dedent("""\
fortunes is you will slip on a banana peel, millionaire, death
print fortunes at dxd""")

self.multi_level_tester(code=code,
exception=hedy.exceptions.UndefinedVarException,
skip_faulty=False,
max_level=11)
Loading