From d3fc8f9812c196c37c61b6ee0b75d1b696bdf9ee Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sun, 29 Jan 2017 14:33:46 -0500 Subject: [PATCH] Refactored the function table logic --- src/function/builtin/EVAL.java | 21 ++++------------- src/function/builtin/SYMBOL_FUNCTION.java | 3 ++- src/function/builtin/special/DEFUN.java | 12 +++------- src/{function => table}/FunctionTable.java | 27 +++++++++++++++++++--- src/table/SymbolTable.java | 6 ----- 5 files changed, 34 insertions(+), 35 deletions(-) rename src/{function => table}/FunctionTable.java (69%) diff --git a/src/function/builtin/EVAL.java b/src/function/builtin/EVAL.java index 8fd9d3b..3b63d9e 100644 --- a/src/function/builtin/EVAL.java +++ b/src/function/builtin/EVAL.java @@ -1,24 +1,18 @@ package function.builtin; -import static function.FunctionTable.functionTable; - import java.text.MessageFormat; -import java.util.HashMap; import error.LispException; import function.*; import function.builtin.cons.LIST; import function.builtin.special.*; import sexpression.*; +import table.FunctionTable; public class EVAL extends LispFunction { - public static HashMap getFunctionTable() { - return functionTable; - } - public static LispFunction lookupFunctionOrLambda(SExpression functionExpression) { - LispFunction function = lookupFunction(functionExpression.toString()); + LispFunction function = FunctionTable.lookupFunction(functionExpression.toString()); if (function == null) function = createLambdaFunction(functionExpression); @@ -26,10 +20,6 @@ public class EVAL extends LispFunction { return function; } - public static LispFunction lookupFunction(String functionName) { - return functionTable.get(functionName); - } - private static LispFunction createLambdaFunction(SExpression lambdaExpression) { if (lambdaExpression.functionp()) return ((LambdaExpression) lambdaExpression).getFunction(); @@ -40,13 +30,12 @@ public class EVAL extends LispFunction { } public static SExpression lookupSymbol(String symbolName) { - if (symbolName.equals("NIL")) { + if (symbolName.equals("NIL")) return Nil.getInstance(); - } else if (symbolName.equals("T")) { + else if (symbolName.equals("T")) return Symbol.T; - } else if (symbolName.startsWith(":")) { + else if (symbolName.startsWith(":")) return new Symbol(symbolName); - } return SETF.lookupSymbolValue(symbolName); } diff --git a/src/function/builtin/SYMBOL_FUNCTION.java b/src/function/builtin/SYMBOL_FUNCTION.java index 42c310b..20327b8 100644 --- a/src/function/builtin/SYMBOL_FUNCTION.java +++ b/src/function/builtin/SYMBOL_FUNCTION.java @@ -5,6 +5,7 @@ import java.text.MessageFormat; import error.LispException; import function.*; import sexpression.*; +import table.FunctionTable; public class SYMBOL_FUNCTION extends LispFunction { @@ -20,7 +21,7 @@ public class SYMBOL_FUNCTION extends LispFunction { argumentValidator.validate(argumentList); SExpression symbol = argumentList.getCar(); - LispFunction function = EVAL.lookupFunction(symbol.toString()); + LispFunction function = FunctionTable.lookupFunction(symbol.toString()); if (function != null) { if (function instanceof UserDefinedFunction) diff --git a/src/function/builtin/special/DEFUN.java b/src/function/builtin/special/DEFUN.java index 75c1dc0..afbcf36 100644 --- a/src/function/builtin/special/DEFUN.java +++ b/src/function/builtin/special/DEFUN.java @@ -1,13 +1,12 @@ package function.builtin.special; import java.text.MessageFormat; -import java.util.HashMap; import environment.Environment; import function.*; -import function.builtin.EVAL; import function.builtin.cons.LIST; import sexpression.*; +import table.FunctionTable; public class DEFUN extends LispFunction { @@ -43,20 +42,15 @@ public class DEFUN extends LispFunction { Cons functionBody = (Cons) remainingArguments.getCdr(); UserDefinedFunction function = new UserDefinedFunction(functionName.toString(), lambdaList, functionBody); - HashMap functionTable = EVAL.getFunctionTable(); - if (isAlreadyDefined(functionName, functionTable)) + if (FunctionTable.isAlreadyDefined(functionName.toString())) printWarning(functionName); - functionTable.put(functionName.toString(), function); + FunctionTable.defineFunction(functionName.toString(), function); return functionName; } - private boolean isAlreadyDefined(SExpression functionName, HashMap functionTable) { - return functionTable.containsKey(functionName.toString()); - } - private void printWarning(SExpression functionName) { String message = MessageFormat.format("WARNING: redefining function {0}", functionName.toString()); environment.getOutput().println(message); diff --git a/src/function/FunctionTable.java b/src/table/FunctionTable.java similarity index 69% rename from src/function/FunctionTable.java rename to src/table/FunctionTable.java index 4aad6a3..7ce6ab6 100644 --- a/src/function/FunctionTable.java +++ b/src/table/FunctionTable.java @@ -1,7 +1,8 @@ -package function; +package table; import java.util.HashMap; +import function.LispFunction; import function.builtin.*; import function.builtin.cons.*; import function.builtin.math.*; @@ -9,10 +10,30 @@ import function.builtin.predicate.*; import function.builtin.special.*; public class FunctionTable { + + private static FunctionTable uniqueInstance = new FunctionTable(); + + public static LispFunction lookupFunction(String functionName) { + return uniqueInstance.functionTable.get(functionName); + } + + public static boolean isAlreadyDefined(String functionName) { + return uniqueInstance.functionTable.containsKey(functionName); + } + + public static void defineFunction(String functionName, LispFunction function) { + uniqueInstance.functionTable.put(functionName, function); + } + + private HashMap functionTable; - public static final HashMap functionTable = new HashMap(); + private FunctionTable() { + this.functionTable = new HashMap<>(); + + initializeFunctionTable(); + } - static { + private void initializeFunctionTable() { functionTable.put("*", new MULTIPLY()); functionTable.put("+", new PLUS()); functionTable.put("-", new MINUS()); diff --git a/src/table/SymbolTable.java b/src/table/SymbolTable.java index b33f4f0..8ab959b 100644 --- a/src/table/SymbolTable.java +++ b/src/table/SymbolTable.java @@ -9,16 +9,10 @@ public class SymbolTable { private HashMap table; private SymbolTable parent; - /** - * Create a new symbol table with no parent. - */ public SymbolTable() { this(null); } - /** - * Create a new symbol table with the specified parent. - */ public SymbolTable(SymbolTable parent) { this.table = new HashMap(); this.parent = parent;