Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linlox 🚧

Lox interpreter implementation written in Kotlin

Syntax BNF


program       -> declarations* EOF ;

Declarations

declarations        -> funDecl | varDecl | statement ;
funDecl             -> "fun" IDENTIFIER functionBody ;
varDecl             -> "var" IDENTIFIER ( "=" expression )? ";" ;

Statements 

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

Expressions

expression       -> comma  ;
comma            -> assignment ( "," assignment )* ;
assignment       -> logic_or ( "=" assignment )? ;
logic_or         -> logic_and ( "or" logic_and )* ;
logic_and        -> condition ( "and" condition )* ;
condition        -> equality ( "?" expression ":" condition )? ;
equality         -> comparison ( ( "!=" | "==" ) comparison )* ;
comparison       -> term ( ( "<" | "<=" | ">" | ">=") term )* ;
term             -> factor ( ( "-" | "+" ) factor )* ;
factor           -> unary ( ( "/" | "*" ) unary )* ;
unary            -> ( "!" | "-" ) unary | call ;
call             -> primary ( "(" arguments? ")" )*  ;
arguments        -> assignment ( "," assignment )* ;
primary          -> NUMBER | STRING | "true" | "false" | "nil" | "(" expression ")" | IDENTIFIER | anonymousFun;
anonymousFun    -> "fun" functionBody ;
functionBody    -> "(" parameters? ")" block;
parameters      -> IDENTIFIER ( "," IDENTIFIER )* ;

What's new

This implementation includes several enhancements beyond standard Lox specification

  • Comma operator: Evaluates expressions sequentially frrom left to right, discards the early results and returns the final value
var x =  (1, 3, 4); // x is 4
  • Ternary operator
var result = isTrue ? "yes" : "no";
  • Supports C-style block comments
/* This is a 
   multiline block comment
 */
  • Supports anonymous functions/lambdas

About

A kotlin interpreter for the Lox language

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages