Kaleidoscope Language and its Lexer

1.1 The Basic Language

This tutorial will be illustrated with a toy language that we’ll call “Kaleidoscope” (derived from “meaning beautiful, form, and view”). Kaleidoscope is a procedural language that allows you to define functions, use conditionals, math, etc. Over the course of the tutorial, we’ll extend Kaleidoscope to support the if/then/else construct, a for loop, user defined operators, JIT compilation with a simple command line interface, etc.

Because we want to keep things simple, the only datatype in Kaleidoscope is a 64-bit floating point type (aka double in C parlance). As such, all values are implicitly double precision and the language doesn’t require type declarations. This gives the language a very nice and simple syntax. For example, the following simple example computes Fibonacci numbers:

  1. # Compute the x'th fibonacci number.
  2. def fib(x)
  3. if x < 3 then
  4. 1
  5. else
  6. fib(x-1)+fib(x-2)
  7. # This expression will compute the 40th number.
  8. fib(40)

We also allow Kaleidoscope to call into standard library functions (the LLVM JIT makes this completely trivial). This means that you can use the extern keyword to define a function before you use it (this is also useful for mutually recursive functions). For example:

  1. extern sin(arg);
  2. extern cos(arg);
  3. extern atan2(arg1 arg2);
  4. atan2(sin(.4), cos(42))

A more interesting example is included in Chapter 6 where we write a little Kaleidoscope application that displays a Mandelbrot Set at various levels of magnification.

Lets dive into the implementation of this language!

1.2 The Lexer

When it comes to implementing a language, the first thing needed is the ability to process a text file and recognize what it says. The traditional way to do this is to use a “lexer” (aka ‘scanner’) to break the input up into “tokens”. Each token returned by the lexer includes a token code and potentially some metadata (e.g. the numeric value of a number). First, we define the possibilities:

  1. // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
  2. // of these for known things.
  3. enum Token {
  4. tok_eof = -1,
  5. // commands
  6. tok_def = -2, tok_extern = -3,
  7. // primary
  8. tok_identifier = -4, tok_number = -5,
  9. };
  10. static std::string IdentifierStr; // Filled in if tok_identifier
  11. static double NumVal; // Filled in if tok_number

Each token returned by our lexer will either be one of the Token enum values or it will be an ‘unknown’ character like +, which is returned as its ASCII value. If the current token is an identifier, the IdentifierStr global variable holds the name of the identifier. If the current token is a numeric literal (like 1.0), NumVal holds its value. Note that we use global variables for simplicity, this is not the best choice for a real language implementation :).

The actual implementation of the lexer is a single function named gettok. The gettok function is called to return the next token from standard input. Its definition starts as:

  1. /// gettok - Return the next token from standard input.
  2. static int gettok() {
  3. static int LastChar = ' ';
  4. // Skip any whitespace.
  5. while (isspace(LastChar))
  6. LastChar = getchar();

gettok works by calling the C getchar() function to read characters one at a time from standard input. It eats them as it recognizes them and stores the last character read, but not processed, in LastChar. The first thing that it has to do is ignore whitespace between tokens. This is accomplished with the loop above.

The next thing gettok needs to do is recognize identifiers and specific keywords like def. Kaleidoscope does this with this simple loop:

  1. if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
  2. IdentifierStr = LastChar;
  3. while (isalnum((LastChar = getchar())))
  4. IdentifierStr += LastChar;
  5. if (IdentifierStr == "def") return tok_def;
  6. if (IdentifierStr == "extern") return tok_extern;
  7. return tok_identifier;
  8. }

Note that this code sets the IdentifierStr global whenever it lexes an identifier. Also, since language keywords are matched by the same loop, we handle them here inline. Numeric values are similar:

  1. if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
  2. std::string NumStr;
  3. do {
  4. NumStr += LastChar;
  5. LastChar = getchar();
  6. } while (isdigit(LastChar) || LastChar == '.');
  7. NumVal = strtod(NumStr.c_str(), 0);
  8. return tok_number;
  9. }

This is all pretty straight-forward code for processing input. When reading a numeric value from input, we use the C strtod function to convert it to a numeric value that we store in NumVal. Note that this isn’t doing sufficient error checking: it will incorrectly read “1.23.45.67” and handle it as if you typed in “1.23”. Feel free to extend it :). Next we handle comments:

  1. if (LastChar == '#') {
  2. // Comment until end of line.
  3. do LastChar = getchar();
  4. while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
  5. if (LastChar != EOF)
  6. return gettok();
  7. }

We handle comments by skipping to the end of the line and then return the next token. Finally, if the input doesn’t match one of the above cases, it is either an operator character like + or the end of the file. These are handled with this code:

  1. // Check for end of file. Don't eat the EOF.
  2. if (LastChar == EOF)
  3. return tok_eof;
  4. // Otherwise, just return the character as its ascii value.
  5. int ThisChar = LastChar;
  6. LastChar = getchar();
  7. return ThisChar;
  8. }

With this, we have the complete lexer for the basic Kaleidoscope language (the full code listing for the Lexer is available in the next chapter of the tutorial). Next we’ll build a simple parser that uses this to build an Abstract Syntax Tree. When we have that, we’ll include a driver so that you can use the lexer and parser together.