Remove SETF function

This commit is contained in:
Mike Cifelli 2017-03-07 16:41:26 -05:00
parent 6cf45219f1
commit 87eb0204f5
18 changed files with 57 additions and 62 deletions

View File

@ -1,3 +1,4 @@
|TranscendentalLisp.LexicalClosures||16:39:59 Tue, Mar 07, 2017|
|FrontPage||10:19:45 Tue, Mar 07, 2017| |FrontPage||10:19:45 Tue, Mar 07, 2017|
|TranscendentalLisp||10:14:02 Tue, Mar 07, 2017| |TranscendentalLisp||10:14:02 Tue, Mar 07, 2017|
|LispInterpreter.MultipleMethodObject||16:50:19 Mon, Mar 06, 2017| |LispInterpreter.MultipleMethodObject||16:50:19 Mon, Mar 06, 2017|

View File

@ -5,5 +5,5 @@ A simple lexical closure.
| script | lisp interpreter fixture | | script | lisp interpreter fixture |
| show | evaluate text | (defun adder-x (x) (lambda (y) (+ x y))) | | show | evaluate text | (defun adder-x (x) (lambda (y) (+ x y))) |
| show | evaluate text | (setf adder-20 (adder-x 20)) | | show | evaluate text | (setq adder-20 (adder-x 20)) |
| check | evaluate text | (funcall adder-20 2) | 22 | | check | evaluate text | (funcall adder-20 2) | 22 |

View File

@ -2,5 +2,5 @@
(if (< n 1) nil (if (< n 1) nil
(cons n (problem (- n 1))))) (cons n (problem (- n 1)))))
(setf y (problem 20)) (setq y (problem 20))
(setf x (problem 20000)) (setq x (problem 20000))

View File

