/dynamik

A dynamically typed language, written in Kotlin.

Primary LanguageKotlin

dynamik

Dynamik is a toy, dynamically-typed language, written in Kotlin.

Features

  • Expressions with Strings and Integers. (At present, the result of an operation with Integers is implicitly converted to a Double.)
  • (Im)mutable variable bindings with val/var
  • If/Else statements
  • For/While loops
  • Functions
  • Comments
  • Memoization builtin
  • Collections (list, map)
  • Classes
  • Builtin timer
  • Repl

Install

git clone https://github.com/NavyaZaveri/dynamik
./gradlew build 

To run code in file

java -jar build/libs/dynamik-1.0-SNAPSHOT-all.jar --file=<filename>

To start repl

java -jar build/libs/dynamik-1.0-SNAPSHOT-all.jar --repl

Examples

Fibonacci

//the @memo annotation caches the output of fib against its input and uses it 
//when needed
@memo  
fn fib(n) {
    if (n<2) { return n;}
    return fib(n-1) + fib(n-2);
}

val res = fib(100);
print(res);

Variable Bindings


//create a "final" variable 
val hello = "world";

// can't do this (throws ValError) 
// hello = "world";


var foo = 1;
foo = 2; //works 

Classes

class Animal(name) {
    fn change_name(new_name) {
        this.name = new_name;
    }
}

val dog = Animal("foo");
dog.change_name("bar");
print(dog.name);

Collections

//initialzie a list with one value. 
val my_list  = list(0); 
for (var i=1;i<=10;i = i+1) {
    my_list.add(i);
}

val my_map = map();
my_map.insert("hello", my_list.get(1));
assert(my_map.get("hello") == 1);