Skip to content

Commit

Permalink
[CONTENT] Allow non-latin letters for pressed commands (hedyorg#3664)
Browse files Browse the repository at this point in the history
Fixes hedyorg#3663, makes it possible to use non-Latin letters for the pressed commands.

![image](https://user-images.githubusercontent.com/48225550/202308388-74a9574f-d13c-434d-a81f-04aeee1cd07c.png)

Also, this implementation makes it possible to use 'special' buttons such as:

![image](https://user-images.githubusercontent.com/48225550/202311138-06eae541-85c1-4b2a-8ad7-9ec21ca9eba6.png)


**Test**

1. There is an extra simple test _test_if_pressed_non_latin_ in _test_level_05.py_

2.  Try out your non-Latin language!
  • Loading branch information
ToniSkulj authored Nov 21, 2022
1 parent 93daf5c commit 2eabf03
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 64 deletions.
1 change: 0 additions & 1 deletion grammars/level1.lark
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ NAME: LETTER_OR_UNDERSCORE LETTER_OR_NUMERAL*

LETTER_OR_UNDERSCORE: /[\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}_]+/
LETTER_OR_NUMERAL: LETTER_OR_UNDERSCORE | /[\p{Mn}\p{Mc}\p{Nd}\p{Pc}·]+/
SMALL_LETTER_OR_NUMERAL: /[a-z]/ | /[0-9]/

// Internal symbol added by the preprocess_blocks function to indicate the end of blocks
_END_BLOCK: "#ENDBLOCK"
4 changes: 2 additions & 2 deletions grammars/level5-Additions.lark
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ assign: var _IS textwithspaces
error_print_no_quotes: _PRINT (textwithoutspaces | list_access | var_access) (_SPACE (textwithoutspaces | list_access | var_access))* -> error_print_nq

// new commands for level 5
ifpressed_else: _IF SMALL_LETTER_OR_NUMERAL _IS _PRESSED _EOL* _if_less_command (_SPACE+ _EOL* | _SPACE* _EOL+) _ELSE (_SPACE+ _EOL* | _SPACE* _EOL+) _if_less_command
ifpressed: _IF SMALL_LETTER_OR_NUMERAL _IS _PRESSED _EOL* _if_less_command
ifpressed_else: _IF LETTER_OR_NUMERAL _IS _PRESSED _EOL* _if_less_command (_SPACE+ _EOL* | _SPACE* _EOL+) _ELSE (_SPACE+ _EOL* | _SPACE* _EOL+) _if_less_command
ifpressed: _IF LETTER_OR_NUMERAL _IS _PRESSED _EOL* _if_less_command

_if_clause: _IF (condition (_SPACE+ _EOL* | _SPACE* _EOL+) | condition_spaces _SPACE* _EOL+ | error_condition) _if_less_command
error_condition: condition_spaces _SPACE
Expand Down
4 changes: 2 additions & 2 deletions grammars/level8-Additions.lark
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ condition: equality_check | in_list_check
equality_check: (textwithoutspaces | NUMBER) (_IS | _EQUALS) (textwithoutspaces | NUMBER) (_SPACE | textwithoutspaces | NUMBER)*

// from level 8 on ifpressed is implemented slightly differently
ifpressed: _IF SMALL_LETTER_OR_NUMERAL _IS _PRESSED _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK
ifpressed_else: _IF SMALL_LETTER_OR_NUMERAL _IS _PRESSED _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK (_SPACE)* _ELSE (_SPACE)* _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK
ifpressed: _IF LETTER_OR_NUMERAL _IS _PRESSED _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK
ifpressed_else: _IF LETTER_OR_NUMERAL _IS _PRESSED _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK (_SPACE)* _ELSE (_SPACE)* _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK
// from level 8 on if is implemented slightly differently
elses: _EOL (_SPACE)* _ELSE (_SPACE)* _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK
ifpressed_elses: elses
Expand Down
8 changes: 4 additions & 4 deletions hedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,13 +1591,13 @@ def make_ifpressed_command(self, command):

def ifpressed(self, met, args):
return self.make_ifpressed_command(f"""\
if event.key == pygame.K_{args[0]}:
if event.unicode == '{args[0]}':
{ConvertToPython.indent(args[1], 6)}
break""")

def ifpressed_else(self, met, args):
return self.make_ifpressed_command(f"""\
if event.key == pygame.K_{args[0]}:
if event.unicode == '{args[0]}':
{ConvertToPython.indent(args[1], 6)}
break
else:
Expand Down Expand Up @@ -1751,7 +1751,7 @@ def ifpressed(self, met, args):
all_lines = ConvertToPython.indent(all_lines, 6)

return self.make_ifpressed_command(f"""\
if event.key == pygame.K_{args[0]}:
if event.unicode == '{args[0]}':
{all_lines}
break""")

Expand All @@ -1762,7 +1762,7 @@ def ifpressed_else(self, met, args):
all_lines = ConvertToPython.indent(all_lines, 6)

return self.make_ifpressed_command(f"""\
if event.key == pygame.K_{args[0]}:
if event.unicode == '{args[0]}':
{all_lines}
break""")

Expand Down
2 changes: 1 addition & 1 deletion static/vendor/pygame_4_skulpt/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var createKeyboardEvent = function (event) {
var difference = 0;
if ((event.which <= 90) && (event.which >= 65))
difference = 32;
return [keyPGConstant, { key: (event.which + difference)}];
return [keyPGConstant, { key: (event.which + difference), unicode: event.key}];
}
};

Expand Down
73 changes: 57 additions & 16 deletions tests/test_level/test_level_05.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ def test_if_pressed_x_is_letter_key(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
print(f'it is a letter key')
break""")

Expand All @@ -783,10 +783,10 @@ def test_double_if_pressed(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
print(f'first key')
break
if event.key == pygame.K_y:
if event.unicode == 'y':
print(f'second key')
break""")

Expand All @@ -806,7 +806,7 @@ def test_if_pressed_has_enter_after_pressed(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
print(f'it is a letter key')
break""")

Expand All @@ -825,7 +825,7 @@ def test_if_pressed_1_is_number_key(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
if event.unicode == '1':
print(f'it is a number key')
break""")

Expand All @@ -844,7 +844,7 @@ def test_if_pressed_with_trailing_spaces_after_key(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
print(f'trailing spaces!')
break""")

Expand All @@ -866,7 +866,7 @@ def test_if_pressed_x_else(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
print(f'x is pressed!')
break
else:
Expand All @@ -889,7 +889,7 @@ def test_if_pressed_has_enter_before_else(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
print(f'x is pressed!')
break
else:
Expand All @@ -914,7 +914,7 @@ def test_if_pressed_has_enter_before_both_prints_and_else(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
print(f'x is pressed!')
break
else:
Expand All @@ -938,7 +938,7 @@ def test_if_pressed_has_enter_before_first_print_and_else(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
print(f'x is pressed!')
break
else:
Expand All @@ -962,7 +962,7 @@ def test_if_pressed_has_enter_before_second_print_and_else(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
print(f'x is pressed!')
break
else:
Expand All @@ -986,7 +986,7 @@ def test_if_pressed_has_enter_before_both_prints(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
print(f'x is pressed!')
break
else:
Expand All @@ -1011,7 +1011,7 @@ def test_if_pressed_with_turtleforward(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
{HedyTester.indent(
HedyTester.forward_transpiled(50, self.level),
14, True)
Expand All @@ -1038,7 +1038,7 @@ def test_if_pressed_with_turtleturn(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
{HedyTester.indent(
HedyTester.turn_transpiled(90, self.level),
14, True)
Expand All @@ -1065,7 +1065,7 @@ def test_if_pressed_with_turtlecolor(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
{HedyTester.indent(
HedyTester.turtle_color_command_transpiled('red'),
14, True)
Expand All @@ -1092,7 +1092,7 @@ def test_if_pressed_else_with_turtle(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
{HedyTester.indent(
HedyTester.forward_transpiled(25, self.level),
14, True)
Expand All @@ -1112,6 +1112,47 @@ def test_if_pressed_else_with_turtle(self):
max_level=7
)

def test_if_pressed_non_latin(self):
code = textwrap.dedent("""\
if ش is pressed forward 25
if ש is pressed forward 25
if ф is pressed forward 25""")

expected = HedyTester.dedent(f"""\
while not pygame_end:
pygame.display.update()
event = pygame.event.wait()
if event.type == pygame.QUIT:
pygame_end = True
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.unicode == 'ش':
{HedyTester.indent(
HedyTester.forward_transpiled(25, self.level),
14, True)
}
break
if event.unicode == 'ש':
{HedyTester.indent(
HedyTester.forward_transpiled(25, self.level),
14, True)
}
break
if event.unicode == 'ф':
{HedyTester.indent(
HedyTester.forward_transpiled(25, self.level),
14, True)
}
break""")

self.multi_level_tester(
code=code,
expected=expected,
extra_check_function=self.is_turtle(),
max_level=7
)

#
# pressed negative tests
#
Expand Down
2 changes: 1 addition & 1 deletion tests/test_level/test_level_07.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def test_if_pressed_repeat(self):
code = "if x is pressed repeat 5 times print 'doe het 5 keer!'"

expected = HedyTester.dedent("""\
if event.key == pygame.K_x:
if event.unicode == 'x':
for i in range(int('5')):
print(f'doe het 5 keer!')
time.sleep(0.1)""")
14 changes: 7 additions & 7 deletions tests/test_level/test_level_08.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ def test_if_pressed_x_print(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
print(f'it is a letter key')
break""")
self.multi_level_tester(code=code, expected=expected, max_level=11)
Expand All @@ -829,10 +829,10 @@ def test_double_if_pressed(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
print(f'first key')
break
if event.key == pygame.K_y:
if event.unicode == 'y':
print(f'second key')
break""")

Expand All @@ -852,7 +852,7 @@ def test_if_pressed_is_number_key_print(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
if event.unicode == '1':
print(f'it is a number key')
break""")

Expand All @@ -878,7 +878,7 @@ def test_if_pressed_x_else(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
print(f'x is pressed!')
break
else:
Expand All @@ -905,7 +905,7 @@ def test_if_pressed_with_turtlecolor(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
{HedyTester.indent(
HedyTester.turtle_color_command_transpiled('red'),
14, True)
Expand Down Expand Up @@ -935,7 +935,7 @@ def test_if_pressed_else_with_turtle(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
{HedyTester.indent(
HedyTester.forward_transpiled(25, self.level),
14, True)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_level/test_level_09.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def test_if_pressed_repeat(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
for i in range(int('5')):
print(f'doe het 5 keer!')
time.sleep(0.1)
Expand Down Expand Up @@ -259,7 +259,7 @@ def test_if_pressed_repeat_turtle_moves_multiple_keys(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
if event.unicode == 'w':
trtl = 25
try:
trtl = int(trtl)
Expand All @@ -268,23 +268,23 @@ def test_if_pressed_repeat_turtle_moves_multiple_keys(self):
t.forward(min(600, trtl) if trtl > 0 else max(-600, trtl))
time.sleep(0.1)
break
if event.key == pygame.K_a:
if event.unicode == 'a':
trtl = -90
try:
trtl = int(trtl)
except ValueError:
raise Exception(f'While running your program the command <span class="command-highlighted">turn</span> received the value <span class="command-highlighted">{trtl}</span> which is not allowed. Try changing the value to a number.')
t.right(min(600, trtl) if trtl > 0 else max(-600, trtl))
break
if event.key == pygame.K_d:
if event.unicode == 'd':
trtl = 90
try:
trtl = int(trtl)
except ValueError:
raise Exception(f'While running your program the command <span class="command-highlighted">turn</span> received the value <span class="command-highlighted">{trtl}</span> which is not allowed. Try changing the value to a number.')
t.right(min(600, trtl) if trtl > 0 else max(-600, trtl))
break
if event.key == pygame.K_s:
if event.unicode == 's':
trtl = 180
try:
trtl = int(trtl)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_level/test_level_10.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_if_pressed_with_list_and_for(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if event.unicode == 'x':
for dier in lijstje:
print(f'dier')
time.sleep(0.1)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_level/test_level_11.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,12 @@ def test_if_pressed_works_in_for_loop(self):
pygame.quit()
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
if event.unicode == 'p':
print(f'press')
break
time.sleep(0.1)""")

self.single_level_tester(
code=code,
expected=expected,
)
)
Loading

0 comments on commit 2eabf03

Please sign in to comment.