-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.gr
More file actions
33 lines (30 loc) · 1.62 KB
/
grammar.gr
File metadata and controls
33 lines (30 loc) · 1.62 KB
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
program → declaration* EOF ;
declaration → classDecl | funDecl | varDecl | statement ;
classDecl → "native"? "class" IDENTIFIER (":" IDENTIFIER)? "{" function* "}" ;
funDecl → "fun" function ;
function → "native"? "static"? IDENTIFIER "(" parameters? ")" block ;
lambda → "fun" "(" parameters? ")" block;
parameters → IDENTIFIER ("," IDENTIFIER)* ;
varDecl → "let" IDENTIFIER ( "=" expression )? ";" ;
statement → exprStmt | forStmt | ifStmt | returnStmt | whileStmt | breakStmt | importStmt | block ;
exprStmt → expression ";" ;
forStmt → "for" "(" ( varDecl | exprStmt | ";" ) expression? ";" expression? ")" statement ;
ifStmt → "if" "(" expression ")" statement ("else" statement)? ;
returnStmt → "return" expression? ";" ;
whileStmt → "while" "(" expression ")" statement ;
breakStmt → "break" ";" ;
importStmt → "import" STRING ";" ;
block → "{" declaration* "}" ;
expression → assignment ;
assignment → (call ".")? IDENTIFIER "=" assignment | ternary ;
ternary → logic_or ( "?" expression ":" expression )? ";" ;
logic_or → logic_and ( "or" logic_and )* ;
logic_and → equality ( "and" equality )* ;
equality → comparison ( ( "!=" | "==" ) comparison )* ;
comparison → addition ( ( ">" | ">=" | "<" | "<=" ) addition )* ;
addition → multiplication ( ( "-" | "+" ) multiplication )* ;
multiplication → unary ( ( "/" | "*" ) unary )* ;
unary → ( "!" | "-" ) unary | call ;
call → primary ("(" arguments? ")" | "." IDENTIFIER)* ;
arguments → expression ("," expression)* ;
primary → NUMBER | STRING | "false" | "true" | "nil" | "(" expression ")" | IDENTIFIER | "super" "." IDENTIFIER | lambda;