/
pycante.ohm
93 lines (79 loc) · 3.13 KB
/
pycante.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
92
93
Pycante {
Program = Statement*
Statement = Assignment
| increment Exp7 -- inc
| decrement Exp7 -- dec
| FunctionCall
| Declaration
| PrintStatement
| Loop
| IfStatement
| Return
Declaration = VarDec
| FuncDec
| ClassDec
VarDec = "let" id "=" Exp
FuncDec = "🌶" id "(" Params ")" Type Statement* "end"
ClassDec = "♨️" id VarDec* FuncDec* "end"
Params = ListOf<Param, ",">
Param = id ":" Type
Exp = Exp "or" Exp1 -- or
| Exp1
Exp1 = Exp1 "and" Exp2 -- and
| Exp2
Exp2 = Exp2 relop Exp3 -- binary
| Exp3
Exp3 = Exp3 addop Exp4 -- binary
| Exp4
Exp4 = Exp4 mulop Exp5 -- binary
| Exp5
Exp5 = Exp5 "**" Exp6 -- binary
| Exp6
Exp6 = increment Exp7 -- inc
| decrement Exp7 -- dec
| Exp7
Exp7 = Exp7 "." FunctionCall -- methodCall
| Dictionary
| Exp7 "[" Exp "]" -- subscripted
| NumberLiteral
| StringLiteral
| BoolLiteral
| FunctionCall
| id -- id
| List
| WhatExp
| "(" Exp ")" -- parens
Exps = Exp ("," Exp)*
Type = "Boolean" | "Number" | "String"
keyword = ("let" | "♨️" | "🌶" | "for" | "while" | "return" | "end" | "if"
| "else" | "else if" | "what" | "true" | "false" | "print") ~idrest
FunctionCall = id "(" Exps? ")"
Assignment = id "=" Exp
Return = "return" Exp -- returnExpression
| "return" -- returnNothing
WhatExp = "what" "(" Exp ")"
List = "[" ListOf<Exp, ","> "]"
Dictionary = DictTerm ("," DictTerm)* "."
DictTerm = id ":" Exp
Loop = ForStatement | WhileStatement
ForStatement = "for" "(" VarDec ";" Exp ";" Exp ")" Statement* "end"
WhileStatement = "while" Test Statement* "end"
IfStatement = "if" Test Statement* ("else if" Test Statement*)* ("else" Statement*)? "end"
PrintStatement = "print:" (StringLiteral | Exp)
Test = "(" Exp ")"
NumberLiteral = digit+ ("." digit+)?
StringLiteral = "\"" (~"\""any)* "\""
BoolLiteral = "false" | "true"
addop = "+" | "-"
mulop = "*"| "%" | "/" | "//"
relop = "<=" | ">=" | ">" | "<" | "==" | "!="
increment = "++"
decrement = "--"
id = ~keyword ("_" | lower) idrest*
idrest = "_" | alnum
newline = "\n"+
space := " " | "\t" | "\n" | comment
comment = "~" (~"\n" any)* -- comment
| multiLineComment
multiLineComment = "|-" (~"-|" any)* "-|" -- multiLineComment
}