@ -1,5 +1,5 @@
;; A list containing the values of single-letter Roman numerals. ;; A list containing the values of single-letter Roman numerals.
(setf roman-number-list (setq roman-number-list
'((I 1) (V 5) (X 10) (L 50) (C 100) (D 500) (M 1000))) '((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. ;; Converts a single Roman numeral letter into its equivalent decimal value.

View File

@ -7,12 +7,12 @@ import static function.builtin.cons.LIST.makeList;
import function.*; import function.*;
import sexpression.*; import sexpression.*;
@FunctionNames({ "SETF", "SETQ" }) @FunctionNames({ "SETQ" })
public class SETF extends LispSpecialFunction { public class SETQ extends LispSpecialFunction {
private ArgumentValidator argumentValidator; private ArgumentValidator argumentValidator;
public SETF(String name) { public SETQ(String name) {
this.argumentValidator = new ArgumentValidator(name); this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(2); this.argumentValidator.setExactNumberOfArguments(2);
this.argumentValidator.setFirstArgumentExpectedType(Symbol.class); this.argumentValidator.setFirstArgumentExpectedType(Symbol.class);

View File

@ -52,7 +52,7 @@ public class FunctionTable {
allBuiltIns.add(QUOTE.class); allBuiltIns.add(QUOTE.class);
allBuiltIns.add(REST.class); allBuiltIns.add(REST.class);
allBuiltIns.add(SET.class); allBuiltIns.add(SET.class);
allBuiltIns.add(SETF.class); allBuiltIns.add(SETQ.class);
allBuiltIns.add(SYMBOL_FUNCTION.class); allBuiltIns.add(SYMBOL_FUNCTION.class);
} }

View File

@ -74,8 +74,8 @@ public class EQTester {
@Test @Test
public void eqWithSameList() { public void eqWithSameList() {
String initializeL1 = "(setf l1 '(1 2 3))"; String initializeL1 = "(setq l1 '(1 2 3))";
String initializeL2 = "(setf l2 l1)"; String initializeL2 = "(setq l2 l1)";
String input = "(eq l1 l2)"; String input = "(eq l1 l2)";
evaluateString(initializeL1); evaluateString(initializeL1);

View File

@ -71,7 +71,7 @@ public class ANDTester {
@Test(expected = UndefinedSymbolException.class) @Test(expected = UndefinedSymbolException.class)
public void andShortCircuits() { public void andShortCircuits() {
String input = "(and nil (setf x 22))"; String input = "(and nil (setq x 22))";
assertNil(evaluateString(input)); assertNil(evaluateString(input));
evaluateString("x"); evaluateString("x");

View File

@ -160,7 +160,7 @@ public class CASETester {
@Test @Test
public void caseEvaluatesMultipleConsequents() { public void caseEvaluatesMultipleConsequents() {
String input = "(case 2 (1 1) (2 (setf x 'x) (setf y 'y)) (3 3)))"; String input = "(case 2 (1 1) (2 (setq x 'x) (setq y 'y)) (3 3)))";
evaluateString(input); evaluateString(input);
@ -177,10 +177,10 @@ public class CASETester {
@Test @Test
public void caseOnlyEvaluatesConsequentInFirstMatchingClause() { public void caseOnlyEvaluatesConsequentInFirstMatchingClause() {
String input = "(case 2 ((0) (setf zero 0)) ((1) (setf one 1)) ((2) (setf two '2)) ((2) (setf two 'two)))"; String input = "(case 2 ((0) (setq zero 0)) ((1) (setq one 1)) ((2) (setq two '2)) ((2) (setq two 'two)))";
evaluateString("(setf zero nil)"); evaluateString("(setq zero nil)");
evaluateString("(setf one nil)"); evaluateString("(setq one nil)");
evaluateString(input); evaluateString(input);
assertSExpressionsMatch(parseString("nil"), evaluateString("zero")); assertSExpressionsMatch(parseString("nil"), evaluateString("zero"));

View File

@ -80,7 +80,7 @@ public class DEFINE_SPECIALTester {
@Test @Test
public void defineSpecialVariableCapture() { public void defineSpecialVariableCapture() {
evaluateString("(setf x 0)"); evaluateString("(setq x 0)");
evaluateString("(define-special f (x) (set x 23))"); evaluateString("(define-special f (x) (set x 23))");
evaluateString("(f x)"); evaluateString("(f x)");
assertSExpressionsMatch(parseString("0"), evaluateString("x")); assertSExpressionsMatch(parseString("0"), evaluateString("x"));
@ -88,7 +88,7 @@ public class DEFINE_SPECIALTester {
@Test @Test
public void defineSpecialAvoidVariableCaptureConvention() { public void defineSpecialAvoidVariableCaptureConvention() {
evaluateString("(setf x 0)"); evaluateString("(setq x 0)");
evaluateString("(define-special f (-x-) (set -x- 23))"); evaluateString("(define-special f (-x-) (set -x- 23))");
evaluateString("(f x)"); evaluateString("(f x)");
assertSExpressionsMatch(parseString("23"), evaluateString("x")); assertSExpressionsMatch(parseString("23"), evaluateString("x"));

View File

@ -79,8 +79,8 @@ public class DEFUNTester {
@Test @Test
public void defunSimpleClass() { public void defunSimpleClass() {
evaluateString("(defun counter-class () (let ((counter 0)) (lambda () (setf counter (+ 1 counter)))))"); evaluateString("(defun counter-class () (let ((counter 0)) (lambda () (setq counter (+ 1 counter)))))");
evaluateString("(setf my-counter (counter-class))"); evaluateString("(setq my-counter (counter-class))");
assertSExpressionsMatch(parseString("1"), evaluateString("(funcall my-counter)")); assertSExpressionsMatch(parseString("1"), evaluateString("(funcall my-counter)"));
assertSExpressionsMatch(parseString("2"), evaluateString("(funcall my-counter)")); assertSExpressionsMatch(parseString("2"), evaluateString("(funcall my-counter)"));

View File

@ -64,7 +64,7 @@ public class IFTester {
@Test(expected = UndefinedSymbolException.class) @Test(expected = UndefinedSymbolException.class)
public void ifWithNilCondition_DoesNotEvaluateThenForm() { public void ifWithNilCondition_DoesNotEvaluateThenForm() {
String input = "(if nil (setf x 22))"; String input = "(if nil (setq x 22))";
assertNil(evaluateString(input)); assertNil(evaluateString(input));
evaluateString("x"); evaluateString("x");
@ -72,7 +72,7 @@ public class IFTester {
@Test(expected = UndefinedSymbolException.class) @Test(expected = UndefinedSymbolException.class)
public void ifWithTrueCondition_DoesNotEvaluateElseForm() { public void ifWithTrueCondition_DoesNotEvaluateElseForm() {
String input = "(if t nil (setf x 22))"; String input = "(if t nil (setq x 22))";
assertNil(evaluateString(input)); assertNil(evaluateString(input));
evaluateString("x"); evaluateString("x");

View File

@ -111,7 +111,7 @@ public class LAMBDATester {
@Test @Test
public void lexicalClosure() { public void lexicalClosure() {
evaluateString("(setf increment-count (let ((counter 0)) (lambda () (setf counter (+ 1 counter)))))"); evaluateString("(setq increment-count (let ((counter 0)) (lambda () (setq counter (+ 1 counter)))))");
assertSExpressionsMatch(parseString("1"), evaluateString("(funcall increment-count)")); assertSExpressionsMatch(parseString("1"), evaluateString("(funcall increment-count)"));
assertSExpressionsMatch(parseString("2"), evaluateString("(funcall increment-count)")); assertSExpressionsMatch(parseString("2"), evaluateString("(funcall increment-count)"));

View File

@ -51,7 +51,7 @@ public class LETTester {
@Test @Test
public void letWithSetf_DoesNotAlterGlobalVariable() { public void letWithSetf_DoesNotAlterGlobalVariable() {
String before = "(setf x 22)"; String before = "(setq x 22)";
String input = "(let ((x 1)) x)"; String input = "(let ((x 1)) x)";
String after = "x"; String after = "x";
@ -62,8 +62,8 @@ public class LETTester {
@Test @Test
public void letWithNestedSetf_DoesNotAlterGlobalVariable() { public void letWithNestedSetf_DoesNotAlterGlobalVariable() {
String before = "(setf x 22)"; String before = "(setq x 22)";
String input = "(let ((x 33)) (setf x 44) x)"; String input = "(let ((x 33)) (setq x 44) x)";
String after = "x"; String after = "x";
assertSExpressionsMatch(new LispNumber("22"), evaluateString(before)); assertSExpressionsMatch(new LispNumber("22"), evaluateString(before));
@ -80,7 +80,7 @@ public class LETTester {
@Test @Test
public void nestedLetWithGlobals() { public void nestedLetWithGlobals() {
String before = "(setf x 92)"; String before = "(setq x 92)";
String input = "(let ((x 1)) (let ((y (+ 1 x))) y))"; String input = "(let ((x 1)) (let ((y (+ 1 x))) y))";
String after = "x"; String after = "x";
@ -91,8 +91,8 @@ public class LETTester {
@Test @Test
public void alterGlobalVariableFromLet() { public void alterGlobalVariableFromLet() {
String before = "(setf x 1)"; String before = "(setq x 1)";
String input = "(let ((y 1)) (setf x 2))"; String input = "(let ((y 1)) (setq x 2))";
String after = "x"; String after = "x";
assertSExpressionsMatch(new LispNumber("1"), evaluateString(before)); assertSExpressionsMatch(new LispNumber("1"), evaluateString(before));
@ -102,7 +102,7 @@ public class LETTester {
@Test @Test
public void accessGlobalVariableFromLet() { public void accessGlobalVariableFromLet() {
String before = "(setf x 1)"; String before = "(setq x 1)";
String input = "(let () x)"; String input = "(let () x)";
assertSExpressionsMatch(new LispNumber("1"), evaluateString(before)); assertSExpressionsMatch(new LispNumber("1"), evaluateString(before));

View File

@ -51,7 +51,7 @@ public class LET_STARTester {
@Test @Test
public void letStarWithSetf_DoesNotAlterGlobalVariable() { public void letStarWithSetf_DoesNotAlterGlobalVariable() {
String before = "(setf x 22)"; String before = "(setq x 22)";
String input = "(let* ((x 1)) x)"; String input = "(let* ((x 1)) x)";
String after = "x"; String after = "x";
@ -62,8 +62,8 @@ public class LET_STARTester {
@Test @Test
public void letStarWithNestedSetf_DoesNotAlterGlobalVariable() { public void letStarWithNestedSetf_DoesNotAlterGlobalVariable() {
String before = "(setf x 22)"; String before = "(setq x 22)";
String input = "(let* ((x 33)) (setf x 44) x)"; String input = "(let* ((x 33)) (setq x 44) x)";
String after = "x"; String after = "x";
assertSExpressionsMatch(new LispNumber("22"), evaluateString(before)); assertSExpressionsMatch(new LispNumber("22"), evaluateString(before));
@ -80,7 +80,7 @@ public class LET_STARTester {
@Test @Test
public void nestedLetWithGlobals() { public void nestedLetWithGlobals() {
String before = "(setf x 92)"; String before = "(setq x 92)";
String input = "(let* ((x 1)) (let* ((y (+ 1 x))) y))"; String input = "(let* ((x 1)) (let* ((y (+ 1 x))) y))";
String after = "x"; String after = "x";
@ -91,8 +91,8 @@ public class LET_STARTester {
@Test @Test
public void alterGlobalVariableFromLet() { public void alterGlobalVariableFromLet() {
String before = "(setf x 1)"; String before = "(setq x 1)";
String input = "(let* ((y 1)) (setf x 2))"; String input = "(let* ((y 1)) (setq x 2))";
String after = "x"; String after = "x";
assertSExpressionsMatch(new LispNumber("1"), evaluateString(before)); assertSExpressionsMatch(new LispNumber("1"), evaluateString(before));
@ -102,7 +102,7 @@ public class LET_STARTester {
@Test @Test
public void accessGlobalVariableFromLet() { public void accessGlobalVariableFromLet() {
String before = "(setf x 1)"; String before = "(setq x 1)";
String input = "(let* () x)"; String input = "(let* () x)";
assertSExpressionsMatch(new LispNumber("1"), evaluateString(before)); assertSExpressionsMatch(new LispNumber("1"), evaluateString(before));

View File

@ -71,7 +71,7 @@ public class ORTester {
@Test(expected = UndefinedSymbolException.class) @Test(expected = UndefinedSymbolException.class)
public void orShortCircuits() { public void orShortCircuits() {
String input = "(or t (setf x 22))"; String input = "(or t (setq x 22))";
assertT(evaluateString(input)); assertT(evaluateString(input));
evaluateString("x"); evaluateString("x");

View File

@ -10,11 +10,11 @@ import function.builtin.EVAL.UndefinedSymbolException;
import sexpression.LispNumber; import sexpression.LispNumber;
import table.*; import table.*;
public class SETFTester { public class SETQTester {
private ExecutionContext executionContext; private ExecutionContext executionContext;
public SETFTester() { public SETQTester() {
this.executionContext = ExecutionContext.getInstance(); this.executionContext = ExecutionContext.getInstance();
} }
@ -28,12 +28,6 @@ public class SETFTester {
executionContext.clearContext(); executionContext.clearContext();
} }
@Test
public void setf() {
evaluateString("(setf a 23)");
assertSExpressionsMatch(new LispNumber("23"), evaluateString("a"));
}
@Test @Test
public void setq() { public void setq() {
evaluateString("(setq a 23)"); evaluateString("(setq a 23)");
@ -42,7 +36,7 @@ public class SETFTester {
@Test @Test
public void lookupDefinedSymbol() { public void lookupDefinedSymbol() {
evaluateString("(setf a 23)"); evaluateString("(setq a 23)");
assertSExpressionsMatch(new LispNumber("23"), executionContext.lookupSymbolValue("A")); assertSExpressionsMatch(new LispNumber("23"), executionContext.lookupSymbolValue("A"));
} }
@ -52,63 +46,63 @@ public class SETFTester {
} }
@Test @Test
public void setfGlobalVariable() { public void setqGlobalVariable() {
evaluateString("(setf a 23)"); evaluateString("(setq a 23)");
SymbolTable global = executionContext.getScope(); SymbolTable global = executionContext.getScope();
executionContext.setScope(new SymbolTable(global)); executionContext.setScope(new SymbolTable(global));
evaluateString("(setf a 94)"); evaluateString("(setq a 94)");
executionContext.setScope(global); executionContext.setScope(global);
assertSExpressionsMatch(new LispNumber("94"), evaluateString("a")); assertSExpressionsMatch(new LispNumber("94"), evaluateString("a"));
} }
@Test @Test
public void setfLocalVariable() { public void setqLocalVariable() {
SymbolTable global = executionContext.getScope(); SymbolTable global = executionContext.getScope();
SymbolTable local = new SymbolTable(global); SymbolTable local = new SymbolTable(global);
local.put("A", new LispNumber("99")); local.put("A", new LispNumber("99"));
executionContext.setScope(local); executionContext.setScope(local);
evaluateString("(setf a 94)"); evaluateString("(setq a 94)");
assertSExpressionsMatch(new LispNumber("94"), evaluateString("a")); assertSExpressionsMatch(new LispNumber("94"), evaluateString("a"));
} }
@Test(expected = UndefinedSymbolException.class) @Test(expected = UndefinedSymbolException.class)
public void setfLocalVariableDefined_DoesNotSetGlobal() { public void setqLocalVariableDefined_DoesNotSetGlobal() {
SymbolTable global = executionContext.getScope(); SymbolTable global = executionContext.getScope();
SymbolTable local = new SymbolTable(global); SymbolTable local = new SymbolTable(global);
local.put("A", new LispNumber("99")); local.put("A", new LispNumber("99"));
executionContext.setScope(local); executionContext.setScope(local);
evaluateString("(setf a 94)"); evaluateString("(setq a 94)");
executionContext.setScope(global); executionContext.setScope(global);
evaluateString("a"); evaluateString("a");
} }
@Test @Test
public void setfLocalVariableUndefined_SetsGlobal() { public void setqLocalVariableUndefined_SetsGlobal() {
SymbolTable global = executionContext.getScope(); SymbolTable global = executionContext.getScope();
SymbolTable local = new SymbolTable(global); SymbolTable local = new SymbolTable(global);
executionContext.setScope(local); executionContext.setScope(local);
evaluateString("(setf a 94)"); evaluateString("(setq a 94)");
executionContext.setScope(global); executionContext.setScope(global);
assertSExpressionsMatch(new LispNumber("94"), evaluateString("a")); assertSExpressionsMatch(new LispNumber("94"), evaluateString("a"));
} }
@Test(expected = BadArgumentTypeException.class) @Test(expected = BadArgumentTypeException.class)
public void setfWithNonSymbol() { public void setqWithNonSymbol() {
evaluateString("(setf 1 2)"); evaluateString("(setq 1 2)");
} }
@Test(expected = TooFewArgumentsException.class) @Test(expected = TooFewArgumentsException.class)
public void setfWithTooFewArguments() { public void setqWithTooFewArguments() {
evaluateString("(setf x)"); evaluateString("(setq x)");
} }
@Test(expected = TooManyArgumentsException.class) @Test(expected = TooManyArgumentsException.class)
public void setfWithTooManyArguments() { public void setqWithTooManyArguments() {
evaluateString("(setf a b c)"); evaluateString("(setq a b c)");
} }
} }

View File

@ -150,7 +150,7 @@ public class LispParserTester {
@Test @Test
public void givenMultipleExpressions_CreatesCorrectSExpressions() { public void givenMultipleExpressions_CreatesCorrectSExpressions() {
String input = "(setf x 2) x \"hi\" () 29"; String input = "(setq x 2) x \"hi\" () 29";
LispParser parser = createLispParser(input); LispParser parser = createLispParser(input);
assertList(parser.getNextSExpression()); assertList(parser.getNextSExpression());