From 8fdaaeb23d11f3a4010f66ebecad077e2ec81c81 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sun, 26 Feb 2017 13:01:47 -0500 Subject: [PATCH] Removed duplicate code in SET and SETF --- src/function/builtin/APPLY.java | 4 ++-- src/function/builtin/FUNCALL.java | 4 ++-- src/function/builtin/SET.java | 4 ++++ src/function/builtin/special/SETF.java | 32 ++++++-------------------- 4 files changed, 15 insertions(+), 29 deletions(-) diff --git a/src/function/builtin/APPLY.java b/src/function/builtin/APPLY.java index 4a9797c..b1836a7 100644 --- a/src/function/builtin/APPLY.java +++ b/src/function/builtin/APPLY.java @@ -7,8 +7,8 @@ public class APPLY extends LispFunction { private ArgumentValidator argumentValidator; - public static SExpression apply(Cons argList) { - return new APPLY().call(argList); + public static SExpression apply(Cons argumentList) { + return new APPLY().call(argumentList); } public APPLY() { diff --git a/src/function/builtin/FUNCALL.java b/src/function/builtin/FUNCALL.java index 0603993..a8f8291 100644 --- a/src/function/builtin/FUNCALL.java +++ b/src/function/builtin/FUNCALL.java @@ -1,9 +1,9 @@ package function.builtin; import static function.builtin.APPLY.apply; +import static function.builtin.cons.LIST.makeList; import function.*; -import function.builtin.cons.LIST; import sexpression.*; public class FUNCALL extends LispFunction { @@ -17,7 +17,7 @@ public class FUNCALL extends LispFunction { public SExpression call(Cons argumentList) { argumentValidator.validate(argumentList); - Cons applyArgs = new Cons(argumentList.getFirst(), LIST.makeList(argumentList.getRest())); + Cons applyArgs = new Cons(argumentList.getFirst(), makeList(argumentList.getRest())); return apply(applyArgs); } diff --git a/src/function/builtin/SET.java b/src/function/builtin/SET.java index ccb8dab..1041f47 100644 --- a/src/function/builtin/SET.java +++ b/src/function/builtin/SET.java @@ -9,6 +9,10 @@ public class SET extends LispFunction { private ArgumentValidator argumentValidator; private ExecutionContext executionContext; + public static SExpression set(Cons argumentList) { + return new SET().call(argumentList); + } + public SET() { this.argumentValidator = new ArgumentValidator("SET"); this.argumentValidator.setExactNumberOfArguments(2); diff --git a/src/function/builtin/special/SETF.java b/src/function/builtin/special/SETF.java index 70bd318..83086d3 100644 --- a/src/function/builtin/special/SETF.java +++ b/src/function/builtin/special/SETF.java @@ -1,51 +1,33 @@ package function.builtin.special; import static function.builtin.EVAL.eval; +import static function.builtin.SET.set; +import static function.builtin.cons.LIST.makeList; import function.*; import sexpression.*; -import table.*; public class SETF extends LispSpecialFunction { private ArgumentValidator argumentValidator; - private ExecutionContext executionContext; public SETF() { this.argumentValidator = new ArgumentValidator("SETF"); this.argumentValidator.setExactNumberOfArguments(2); this.argumentValidator.setFirstArgumentExpectedType(Symbol.class); - this.executionContext = ExecutionContext.getInstance(); } public SExpression call(Cons argumentList) { argumentValidator.validate(argumentList); + return set(evaluateValueOnly(argumentList)); + } + + private Cons evaluateValueOnly(Cons argumentList) { Cons rest = (Cons) argumentList.getRest(); - SExpression symbol = argumentList.getFirst(); SExpression value = eval(rest.getFirst()); - SymbolTable table = findScopeOfSymbol(symbol); - table.put(symbol.toString(), value); - - return value; - } - - private SymbolTable findScopeOfSymbol(SExpression symbol) { - SymbolTable table = executionContext.getScope(); - - while (!isSymbolInTable(symbol, table) && !isGlobalTable(table)) - table = table.getParent(); - - return table; - } - - private boolean isSymbolInTable(SExpression symbol, SymbolTable table) { - return table.contains(symbol.toString()); - } - - private boolean isGlobalTable(SymbolTable table) { - return table.getParent() == null; + return new Cons(argumentList.getFirst(), makeList(value)); } }