|
|
2 سال پیش | |
|---|---|---|
| .idea | 2 سال پیش | |
| doc | 2 سال پیش | |
| src | 2 سال پیش | |
| .gitignore | 2 سال پیش | |
| README.md | 2 سال پیش | |
| pom.xml | 2 سال پیش |
// Your first Lox program!
print "Hello, world!";
Unary expressions. A prefix ! to perform a logical not, and - to negate a number.
Binary expressions. The infix arithmetic(+,-,*,/) and logic operators (==, !=, <, <=,>,>=)
Parentheses. A pair of ( and ) wrapped around and expression.
Syntax tree - Expression
expression → literal
| unary
| binary
| grouping ;
literal → NUMBER | STRING | "true" | "false" | "nil" ;
grouping → "(" expression ")" ;
unary → ( "-" | "!" ) expression ;
binary → expression operator expression ;
operator → "==" | "!=" | "<" | "<=" | ">" | ">="
| "+" | "-" | "*" | "/" ;
| Name | Operators | Associates |
|------------|-----------|------------|
| Equality | == != | Left | | Comparison | > >= < <= | Left | | Term | - + | Left | | Factor | / * | Left | | Unary | ! - | Right |
```
expression → equality ;
equality → comparison ( ( "!=" | "==" ) comparison )* ;
comparison → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
term → factor ( ( "-" | "+" ) factor )* ;
factor → unary ( ( "/" | "*" ) unary )* ;
unary → ( "!" | "-" ) unary
| primary | call ;
call → primary( "(" arguments? ")" )* ;
arguments → expression ( "," expression)* ;
primary → NUMBER | STRING | "true" | "false" | "nil"
| "(" expression ")" | IDENTIFIER ;
- Syntax tree - Statement
```
program → statement* EOF ;
statement → exprStmt
| printStmt
| block;
block → "{" declaration* "}"
exprStmt → expression ";" ;
printStmt → "print" expression ";" ;
```
A program is a list of statements followed by the special "end of file" token.
The mandatory end token ensures the parse consumes the entire input and don't
silently ignore erroneous unconsumed tokens at the end of a script.
- Syntax tree - add Variable syntax
```
program → declaration* EOF ;
declaration → classDecl
| funDecl
| varDecl
| statement ;
statement → exprStmt
| forStmt
| ifStmt
| printStmt
| returnStmt
| whileStmt
| block ;
funDecl → "fun" function;
function → IDENTIFIER "(" parameters? ")" block ;
block → "{" declaration* "}"
ifStmt → "if" "(" expression ")" statement ( "else" statement)? ;
varDecl → "var" IDENTIFIER ( "=" expression )? ";" ;
returnStmt → "return" expression? ";";
classDecl -> "class" IDENTIFIER ("<" IDENTIFIER )? "{" function* "}" ;
function -> IDENTIFIER "(" parameters? ")" block;
parameters -> IDENTIFIER ( "," IDENTIFIER )* ;
```
- Syntax tree -- Logical Operators
expression → assignment ;
assignment → IDENTIFIER "=" assignment
| logic_or ;
logic_or → logic_and ( "or" logic_and )* ;
logic_and → equality ( "and" equality )* ;
```
whileStmt → "while" "(" expression ")" statement ; forStmt → "for" " (" (varDecl | exprStmt | ";") expression? ";" expression? ")" statement ;