Skip to content

Latest commit

 

History

History
95 lines (68 loc) · 2.79 KB

README.md

File metadata and controls

95 lines (68 loc) · 2.79 KB

jlox

TS Implementation of Lox interpreter (Crafting Interpreters by Bob Nystrom) with some differences.

Lox interpreter的TS实现.

A live editor that can modify code. A button clicked to compile & run the code.

Install

npm install

npm run start

Grammer

declaration → classDecl | funDecl | varDecl | statement ;

classDecl → "class" IDENTIFIER ( "<" IDENTIFIER )? "{" function* "}" ; funDecl → "fun" function ; varDecl → "var" IDENTIFIER ( "=" expression )? ";" ;

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

exprStmt → expression ";" ; forStmt → "for" "(" ( varDecl | exprStmt | ";" ) expression? ";" expression? ")" statement ; ifStmt → "if" "(" expression ")" statement ( "else" statement )? ; printStmt → "print" expression ";" ; returnStmt → "return" expression? ";" ; whileStmt → "while" "(" expression ")" statement ; block → "{" declaration* "}" ;

expression → comma ;

comma → assignment ( "," assignment )* ;

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

ternary → logic_or ( "?" ternary ":" ternary ) ;

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 )* ; primary → "true" | "false" | "nil" | "this" | NUMBER | STRING | IDENTIFIER | "(" expression ")" | "super" "." IDENTIFIER ;

function → IDENTIFIER "(" parameters? ")" block ; parameters → IDENTIFIER ( "," IDENTIFIER )* ; arguments → expression ( "," expression )* ;

NUMBER → DIGIT+ ( "." DIGIT+ )? ; STRING → """ <any char except """>* """ ; IDENTIFIER → ALPHA ( ALPHA | DIGIT )* ; ALPHA → "a" ... "z" | "A" ... "Z" | "_" ; DIGIT → "0" ... "9" ;

Additional Operators & Statements:

  1. Support right associative ternary operator
  2. Support comma operator
  3. Support break statement
  4. Support anonymous function expression

Differences:

  1. Slightly different error handling
  2. Partly use js coerion (TODO: More consistent coersion support)
  3. 'Tag' (just for fun, surrounded by single quote('xxxx'))