-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar
44 lines (33 loc) · 1.81 KB
/
grammar
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
program => (statement)*
statement => if | while | codeBlock | function | return | valueDeclaration | input | output | assignment | expression | typeDeclaration
if => "if" expression "then" (statement)* ("else" (statement)*)? "end"
while => "while" expression codeBlock
codeBlock => "do" program "end"
function => STRING "(" (STRING (",")*)* ")" functionReturnType codeBlock
functionReturnType => NOTHING | ":" typeNotation
return => "return" expression
valueDeclaration => "val" pattern (":" typeNotation)? "":=" expression
assignment => STRING ":=" expression
input => "input" STRING
output => "output" expression
typeNotation => primitiveType
primitiveType => "unit" | "list" | ((atomicType | functionType | tupleType) "list"?)
functionType => ("(" (typeNotation)? (","typeNotation)* ")" ("->" typeNotation)?)
tupleType => "(" typeNotation ("," typeNotation)* ")"
atomicType => STRING | CONSTRUCTOR
pattern => STRING (":" typeNotation)? | constructorPattern | tuplePattern
constructorPattern => CONSTRUCTOR tuplePattern
tuplePattern => "(" (pattern ","?)+ ")"
listDeclaration = "[" expressionList "]"
expressionList = (expression (",")*)*
expression => logical
logical => equality ( ( "and" | "or" ) equality )*
equality => comparison ( ( "!=" | "==" ) comparison )*
comparison => addition ( ( ">" | ">=" | "<" | "<=" ) addition )*
addition => multiplication ( ( "-" | "+" ) multiplication )*
multiplication => unary ( ( "/" | "*" ) unary )*
unary => ( "!" | "-" ) unary | primary ("[" expressionList "]" | "(" expressionList ")")*
primary => NUMBER | STRING | "false" | "true" | "(" expression ")" | listDeclaration
anonymousFunction => "function" "(" (STRING typeNotation(",")?)* ")" functionReturnType codeBlock
typeDeclaration => "type" STRING ("<" ((STRING)(",")?)+ ">")? ":=" typeDefinition
typeDefinition => STRING ("of" typeNotation)?