Skip to content

Commit

Permalink
now allowing empty lines in the grammar, and created scaffold for bet…
Browse files Browse the repository at this point in the history
…ter error messages
  • Loading branch information
Felienne committed Mar 20, 2020
1 parent 17c5654 commit 8d7f424
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion grammars/level1.txt
@@ -1,5 +1,5 @@
start: program
program: command ("\n" command)*
program: command ("\n"+ command)* "\n"*
command: "print " text -> print
| "ask " text -> ask
| "echo " text -> echo
Expand Down
2 changes: 1 addition & 1 deletion grammars/level2.txt
@@ -1,5 +1,5 @@
start: program
program: command ("\n" command)*
program: command ("\n"+ command)* "\n"*
command: "print " ( list_access | (text punctuation)| text) (" " ( list_access | (text punctuation)| text) )* -> print
| text " is ask " textwithspaces punctuation* -> ask
| text " is " text ((", "|",") text)+ -> assign_list
Expand Down
2 changes: 1 addition & 1 deletion grammars/level3.txt
@@ -1,5 +1,5 @@
start: program
program: command ("\n" command)*
program: command ("\n"+ command)* "\n"*
command: "print " (quoted_text | list_access | var) (" " (quoted_text | list_access | var))* -> print
| text " is ask " textwithspaces punctuation* -> ask
| text " is " text ((", "|",") text)+ -> assign_list
Expand Down
2 changes: 1 addition & 1 deletion grammars/level4.txt
@@ -1,5 +1,5 @@
start: program
program: command ("\n" command)*
program: command ("\n"+ command)* "\n"*
command: "print " (quoted_text | list_access | var) (" " (quoted_text | list_access | var))* -> print
| "if " equality_check " " command " else " command -> ifelse
| "if " equality_check " " command -> ifs //if cannot be used in Python, hence ifs
Expand Down
13 changes: 10 additions & 3 deletions hedy.py
Expand Up @@ -8,6 +8,7 @@ def __init__(self, message, **arguments):
self.arguments = arguments



class AllCommands(Transformer):
#creates a list of all commands in a tree for further processing
# it removes command and program nodes
Expand Down Expand Up @@ -325,9 +326,14 @@ def transpile(input_string, level):
punctuation_symbols = ['!', '?', '.']
level = int(level)
parser = create_parser(level)
program_root = parser.parse(input_string).children[0] # getting rid of the root could also be done in the transformer would be nicer
lookup_table = all_assignments(program_root)
flattened_tree = FlattenText().transform(program_root)
try:
program_root = parser.parse(input_string).children[0] # getting rid of the root could also be done in the transformer would be nicer
lookup_table = all_assignments(program_root)
flattened_tree = FlattenText().transform(program_root)
except Exception as e:
# TODO: here we could translate Lark error messages into more sensible texts!
raise HedyException('Parse', level=level, parse_error=e.args[0])

is_valid = IsValid().transform(program_root)

if is_valid[0]:
Expand Down Expand Up @@ -360,6 +366,7 @@ def transpile(input_string, level):
else:
raise Exception('Levels 5 to 7 are not implemented yet')


def execute(input_string):
python = transpile(input_string)
exec(python)
Expand Down
9 changes: 6 additions & 3 deletions static/texts.json
Expand Up @@ -18,7 +18,8 @@
"Execute_error": "Er ging iets fout bij het uitvoeren van het programma."
},
"HedyErrorMessages": {
"Invalid": "{command} is geen command in Hedy level {level}."
"Invalid": "{command} is geen command in Hedy level {level}.",
"Parse": "De code die jij intypte is geen geldige Hedy code. De foutmelding is: {parse_error}"
}
},
"en": {
Expand All @@ -40,7 +41,8 @@
"Execute_error": "Something went wrong while running the program."
},
"HedyErrorMessages": {
"Invalid": "{command} is not a Hedy level {level} command."
"Invalid": "{command} is not a Hedy level {level} command.",
"Parse": "The code you entered is not valid Hedy code"
}
},
"es": {
Expand All @@ -62,7 +64,8 @@
"Execute_error": "Algo estuvo mal mientras se ejecuto el programa."
},
"HedyErrorMessages": {
"Invalid": "{command} no es un comando de Hedy en nivel {level}."
"Invalid": "{command} no es un comando de Hedy en nivel {level}.",
"Parse": "El servidor no puede traducir este programa de Hedy a Python."
}
}
}
5 changes: 4 additions & 1 deletion tests.py
Expand Up @@ -27,14 +27,17 @@ class TestsLevel1(unittest.TestCase):
def test_transpile_other(self):
with self.assertRaises(Exception) as context:
result = hedy.transpile("abc felienne 123", 1)

self.assertEqual(str(context.exception), 'Invalid')

def test_transpile_print(self):
result = hedy.transpile("print Hallo welkom bij Hedy!", 1)
self.assertEqual(result, "print('Hallo welkom bij Hedy!')")
self.assertEqual(run_code(result), 'Hallo welkom bij Hedy!')

def test_transpile_empty(self):
with self.assertRaises(hedy.HedyException) as context:
result = hedy.transpile("", 1)

def test_transpile_ask(self):
result = hedy.transpile("ask wat is je lievelingskleur?", 1)
self.assertEqual(result, "answer = input('wat is je lievelingskleur?')")
Expand Down

0 comments on commit 8d7f424

Please sign in to comment.