Skip to content
/ kl-rs Public

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

Notifications You must be signed in to change notification settings

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

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

How to build it?

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

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

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