This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel11.lark
77 lines (56 loc) · 3.21 KB
/
level11.lark
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// trying to simply copy level 6 allowing for spaces
// the idea here is:
// allows for almost the same grammar as 6, easy for us
// gives better error messages out of the box
// we check indents in code, we can give better indent errors
start: program
program: _EOL* command (" ")* (_EOL+ command (" ")*)* _EOL* //lines may end on spaces and might be separated by many newlines
?command: print
| ifs elifs? elses?
| input
| for_loop
| assign_list
| list_access_var
| assign //placing it here means print is will print 'is' and print is Felienne will print 'is Felienne'
//we had to drop invalid, I think because of the if-ifelse, but for now it is pretty ok!!
_EOL: "\r"?"\n"
print : "print(" (quoted_text | list_access | var | sum) (" " (quoted_text | list_access | var | sum))* ")"
input : var " is input(" (quoted_text | list_access | var | sum) (" " (quoted_text | list_access | var | sum))* ")"
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: textwithoutspaces " " textwithspaces
// new commands for level 4
elses : _EOL (" ")* "else"":" _EOL (" "+ command) (_EOL " "+ command)* _EOL "end-block"
ifs: "if " condition ":" _EOL (" "+ command) (_EOL " "+ command)* _EOL "end-block" //'if' cannot be used in Python, hence the name of the rule is 'ifs'
elifs: _EOL (" ")* "elif " condition ":"_EOL (" "+ command) (_EOL " "+ command)* _EOL "end-block"
condition: (equality_check|in_list_check) (" and " condition)*
list_access_var : var " is " var " at " (index | random)
equality_check: textwithoutspaces " is " textwithoutspaces
in_list_check: textwithoutspaces " in " var
//new for level 6
?sum: product
| sum " "* "+" " "* product -> addition
| sum " "* "-" " "* product -> substraction
?product: atom
| product " "* "*" " "* atom -> multiplication
| product " "* "/" " "* atom -> division
?atom: NUMBER | var | NAME //TODO: means we cannot assign strings with spaces? would we want that?
//new for level 8
for_loop: "for " (NAME | var) " in " "range(" (NUMBER | var) (", "|",") (NUMBER | var) "):" _EOL (" "+ command) (_EOL " "+ command)* _EOL "end-block"
var: NAME -> var
list_access : var " at " (index | random) -> list_access //todo: could be merged with list_access_var?
index : NUMBER
random : "random"
textwithspaces: /([^\n,]+)/ -> text //anything can be parsed except for a newline and a comma for list separators
textwithoutspaces: /([^\n, :*+-\/]+)/ -> text //anything can be parsed except for spaces (plus: a newline and a comma for list separators)
//plus in level 6, calculation elements
//plus in level 8, colon
quoted_text_no_escape: /'([^']*)'/ -> text //simply all between quotes should this be used at earlier levels?
quoted_text: /'((?:[^\\']|\\.)*)'/ -> text //text can be between single quotes, but quotes may be escaped with \
%import common.LETTER // imports from terminal library
%import common.DIGIT // imports from terminal library
%import common.WS_INLINE // imports from terminal library
%import common.NEWLINE // imports from terminal library
%import common.SIGNED_INT -> NUMBER
%import common.CNAME -> NAME