Skip to content

Commit

Permalink
🧹 Refactor Pygame Code (hedyorg#5117)
Browse files Browse the repository at this point in the history
PR for removing the use of Pygame within Hedy.
We still want to keep the functionality of "if button pressed".
  • Loading branch information
ToniSkulj authored Mar 27, 2024
1 parent 3a94a4d commit 5494e07
Show file tree
Hide file tree
Showing 99 changed files with 932 additions and 5,458 deletions.
4 changes: 2 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,8 @@ def parse():

response['source_map'] = source_map_result

if transpile_result.has_pygame:
response['has_pygame'] = True
if transpile_result.has_pressed:
response['has_pressed'] = True

if transpile_result.has_turtle:
response['has_turtle'] = True
Expand Down
4 changes: 2 additions & 2 deletions build-tools/heroku/generate-prefixes-ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cat <<EOF > $python_prefix_ts
*
* !!! THIS FILE HAS BEEN GENERATED. DO NOT EDIT !!!
*
* Make desired changes to prefixes/{normal,pygame,turtle,music}.py
* Make desired changes to prefixes/{normal,pressed,turtle,music}.py
*
* Add/edit tests for those changes in tests/test_python_prefixes.py
*
Expand All @@ -19,7 +19,7 @@ cat <<EOF > $python_prefix_ts
*******************************************************************************/
EOF

for prefix in turtle pygame normal music; do
for prefix in turtle pressed normal music; do
echo >> $python_prefix_ts
echo "export const ${prefix}_prefix = " >> $python_prefix_ts
echo -n '`' >> $python_prefix_ts
Expand Down
2 changes: 1 addition & 1 deletion grammars/level15-Additions.lark
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
command: += while_loop | ifpressed -= error_ifpressed_missing_else >> error_ifelse | ifs
command: += while_loop | if_pressed -= error_if_pressed_missing_else >> error_ifelse | ifs

// new : while loop
while_loop: _WHILE (_conditions) _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK
Expand Down
9 changes: 5 additions & 4 deletions grammars/level17-Additions.lark
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// We add the : at the end of if and else and elif and for/while

command: += ifpressed (ifpressed_elifs)* ifpressed_elses? | ifs (elifs)* elses? -= ifs elses?
command: += if_pressed (if_pressed_elifs)* if_pressed_elses? | ifs (elifs)* elses? -= ifs elses?

// These will be handled by the preprocessor step in the merger
// It will find the appropiate function for this anotation and modify it accordingly
ifpressed_else<needs_colon>
ifpressed<needs_colon>
ifpressed_elifs: _EOL _ELIF (LETTER_OR_NUMERAL | NAME) _IS _PRESSED _COLON _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK
if_pressed<needs_colon>
if_pressed_else<needs_colon>
if_pressed_elses<needs_colon>
if_pressed_elifs: _EOL _ELIF (LETTER_OR_NUMERAL | NAME) _IS _PRESSED _COLON _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK

ifs<needs_colon>
elses<needs_colon>
Expand Down
7 changes: 4 additions & 3 deletions grammars/level5-Additions.lark
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ _empty_program: (_EOL | _SPACE)*
_non_empty_program: _EOL* (command | error_invalid) _SPACE* (_EOL+ command _SPACE*)* _EOL* //lines may end on spaces and might be separated by many newlines

//placing assign after print means print is will print 'is' and print is Felienne will print 'is Felienne'
command: += assign_button | ifpressed_else | error_ifpressed_missing_else | ifelse | ifs -= error_invalid >> assign
command: += assign_button | if_pressed_else | error_if_pressed_missing_else | if_pressed_without_else | ifelse | ifs -= error_invalid >> assign
_if_less_command: print | ask | play | turtle | assign_button | assign_list | add | remove | sleep | error_print_no_quotes | assign

// error_invalid is moved from to command to program, so that command rules have priority over error_invalid
Expand All @@ -21,8 +21,9 @@ assign: var _IS (list_access | 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 (LETTER_OR_NUMERAL | var) _IS _PRESSED _EOL* _if_less_command (_SPACE+ _EOL* | _SPACE* _EOL+) _ELSE (_SPACE+ _EOL* | _SPACE* _EOL+) _if_less_command
error_ifpressed_missing_else: _IF (LETTER_OR_NUMERAL | var) _IS _PRESSED _EOL* _if_less_command
if_pressed_else: _IF (LETTER_OR_NUMERAL | var) _IS _PRESSED _EOL* _if_less_command (_SPACE+ _EOL* | _SPACE* _EOL+) _ELSE (_SPACE+ _EOL* | _SPACE* _EOL+) _if_less_command
if_pressed_without_else: _IF (LETTER_OR_NUMERAL | var) _IS _PRESSED _EOL* _if_less_command
error_if_pressed_missing_else: if_pressed_without_else

_if_clause: _IF (condition (_SPACE+ _EOL* | _SPACE* _EOL+) | condition_spaces _SPACE* _EOL+ | error_condition) _if_less_command
error_condition: condition_spaces _SPACE
Expand Down
2 changes: 1 addition & 1 deletion grammars/level6-Additions.lark
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ _print_argument: (_SPACE | quoted_text | list_access | expression | print_expres
?print_expression: var_access_print | error_unsupported_number | INT

// redefining it entirely since it has many order-depending rules
command: clear | error_non_decimal | print | turtle | play | add | remove | error_add_missing_to | error_remove_missing_from | sleep | error_print_no_quotes | error_print_one_quote_only | ifpressed_else | error_ifpressed_missing_else | ifelse | ifs | ask | error_ask_no_quotes| assign_button | assign | assign_list | error_invalid_space | error_text_no_print | empty_line
command: clear | error_non_decimal | print | turtle | play | add | remove | error_add_missing_to | error_remove_missing_from | sleep | error_print_no_quotes | error_print_one_quote_only | if_pressed_else | error_if_pressed_missing_else | if_pressed_without_else | ifelse | ifs | ask | error_ask_no_quotes| assign_button | assign | assign_list | error_invalid_space | error_text_no_print | empty_line
_if_less_command: print | turtle | play | add | remove | sleep | error_print_no_quotes | error_print_one_quote_only | ask | error_ask_no_quotes | assign_button | assign | assign_list

//splitting these commands into two rules, one for equals and one for is so they can be properly handled in the translator
Expand Down
12 changes: 6 additions & 6 deletions grammars/level8-Additions.lark
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
command: += error_repeat_dep_8 | error_ifelse | error_ifpressed_missing_else | ifpressed ifpressed_elses? | ifs elses? -= error_invalid_space | ifelse
command: += error_repeat_dep_8 | error_ifelse | if_pressed_without_else | if_pressed if_pressed_elses? | ifs elses? -= error_invalid_space | ifelse

repeat.1: _REPEAT (INT | var_access) _TIMES _SPACE? _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK
error_repeat_dep_8: _REPEAT (INT | var_access) _TIMES _SPACE command
Expand All @@ -7,14 +7,14 @@ error_repeat_dep_8: _REPEAT (INT | var_access) _TIMES _SPACE command
// not sure of we want it, but we could
equality_check: (textwithoutspaces | NUMBER) (_IS | _EQUALS) (textwithoutspaces | NUMBER) (_SPACE | textwithoutspaces | NUMBER)*

// from level 8 on ifpressed is implemented slightly differently
ifpressed: _IF (LETTER_OR_NUMERAL | NAME) _IS _PRESSED _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK
ifpressed_else: _IF (LETTER_OR_NUMERAL | NAME) _IS _PRESSED _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK (_SPACE)* _ELSE (_SPACE)* _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK
error_ifpressed_missing_else: ifpressed
// from level 8 on if_pressed is implemented slightly differently
if_pressed: _IF (LETTER_OR_NUMERAL | NAME) _IS _PRESSED _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK
if_pressed_else: _IF (LETTER_OR_NUMERAL | NAME) _IS _PRESSED _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK (_SPACE)* _ELSE (_SPACE)* _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK
if_pressed_without_else: if_pressed

// 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
if_pressed_elses: _EOL (_SPACE)* _ELSE (_SPACE)* _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK
//'if' cannot be used in Python, hence the name of the rule is 'ifs'
ifs: _IF condition _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK

Expand Down
Loading

0 comments on commit 5494e07

Please sign in to comment.