Skip to content

Latest commit

 

History

History
72 lines (46 loc) · 1.91 KB

syntax.md

File metadata and controls

72 lines (46 loc) · 1.91 KB

Expression

expression → assignment ;

assignment → ( call "." )? IDENTIFIER "=" assignment | logic_or ;

logic_or → logic_and ( "or" logic_and )* ;

logic_and → equality ( "and" equality )* ;

equality → comparison ( ( "!=" | "==" ) comparison )* ;

comparison → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;

term → factor ( ( "-" | "+" ) factor )* ;

factor → unary ( ( "/" | "" ) unary ) ;

unary → ( "!" | "-" ) unary | call ;

call → primary ( "(" arguments? ")" | "." IDENTIFIER )* ;

arguments → expression ( "," expression )* ;

primary → NUMBER | STRING | "true" | "false" | "nil" | "this" | "(" expression ")" | IDENTIFIER ; | "super" "." IDENTIFIER ;

Statement

program → declaration* EOF ;

declaration → classDecl | funDecl | varDecl | statement ;

classDecl → "class" IDENTIFIER ( "<" IDENTIFIER )? "{" function* "}" ;

funDecl → "fun" function ;

function → IDENTIFIER "(" parameters? ")" block ;

parameters → IDENTIFIER ( "," IDENTIFIER )* ;

statement → exprStmt | forStmt | ifStmt | printStmt | returnStmt | whileStmt | block ;

returnStmt → "return" expression? ";" ;

forStmt → "for" "(" ( varDecl | exprStmt | ";" ) expression? ";" expression? ")" statement ;

whileStmt → "while" "(" expression ")" statement ;

ifStmt → "if" "(" expression ")" statement ( "else" statement )? ;

block → "{" declaration* "}" ;

varDecl → "var" IDENTIFIER ( "=" expression )? ";"

exprStmt → expression ";" ;

printStmt → "print" expression ";" ;