Skip to content

A fully operational object-oriented language with elements of functional programming. This is a Kotlin variant of Lox language from http://craftinginterpreters.com.

Notifications You must be signed in to change notification settings

Dualeco/KLox-language

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 

Repository files navigation

KLox-language

A variant of the Lox language with enhanced syntax

(Great thanks to GraydenH for Expr.kt and Stmt.kt)

Syntax in BNF:

program        → declaration* EOF ;
1. Declarations

declaration    → classDecl
               | funDecl
               | varDecl
               | statement ;

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

funDecl        → "fun" function ;

varDecl        → "var" IDENTIFIER ( "=" expression )? ;
2. Statements

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

exprStmt       → expression ;

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

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

returnStmt     → "return" expression? ;

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

block          → "{" declaration* "}" ;
3. Expressions

expression     → assignment ;

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

logic_and      → equality ( "and" equality )* ;

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

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

addition       → multiplication ( ( "-" | "+" ) multiplication )* ;

multiplication → power ( ( "/" | "*" ) power )* ;

power → mod ( "^" mod )* ;

mod → unary ( "%" unary )* ;

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

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

primary        → "true" | "false" | "nil" | "this" | "break | "continue"
               | NUMBER | STRING | IDENTIFIER | "(" expression ")"
               | "super" "." IDENTIFIER ;
4. Utility Rules

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

About

A fully operational object-oriented language with elements of functional programming. This is a Kotlin variant of Lox language from http://craftinginterpreters.com.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages