Fixed function calling mechanism.

This commit is contained in:
Jonathan Bernard 2009-11-20 13:59:40 -06:00
parent 1642e5a51a
commit 943c91dedc
7 changed files with 31 additions and 118 deletions

View File

@ -27,7 +27,7 @@ public class FunctionEntry implements FormEntry {
parameters.length, i);
// bind one arg to param
localScope.bind(parameters[i], new VariableEntry(arguments.car));
localScope.bind(parameters[i], new VariableEntry(arguments.car.eval(symbolTable)));
arguments = arguments.cdr;
++i;

View File

@ -68,8 +68,11 @@ public class Lisp {
continue;
}
// DEBUG: print abstract syntax TODO: remove
out.println(sexp.display(" "));
out.flush();
try {
out.println(sexp.display(" "));
out.println(sexp.eval(globalSymbolTable));
} catch (LispException le) {
out.println(le.getLocalizedMessage());

View File

@ -32,10 +32,9 @@ TOKEN : /* PUNCTUATION */
TOKEN : /* LITERALS & SYMBOLS */
{ < NUMB: (["+", "-"])? (["0"-"9"])+ ("." (["0"-"9"])+ )? >
| < STRG: "\"" (["A"-"Z", "a"-"z", "_"])* "\"" >
| < SYMB: ([ "A"-"Z",
"a"-"z",
"_",
"+", "-", "*", "/", "="])+ >
| < SYMB: (["A"-"Z", "a"-"z", "_", "+", "-", "*", "/", "="])+
(["A"-"Z", "a"-"z", "0"-"9",
"_", "+", "-", "*", "/", "="])? >
}
/**

View File

@ -112,6 +112,7 @@ public abstract class SpecialFormEntry implements FormEntry {
} catch (ClassCastException cce) {
throw new TypeException(arguments.car, Num.class);
}
arguments = arguments.cdr;
}
return sum;
@ -150,6 +151,7 @@ public abstract class SpecialFormEntry implements FormEntry {
} catch (ClassCastException cce) {
throw new TypeException(arguments.car, Num.class);
}
arguments = arguments.cdr;
}
return difference;
@ -170,6 +172,7 @@ public abstract class SpecialFormEntry implements FormEntry {
} catch (ClassCastException cce) {
throw new TypeException(arguments.car, Num.class);
}
arguments = arguments.cdr;
}
return product;
@ -208,6 +211,7 @@ public abstract class SpecialFormEntry implements FormEntry {
} catch (ClassCastException cce) {
throw new TypeException(arguments.car, Num.class);
}
arguments = arguments.cdr;
}
return dividend;

View File

@ -1,101 +0,0 @@
package edu.utexas.cs345.jdblisp;
import java.util.ArrayList;
/**
* SpecialFormEntry
* @author Jonathan Bernard (jdbernard@gmail.com)
*/
public abstract class SpecialFormEntry extends FunctionEntry {
protected Lisp environment;
public SpecialFormEntry(Lisp environment) { this.environment = environment; }
public abstract SExp call(SymbolTable symbolTable, Seq arguments) throws LispException;
public static definSpecialForms(Lisp environment) {
SpecialFormEntry DEFUN = new SpecialFormEntry(environment) {
public SExp call(SymbolTable symbolTable, Seq arguments) throws LispException {
Symbol functionName;
ArrayList<Symbol> parameters;
SExp body;
// TODO: check to see if a function for this symbol exists
// and warn if so
if (arguments.length() != 3)
new InvalidArgumentQuantityException(3, arguments.length());
// first argument: Symbol for function name
if (!(arguments.car instanceof Symbol))
// TODO: error: has to be a symbol
functionName = (Symbol) arguments.car;
// second argument, parameter list
arguments = arguments.cdr;
assert (arguments != null);
if (!(arguments.car instanceof List))
// TODO: error, need parameter list
// read parameters
parameters = new ArrayList<Symbol>();
Seq paramSeq = ((List) arguments.car).seq;
while (seq != null) {
if (!(seq.car instanceof Symbol))
// TODO: error, expected symbol
parameters.add((Symbol) seq.car);
seq = seq.cdr;
}
// third argument: function body
arguments = arguments.cdr;
assert (arguments != null);
// TODO: necessary? if (!(arguments.car instanceof List))
body = arguments.car;
environment.globalSymbolTable.define(functionName,
new FunctionEntry(parameters.toArray(new Symbol[]{}), body));
return functionName;
}
};
SpecialFormEntry SETQ = new SpecialFormEntry(environment) throws LispException {
public SExp call(SymbolTable symbolTable, Seq arguments) {
Symbol variableName;
SExp variableValue;
// TODO: check for redifinition of variable (and warn)
if (arguments.length() != 2)
throw new InvalidArgumentQuantityException(2,
arguments.length());
// first argument: Symbol for variable name
if (!(arguments.car instanceof Symbol))
// TODO: error: expected symbol
variableName = (Symbol) arguments.car;
// second argument: variable value
arguments = arguments.cdr;
assert (arguments != null);
variableValue = arguments.car;
}
};
environment.globalSymbolTable.defin(new Symbol("DEFUN"), DEFUN);
environment.globalSymbolTable.defin(new Symbol("SETQ"), SETQ);
}
}

View File

@ -11,9 +11,9 @@ public class Symbol implements SExp {
}
/** {@inheritdoc}*/
public SExp eval(SymbolTable table) {
// TODO
return null;
public SExp eval(SymbolTable table) throws LispException {
VariableEntry ve = table.lookupVariable(this);
return ve.eval(table);
}
public String display(String offset) {

View File

@ -64,7 +64,7 @@ static private int jjMoveStringLiteralDfa1_0(long active0)
static private int jjMoveNfa_0(int startState, int curPos)
{
int startsAt = 0;
jjnewStateCnt = 8;
jjnewStateCnt = 9;
int i = 1;
jjstateSet[0] = startState;
int kind = 0x7fffffff;
@ -90,7 +90,7 @@ static private int jjMoveNfa_0(int startState, int curPos)
{
if (kind > 10)
kind = 10;
jjCheckNAdd(7);
jjCheckNAddTwoStates(7, 8);
}
else if (curChar == 34)
jjAddStates(0, 1);
@ -128,7 +128,11 @@ static private int jjMoveNfa_0(int startState, int curPos)
break;
if (kind > 10)
kind = 10;
jjCheckNAdd(7);
jjCheckNAddTwoStates(7, 8);
break;
case 8:
if ((0x23ffac0000000000L & l) != 0L && kind > 10)
kind = 10;
break;
default : break;
}
@ -147,12 +151,16 @@ static private int jjMoveNfa_0(int startState, int curPos)
break;
if (kind > 10)
kind = 10;
jjCheckNAdd(7);
jjCheckNAddTwoStates(7, 8);
break;
case 5:
if ((0x7fffffe87fffffeL & l) != 0L)
jjAddStates(0, 1);
break;
case 8:
if ((0x7fffffe87fffffeL & l) != 0L && kind > 10)
kind = 10;
break;
default : break;
}
} while(i != startsAt);
@ -176,7 +184,7 @@ static private int jjMoveNfa_0(int startState, int curPos)
kind = 0x7fffffff;
}
++curPos;
if ((i = jjnewStateCnt) == (startsAt = 8 - (jjnewStateCnt = startsAt)))
if ((i = jjnewStateCnt) == (startsAt = 9 - (jjnewStateCnt = startsAt)))
return curPos;
try { curChar = input_stream.readChar(); }
catch(java.io.IOException e) { return curPos; }
@ -201,8 +209,8 @@ static final long[] jjtoSkip = {
0x3eL,
};
static protected SimpleCharStream input_stream;
static private final int[] jjrounds = new int[8];
static private final int[] jjstateSet = new int[16];
static private final int[] jjrounds = new int[9];
static private final int[] jjstateSet = new int[18];
static protected char curChar;
/** Constructor. */
public ParserTokenManager(SimpleCharStream stream){
@ -229,7 +237,7 @@ static private void ReInitRounds()
{
int i;
jjround = 0x80000001;
for (i = 8; i-- > 0;)
for (i = 9; i-- > 0;)
jjrounds[i] = 0x80000000;
}