Altered SYMBOL-FUNCTION behavior and cleaned up some code

This commit is contained in:
Mike Cifelli 2017-02-27 14:10:20 -05:00
parent 2ef285e45c
commit d3e3f52e59
9 changed files with 30 additions and 18 deletions

View File

@ -24,16 +24,21 @@ public class SYMBOL_FUNCTION extends LispFunction {
SExpression symbol = argumentList.getFirst(); SExpression symbol = argumentList.getFirst();
LispFunction function = FunctionTable.lookupFunction(symbol.toString()); LispFunction function = FunctionTable.lookupFunction(symbol.toString());
if (function != null) { if (function != null)
if (function instanceof UserDefinedFunction) return createSymbolRepresentation(symbol, function);
return ((UserDefinedFunction) function).getLambdaExpression();
return new Symbol(MessageFormat.format("#<SYSTEM-FUNCTION {0}>", symbol.toString()));
}
throw new UndefinedSymbolFunctionException(symbol); throw new UndefinedSymbolFunctionException(symbol);
} }
private SExpression createSymbolRepresentation(SExpression symbol, LispFunction function) {
if (function instanceof UserDefinedFunction)
return ((UserDefinedFunction) function).getLambdaExpression();
else if (function instanceof LispSpecialFunction)
return new Symbol(MessageFormat.format("#<SPECIAL-FUNCTION {0}>", symbol.toString()));
return new Symbol(MessageFormat.format("#<FUNCTION {0}>", symbol.toString()));
}
public static class UndefinedSymbolFunctionException extends LispException { public static class UndefinedSymbolFunctionException extends LispException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@ -37,7 +37,7 @@ public class LispScanner {
positionTracker.incrementLine(); positionTracker.incrementLine();
} }
return tokenFactory.createEOFToken(positionTracker.getCurrentPosition()); return tokenFactory.createEofToken(positionTracker.getCurrentPosition());
} }
private Token createTokenFromCharacter(char c) { private Token createTokenFromCharacter(char c) {
@ -130,7 +130,7 @@ public class LispScanner {
previousCharacter = currentCharacter; previousCharacter = currentCharacter;
} }
return terminateTokenWithEOF(); return terminateTokenWithEof();
} }
private void addCharacterToToken() { private void addCharacterToToken() {
@ -153,7 +153,7 @@ public class LispScanner {
return (currentCharacter == DOUBLE_QUOTE) && (previousCharacter != BACKSLASH); return (currentCharacter == DOUBLE_QUOTE) && (previousCharacter != BACKSLASH);
} }
private String terminateTokenWithEOF() { private String terminateTokenWithEof() {
if (isStringToken()) if (isStringToken())
throw new UnterminatedStringException(position); throw new UnterminatedStringException(position);

View File

@ -9,7 +9,7 @@ public interface TokenFactory {
Token createToken(String text, FilePosition position); Token createToken(String text, FilePosition position);
Token createEOFToken(FilePosition position); Token createEofToken(FilePosition position);
public static class EmptyTokenTextException extends CriticalLineColumnException { public static class EmptyTokenTextException extends CriticalLineColumnException {

View File

@ -45,7 +45,7 @@ public class TokenFactoryImpl implements TokenFactory {
return (text.length() > 1) && isDigit(text.charAt(1)); return (text.length() > 1) && isDigit(text.charAt(1));
} }
public Token createEOFToken(FilePosition position) { public Token createEofToken(FilePosition position) {
return new Eof("EOF", position); return new Eof("EOF", position);
} }

View File

@ -23,10 +23,17 @@ public class SYMBOL_FUNCTIONTester {
} }
@Test @Test
public void testSymbolFunction_BuiltinFunction() { public void testSymbolFunction_BuiltInFunction() {
String input = "(symbol-function '+)"; String input = "(symbol-function '+)";
assertEquals("#<SYSTEM-FUNCTION +>", evaluateString(input).toString()); assertEquals("#<FUNCTION +>", evaluateString(input).toString());
}
@Test
public void testSymbolFunction_BuiltInSpecialFunction() {
String input = "(symbol-function 'if)";
assertEquals("#<SPECIAL-FUNCTION IF>", evaluateString(input).toString());
} }
@Test @Test

View File

@ -41,7 +41,7 @@ public class LispScannerLineColumnTester {
} }
@Test @Test
public void givenNothing_RecordsCorrectEOFLocation() { public void givenNothing_RecordsCorrectEofLocation() {
String input = ""; String input = "";
LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 0) }; LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 0) };

View File

@ -63,7 +63,7 @@ public class LispScannerTextTester {
} }
@Test @Test
public void givenEOF_ReordsCorrectText() { public void givenEof_ReordsCorrectText() {
String input = ""; String input = "";
String expected = "EOF"; String expected = "EOF";

View File

@ -26,8 +26,8 @@ public class TokenFactoryTester {
} }
@Test @Test
public void testEOFTokenCreation() { public void testEofTokenCreation() {
assertTrue(tokenFactory.createEOFToken(testPosition) instanceof Eof); assertTrue(tokenFactory.createEofToken(testPosition) instanceof Eof);
} }
@Test @Test