Fixed function calling mechanism.
This commit is contained in:
parent
1642e5a51a
commit
943c91dedc
@ -27,7 +27,7 @@ public class FunctionEntry implements FormEntry {
|
|||||||
parameters.length, i);
|
parameters.length, i);
|
||||||
|
|
||||||
// bind one arg to param
|
// 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;
|
arguments = arguments.cdr;
|
||||||
++i;
|
++i;
|
||||||
|
@ -68,8 +68,11 @@ public class Lisp {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DEBUG: print abstract syntax TODO: remove
|
||||||
|
out.println(sexp.display(" "));
|
||||||
|
out.flush();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
out.println(sexp.display(" "));
|
|
||||||
out.println(sexp.eval(globalSymbolTable));
|
out.println(sexp.eval(globalSymbolTable));
|
||||||
} catch (LispException le) {
|
} catch (LispException le) {
|
||||||
out.println(le.getLocalizedMessage());
|
out.println(le.getLocalizedMessage());
|
||||||
|
@ -32,10 +32,9 @@ TOKEN : /* PUNCTUATION */
|
|||||||
TOKEN : /* LITERALS & SYMBOLS */
|
TOKEN : /* LITERALS & SYMBOLS */
|
||||||
{ < NUMB: (["+", "-"])? (["0"-"9"])+ ("." (["0"-"9"])+ )? >
|
{ < NUMB: (["+", "-"])? (["0"-"9"])+ ("." (["0"-"9"])+ )? >
|
||||||
| < STRG: "\"" (["A"-"Z", "a"-"z", "_"])* "\"" >
|
| < STRG: "\"" (["A"-"Z", "a"-"z", "_"])* "\"" >
|
||||||
| < SYMB: ([ "A"-"Z",
|
| < SYMB: (["A"-"Z", "a"-"z", "_", "+", "-", "*", "/", "="])+
|
||||||
"a"-"z",
|
(["A"-"Z", "a"-"z", "0"-"9",
|
||||||
"_",
|
"_", "+", "-", "*", "/", "="])? >
|
||||||
"+", "-", "*", "/", "="])+ >
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -112,6 +112,7 @@ public abstract class SpecialFormEntry implements FormEntry {
|
|||||||
} catch (ClassCastException cce) {
|
} catch (ClassCastException cce) {
|
||||||
throw new TypeException(arguments.car, Num.class);
|
throw new TypeException(arguments.car, Num.class);
|
||||||
}
|
}
|
||||||
|
arguments = arguments.cdr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sum;
|
return sum;
|
||||||
@ -150,6 +151,7 @@ public abstract class SpecialFormEntry implements FormEntry {
|
|||||||
} catch (ClassCastException cce) {
|
} catch (ClassCastException cce) {
|
||||||
throw new TypeException(arguments.car, Num.class);
|
throw new TypeException(arguments.car, Num.class);
|
||||||
}
|
}
|
||||||
|
arguments = arguments.cdr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return difference;
|
return difference;
|
||||||
@ -170,6 +172,7 @@ public abstract class SpecialFormEntry implements FormEntry {
|
|||||||
} catch (ClassCastException cce) {
|
} catch (ClassCastException cce) {
|
||||||
throw new TypeException(arguments.car, Num.class);
|
throw new TypeException(arguments.car, Num.class);
|
||||||
}
|
}
|
||||||
|
arguments = arguments.cdr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return product;
|
return product;
|
||||||
@ -208,6 +211,7 @@ public abstract class SpecialFormEntry implements FormEntry {
|
|||||||
} catch (ClassCastException cce) {
|
} catch (ClassCastException cce) {
|
||||||
throw new TypeException(arguments.car, Num.class);
|
throw new TypeException(arguments.car, Num.class);
|
||||||
}
|
}
|
||||||
|
arguments = arguments.cdr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return dividend;
|
return dividend;
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,9 +11,9 @@ public class Symbol implements SExp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritdoc}*/
|
/** {@inheritdoc}*/
|
||||||
public SExp eval(SymbolTable table) {
|
public SExp eval(SymbolTable table) throws LispException {
|
||||||
// TODO
|
VariableEntry ve = table.lookupVariable(this);
|
||||||
return null;
|
return ve.eval(table);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String display(String offset) {
|
public String display(String offset) {
|
||||||
|
@ -64,7 +64,7 @@ static private int jjMoveStringLiteralDfa1_0(long active0)
|
|||||||
static private int jjMoveNfa_0(int startState, int curPos)
|
static private int jjMoveNfa_0(int startState, int curPos)
|
||||||
{
|
{
|
||||||
int startsAt = 0;
|
int startsAt = 0;
|
||||||
jjnewStateCnt = 8;
|
jjnewStateCnt = 9;
|
||||||
int i = 1;
|
int i = 1;
|
||||||
jjstateSet[0] = startState;
|
jjstateSet[0] = startState;
|
||||||
int kind = 0x7fffffff;
|
int kind = 0x7fffffff;
|
||||||
@ -90,7 +90,7 @@ static private int jjMoveNfa_0(int startState, int curPos)
|
|||||||
{
|
{
|
||||||
if (kind > 10)
|
if (kind > 10)
|
||||||
kind = 10;
|
kind = 10;
|
||||||
jjCheckNAdd(7);
|
jjCheckNAddTwoStates(7, 8);
|
||||||
}
|
}
|
||||||
else if (curChar == 34)
|
else if (curChar == 34)
|
||||||
jjAddStates(0, 1);
|
jjAddStates(0, 1);
|
||||||
@ -128,7 +128,11 @@ static private int jjMoveNfa_0(int startState, int curPos)
|
|||||||
break;
|
break;
|
||||||
if (kind > 10)
|
if (kind > 10)
|
||||||
kind = 10;
|
kind = 10;
|
||||||
jjCheckNAdd(7);
|
jjCheckNAddTwoStates(7, 8);
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
if ((0x23ffac0000000000L & l) != 0L && kind > 10)
|
||||||
|
kind = 10;
|
||||||
break;
|
break;
|
||||||
default : break;
|
default : break;
|
||||||
}
|
}
|
||||||
@ -147,12 +151,16 @@ static private int jjMoveNfa_0(int startState, int curPos)
|
|||||||
break;
|
break;
|
||||||
if (kind > 10)
|
if (kind > 10)
|
||||||
kind = 10;
|
kind = 10;
|
||||||
jjCheckNAdd(7);
|
jjCheckNAddTwoStates(7, 8);
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
if ((0x7fffffe87fffffeL & l) != 0L)
|
if ((0x7fffffe87fffffeL & l) != 0L)
|
||||||
jjAddStates(0, 1);
|
jjAddStates(0, 1);
|
||||||
break;
|
break;
|
||||||
|
case 8:
|
||||||
|
if ((0x7fffffe87fffffeL & l) != 0L && kind > 10)
|
||||||
|
kind = 10;
|
||||||
|
break;
|
||||||
default : break;
|
default : break;
|
||||||
}
|
}
|
||||||
} while(i != startsAt);
|
} while(i != startsAt);
|
||||||
@ -176,7 +184,7 @@ static private int jjMoveNfa_0(int startState, int curPos)
|
|||||||
kind = 0x7fffffff;
|
kind = 0x7fffffff;
|
||||||
}
|
}
|
||||||
++curPos;
|
++curPos;
|
||||||
if ((i = jjnewStateCnt) == (startsAt = 8 - (jjnewStateCnt = startsAt)))
|
if ((i = jjnewStateCnt) == (startsAt = 9 - (jjnewStateCnt = startsAt)))
|
||||||
return curPos;
|
return curPos;
|
||||||
try { curChar = input_stream.readChar(); }
|
try { curChar = input_stream.readChar(); }
|
||||||
catch(java.io.IOException e) { return curPos; }
|
catch(java.io.IOException e) { return curPos; }
|
||||||
@ -201,8 +209,8 @@ static final long[] jjtoSkip = {
|
|||||||
0x3eL,
|
0x3eL,
|
||||||
};
|
};
|
||||||
static protected SimpleCharStream input_stream;
|
static protected SimpleCharStream input_stream;
|
||||||
static private final int[] jjrounds = new int[8];
|
static private final int[] jjrounds = new int[9];
|
||||||
static private final int[] jjstateSet = new int[16];
|
static private final int[] jjstateSet = new int[18];
|
||||||
static protected char curChar;
|
static protected char curChar;
|
||||||
/** Constructor. */
|
/** Constructor. */
|
||||||
public ParserTokenManager(SimpleCharStream stream){
|
public ParserTokenManager(SimpleCharStream stream){
|
||||||
@ -229,7 +237,7 @@ static private void ReInitRounds()
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
jjround = 0x80000001;
|
jjround = 0x80000001;
|
||||||
for (i = 8; i-- > 0;)
|
for (i = 9; i-- > 0;)
|
||||||
jjrounds[i] = 0x80000000;
|
jjrounds[i] = 0x80000000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user