Repository: hedyorg/hedy

Issue:
Program repair for level 4 and up should suggest better
`pront 'Hedy is leuk!'`

results in: "r is geen commando in Hedy level 4. Bedoelde je ask?"

Bo clue where the r comes form but that can clearly be improved :) 


Discussion:

Resolving patch:
diff --git a/hedy.py b/hedy.py
--- a/hedy.py
+++ b/hedy.py
@@ -6,6 +6,16 @@
 
 reserved_words = ['and','except','lambda','with','as','finally','nonlocal','while','assert','false','None','yield','break','for','not','class','from','or','continue','global','pass','def','if','raise','del','import','return','elif','in','True','else','is','try']
 
+# commands are used to suggest the right command when kids make a mistake
+commands_per_level = {1: ['print', 'ask', 'echo'] ,
+                      2: ['print', 'ask', 'echo', 'is'],
+                      3: ['print', 'ask', 'is'],
+                      4: ['print', 'ask', 'is', 'if'],
+                      5: ['print', 'ask', 'is', 'if', 'repeat'],
+                      6: ['print', 'ask', 'is', 'if', 'repeat']
+                      }
+
+
 def closest_command(command, commands):
     #simple string distance, could be more sophisticated MACHINE LEARNING!
     min = 1000
@@ -158,6 +168,8 @@ def list_access_var(self, args):
         return all_arguments_true(args)
     def ifs(self, args):
         return all_arguments_true(args)
+    def valid_command(self, args):
+        return all_arguments_true(args)
     def ifelse(self, args):
         return all_arguments_true(args)
     def condition(self, args):
@@ -677,7 +689,7 @@ def transpile_inner(input_string, level):
                 raise HedyException('Invalid Space', level=level, line_number=line, fixed_code = result)
             else:
                 invalid_command = is_valid[1]
-                closest = closest_command(invalid_command, ['print', 'ask', 'echo'])
+                closest = closest_command(invalid_command, commands_per_level[level])
                 raise HedyException('Invalid', invalid_command=invalid_command, level=level, guessed_command=closest)
 
         is_complete = IsComplete().transform(program_root)


Test patch:
diff --git a/tests.py b/tests.py
--- a/tests.py
+++ b/tests.py
@@ -48,7 +48,7 @@ def test_parse_error_shows_right_level(self):
 
         # This isn't correct Hedy at level 5 nor level 4
         try:
-            hedy.transpile("print Hello world!", 5)
+            hedy.transpile("printHelloworld!", 5)
             self.fail('Should have thrown')
         except hedy.HedyException as e:
             self.assertEqual(e.error_code, 'Parse')
@@ -413,6 +413,15 @@ def test_if_in_array(self):
         self.assertEqual(expected, actual)
         self.assertEqual('found!', run_code(actual))
 
+    def test_pront_should_suggest_print(self):
+        program = "pront 'Hedy is leuk!'"
+
+        with self.assertRaises(Exception) as context:
+            result = hedy.transpile(program, 4)
+        self.assertEqual('Invalid', str(context.exception))
+        self.assertEqual('print', str(context.exception.arguments['guessed_command']))
+
+
 class TestsLevel5(unittest.TestCase):
     #print should still work
     def test_print_with_var(self):