From 311a3b403673c5dc084a5e9d2046e39c976ec02e Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Fri, 3 Mar 2017 15:06:49 -0500 Subject: [PATCH] Cleaned up code and tests --- .../ExternalFunctionUnitTests.wiki | 5 +- .../LispInterpreter/LexicalClosures.wiki | 8 +- .../LispInterpreter/MultipleMethodObject.wiki | 18 +- .../LispInterpreter/ObjectComposition.wiki | 16 +- fitnesse/FitNesseRoot/RecentChanges.wiki | 7 +- lisp/compound-interest.lisp | 8 +- lisp/dlambda-test.lisp | 186 +++++++++--------- lisp/fact.lisp | 1 + lisp/fruit-counter.lisp | 14 +- lisp/paydays.lisp | 20 +- lisp/reverse.lisp | 28 +-- lisp/roman.lisp | 30 +++ lisp/test.lisp | 15 -- lisp/this.lisp | 19 +- lisp/unit-test.lisp | 2 +- src/function/builtin/APPLY.java | 11 +- src/function/builtin/EVAL.java | 8 +- src/function/builtin/SET.java | 4 +- src/function/builtin/SYMBOL_FUNCTION.java | 5 +- src/function/builtin/cons/LENGTH.java | 6 +- src/function/builtin/special/Define.java | 11 +- src/table/FunctionTable.java | 4 +- .../fixture/LispInterpreterFixture.java | 6 +- test/function/builtin/FUNCALLTester.java | 6 +- .../builtin/SYMBOL_FUNCTIONTester.java | 6 +- .../builtin/special/DEFINE_MACROTester.java | 6 +- .../function/builtin/special/DEFUNTester.java | 6 +- test/table/FunctionTableTester.java | 49 ++--- 28 files changed, 269 insertions(+), 236 deletions(-) create mode 100644 lisp/roman.lisp delete mode 100644 lisp/test.lisp diff --git a/fitnesse/FitNesseRoot/LispInterpreter/ExternalFunctionUnitTests.wiki b/fitnesse/FitNesseRoot/LispInterpreter/ExternalFunctionUnitTests.wiki index cc29ebe..38c2b32 100644 --- a/fitnesse/FitNesseRoot/LispInterpreter/ExternalFunctionUnitTests.wiki +++ b/fitnesse/FitNesseRoot/LispInterpreter/ExternalFunctionUnitTests.wiki @@ -3,5 +3,6 @@ Test --- Unit tests for the dlambda special function. -| script | lisp interpreter fixture | -| show | evaluate | (load "lisp/dlambda-test.lisp") | +| script | lisp interpreter fixture | +| check | evaluate | (load "lisp/dlambda-test.lisp") | T | +| check | evaluate | (test-dlambda) | =~/T$/ | diff --git a/fitnesse/FitNesseRoot/LispInterpreter/LexicalClosures.wiki b/fitnesse/FitNesseRoot/LispInterpreter/LexicalClosures.wiki index 00a89f5..fab5e23 100644 --- a/fitnesse/FitNesseRoot/LispInterpreter/LexicalClosures.wiki +++ b/fitnesse/FitNesseRoot/LispInterpreter/LexicalClosures.wiki @@ -3,7 +3,7 @@ Test --- A simple lexical closure. -| script | lisp interpreter fixture | -| show | evaluate | (defun adderx (x) (lambda (y) (+ x y))) | -| show | evaluate | (setf adder20 (adderx 20)) | -| check | evaluate | (funcall adder20 2) | 22 | +| script | lisp interpreter fixture | +| show | evaluate | (defun adder-x (x) (lambda (y) (+ x y))) | +| show | evaluate | (setf adder-20 (adder-x 20)) | +| check | evaluate | (funcall adder-20 2) | 22 | diff --git a/fitnesse/FitNesseRoot/LispInterpreter/MultipleMethodObject.wiki b/fitnesse/FitNesseRoot/LispInterpreter/MultipleMethodObject.wiki index 38a1828..e212df9 100644 --- a/fitnesse/FitNesseRoot/LispInterpreter/MultipleMethodObject.wiki +++ b/fitnesse/FitNesseRoot/LispInterpreter/MultipleMethodObject.wiki @@ -3,7 +3,7 @@ Test --- An object with multiple methods. -| script | lisp interpreter fixture | +| script | lisp interpreter fixture | | show | evaluate |!- (defun counter-class () @@ -15,11 +15,11 @@ An object with multiple methods. ((:dec) (setq counter (- counter 1))))))) - -!| -| show | evaluate | (setq my-counter (counter-class)) | -| check | evaluate | (funcall my-counter :inc) | 1 | -| check | evaluate | (funcall my-counter :inc) | 2 | -| check | evaluate | (funcall my-counter :inc) | 3 | -| check | evaluate | (funcall my-counter :dec) | 2 | -| check | evaluate | (funcall my-counter :dec) | 1 | -| check | evaluate | (funcall my-counter :dec) | 0 | + -!| +| show | evaluate | (setq my-counter (counter-class)) | +| check | evaluate | (funcall my-counter :inc) | 1 | +| check | evaluate | (funcall my-counter :inc) | 2 | +| check | evaluate | (funcall my-counter :inc) | 3 | +| check | evaluate | (funcall my-counter :dec) | 2 | +| check | evaluate | (funcall my-counter :dec) | 1 | +| check | evaluate | (funcall my-counter :dec) | 0 | diff --git a/fitnesse/FitNesseRoot/LispInterpreter/ObjectComposition.wiki b/fitnesse/FitNesseRoot/LispInterpreter/ObjectComposition.wiki index 8fe1739..6bc48f8 100644 --- a/fitnesse/FitNesseRoot/LispInterpreter/ObjectComposition.wiki +++ b/fitnesse/FitNesseRoot/LispInterpreter/ObjectComposition.wiki @@ -3,11 +3,11 @@ Test --- Shows object composition, a default method, and two different ways of referencing objects. -| script | lisp interpreter fixture | -| show | evaluate | (load "lisp/fruit-counter.lisp") | -| check | evaluate | (my-counter :inc-apples) | 1 | -| check | evaluate | (my-counter :inc-apples) | 2 | -| check | evaluate | (funcall my-counter2 :dec-bananas) | 9999 | -| check | evaluate | (my-counter :set-coconuts 12) | 12 | -| check | evaluate | (my-counter) |(APPLES 2 BANANAS 0 COCONUTS 12) | -| check | evaluate | (funcall my-counter2) |(APPLES 10000 BANANAS 9999 COCONUTS 10000) | \ No newline at end of file +| script | lisp interpreter fixture | +| check | evaluate | (load "lisp/fruit-counter.lisp") | T | +| check | evaluate | (my-fruits :inc-apples) | 1 | +| check | evaluate | (my-fruits :inc-apples) | 2 | +| check | evaluate | (funcall my-fruits2 :dec-bananas) | 9999 | +| check | evaluate | (my-fruits :set-coconuts 12) | 12 | +| check | evaluate | (my-fruits) | ((APPLES 2) (BANANAS 0) (COCONUTS 12)) | +| check | evaluate | (funcall my-fruits2) | ((APPLES 10000) (BANANAS 9999) (COCONUTS 10000)) | \ No newline at end of file diff --git a/fitnesse/FitNesseRoot/RecentChanges.wiki b/fitnesse/FitNesseRoot/RecentChanges.wiki index b85b8c1..0c83a5d 100644 --- a/fitnesse/FitNesseRoot/RecentChanges.wiki +++ b/fitnesse/FitNesseRoot/RecentChanges.wiki @@ -1,9 +1,10 @@ -|LispInterpreter.ExternalFunctionUnitTests||12:36:10 Fri, Mar 03, 2017| -|LispInterpreter.ObjectComposition||12:36:01 Fri, Mar 03, 2017| +|LispInterpreter.ExternalFunctionUnitTests||14:30:55 Fri, Mar 03, 2017| +|LispInterpreter.MultipleMethodObject||14:19:50 Fri, Mar 03, 2017| +|LispInterpreter.LexicalClosures||14:18:39 Fri, Mar 03, 2017| +|LispInterpreter.ObjectComposition||14:17:58 Fri, Mar 03, 2017| |LispInterpreter.DlambdaUnitTests||10:16:42 Fri, Mar 03, 2017| |LispInterpreter.dlambda||10:12:59 Fri, Mar 03, 2017| |LispInterpreter||09:04:51 Fri, Mar 03, 2017| -|LispInterpreter.LexicalClosures||16:31:40 Thu, Mar 02, 2017| |LispInterpreter.LexicalClosure||16:26:49 Thu, Mar 02, 2017| |LispInterpreter.FruitCounter||16:13:51 Thu, Mar 02, 2017| |LispInterpreter.SimpleClosure||16:12:31 Thu, Mar 02, 2017| diff --git a/lisp/compound-interest.lisp b/lisp/compound-interest.lisp index 6858ba6..263580f 100644 --- a/lisp/compound-interest.lisp +++ b/lisp/compound-interest.lisp @@ -10,9 +10,11 @@ (defun compound-interest (principal rate years) (if (= years 0) principal - (compound-interest (+ principal (percent principal rate)) - rate - (decrement years) + (compound-interest + (+ principal (percent principal rate)) + rate + (decrement years) ) ) ) + diff --git a/lisp/dlambda-test.lisp b/lisp/dlambda-test.lisp index 7592428..5b533f4 100644 --- a/lisp/dlambda-test.lisp +++ b/lisp/dlambda-test.lisp @@ -1,111 +1,115 @@ (load "unit-test.lisp") (load "dlambda.lisp") -(unit - (list +(defun test-dlambda () - (defun empty-dlambda () - (assert-equal - '(lambda (&rest arguments) (case (first arguments))) - (dlambda) - ) - ) + (unit + (list - (defun dlambda-default-method-only () - (assert-equal - '(lambda (&rest arguments) - (case (first arguments) - (t (apply (lambda () (print "nothing")) arguments)) - ) - ) - (dlambda - (t () (print "nothing")) + (defun empty-dlambda () + (assert-equal + '(lambda (&rest arguments) (case (first arguments))) + (dlambda) ) ) - ) - (defun dlambda-named-method-only () - (assert-equal - '(lambda (&rest arguments) - (case (first arguments) - (:write (apply (lambda () (print "something")) (rest arguments))) - ) - ) - (dlambda - (:write () (print "something")) - ) - ) - ) - - (defun dlambda-named-and-default-method () - (assert-equal - '(lambda (&rest arguments) - (case (first arguments) - (:write (apply (lambda () (print "something")) (rest arguments))) - (t (apply (lambda () (print "nothing")) arguments)) - ) - ) - (dlambda - (:write () (print "something")) - (t () (print "nothing")) - ) - ) - ) - - (defun dlambda-methods-with-arguments () - (assert-equal - '(lambda (&rest arguments) - (case (first arguments) - (:write (apply (lambda (message) (print message)) (rest arguments))) - (t (apply (lambda (&rest messages) (print messages)) arguments)) - ) - ) - (dlambda - (:write (message) (print message)) - (t (&rest messages) (print messages)) - ) - ) - ) - - (defun dlambda-methods-with-multiple-arguments () - (assert-equal - '(lambda (&rest arguments) - (case (first arguments) - (:write - (apply - (lambda (message &rest other-stuff) - (print message) - (print other-stuff) - ) - (rest arguments) - ) + (defun dlambda-default-method-only () + (assert-equal + '(lambda (&rest arguments) + (case (first arguments) + (t (apply (lambda () (print "nothing")) arguments)) ) - (t - (apply - (lambda (message1 message2 &rest other-stuff) - (print message1) - (print message2) - (print other-stuff) + ) + (dlambda + (t () (print "nothing")) + ) + ) + ) + + (defun dlambda-named-method-only () + (assert-equal + '(lambda (&rest arguments) + (case (first arguments) + (:write (apply (lambda () (print "something")) (rest arguments))) + ) + ) + (dlambda + (:write () (print "something")) + ) + ) + ) + + (defun dlambda-named-and-default-method () + (assert-equal + '(lambda (&rest arguments) + (case (first arguments) + (:write (apply (lambda () (print "something")) (rest arguments))) + (t (apply (lambda () (print "nothing")) arguments)) + ) + ) + (dlambda + (:write () (print "something")) + (t () (print "nothing")) + ) + ) + ) + + (defun dlambda-methods-with-arguments () + (assert-equal + '(lambda (&rest arguments) + (case (first arguments) + (:write (apply (lambda (message) (print message)) (rest arguments))) + (t (apply (lambda (&rest messages) (print messages)) arguments)) + ) + ) + (dlambda + (:write (message) (print message)) + (t (&rest messages) (print messages)) + ) + ) + ) + + (defun dlambda-methods-with-multiple-arguments () + (assert-equal + '(lambda (&rest arguments) + (case (first arguments) + (:write + (apply + (lambda (message &rest other-stuff) + (print message) + (print other-stuff) + ) + (rest arguments) + ) + ) + (t + (apply + (lambda (message1 message2 &rest other-stuff) + (print message1) + (print message2) + (print other-stuff) + ) + arguments ) - arguments ) ) ) - ) - (dlambda - (:write (message &rest other-stuff) - (print message) - (print other-stuff) - ) - (t (message1 message2 &rest other-stuff) - (print message1) - (print message2) - (print other-stuff) + (dlambda + (:write (message &rest other-stuff) + (print message) + (print other-stuff) + ) + (t (message1 message2 &rest other-stuff) + (print message1) + (print message2) + (print other-stuff) + ) ) ) ) - ) + ) ) + ) diff --git a/lisp/fact.lisp b/lisp/fact.lisp index f639712..34531ad 100644 --- a/lisp/fact.lisp +++ b/lisp/fact.lisp @@ -3,3 +3,4 @@ (* x (fact (- x 1))) ) ) + diff --git a/lisp/fruit-counter.lisp b/lisp/fruit-counter.lisp index c32041f..6449912 100644 --- a/lisp/fruit-counter.lisp +++ b/lisp/fruit-counter.lisp @@ -87,9 +87,9 @@ (t (&rest arguments) (list - 'apples (funcall apple-counter :get) - 'bananas (funcall banana-counter :get) - 'coconuts (funcall coconut-counter :get) + (list 'apples (funcall apple-counter :get)) + (list 'bananas (funcall banana-counter :get)) + (list 'coconuts (funcall coconut-counter :get)) ) ) @@ -102,18 +102,18 @@ ; Create an instance ; ; usage: -; ~ (my-counter :set-apples 23) +; ~ (my-fruits :set-apples 23) ; 23 ; (let ((instance (fruit-counter 0))) - (defun my-counter (&rest args) (apply instance args)) + (defun my-fruits (&rest args) (apply instance args)) ) ; Another way ; ; usage: -; ~ (funcall my-counter2 :set-apples 23) +; ~ (funcall my-fruits2 :set-apples 23) ; 23 ; -(setf my-counter2 (fruit-counter 10000)) +(setf my-fruits2 (fruit-counter 10000)) diff --git a/lisp/paydays.lisp b/lisp/paydays.lisp index e22052d..97489f9 100644 --- a/lisp/paydays.lisp +++ b/lisp/paydays.lisp @@ -3,9 +3,19 @@ (days-in-year 365) (two-weeks 14)) - (+ 1 - (/ (- (+ days-in-year - leap-year) - first-payday-day) - two-weeks)) + (+ + (/ + (- + (+ + days-in-year + leap-year + ) + first-payday-day + ) + two-weeks + ) + 1 + ) + ) + diff --git a/lisp/reverse.lisp b/lisp/reverse.lisp index 6b751ff..4f58dfd 100644 --- a/lisp/reverse.lisp +++ b/lisp/reverse.lisp @@ -1,23 +1,23 @@ (defun reverse (the-list) - (cond - (the-list (append (reverse (rest the-list)) - (list (first the-list)) - ) + (if the-list + (append + (reverse (rest the-list)) + (list (first the-list)) ) ) ) (defun deep-reverse (the-list) - (cond - (the-list (append (deep-reverse (rest the-list)) - (list (cond - ((listp (first the-list)) - (deep-reverse (first the-list)) - ) - (t (first the-list)) - ) - ) - ) + (if the-list + (append + (deep-reverse (rest the-list)) + (list + (if (listp (first the-list)) + (deep-reverse (first the-list)) + (first the-list) + ) + ) ) ) ) + diff --git a/lisp/roman.lisp b/lisp/roman.lisp new file mode 100644 index 0000000..ae27cc4 --- /dev/null +++ b/lisp/roman.lisp @@ -0,0 +1,30 @@ +;; A list containing the values of single-letter Roman numerals. +(setf roman-number-list + '((I 1) (V 5) (X 10) (L 50) (C 100) (D 500) (M 1000)) +) + +;; Converts a single Roman numeral letter into its equivalent decimal value. +(defun letter-to-decimal (letter) + (car + (cdr + ( + (lambda (lst f) + (cond + ((null lst) ()) + ((eq (car (car lst)) letter) (car lst)) + (t (funcall f (cdr lst) f)) + ) + ) + roman-number-list + (lambda (lst f) + (cond + ((null lst) ()) + ((eq (car (car lst)) letter) (car lst)) + (t (funcall f (cdr lst) f)) + ) + ) + ) + ) + ) +) + diff --git a/lisp/test.lisp b/lisp/test.lisp deleted file mode 100644 index 16aa63f..0000000 --- a/lisp/test.lisp +++ /dev/null @@ -1,15 +0,0 @@ -;; A list containing the values of single-letter Roman numerals. -(setf *roman-number-list* - '((I 1) (V 5) (X 10) (L 50) (C 100) (D 500) (M 1000))) - -;; Converts a single Roman numeral letter into its equivalent decimal value. -(defun letter-to-decimal (letter) - (car (cdr ((lambda (lst f) - (cond ((null lst) ()) - ((eq (car (car lst)) letter) (car lst)) - (t (funcall f (cdr lst) f)))) - *roman-number-list* - (lambda (lst f) - (cond ((null lst) ()) - ((eq (car (car lst)) letter) (car lst)) - (t (funcall f (cdr lst) f)))))))) diff --git a/lisp/this.lisp b/lisp/this.lisp index c9e6003..d48c482 100644 --- a/lisp/this.lisp +++ b/lisp/this.lisp @@ -2,10 +2,10 @@ (defun counter (initial-count) (let ((count initial-count) - (print-prefix nil) + (name nil) (this nil)) - (setf print-prefix "Counter") + (setf name "Counter") (setf this (eval @@ -40,7 +40,7 @@ ) (t () - (cons print-prefix count) + (cons name count) ) ) @@ -50,21 +50,8 @@ ) ) -; Create an instance -; -; usage: -; ~ (my-counter :inc-3) -; 3 -; (let ((instance (counter 0))) (defun my-counter (&rest args) (apply instance args)) ) -; Another way -; -; usage: -; ~ (funcall my-counter2 :dec-3) -; 997 -; (setf my-counter2 (counter 10000)) - diff --git a/lisp/unit-test.lisp b/lisp/unit-test.lisp index 912ad3f..430d0fe 100644 --- a/lisp/unit-test.lisp +++ b/lisp/unit-test.lisp @@ -5,7 +5,7 @@ t ) (progn - (print (cons 'F unit-test)) + (print (cons 'f unit-test)) nil ) ) diff --git a/src/function/builtin/APPLY.java b/src/function/builtin/APPLY.java index 3f77a0c..44fb6fe 100644 --- a/src/function/builtin/APPLY.java +++ b/src/function/builtin/APPLY.java @@ -1,5 +1,8 @@ package function.builtin; +import static function.builtin.EVAL.lookupFunctionOrLambda; +import static table.FunctionTable.lookupFunction; + import function.*; import sexpression.*; @@ -9,7 +12,7 @@ public class APPLY extends LispFunction { private ArgumentValidator argumentValidator; public static SExpression apply(Cons argumentList) { - return new APPLY().call(argumentList); + return lookupFunction("APPLY").call(argumentList); } public APPLY() { @@ -22,11 +25,11 @@ public class APPLY extends LispFunction { argumentValidator.validate(argumentList); Cons rest = (Cons) argumentList.getRest(); + Cons functionArguments = (Cons) rest.getFirst(); SExpression functionName = argumentList.getFirst(); - SExpression functionArguments = rest.getFirst(); - LispFunction function = EVAL.lookupFunctionOrLambda(functionName); + LispFunction function = lookupFunctionOrLambda(functionName); - return function.call((Cons) functionArguments); + return function.call(functionArguments); } } diff --git a/src/function/builtin/EVAL.java b/src/function/builtin/EVAL.java index c372404..5847f88 100644 --- a/src/function/builtin/EVAL.java +++ b/src/function/builtin/EVAL.java @@ -2,6 +2,7 @@ package function.builtin; import static sexpression.Nil.NIL; import static sexpression.Symbol.T; +import static table.FunctionTable.lookupFunction; import java.text.MessageFormat; @@ -10,13 +11,13 @@ import function.*; import function.builtin.cons.LIST; import function.builtin.special.LAMBDA; import sexpression.*; -import table.*; +import table.ExecutionContext; @FunctionNames({ "EVAL" }) public class EVAL extends LispFunction { public static LispFunction lookupFunctionOrLambda(SExpression functionExpression) { - LispFunction function = FunctionTable.lookupFunction(functionExpression.toString()); + LispFunction function = lookupFunction(functionExpression.toString()); if (function == null) function = createLambdaFunction(functionExpression); @@ -46,9 +47,8 @@ public class EVAL extends LispFunction { public static SExpression eval(SExpression sExpression) { Cons argumentList = LIST.makeList(sExpression); - EVAL eval = new EVAL(); - return eval.call(argumentList); + return lookupFunction("EVAL").call(argumentList); } private ArgumentValidator argumentValidator; diff --git a/src/function/builtin/SET.java b/src/function/builtin/SET.java index 6ce9801..020faaf 100644 --- a/src/function/builtin/SET.java +++ b/src/function/builtin/SET.java @@ -1,5 +1,7 @@ package function.builtin; +import static table.FunctionTable.lookupFunction; + import function.*; import sexpression.*; import table.*; @@ -8,7 +10,7 @@ import table.*; public class SET extends LispFunction { public static SExpression set(Cons argumentList) { - return new SET().call(argumentList); + return lookupFunction("SET").call(argumentList); } private ArgumentValidator argumentValidator; diff --git a/src/function/builtin/SYMBOL_FUNCTION.java b/src/function/builtin/SYMBOL_FUNCTION.java index d7eb4d0..1c68f31 100644 --- a/src/function/builtin/SYMBOL_FUNCTION.java +++ b/src/function/builtin/SYMBOL_FUNCTION.java @@ -1,11 +1,12 @@ package function.builtin; +import static table.FunctionTable.lookupFunction; + import java.text.MessageFormat; import error.LispException; import function.*; import sexpression.*; -import table.FunctionTable; @FunctionNames({ "SYMBOL-FUNCTION" }) public class SYMBOL_FUNCTION extends LispFunction { @@ -22,7 +23,7 @@ public class SYMBOL_FUNCTION extends LispFunction { argumentValidator.validate(argumentList); SExpression symbol = argumentList.getFirst(); - LispFunction function = FunctionTable.lookupFunction(symbol.toString()); + LispFunction function = lookupFunction(symbol.toString()); if (function != null) return createRepresentation(symbol, function); diff --git a/src/function/builtin/cons/LENGTH.java b/src/function/builtin/cons/LENGTH.java index aae5b67..85172e7 100644 --- a/src/function/builtin/cons/LENGTH.java +++ b/src/function/builtin/cons/LENGTH.java @@ -1,5 +1,7 @@ package function.builtin.cons; +import static function.builtin.cons.LIST.makeList; + import java.math.BigInteger; import function.*; @@ -10,7 +12,7 @@ public class LENGTH extends LispFunction { public static BigInteger getLength(Cons list) { LENGTH lengthFunction = new LENGTH(); - LispNumber length = lengthFunction.callWithoutArgumentValidation(LIST.makeList(list)); + LispNumber length = lengthFunction.callWithoutArgumentValidation(makeList(list)); return length.getValue(); } @@ -35,7 +37,7 @@ public class LENGTH extends LispFunction { private LispNumber callTailRecursive(BigInteger accumulatedLength, Cons argumentList) { Cons list = (Cons) argumentList.getFirst(); - Cons restOfList = LIST.makeList(list.getRest()); + Cons restOfList = makeList(list.getRest()); if (list.isNull()) return new LispNumber(accumulatedLength); diff --git a/src/function/builtin/special/Define.java b/src/function/builtin/special/Define.java index db31c1b..d5c58fb 100644 --- a/src/function/builtin/special/Define.java +++ b/src/function/builtin/special/Define.java @@ -1,13 +1,14 @@ package function.builtin.special; +import static function.builtin.cons.LIST.makeList; +import static table.FunctionTable.*; + import java.text.MessageFormat; import environment.RuntimeEnvironment; import error.*; import function.*; -import function.builtin.cons.LIST; import sexpression.*; -import table.FunctionTable; public abstract class Define extends LispSpecialFunction { @@ -36,7 +37,7 @@ public abstract class Define extends LispSpecialFunction { Cons remainingArguments = (Cons) argumentList.getRest(); SExpression functionName = argumentList.getFirst(); SExpression secondArgument = remainingArguments.getFirst(); - lambdaListIsListValidator.validate(LIST.makeList(secondArgument)); + lambdaListIsListValidator.validate(makeList(secondArgument)); Cons lambdaList = (Cons) secondArgument; lambdaListValidator.validate(lambdaList); @@ -44,10 +45,10 @@ public abstract class Define extends LispSpecialFunction { Cons functionBody = (Cons) remainingArguments.getRest(); UserDefinedFunction function = createFunction(functionName, lambdaList, functionBody); - if (FunctionTable.isAlreadyDefined(functionName.toString())) + if (isAlreadyDefined(functionName.toString())) errorManager.handle(new RedefiningFunctionWarning(functionName.toString())); - FunctionTable.defineFunction(functionName.toString(), function); + defineFunction(functionName.toString(), function); return functionName; } diff --git a/src/table/FunctionTable.java b/src/table/FunctionTable.java index 46dd7c7..0ad7a42 100644 --- a/src/table/FunctionTable.java +++ b/src/table/FunctionTable.java @@ -67,11 +67,11 @@ public class FunctionTable { getTable().put(functionName, function); } - public static void reset() { + public static void resetFunctionTable() { getUniqueInstance().initializeFunctionTable(allBuiltIns); } - static void reset(Set> builtIns) { + static void resetFunctionTable(Set> builtIns) { getUniqueInstance().initializeFunctionTable(builtIns); } diff --git a/test/acceptance/fixture/LispInterpreterFixture.java b/test/acceptance/fixture/LispInterpreterFixture.java index e6fb6b5..421e913 100644 --- a/test/acceptance/fixture/LispInterpreterFixture.java +++ b/test/acceptance/fixture/LispInterpreterFixture.java @@ -1,9 +1,11 @@ package acceptance.fixture; +import static table.FunctionTable.resetFunctionTable; + import java.io.*; import interpreter.*; -import table.*; +import table.ExecutionContext; public class LispInterpreterFixture { @@ -22,7 +24,7 @@ public class LispInterpreterFixture { } public void reset() { - FunctionTable.reset(); + resetFunctionTable(); ExecutionContext.getInstance().clearContext(); } diff --git a/test/function/builtin/FUNCALLTester.java b/test/function/builtin/FUNCALLTester.java index 9a37d4c..33ae61b 100644 --- a/test/function/builtin/FUNCALLTester.java +++ b/test/function/builtin/FUNCALLTester.java @@ -1,22 +1,22 @@ package function.builtin; +import static table.FunctionTable.resetFunctionTable; import static testutil.TestUtilities.*; import org.junit.*; import function.ArgumentValidator.TooFewArgumentsException; -import table.FunctionTable; public class FUNCALLTester { @Before public void setUp() { - FunctionTable.reset(); + resetFunctionTable(); } @After public void tearDown() { - FunctionTable.reset(); + resetFunctionTable(); } @Test diff --git a/test/function/builtin/SYMBOL_FUNCTIONTester.java b/test/function/builtin/SYMBOL_FUNCTIONTester.java index ed3d6c4..6fd5cab 100644 --- a/test/function/builtin/SYMBOL_FUNCTIONTester.java +++ b/test/function/builtin/SYMBOL_FUNCTIONTester.java @@ -3,24 +3,24 @@ package function.builtin; import static error.ErrorManager.Severity.ERROR; import static org.junit.Assert.*; import static sexpression.Nil.NIL; +import static table.FunctionTable.resetFunctionTable; import static testutil.TestUtilities.evaluateString; import org.junit.*; import function.ArgumentValidator.*; import function.builtin.SYMBOL_FUNCTION.UndefinedSymbolFunctionException; -import table.FunctionTable; public class SYMBOL_FUNCTIONTester { @Before public void setUp() { - FunctionTable.reset(); + resetFunctionTable(); } @After public void tearDown() { - FunctionTable.reset(); + resetFunctionTable(); } @Test diff --git a/test/function/builtin/special/DEFINE_MACROTester.java b/test/function/builtin/special/DEFINE_MACROTester.java index d54abc1..82f5916 100644 --- a/test/function/builtin/special/DEFINE_MACROTester.java +++ b/test/function/builtin/special/DEFINE_MACROTester.java @@ -1,6 +1,7 @@ package function.builtin.special; import static org.junit.Assert.assertTrue; +import static table.FunctionTable.resetFunctionTable; import static testutil.TestUtilities.*; import java.io.*; @@ -11,7 +12,6 @@ import environment.RuntimeEnvironment; import error.ErrorManager; import function.ArgumentValidator.*; import function.UserDefinedFunction.IllegalKeywordRestPositionException; -import table.FunctionTable; public class DEFINE_MACROTester { @@ -34,12 +34,12 @@ public class DEFINE_MACROTester { environment.setErrorManager(new ErrorManager()); environment.setWarningOutputDecorator(s -> s); - FunctionTable.reset(); + resetFunctionTable(); } @After public void tearDown() { - FunctionTable.reset(); + resetFunctionTable(); } @Test diff --git a/test/function/builtin/special/DEFUNTester.java b/test/function/builtin/special/DEFUNTester.java index bf1a852..5808f59 100644 --- a/test/function/builtin/special/DEFUNTester.java +++ b/test/function/builtin/special/DEFUNTester.java @@ -1,6 +1,7 @@ package function.builtin.special; import static org.junit.Assert.assertTrue; +import static table.FunctionTable.resetFunctionTable; import static testutil.TestUtilities.*; import java.io.*; @@ -11,7 +12,6 @@ import environment.RuntimeEnvironment; import error.ErrorManager; import function.ArgumentValidator.*; import function.UserDefinedFunction.IllegalKeywordRestPositionException; -import table.FunctionTable; public class DEFUNTester { @@ -34,12 +34,12 @@ public class DEFUNTester { environment.setErrorManager(new ErrorManager()); environment.setWarningOutputDecorator(s -> s); - FunctionTable.reset(); + resetFunctionTable(); } @After public void tearDown() { - FunctionTable.reset(); + resetFunctionTable(); } @Test diff --git a/test/table/FunctionTableTester.java b/test/table/FunctionTableTester.java index 8293003..bb0d615 100644 --- a/test/table/FunctionTableTester.java +++ b/test/table/FunctionTableTester.java @@ -3,6 +3,7 @@ package table; import static org.junit.Assert.*; import static sexpression.Nil.NIL; import static sexpression.Symbol.T; +import static table.FunctionTable.*; import java.util.*; @@ -57,58 +58,58 @@ public class FunctionTableTester { @Before public void setUp() { - FunctionTable.reset(); + resetFunctionTable(); } @After public void tearDown() { - FunctionTable.reset(); + resetFunctionTable(); } @Test public void builtInFunctionIsDefined() { - assertTrue(FunctionTable.isAlreadyDefined("CONS")); + assertTrue(isAlreadyDefined("CONS")); } @Test public void undefinedFunctionIsNotDefined() { - assertFalse(FunctionTable.isAlreadyDefined("undefined")); + assertFalse(isAlreadyDefined("undefined")); } @Test public void lookupBuiltInFunction_ReturnsFunction() { - assertNotNull(FunctionTable.lookupFunction("CONS")); + assertNotNull(lookupFunction("CONS")); } @Test public void lookupUndefinedFunction_ReturnsNull() { - assertNull(FunctionTable.lookupFunction("undefined")); + assertNull(lookupFunction("undefined")); } @Test - public void defineFunction() { + public void defineFunctionWorks() { String functionName = "testFunction"; LispFunction testFunction = createLispFunction(); - assertNull(FunctionTable.lookupFunction(functionName)); - assertFalse(FunctionTable.isAlreadyDefined(functionName)); + assertNull(lookupFunction(functionName)); + assertFalse(isAlreadyDefined(functionName)); - FunctionTable.defineFunction(functionName, testFunction); + defineFunction(functionName, testFunction); - assertTrue(FunctionTable.isAlreadyDefined(functionName)); - assertEquals(testFunction, FunctionTable.lookupFunction(functionName)); + assertTrue(isAlreadyDefined(functionName)); + assertEquals(testFunction, lookupFunction(functionName)); } @Test - public void resetFunctionTable() { + public void resetFunctionTableWorks() { String functionName = "testFunction"; LispFunction testFunction = createLispFunction(); - FunctionTable.defineFunction(functionName, testFunction); + defineFunction(functionName, testFunction); - FunctionTable.reset(); + resetFunctionTable(); - assertFalse(FunctionTable.isAlreadyDefined(functionName)); - assertNull(FunctionTable.lookupFunction(functionName)); + assertFalse(isAlreadyDefined(functionName)); + assertNull(lookupFunction(functionName)); } @Test @@ -116,10 +117,10 @@ public class FunctionTableTester { Set> goodBuiltIns = new HashSet<>(); goodBuiltIns.add(GoodFunction.class); - FunctionTable.reset(goodBuiltIns); + resetFunctionTable(goodBuiltIns); - assertTrue(FunctionTable.isAlreadyDefined("GOOD")); - assertNotNull(FunctionTable.lookupFunction("GOOD")); + assertTrue(isAlreadyDefined("GOOD")); + assertNotNull(lookupFunction("GOOD")); } @Test(expected = LispFunctionInstantiationException.class) @@ -127,7 +128,7 @@ public class FunctionTableTester { Set> badBuiltIns = new HashSet<>(); badBuiltIns.add(BadFunction.class); - FunctionTable.reset(badBuiltIns); + resetFunctionTable(badBuiltIns); } @Test @@ -144,10 +145,10 @@ public class FunctionTableTester { Set> namelessBuiltins = new HashSet<>(); namelessBuiltins.add(UglyFunction.class); - FunctionTable.reset(namelessBuiltins); + resetFunctionTable(namelessBuiltins); - assertFalse(FunctionTable.isAlreadyDefined("UGLY")); - assertNull(FunctionTable.lookupFunction("UGLY")); + assertFalse(isAlreadyDefined("UGLY")); + assertNull(lookupFunction("UGLY")); } }