From ebbcb8774587347069b9aecaa517352ffbe7e30b Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Thu, 19 Nov 2009 09:57:41 -0600 Subject: [PATCH] Initial thoughts and groundwork for scoping, activation records, and functions. --- .../utexas/cs345/jdblisp/SpecialForms.java | 14 +++++ src/edu/utexas/cs345/jdblisp/SymbolTable.java | 52 +++++++++++++++++++ src/edu/utexas/cs345/jdblisp/TableEntry.java | 18 +++++++ 3 files changed, 84 insertions(+) create mode 100644 src/edu/utexas/cs345/jdblisp/SpecialForms.java create mode 100644 src/edu/utexas/cs345/jdblisp/SymbolTable.java create mode 100644 src/edu/utexas/cs345/jdblisp/TableEntry.java diff --git a/src/edu/utexas/cs345/jdblisp/SpecialForms.java b/src/edu/utexas/cs345/jdblisp/SpecialForms.java new file mode 100644 index 0000000..92dbf62 --- /dev/null +++ b/src/edu/utexas/cs345/jdblisp/SpecialForms.java @@ -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 +} diff --git a/src/edu/utexas/cs345/jdblisp/SymbolTable.java b/src/edu/utexas/cs345/jdblisp/SymbolTable.java new file mode 100644 index 0000000..f6ba111 --- /dev/null +++ b/src/edu/utexas/cs345/jdblisp/SymbolTable.java @@ -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); + } +} diff --git a/src/edu/utexas/cs345/jdblisp/TableEntry.java b/src/edu/utexas/cs345/jdblisp/TableEntry.java new file mode 100644 index 0000000..348a792 --- /dev/null +++ b/src/edu/utexas/cs345/jdblisp/TableEntry.java @@ -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; + } + +}