Kelpie Object Oriented Language Description

Intro

What is it? You might ask. It's object oriented framework for creating rouge-like games. It has been created mainly to test some ideas about writing compilers, virtual machines and text-mode GUI.

Language has large influence of C++. While building it on base of Prolog, Lisp or SmallTalk would be probably smarter, I'm not expert in any of these languages, opposite to C++.

Concepts

Sys class

Main object used in programming is class. There is no global range (like in Java), no global functions. However to make things (and programmer's live ;) simpler a special class has been introduces. It's called Sys. Note the first uppercase letter. Kelpie is case sensitive, so it matters. Also note that it's a convention to write class' names with first capital letter, like this SomeClassName. Conventions comes from C++.

Methods and members of class Sys are similar to global objects in C. When name is resolved by compiler inside of function, first of all local variables are checked, then formal arguments, then members of class inside which function is defined, and then members of Sys class (but only if it's definition was seen by compiler before!).

Sys class has also one more special meaning. Its constructor Sys::Sys() is considered entry point for program in kelpie. After executing it, Sys::die() (destructor of Sys, destructor are always named die. This is different from C++/Java), is executed, and if even it returns, program is abnormally halted. The proper way to finish program is to call exit() syscall.

Syscalls

Language would have been useless if it lacked syscalls. Syscalls are way of accessing kernel. Program don't know what to do to print "hello world" on the screen. And it doesn't have to know what to exactly, it's enough to tell the kernel to do it. But there has to some way of informing kernel what has to be done. So the syscalls were invented.

Concept of Unix syscalls was borrowed by kelpie. Of course the nature of syscalls in game description language and the Unix kernel/C are "little bit" different...

Anyway like in Unix syscalls are numbered, and the compiler doesn't know their numbers. It has to be told them. It also doesn't know how to pass the arguments. There is simple way of doing it in kelpie.

First program

After this bit of theory we can at last write hello.k program. (the suffix .k is convention for calling kelpie sources, but it has no meaning for compiler).

<16:42:doc>% cat hello.k
class Sys {
proc:
        void Sys();
        void exit(int) @ 0;
        void print(string) @ 1;
};

void Sys::Sys()
{
        print("Hello world\n");
        exit(0);
}
<16:42:doc>% kelpie-cc hello.k
<16:42:doc>% kelpie-ld hello hello.ok
<16:42:doc>% kelpie-vm hello
Hello world
<16:43:doc>%

As you see it's shell session transcription. For people not familiar with Unix shell - cat command displays specified file. It was used here to show how does the hello.k file looks like. kelpie-cc is name of kelpie compiler. It compiles specified .k file to .ok object. kelpie-ld is name of kelpie linker. It merges several .ok files into one file suitable for use with kelpie-vm virtual machine. `<16:42:doc>%' is only shell prompt, and you shouldn't care about it, you can see what lines was entered by me and which was written by computer.

What's interesting about hello.k program? First line: classes are defined by keyword class followed by class name, it's definition in '{' '}' and a ';'. After class name there can be also a ':' and name of base class. It will be explained later.

In definition of class there can be variables and methods declarations. Their must be divided by keyword/label proc:. There can be only one proc: per class definition. Also first come variables and then functions. C++ style definition of functions inside of class (inline) are not allowed.

There are no data members of Sys class defined. There is nothing wrong about it. There are three functions declared in Sys class. First is constructor: void Sys();. We can see here important points of function declaration: void donates return type of function. Types will be explained later, but void is pseudo-type to point that Sys() function doesn't return any value. Sys is name of function. Name (of function, variable and anything else) can contain lower- and uppercase letters, digits and `_' sign. It cannot start with a digit, so _foo12 and bAr are proper names, while 32bit or baz%abc are not. Empty pair of round parentheses () means that Sys function doesn't take any arguments. Note that even empty () cannot be omitted neither in function declaration/definition nor call. Then comes the ';' meaning end of declaration.

Sys() is special function in class Sys (and in general Foo is special in class Foo). It's called constructor. Unlike in C++ it can't take any arguments, and it has to be clearly marked as returning void. Class must have constructor. It's constructor duty to set up object for future use, it should assign initial values to variables of class. Unlike in C++ all variables of class are automagicly set up to contain 0, but constructor cannot have initialization list.

Of course Sys::Sys() constructor beside initializing Sys object has to do other things. As stated before it's main entry point for program in kelpie, so it's equivalent of C main() function.

There are two declarations of syscalls after declaration of constructor. Declaration of syscall is similar to declaration of normal function. It differs in two important points:

  1. declaration of syscall is followed by '@' sign and an integer number telling the compiler which syscall is it.
  2. syscalls are handled internally by virtual machine and cannot be defined by user.

Note that you can give syscalls other names. But it's not recommended to do it. It's best to include standard list of syscalls from a file. It will be explained later how to do this.

Also note that you can give syscalls different arguments than they actually has. But it can cause serious problems, so better don't do it.

In our simple (classical ;) example Sys::Sys() (double colon ('::') is range operator, so Sys::Sys() means Sys() function of class Sys) constructor prints string "Hello world" finished with new line. New line can be included in strings by simply writing "\n". There are also other escape sequences, but you don't need to know them all at the moment.

So now, we'll point parts of function definition. First comes the header which is very similar to declaration: void Sys::Sys(). It don't have ';'. Then in '{' '}' comes block of instruction to execute when function is called. Here we first call syscall print: print("Hello world\n");. This is regular function call in kelpie. Function name, followed by '(', list of arguments (there can be zero args: (), only one, like in this example, or more separated by commas (',')), followed by ')' and ';' donating end of statement. Note that return value of print function is ignored. It possibly have connection with declaration of print() returning void ;)

Then we have call to exit function. It's similar to print function, except taking integer argument. exit function terminates the program returning its argument to environment.

Diffrences between kelpie and C++

First of all: kelpie is much simpler language then C++. Most of things allowed in C++ are forbidden here ;)

Ctor/Dtor:

Lang:

Types:

Functions: