That's an implementation of a interpreter for my own programming language, called kl-rs(kevin's language), the postfix is of course because i will be using rust as its host language!
This language is hightly expression-based, but there are 2 types of statements
in it: return
statements and let
statements. Everything else is an
expression!
That has some interesting implications, we can bind any expresion to a name
using the let
statement, so we can do stuff like this for example:
let result = if (<expression>) { <block of code> } else { <block of code> }
This works because the if statement itself is an expression, so the block of code that is evaluated generates an expresion value, which is in turn binded to the result variable!
This is a really simple language with just a few keywords: let
, return
,
fn
, else
, if
, false
, true
.
To define a function, as it is an expresion, we can just use a bind:
let foo = fn(params...) { <body> }
but we can also create clojures quite easily:
let foo = fn(params...) { <body> }
foo(fn(...) {})
In the example above, we're defining the foo
function and running it with an
unnamed function, a clojure.
As in most languages, strings here are also represented using ""
, and they are
expressions as well, that means we can bind them to variables, return them from
functions or even let them as the last expression in a block of code, in which
case they will be evaluated!
let foo = "hello world";
let foo = if(<condition>) { "foo" } else { "bar" };
In this case, if the condition is true, the result binded to the variable foo will be "foo", otherwise it will be "bar"
let foo = fn(...) { return "foo" };
foo(); # "foo"
In this language, there is support for true
and false
booleans, they can be
used as you would expect!
In this language, just like C, numbers can be used in if-else expressions. 0
Meaning false
and any other number meaning true
It's possible to comment code out using the syntax:
/* comment */
This syntax works for both, single line and multiline comments!
In order to run this project on you machine, first make sure you got rust correctly installed.
Then, you can run it directly using cargo with the following command:
cargo run
Afterwards, a REPL will appear and you can start writing kl-rs code!
There are a couple flags that you can use to inspect this program, you can see all of them using the command:
cargo run -- -h
Building this project is extremely straightforward, you just run:
cargo build --release
After that, an executable will be generated in the folder
target/release/kl-rs
, then you can just use it like normal
- Add support for math expressions
- Add support for return statements
- Add support for if-else expressions
- Add support for function expressions
- Add support for comments
- Add support for boolean expressions
- Add support for let statements
- Add standard library len function
- Add loops
- Add support for arrays
- Add build in functions
- Add support for hashes
- Refactor tests
- Optimize project