Kite Language Guide

Your First Kite Application

Kite is very easy to use. Here's the customary "Hello World" application written in Kite:

#!/usr/local/bin/kite

"Hello, World!"|print;

At the shell, this produces the following:

$ kite hello.kt
Hello, World!
$

Your First Application, in detail

We were able to output the string "Hello, World!" above in one single line. This single line does a lot behind the scenes. Ultimately, it does three things:

  • Creates an object containing the string we wanted to print.
  • Dereferences a method* inside said object.
  • Runs the code the method points to.

(* Kite doesn't actually have "methods" per-se, since every function is anonymous, but that's just splitting hairs.)

First, we need to explain how objects are created. Kite supports seven different built-in objects: strings, numbers, code objects, lists, Boolean values, files and exceptions. The first five are specified using language syntax:

  • Strings: anything between double quotes (i.e. "hello"). Standard Unix escape sequences are supported--\n for newline and \" for a literal ".
  • Numbers: Digits from 0 to 9, with up to a single period (i.e. 42 and 3.99)
  • Boolean values: true and false
  • Code objects: anything in braces
  • Lists: anything between a set of square brackets

The last two in the above list are special and will be discussed in more detail later.

Next, we dereference the object and find a method. Identifiers in Kite allow the system to find variables; they must start with a letter or '_' and cannot contain whitespace or the following characters: ';', '(', '|', '.', ')', '['. In the above snippet, "print" is the name of a method inside the string object (for more information on the available library methods, visit LibraryGuide).

Now that we have an object and the name of the method that we want to call, we want to actually call it. This is where the "|" operator comes in. "|" dereferences the object on the left hand side, finds a code object stored inside the object as the identifier and executes it. This is akin to Unix's pipe mechanism. Note that this is different from '.', since '.' only returns the object and doesn't attempt to execute it. The way to remember this is that you're "sending" the string to the print method to process.

Finally, we end the statement with a semicolon. As in C-like languages, each statement ends in a semicolon.

Code objects and lists

Two special objects in Kite are code objects and lists. Code objects contain Kite code and are surrounded by curly brackets:

{
    x = 3;
}

This allows classes and functions/methods to work, as all you need to do is use the pipe symbol as in above to execute the method (or the variable name with parenthesis at the end if you don't have an object).

Lists act like in any other language. The difference is that Kite lists support both numerical and dictionary indexing. If you want to access an array element by using a particular string, use "string": or (expression): at the beginning of an array element:

["a":1, 2, "b":3]

Array dereferencing is a matter of simply using [expression] at the end of a variable or expression that returns an array to retrieve a value.

Variable/class declaration and assignment

Kite is a dynamically scoped language, which means that functions and methods can access variables that were defined lower in the stack. That said, there are two ways to assign a value to a variable:

  1. Simple assignment (as in most languages):
x = 3;

Using simple assignment, Kite will assign the value of x to the earliest defined version in the runtime stack. If the variable hasn't been defined yet, it'll be assigned in the current scope (same as method 2 below).

  1. Using the var statement:
var x = 3;

var will always assign the value to the current scope, ignoring any previous definitions of the variable.

In either case, any object can be assigned to a variable, including code objects. Classes are a special case, however. They can take either of two forms:

class x = {...};
class y = x + {...};

The first form is for a base class. On the right hand side is the code object that defines the methods and static properties that the class will have. These are usually a series of var statements, to ensure that Kite does not accidentally modify any other defined variables in the current scope. The second form is for inheritance; the left hand side of the addition is a previously defined class, while the right hand side is as in the first form.

Note that inside classes, the identifier "this" refers to the current instance of the class. Example:

class x = {
    var p = {
        "x"|print;
    };
};

class y = x + {
    var q = {
        "y"|print;
        this|p;
    };
};

y|q;

returns:

y
x

Operators and method invocation

Kite supports all of the standard operators and also has standard operator precedence for them:

  • < (less than)
  • > (greater than)
  • <= (less than or equals)
  • >= (greater than or equals)
  • == (equals)
  • != (not equals)
  • +, -, *, /, mod, >> (right shift), << (left shift)
  • and, or, not (Boolean for boolean variables, bitwise for numbers)

Also, Kite supports operator overloading. To implement an operator inside of a class, use var op(operator), replacing (operator) with one of the operators above.

To create a new instance of a class, use the new method:

class x = {
    var new = {
        this.p = args;
    }; 
};

z = x|new(42);
z.p|print;

(inside methods, args refers to the arguments passed to the mehtod--a list if it's more than one, or a single object if just one parameter.)

Methods return the value returned by the last statement executed. The result of:

var x = {
    2;
    3;
};

x()|print;

would be 3, and not 2.

Exception handling

Kite has built-in support for exceptions (called trees). To use, simply instantiate a new tree object:

tree|new("there was an error");

Kite will immediately recognize that an exception was thrown and output a message. To catch a tree, simply use the catch method of the code object:

var x = {
    tree|new("x");
};

x|catch({
    exception|print;
});

Loading other files

You can load Kite source code from other files by using the load statement. For instance, in one file you can put:

class funcs = {
    var boo = {
        42|print;
    };
};

and then include it in your main file:

load "funcs.kt";

funcs|boo;

As soon as you use load, everything defined inside it becomes available.

That's it!

This is all you need to start developing in Kite. Visit LibraryGuide for information on the standard library (most language constructs you're used to are implemented as methods in Kite).