Language guide

Learn how to play with rookie

C++ Integration

Hello World!

Begins with Hello World as always.

Hello World
class my_first_program {
    @main
    static def main() {
        puts("Hello World!");
    }
} 
  • Class based (OOP)
  • Speficifed entry point
    • Unlike other dynamic languages, rookie has a specific entry point where the program begins.
    • Every rookie program must contain main method and also be static.

Class

Class is a minimal unit of building blocks of your program.

Definition Syntax

Defining a new class is very simple in rookie.

Class definition
class CLASS_NAME {
    FIELD = 1;
    
    def METHOD_NAME(PARAMS) {
        # METHOD_BODY
    }
}

The syntax for instantiating class is new(), inspired by Ruby.

a = CLASS_NAME.new();

Field

Field
class character {
    MaxLength = 16;

    def set_name(name) {
        if (name.length() >= MaxLength)
          puts ("ERROR!");
    }
}

Property

Property is a instance variable and must be started with @.

Property
class person {
    # `age` and `@age` have different meanings!
    def set_age(age) {
        @age = age;
    }
    def print_age() {
        print(@age);
    }
}
a = person.new();
a.set_age(51);
a.print_age(); # 51

Properties are also can be accessed outside of the class. In this time, you should not use @ before its name.

a.age = 22;
a.print_age(); # 22

Inheritance

Inheritance
class person {
    def say_hello() {
        puts("Hello");
    }
}
class engineer : person {
    def say_hello() {
        puts("HelloWorld");
    }
}
# Hello
person.new().say_hello();

# HelloWorld
engineer.new().say_hello();

Methods

Static method

Static methods are a sort of global method. They can be accessed without instantiating an object.

class dog {
    static def kind() {
        puts("animal");
    }
}
class anchovy {
    static def kind() {
        puts("fish");
    }
}
# animal
dog.kind();

# fish
anchovy.kind();

Instance method

class greeter {
    def say_hello() {
        puts("Hello World!");
    }
}

class my_first_program {
    @main
    static def main() {
        greeter.new().say_hello();        
    }
}

Control Flow

if-else

Executes given codeblock only if that conditions is evaluated as true.

if
a = 10;

if (a > 5) {
    puts("a is greater than 5");
}
if - else
a = random();

if (a > 5) {
    puts("a is greater than 5");
} else {
    puts("a is less than or equal to 5");
}

for

for loop
for (i = 0; i < 10; i++) 
    puts(i);

while

The while loop is exactly same as other’s.

while loop
while (true) {
    puts("HElloo");
}

Array

ary = [1, 2, "hello", "world"];

puts (ary);

Accessing to non-existing index is illegal in rookie.

ary = [1, 2, 3];

# Throws an exception
puts (ary[999]);

Concat
Arrays can be concatenated using + operator.

ary = [1, 2, 3] + [4, 5, 6];

# Should be 
# [1, 2, 3, 4, 5 ,6]
puts (ary);

Dictionary

Dictionary is a key-value storage.

person = {
    name: "Jinwoo",
    age : 24
};

puts (person.to_string());

Nested initilization

You can put dictionaries and arrays inside the dictionary.

person = {
    name  : "Jinwoo", 
    skills: ["C", "Ruby", "HTML"],
    inventory: {
        hamberger: 10,
        sword    : 1
    }
};

Indexer

Indexer
class foo {
    def __getitem__ (key) {
        /* ... */
    }
    def __setitem__ (key, value) {
        /* ... */
    }
}
f = foo.new();

f["a"] = 1234;
print(f["a"]);

Type

Rookie has a runtype typedata and you can access to it.

Compare type using `is`
def foo(a) {
    if (a is integer) 
       puts ("a is a integer!");
    else if (a is string)
       puts ("a is a string!");
    else
       puts ("IDK");
}

Exceptions

Throw

Unlike other languages, only exception objects can be thrown. This clarify users what to catch and handle it.

Throwing an exception
throw exception.new("error");

Try~Catch

Catching an exception
try {
    throw exception.new("error"); 
}
catch (e) {
    puts (e);
}

String Interpolation

String literals enclosed with back-ticks(`) may include embedded expressions.

String interpolation
name = "jinwoo";
puts (`Hello, my name is {{name}}.`);

Partially implemented Currently only `ident` expression can be accepted in interploation.