Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sharparam/klox
Kotlin implementation of the Lox language
https://github.com/sharparam/klox
gradle interpreter kotlin lox parser parsing
Last synced: about 2 months ago
JSON representation
Kotlin implementation of the Lox language
- Host: GitHub
- URL: https://github.com/sharparam/klox
- Owner: Sharparam
- License: mit
- Created: 2017-08-28T02:28:51.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-04-03T01:47:37.000Z (over 2 years ago)
- Last Synced: 2024-05-01T18:58:35.641Z (8 months ago)
- Topics: gradle, interpreter, kotlin, lox, parser, parsing
- Language: Kotlin
- Size: 166 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# klox
A Kotlin implementation of the Lox language (mimics the Java implementation from ["Crafting Interpreters"][book]).The implementation follows [the book][book] by [munificent][].
To launch the interpreter or run a file, use `./klox [file]` after building the `jar` target with Gradle.
## Grammar
This is the grammar implemented in klox.
It's mostly the grammar as explained in the book, with some of the "challenges" implemented as well.
Both line (`//`) and multi-line (`/* */`) comments are supported (but not nested multi-line comments, at the moment).
```
program -> declaration* EOF ;declaration -> classDecl | funDecl | varDecl | statement ;
classDecl -> "class" IDENTIFIER ( "<" IDENTIFIER )? "{" function* "}" ;
funDecl -> "fun" function ;
function -> IDENTIFIER "(" parameters? ")" block ;
parameters -> IDENTIFIER ( "," IDENTIFIER )* ;varDecl -> "var" IDENTIFIER ("=" expression)? ";" ;
statement -> exprStmt | forStmt | ifStmt | returnStmt
| whileStmt | block | break | continue ;
exprStmt -> expression ";" ;
forStmt -> "for" "(" ( varDecl | exprStmt | ";" ) expression? ";" expression? ")" statement ;
ifStmt -> "if" "(" expression ")" statement ( "else" statement )? ;
returnStmt -> "return" expression? ";" ;
whileStmt -> "while" "(" expression ")" statement ;
block -> "{" declaration* "}" ;
break -> "break" ";" ;expression -> comma ;
comma -> assignment ( "," assignment )* ;
arguments -> assignment ( "," assignment )* ;
assignment -> ( call "." )? IDENTIFIER ( "-" | "+" )? "=" assignment | conditional ;
conditional -> logic_or ( "?" expression ":" conditional )? ;
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 -> NUMBER | STRING | "false" | "true" | "nil"
| "(" expression ")" | IDENTIFIER | funExpr
| "super" "." IDENTIFIER
// Erroneous grammar
| ( "!=" | "==" ) equality
| ( ">" | ">=" | "<" | "<=" ) comparison
| "+" addition
| ( "/" | "*" ) multiplication ;
funExpr -> "fun" "(" parameters? ")" block ;
```## Built-ins
The implementation contains the built-in functions from the book, as well as some additional ones.
Some of these functions are inspired from their namesakes in the Lua language.
* `print(value)` - Prints `value` to stdout (originally a statement).
* `clock()` - Returns current time in seconds since the Unix epoch.
* `read()` - Reads one line from stdin and returns it.
* `type(value)` - Returns the type of `value`.
* `tonumber(value)` - Converts `value` to a number (or `nil` if conversion fails).
* `tostring(value)` - Returns the string representation of `value`.## License
Copyright (c) 2017 by Adam Hellberg.
This project is licensed under the [MIT License][mit], following the [main repo][main] licensing the code part under MIT.
See the file `LICENSE` for more information.
[book]: http://www.craftinginterpreters.com/
[munificent]: https://github.com/munificent
[mit]: https://opensource.org/licenses/MIT
[main]: https://github.com/munificent/craftinginterpreters