8000 GitHub - gabryon99/klox
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

gabryon99/klox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

klox

This is an implementation of the Lox programming language, invented by Robert Nystrom in his Crafting Interpreters book.

The interpreter is implemented using Kotlin instead of Java, this explains why the k before the name.

Example

fun main() {
    print "42 is the ultimate answer.";
}

Grammar

program         ::=     declaration* EOF

declaration     ::=     classDecl | varDecl | funDecl | statement 

classDecl       ::=     "class" IDENTIFIER ("<:" IDENTIFIER)? "{" function* "}"
varDecl         ::=     "var" IDENTIFIER ("=" expression)? ";"
funDecl         ::      "fun" function
statement       ::=     exprStmr | printStmt | ifStmt | whileStmt | block | alterFlowStmt | returnStmt

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

function        ::=     "static"? IDENTIFIER "(" paramaters? ")" block
paramaters      ::=     IDENTIFIER ("," IDENTIFIER)*

expression      ::=     assignment
assignment      ::=     (call ".")? IDENTIFIER "=" assignment | comparison "?" expression ":" expression | 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" | "(" expression ")" | IDENTIFIER | lambda | "super" "." IDENTIFIER
lambda          ::=     "fun" "(" parameters? ")" block

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0