From d3e3f52e59df7041b971846db5a25d9de9b5a9ac Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Mon, 27 Feb 2017 14:10:20 -0500 Subject: [PATCH] Altered SYMBOL-FUNCTION behavior and cleaned up some code --- src/function/builtin/SYMBOL_FUNCTION.java | 17 +++++++++++------ src/scanner/LispScanner.java | 6 +++--- src/token/TokenFactory.java | 2 +- src/token/TokenFactoryImpl.java | 2 +- src/util/Characters.java | 2 +- .../function/builtin/SYMBOL_FUNCTIONTester.java | 11 +++++++++-- test/scanner/LispScannerLineColumnTester.java | 2 +- test/scanner/LispScannerTextTester.java | 2 +- test/token/TokenFactoryTester.java | 4 ++-- 9 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/function/builtin/SYMBOL_FUNCTION.java b/src/function/builtin/SYMBOL_FUNCTION.java index 8010e9e..1dbf27b 100644 --- a/src/function/builtin/SYMBOL_FUNCTION.java +++ b/src/function/builtin/SYMBOL_FUNCTION.java @@ -24,16 +24,21 @@ public class SYMBOL_FUNCTION extends LispFunction { SExpression symbol = argumentList.getFirst(); LispFunction function = FunctionTable.lookupFunction(symbol.toString()); - if (function != null) { - if (function instanceof UserDefinedFunction) - return ((UserDefinedFunction) function).getLambdaExpression(); - - return new Symbol(MessageFormat.format("#", symbol.toString())); - } + if (function != null) + return createSymbolRepresentation(symbol, function); 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("#", symbol.toString())); + + return new Symbol(MessageFormat.format("#", symbol.toString())); + } + public static class UndefinedSymbolFunctionException extends LispException { private static final long serialVersionUID = 1L; diff --git a/src/scanner/LispScanner.java b/src/scanner/LispScanner.java index 277f1a9..aee3132 100644 --- a/src/scanner/LispScanner.java +++ b/src/scanner/LispScanner.java @@ -37,7 +37,7 @@ public class LispScanner { positionTracker.incrementLine(); } - return tokenFactory.createEOFToken(positionTracker.getCurrentPosition()); + return tokenFactory.createEofToken(positionTracker.getCurrentPosition()); } private Token createTokenFromCharacter(char c) { @@ -130,7 +130,7 @@ public class LispScanner { previousCharacter = currentCharacter; } - return terminateTokenWithEOF(); + return terminateTokenWithEof(); } private void addCharacterToToken() { @@ -153,7 +153,7 @@ public class LispScanner { return (currentCharacter == DOUBLE_QUOTE) && (previousCharacter != BACKSLASH); } - private String terminateTokenWithEOF() { + private String terminateTokenWithEof() { if (isStringToken()) throw new UnterminatedStringException(position); diff --git a/src/token/TokenFactory.java b/src/token/TokenFactory.java index d524306..45003b2 100644 --- a/src/token/TokenFactory.java +++ b/src/token/TokenFactory.java @@ -9,7 +9,7 @@ public interface TokenFactory { Token createToken(String text, FilePosition position); - Token createEOFToken(FilePosition position); + Token createEofToken(FilePosition position); public static class EmptyTokenTextException extends CriticalLineColumnException { diff --git a/src/token/TokenFactoryImpl.java b/src/token/TokenFactoryImpl.java index c153580..d9e57d2 100644 --- a/src/token/TokenFactoryImpl.java +++ b/src/token/TokenFactoryImpl.java @@ -45,7 +45,7 @@ public class TokenFactoryImpl implements TokenFactory { return (text.length() > 1) && isDigit(text.charAt(1)); } - public Token createEOFToken(FilePosition position) { + public Token createEofToken(FilePosition position) { return new Eof("EOF", position); } diff --git a/src/util/Characters.java b/src/util/Characters.java index 90e9bd2..df61bc0 100644 --- a/src/util/Characters.java +++ b/src/util/Characters.java @@ -46,7 +46,7 @@ public final class Characters { public static boolean isLegalStringCharacter(char c) { return true; } - + public static boolean isNumberPrefix(char c) { return c == DASH || c == PLUS; } diff --git a/test/function/builtin/SYMBOL_FUNCTIONTester.java b/test/function/builtin/SYMBOL_FUNCTIONTester.java index 3a892be..583ce93 100644 --- a/test/function/builtin/SYMBOL_FUNCTIONTester.java +++ b/test/function/builtin/SYMBOL_FUNCTIONTester.java @@ -23,10 +23,17 @@ public class SYMBOL_FUNCTIONTester { } @Test - public void testSymbolFunction_BuiltinFunction() { + public void testSymbolFunction_BuiltInFunction() { String input = "(symbol-function '+)"; - assertEquals("#", evaluateString(input).toString()); + assertEquals("#", evaluateString(input).toString()); + } + + @Test + public void testSymbolFunction_BuiltInSpecialFunction() { + String input = "(symbol-function 'if)"; + + assertEquals("#", evaluateString(input).toString()); } @Test diff --git a/test/scanner/LispScannerLineColumnTester.java b/test/scanner/LispScannerLineColumnTester.java index be02516..82c7422 100644 --- a/test/scanner/LispScannerLineColumnTester.java +++ b/test/scanner/LispScannerLineColumnTester.java @@ -41,7 +41,7 @@ public class LispScannerLineColumnTester { } @Test - public void givenNothing_RecordsCorrectEOFLocation() { + public void givenNothing_RecordsCorrectEofLocation() { String input = ""; LineColumn[] expectedLinesAndColumns = { LineColumn.create(1, 0) }; diff --git a/test/scanner/LispScannerTextTester.java b/test/scanner/LispScannerTextTester.java index 5141956..56faa94 100644 --- a/test/scanner/LispScannerTextTester.java +++ b/test/scanner/LispScannerTextTester.java @@ -63,7 +63,7 @@ public class LispScannerTextTester { } @Test - public void givenEOF_ReordsCorrectText() { + public void givenEof_ReordsCorrectText() { String input = ""; String expected = "EOF"; diff --git a/test/token/TokenFactoryTester.java b/test/token/TokenFactoryTester.java index eeb61a6..651678c 100644 --- a/test/token/TokenFactoryTester.java +++ b/test/token/TokenFactoryTester.java @@ -26,8 +26,8 @@ public class TokenFactoryTester { } @Test - public void testEOFTokenCreation() { - assertTrue(tokenFactory.createEOFToken(testPosition) instanceof Eof); + public void testEofTokenCreation() { + assertTrue(tokenFactory.createEofToken(testPosition) instanceof Eof); } @Test