Skip to content

elvnh/kelp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

152 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kelp

kelp is an interpreted, statically typed, C-like programming language. It is compiled to bytecode which is then interpreted in a stack-based virtual machine.

The language is currently mostly complete and has features such as:

  • Arithmetic
  • Conditionals
  • Functions
  • Structs
  • Multidimensional arrays
  • Strings
  • Pointers
  • Basic type inference (auto keyword)
  • Basic introspection (typeof operator)
  • Memory allocation

The language is not yet quite finished. The main features that are currently missing are printing of strings, boolean operators and better error reporting.

For a complete grammar definition, click here.

Cloning

git clone https://github.com/elvnh/kelp.git --recursive

Compiling

make -j

Usage

To compile and interpret a file, simply run the program and provide the filename as a command line argument.

./build/kelp file.txt

Simple examples

For more examples of the language in use, see the examples directory.

// The entry point of the program.
func main() -> void // The return type of the function 'main'
{
    /* In variable declarations, the variable name is followed by a double colon, followed by
       the type name and optionally an initializer. */
    i :: i32 = 100; // Declare a signed 32-bit integer
    k :: u32 = 5;   // Declare an unsigned 32-bit integer

    print(i * k);
}
func fib(n :: i32) -> u64
{
    if (n <= 1) {
        return 1;
    }

    return fib(n - 1) + fib(n - 2);
}

func main()
{
    print(fib(20));
}
struct Foo {
    i :: i32;
    j :: i64;
}

func make_foo(i :: i32, j :: i64) -> Foo
{
    foo :: Foo;
    foo.i = i;
    foo.j = j;

    return foo;
}

func main() -> void
{
    foo :: auto = make_foo(500, 1000);
    print(foo.i * foo.j);
}

Debug flags

Several flags mainly used for debugging purposes are available. Provide one or more of them in addition to a filename. They are as follows:

  • -l/--lex - Tokenize the source file and print the tokens
  • -p/--parse - Parse the source file and pretty print the abstract syntax tree
  • -f/--fold - Log any constant folding performed
  • -d/--disassemble - Print the bytecode emitted by the compiler
  • -t/--trace - Print a trace of each bytecode instruction executed during interpretation, as well as the current state of the value stack in the virtual machine.
  • -r/--resolve - Log during identifier resolution stage
  • -i/--infer-types - Log during type inference stage

About

A statically typed, interpreted programming language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages