about summary refs log tree commit diff
path: root/grammar.ebnf
blob: d2468389645404dcf53ef87d7d46bf9cce8088ab (plain) (blame)
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
Program      ::= Statement+

Statement    ::= (AssignmentStatement | ImportDeclaration | ExternDeclaration | CastStatement | SizeOfStatement | FunctionCallStatement | IfStatement | WhileStatement | ReturnStatement | "break" | "continue") SEMICOLON

AssignmentStatement ::= ("let")? ("*")? Expression EQUALS Expression

ImportDeclaration ::= "import" STRING

ExternDeclaration ::= "extern" IDENTIFIER EQUALS Type

FunctionCallStatement ::= (IDENTIFIER | FunctionDefinition) LPAREN FunctionArguments? RPAREN

IfStatement ::= "if" Expression LBRACE Statement* RBRACE -- TODO: Should function definitions be allowed?

WhileStatement ::= "while" Expression LBRACE Statement* RBRACE

ReturnStatement ::= RETURN (Expression)?

FunctionArguments ::= Expression ("," Expression)*

Expression ::= LogicalExpression

CastStatement ::= "cast" LPAREN TYPE "," Expression RPAREN

SizeOfStatement ::= "sizeof" LPAREN TYPE RPAREN

LogicalExpression ::= EqualityExpression (("and" | "or") EqualityExpression)*

EqualityExpression ::= AdditiveExpression (("==" | "!=" | "<=" | ">=" | "<" | ">") AdditiveExpression)*

AdditiveExpression ::= MultiplicativeExpression (("+" | "-") MultiplicativeExpression)*

MultiplicativeExpression ::= UnaryExpression (("*" | "/" | "%") UnaryExpression)*

UnaryExpression ::= ("!" | "-" | "*") UnaryExpression | PostfixExpression

PostfixExpression ::= PrimaryExpression (FieldAccess | FunctionCallStatement | CastStatement | SizeOfStatement)*

PrimaryExpression ::= NULL | NUMBER | BOOLEAN | CHAR | STRING | IDENTIFIER | FunctionDefinition | TypeDefinition | StructDefinition | StructInstantiation | FieldAccess | LPAREN Expression RPAREN

FunctionDefinition ::= LPAREN FunctionParameters? RPAREN ARROW IDENTIFIER LBRACE Statement* ReturnStatement SEMICOLON RBRACE

FunctionParameters ::= IDENTIFIER ":" Type ("," IDENTIFIER ":" Type)*

Type ::= IDENTIFIER | FunctionType

FunctionType ::= LPAREN (Type ("," Type)*)? RPAREN ARROW Type

ParameterTypes ::= Type ("," Type)*

TypeDefinition ::= "newtype" Type

StructDefinition ::= "struct" LBRACE (StructField ("," StructField)*)? RBRACE

StructField ::= IDENTIFIER ":" Type

StructInstantiation ::= IDENTIFIER LBRACE RBRACE

FieldAccess ::= Expression DOT IDENTIFIER