-
Notifications
You must be signed in to change notification settings - Fork 0
/
coffeemaker.ohm
91 lines (75 loc) · 3.35 KB
/
coffeemaker.ohm
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
CoffeeMaker {
Program = Statement*
Statement = VarDec -- vardec
| FuncDec -- funcdec
| ClassDec -- classDec
| id "=" Exp -- assign
| print Exp -- print
| LoopStmt -- loop
| IfStmt -- if
| return Exp -- return
| UpdateExp -- update
| Exp -- exp
VarDec = Type id "=" Exp
FuncDec = function Type id "->" "(" Params ")" Block
ClassDec = class id "{" ConstructDec MethodDec* "}"
ConstructDec = construct "(" self "," Params ")" "{" Field* "}"
Field = this "." id "=" Exp
MethodDec = function Type id "->" "(" self "," Params ")" Block
LoopStmt = while Exp Block -- while
UpdateExp = id updateop
IfStmt = if "(" Exp ")" Block else Block -- long
| if "(" Exp ")" Block else IfStmt -- elsif
| if "(" Exp ")" Block -- short
Block = "{" Statement* "}"
Exp = ("-" | "!") Exp7 -- unary
| Exp1 "?" Exp1 ":" Exp -- ternary
| Exp1
Exp1 = Exp1 "||" Exp2 -- or
| Exp2
Exp2 = Exp2 "&&" Exp3 -- and
| Exp3
Exp3 = Exp4 ("<="|"<"|"=="|"!="|">="|">") Exp4 -- compare
| Exp4
Exp4 = Exp4 ("+" | "-") Exp5 -- add
| Exp5
Exp5 = Exp5 ("*" | "/" | "%") Exp6 -- multiply
| Exp6
Exp6 = Exp7 "**" Exp6 -- power
| Exp7
Exp7 = num
| int
| true
| false
| Call
| id -- id
| "(" Exp ")" -- parens
| stringlit
Call = id "(" Args ")"
Params = ListOf<ParamType, ",">
Args = ListOf<Exp, ",">
Type = id -- id
ParamType = Type id
function = "cup" ~idchar
print = "brew" ~idchar
while = "while" ~idchar
for = "stir" ~idchar
in = "in" ~idchar
true = "true" ~idchar
false = "false" ~idchar
if = "sugar" ~idchar
else = "cream" ~idchar
class = "keurig" ~idchar
construct = "create" ~idchar
return = "complete" ~idchar
self = "self"
this = "this"
keyword = function | print | while | for | in | true | false | if | else | class | construct | return | self | this
num = digit+ ("." digit+) (("E" | "e") ("+" | "-")? digit+)?
int = digit+
id = ~keyword letter idchar*
idchar = letter | digit | "_"
stringlit = "\"" (~"\\" ~"\"" ~"\n" any )* "\""
updateop = "++" | "--"
space += "#" (~"\n" any)* "\n" -- comment
}