Initial thoughts and groundwork for scoping, activation records, and functions.

This commit is contained in:
Jonathan Bernard 2009-11-19 09:57:41 -06:00
parent 9a165a2131
commit ebbcb87745
3 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package edu.utexas.cs345.jdblisp;
/**
* SpecialForms
* @author Jonathan Bernard (jdbernard@gmail.com)
*/
public class SpecialForms {
public static SymbolTable createTable() {
}
public static TableEntry
}

View File

@ -0,0 +1,52 @@
package edu.utexas.cs345.jdblisp;
/**
* SymbolTable
* @author Jonathan Bernard (jdbernard@gmail.com)
*/
public class SymbolTable {
public final TableEntry top;
public final SymbolTable rest;
public SymbolTable(TableEntry te, SymbolTable st) {
this.top = te;
this.rest = st;
}
public SymbolTable(TableEntry... te) {
int idx = te.length;
SymbolTable last = null;
while (idx > 1) last = new SymbolTable(te[--idx], last);
assert (--idx == 0) : "Error in SymbolTable construction. "
+ "Invalid ending index";
this.top = te[0];
this.rest = last;
}
public TableEntry findFunction(Symbol symbol) throws LispException {
if (top.name.equals(symbol) && top.parameters != null)
return top;
if (rest == null)
throw new LispException("Undefined function: "
+ symbol.name);
return rest.findFunction(symbol);
}
public TableEntry findVariable(Symbol symbol) throws LispException {
// if this entry is the one we're looking for, return it
if (top.name.equals(symbol) && top.parameters == null)
return top;
// if not, and there are no more entries
if (rest == null)
throw new LispException("Undefined variable: "
+ symbol.name);
// otherwise, search the remainder of the table
return rest.findVariable(symbol);
}
}

View File

@ -0,0 +1,18 @@
package edu.utexas.cs345.jdblisp;
/**
* @author Jonathan Bernard (jdbernard@gmail.com)
*/
public class TableEntry {
public final Symbol name;
public final Symbol[] parameters;
public final SExp body;
public TableEntry(Symbol name, Symbol[] parameters, SExp body) {
this.name = name;
this.parameters = parameters;
this.body = body;
}
}