Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Commit

Permalink
closes hedyorg#178 and fixes small typo in Dutch yaml file
Browse files Browse the repository at this point in the history
  • Loading branch information
Felienne committed Feb 24, 2021
1 parent 3d9c41a commit fd0b3c4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion coursedata/texts/nl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Auth:
unsaved_changes: "Jouw programma is niet opgeslagen. Wil je weggaan zonder het op te slaan?"
save_success: "Gelukt!"
save_success_detail: "Je programma is opgeslagen"
answer_question: "Je kunt het programma pas uitvoeren als je de vraag heeft beantwoord."
answer_question: "Je kunt het programma pas uitvoeren als je de vraag hebt beantwoord."
Programs:
recent: "Mijn programma's"
level: "Level"
Expand Down
7 changes: 6 additions & 1 deletion grammars/level4.lark
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ print : "print " (quoted_text | list_access | var) (" " (quoted_text | list_acce
ask : var " is ask " textwithspaces*
assign_list: var " is " textwithspaces ((", "|",") textwithspaces)+
assign : var " is " textwithspaces
invalid: /^(?!(print|if|and )).+/ //everything that is not the start of a regular command
invalid: textwithoutspaces " " textwithspaces

// old code: /^(?!(print|if|and )).+/ //everything that is not the start of a regular command
// I am not sure why we did this, I think in anticipation of higher levels where we cannot rely on order?
// leaving it here just in case! (Felienne, Feb 24, 2021)


// new commands for level 4
ifelse : "if " condition " " command " else " command
Expand Down
2 changes: 1 addition & 1 deletion grammars/level5.lark
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ print : "print " (quoted_text | list_access | var) (" " (quoted_text | list_acce
ask : var " is ask " textwithspaces*
assign_list: var " is " textwithspaces ((", "|",") textwithspaces)+
assign : var " is " textwithspaces
invalid: /^(?!(repeat|print|if|and )).+/ //everything that is not the start of a regular command
invalid: textwithoutspaces " " textwithspaces

// new commands for level 4
ifelse : "if " condition " " command " else " command
Expand Down
2 changes: 1 addition & 1 deletion grammars/level6.lark
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ assign_list: var " is " textwithspaces ((", "|",") textwithspaces)+
//TODO: sum needs to be expression here too (like in 7 and up)

assign : var " is " sum | var " is " textwithoutspaces
invalid: /^(?!(repeat|print|if|and )).+/ //everything that is not the start of a regular command
invalid: textwithoutspaces " " textwithspaces

// new commands for level 4
ifelse : "if " condition " " command " else " command
Expand Down
12 changes: 11 additions & 1 deletion hedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -677,7 +687,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)
Expand Down
11 changes: 10 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit fd0b3c4

Please sign in to comment.