Skip to content

KPMGE/kl-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kl-rs

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!

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!

Keywords

This is a really simple language with just a few keywords: let, return, fn, else, if, false, true.

Defining functions

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.

Strings

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!

Binding to a name

let foo = "hello world";

Using as the result of an expression

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"

Returning from a function

let foo = fn(...) { return "foo" };
foo(); # "foo"

Booleans

In this language, there is support for true and false booleans, they can be used as you would expect!

Truthy values

In this language, just like C, numbers can be used in if-else expressions. 0 Meaning false and any other number meaning true

Comments

It's possible to comment code out using the syntax:

/* comment */

This syntax works for both, single line and multiline comments!

How to run it?

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 --bin kl-rs

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 --bin kl-rs -- -v

TODOS

  • 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

Virtual Machine

On top of the kl-rs programming language, this project also has a simple stack-based virtual machine, intented to run kl-rs code(not yet finished). This virtual machine has an assembly-like language, called ksm. This assembly language is intented to be human-readable and you can even create programs in it Check ksm/examples/ to get some examples.

The virtual machine itself, called kvm doesn't run the ksm code directly though, it runs a bytecode of it, just like in x86 assembly, when you compile it, it is not human-readable anymore, but optimized for running.

In order to compile a ksm program, you can run the following command:

cargo run --bin ksm -- -i ksm/examples/fibonacci.ksm -o fibonacci.kvm

As you might expect, the -i or --input flag tells the ksm what the input program is. The -o or --output flat states what the output should be. After that, you can run your kvm code on the virtual machine, by running the command:

cargo run --bin kvm -- fibonacci.kvm

About

Implementation of my own programming language, made in rust kl(kevin's language) rs(rust)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages