Synopsis The runtime representation of Lisp programs and data.
Description There are several aspects of the runtime representation of Lisp programs and Lisp data that have to be described:
  • The representation of values (see Lval below).
  • The handling of the scope of variables (see Scope, Env, makeEnv and find below).
  • The way the interpreter returns its results (see Result below).
module demo::lang::Lisra::Runtime

import Prelude;

public data Lval       
     = Integer(int n)   
     | Atom(str name)
     | List(list[Lval] elms)
     | Closure(Result(list[Lval] args, Env env))
public alias Scope  = map[Lval,Lval]; 
public alias Env    = list[Scope];

public Env emptyEnv = [()];

public Env makeEnv(list[Lval] vars, list[Lval] values, Env outer) = 
   [(vars[i] : values[i] | i <- index(vars))] + outer;

public int find(Lval sym, Env e){ 
   for(n <- index(e))
          return n;
   return -1;

public Lval TRUE  = Atom("#t");   
public Lval FALSE = Atom("#f");

public alias Result = tuple[Lval val, Env env]; 

The data type Lval () takes care of the representation of Lisp values. It covers integers, atoms, lists and closures (the representation of a functions and the context in which it will be executed).

A Scope () describes the binding of several related variables to their value. Since scopes may be nested, an environment (Env) consisted of a list of scope. The most inner scope is at the start of the list and the most global one at the end.

Creating a new scope is done by makeEnv () which takes a list of variables (represented by Lvals, in most cases this will be an atom like Atom("X")), a list of values and creates a new scope in front of the current environment.

The function find () tries to locate the scope in which a name was previously defined. It searches the nested scopes inside-out and returns the index in the given environment of the scope in which the name is defined, or -1 if it is not found.

We define () useful constants for true and false (the atoms #t and #f, respectively).

Finally, we define Result () as a tuple of an Lval and an Env. Each step during interpretation will thus return the value it computed and a possibly modified environment.
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit