Klang: Program Structure

I thought I’d keep my posts about Klang, the language I’m designing, shorter (at least I’ll try). So in this post I’ll describe the overall program structure and just a few constructs, focusing more on current status rather than goals.

Version 1 of the Klang compiler is going to be pretty basic. I’m not even sure if it’ll have separate compilation (well, it kind of does have that via externs). As I started moving some stuff from the compiler into a library, I just made it prepend the library source code to the source being compiled. So at least right now, the compiler takes just one source file (or string) and turns it into a single LLVM assembly module (.ll file), which can in turn be compiled to machine code using the LLVM toolchain.

Functions

A Klang module is currently made of function definitions and function declarations (externs). Classes will come later. A function named main is the entry point, as usual. A Hello World program in Klang might look like this:

// puts is defined in the C library
extern puts(string: Byte *): Int
def main() = puts("Hello, World!")

Byte * is a pointer to a Byte, but I added pointer types only as a quick hack to interface with native libraries. I might keep them for that reason, but limit their use to extern declarations — extern declares a function defined in another module.
You probably noticed that functions are defined similarly to Scala.

'def', name, argument list, '=', body expression

Expressions

In Klang, like in Scala, most things are expressions i.e. evaluate to a value. Here’s what the mandatory naive Fibonacci example looks like:

def fib(n: Int): Int =
  if n == 0 then
    0
  else if n == 1 then
    1
  else
    fib(n - 2) + fib(n - 1)

Specifying the return type is necessary here because the function is recursive, and the type inferencer can’t handle that. if is an expression, so both the then and else branches must be of the same type. An if without an else would evaluate to (), the empty tuple value, also known as Unit. It is similar to void, except void is the lack of a value.

def addOneThenSquare(n: Int) = {
  val nPlus1 := n + 1;
  nPlus1 * nPlus1
}

A block is a list of statements surrounded by { and }. Statements are usually separated by semicolons because the current parser is a quick hack implemented with parser combinators and can’t do the semicolon inference well. The statements can be definitions as well as expressions, but the last statement is what is returned and needs to be an expression.

val starts a local value definition. Local values cannot be reassigned after they are defined. I want to avoid mutable local variables for as long as I can, though in the end I will probably have to add them. := is perhaps an unusual choice for assignment and I might revert to using = for that instead.

Loops

I haven’t even implemented Arrays yet, but loops are still useful, especially as there are no tail calls yet and the Fibonacci function above would probably exit with a segmentation fault for a large value when the stack grows too big. But how do you do loops without mutable variables? I’m talking about primitive loops that must be the basis for other constructs such as comprehensions. I thought about it for a couple of days and came up with something like this (there are probably similar constructs in other languages):

def helloHello(n: Int) =
  // loop takes a tuple of parameters similar to a function argument list
  // but it must always specify default (starting) values
  loop(i := 0)
    // and also an expression that ends with continue(...) or break()
    if (i < n) then {
      puts("Hello?");
      // continue "calls" the loop again with the new values
      continue(i + 1)
    } else {
      // break returns from the loop
      break()
    }
  // yield can see the *last* values of the loop parameters
  yield i

def main() = helloHello(3)

This program will print “Hello?” three times and return 3. But this kind of loop seems very verbose and actually what it does is similar to tail recursion, using LLVM’s Phi nodes. I might rethink what kind of loops to have in the future, but for now I’ll stick with this. It can be simplified a bit:

def helloHello(n: Int) =
  loop(i := 0) while (i < n) {
    puts("Hello?");
    i + 1
  } yield i

There are some potential programming errors that can easily arise from this kind of loop, but I’ll talk about that later (or maybe you can guess).

while can only come directly after loop(...) and is defined in terms of if, break and continue:

while (condition) expression = if (condition) continue(expression) else break()

This translation is done immediately after parsing, to simplify the compiler phases.

With these constructs, and passing functions around, one could already write a program that does something a little bit useful. I think my next focus is on adding Strings and Arrays into the mix, but my next post will probably go more into functions and tuples.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s