Remove SETF function
This commit is contained in:
parent
6cf45219f1
commit
87eb0204f5
|
@ -1,3 +1,4 @@
|
|||
|TranscendentalLisp.LexicalClosures||16:39:59 Tue, Mar 07, 2017|
|
||||
|FrontPage||10:19:45 Tue, Mar 07, 2017|
|
||||
|TranscendentalLisp||10:14:02 Tue, Mar 07, 2017|
|
||||
|LispInterpreter.MultipleMethodObject||16:50:19 Mon, Mar 06, 2017|
|
||||
|
|
|
@ -5,5 +5,5 @@ A simple lexical closure.
|
|||
|
||||
| script | lisp interpreter fixture |
|
||||
| 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 |
|
||||
|
|
|
@ -2,5 +2,5 @@
|
|||
(if (< n 1) nil
|
||||
(cons n (problem (- n 1)))))
|
||||
|
||||
(setf y (problem 20))
|
||||
(setf x (problem 20000))
|
||||
(setq y (problem 20))
|
||||
(setq x (problem 20000))
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
;; 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)))
|
||||
|
||||
;; Converts a single Roman numeral letter into its equivalent decimal value.
|
||||
|
|
|
@ -7,12 +7,12 @@ import static function.builtin.cons.LIST.makeList;
|
|||
import function.*;
|
||||
import sexpression.*;
|
||||
|
||||
@FunctionNames({ "SETF", "SETQ" })
|
||||
public class SETF extends LispSpecialFunction {
|
||||
@FunctionNames({ "SETQ" })
|
||||
public class SETQ extends LispSpecialFunction {
|
||||
|
||||
private ArgumentValidator argumentValidator;
|
||||
|
||||
public SETF(String name) {
|
||||
public SETQ(String name) {
|
||||
this.argumentValidator = new ArgumentValidator(name);
|
||||
this.argumentValidator.setExactNumberOfArguments(2);
|
||||
this.argumentValidator.setFirstArgumentExpectedType(Symbol.class);
|
|
@ -52,7 +52,7 @@ public class FunctionTable {
|
|||
allBuiltIns.add(QUOTE.class);
|
||||
allBuiltIns.add(REST.class);
|
||||
allBuiltIns.add(SET.class);
|
||||
allBuiltIns.add(SETF.class);
|
||||
allBuiltIns.add(SETQ.class);
|
||||
allBuiltIns.add(SYMBOL_FUNCTION.class);
|
||||
}
|
||||
|
||||
|
|
|
@ -74,8 +74,8 @@ public class EQTester {
|
|||
|
||||
@Test
|
||||
public void eqWithSameList() {
|
||||
String initializeL1 = "(setf l1 '(1 2 3))";
|
||||
String initializeL2 = "(setf l2 l1)";
|
||||
String initializeL1 = "(setq l1 '(1 2 3))";
|
||||
String initializeL2 = "(setq l2 l1)";
|
||||
String input = "(eq l1 l2)";
|
||||
|
||||
evaluateString(initializeL1);
|
||||
|
|
|
@ -71,7 +71,7 @@ public class ANDTester {
|
|||
|
||||
@Test(expected = UndefinedSymbolException.class)
|
||||
public void andShortCircuits() {
|
||||
String input = "(and nil (setf x 22))";
|
||||
String input = "(and nil (setq x 22))";
|
||||
|
||||
assertNil(evaluateString(input));
|
||||
evaluateString("x");
|
||||
|
|
|
@ -160,7 +160,7 @@ public class CASETester {
|
|||
|
||||
@Test
|
||||
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);
|
||||
|
||||
|
@ -177,10 +177,10 @@ public class CASETester {
|
|||
|
||||
@Test
|
||||
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("(setf one nil)");
|
||||
evaluateString("(setq zero nil)");
|
||||
evaluateString("(setq one nil)");
|
||||
evaluateString(input);
|
||||
|
||||
assertSExpressionsMatch(parseString("nil"), evaluateString("zero"));
|
||||
|
|
|
@ -80,7 +80,7 @@ public class DEFINE_SPECIALTester {
|
|||
|
||||
@Test
|
||||
public void defineSpecialVariableCapture() {
|
||||
evaluateString("(setf x 0)");
|
||||
evaluateString("(setq x 0)");
|
||||
evaluateString("(define-special f (x) (set x 23))");
|
||||
evaluateString("(f x)");
|
||||
assertSExpressionsMatch(parseString("0"), evaluateString("x"));
|
||||
|
@ -88,7 +88,7 @@ public class DEFINE_SPECIALTester {
|
|||
|
||||
@Test
|
||||
public void defineSpecialAvoidVariableCaptureConvention() {
|
||||
evaluateString("(setf x 0)");
|
||||
evaluateString("(setq x 0)");
|
||||
evaluateString("(define-special f (-x-) (set -x- 23))");
|
||||
evaluateString("(f x)");
|
||||
assertSExpressionsMatch(parseString("23"), evaluateString("x"));
|
||||
|
|
|
@ -79,8 +79,8 @@ public class DEFUNTester {
|
|||
|
||||
@Test
|
||||
public void defunSimpleClass() {
|
||||
evaluateString("(defun counter-class () (let ((counter 0)) (lambda () (setf counter (+ 1 counter)))))");
|
||||
evaluateString("(setf my-counter (counter-class))");
|
||||
evaluateString("(defun counter-class () (let ((counter 0)) (lambda () (setq counter (+ 1 counter)))))");
|
||||
evaluateString("(setq my-counter (counter-class))");
|
||||
|
||||
assertSExpressionsMatch(parseString("1"), evaluateString("(funcall my-counter)"));
|
||||
assertSExpressionsMatch(parseString("2"), evaluateString("(funcall my-counter)"));
|
||||
|
|
|
@ -64,7 +64,7 @@ public class IFTester {
|
|||
|
||||
@Test(expected = UndefinedSymbolException.class)
|
||||
public void ifWithNilCondition_DoesNotEvaluateThenForm() {
|
||||
String input = "(if nil (setf x 22))";
|
||||
String input = "(if nil (setq x 22))";
|
||||
|
||||
assertNil(evaluateString(input));
|
||||
evaluateString("x");
|
||||
|
@ -72,7 +72,7 @@ public class IFTester {
|
|||
|
||||
@Test(expected = UndefinedSymbolException.class)
|
||||
public void ifWithTrueCondition_DoesNotEvaluateElseForm() {
|
||||
String input = "(if t nil (setf x 22))";
|
||||
String input = "(if t nil (setq x 22))";
|
||||
|
||||
assertNil(evaluateString(input));
|
||||
evaluateString("x");
|
||||
|
|
|
@ -111,7 +111,7 @@ public class LAMBDATester {
|
|||
|
||||
@Test
|
||||
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("2"), evaluateString("(funcall increment-count)"));
|
||||
|
|
|
@ -51,7 +51,7 @@ public class LETTester {
|
|||
|
||||
@Test
|
||||
public void letWithSetf_DoesNotAlterGlobalVariable() {
|
||||
String before = "(setf x 22)";
|
||||
String before = "(setq x 22)";
|
||||
String input = "(let ((x 1)) x)";
|
||||
String after = "x";
|
||||
|
||||
|
@ -62,8 +62,8 @@ public class LETTester {
|
|||
|
||||
@Test
|
||||
public void letWithNestedSetf_DoesNotAlterGlobalVariable() {
|
||||
String before = "(setf x 22)";
|
||||
String input = "(let ((x 33)) (setf x 44) x)";
|
||||
String before = "(setq x 22)";
|
||||
String input = "(let ((x 33)) (setq x 44) x)";
|
||||
String after = "x";
|
||||
|
||||
assertSExpressionsMatch(new LispNumber("22"), evaluateString(before));
|
||||
|
@ -80,7 +80,7 @@ public class LETTester {
|
|||
|
||||
@Test
|
||||
public void nestedLetWithGlobals() {
|
||||
String before = "(setf x 92)";
|
||||
String before = "(setq x 92)";
|
||||
String input = "(let ((x 1)) (let ((y (+ 1 x))) y))";
|
||||
String after = "x";
|
||||
|
||||
|
@ -91,8 +91,8 @@ public class LETTester {
|
|||
|
||||
@Test
|
||||
public void alterGlobalVariableFromLet() {
|
||||
String before = "(setf x 1)";
|
||||
String input = "(let ((y 1)) (setf x 2))";
|
||||
String before = "(setq x 1)";
|
||||
String input = "(let ((y 1)) (setq x 2))";
|
||||
String after = "x";
|
||||
|
||||
assertSExpressionsMatch(new LispNumber("1"), evaluateString(before));
|
||||
|
@ -102,7 +102,7 @@ public class LETTester {
|
|||
|
||||
@Test
|
||||
public void accessGlobalVariableFromLet() {
|
||||
String before = "(setf x 1)";
|
||||
String before = "(setq x 1)";
|
||||
String input = "(let () x)";
|
||||
|
||||
assertSExpressionsMatch(new LispNumber("1"), evaluateString(before));
|
||||
|
|
|
@ -51,7 +51,7 @@ public class LET_STARTester {
|
|||
|
||||
@Test
|
||||
public void letStarWithSetf_DoesNotAlterGlobalVariable() {
|
||||
String before = "(setf x 22)";
|
||||
String before = "(setq x 22)";
|
||||
String input = "(let* ((x 1)) x)";
|
||||
String after = "x";
|
||||
|
||||
|
@ -62,8 +62,8 @@ public class LET_STARTester {
|
|||
|
||||
@Test
|
||||
public void letStarWithNestedSetf_DoesNotAlterGlobalVariable() {
|
||||
String before = "(setf x 22)";
|
||||
String input = "(let* ((x 33)) (setf x 44) x)";
|
||||
String before = "(setq x 22)";
|
||||
String input = "(let* ((x 33)) (setq x 44) x)";
|
||||
String after = "x";
|
||||
|
||||
assertSExpressionsMatch(new LispNumber("22"), evaluateString(before));
|
||||
|
@ -80,7 +80,7 @@ public class LET_STARTester {
|
|||
|
||||
@Test
|
||||
public void nestedLetWithGlobals() {
|
||||
String before = "(setf x 92)";
|
||||
String before = "(setq x 92)";
|
||||
String input = "(let* ((x 1)) (let* ((y (+ 1 x))) y))";
|
||||
String after = "x";
|
||||
|
||||
|
@ -91,8 +91,8 @@ public class LET_STARTester {
|
|||
|
||||
@Test
|
||||
public void alterGlobalVariableFromLet() {
|
||||
String before = "(setf x 1)";
|
||||
String input = "(let* ((y 1)) (setf x 2))";
|
||||
String before = "(setq x 1)";
|
||||
String input = "(let* ((y 1)) (setq x 2))";
|
||||
String after = "x";
|
||||
|
||||
assertSExpressionsMatch(new LispNumber("1"), evaluateString(before));
|
||||
|
@ -102,7 +102,7 @@ public class LET_STARTester {
|
|||
|
||||
@Test
|
||||
public void accessGlobalVariableFromLet() {
|
||||
String before = "(setf x 1)";
|
||||
String before = "(setq x 1)";
|
||||
String input = "(let* () x)";
|
||||
|
||||
assertSExpressionsMatch(new LispNumber("1"), evaluateString(before));
|
||||
|
|
|
@ -71,7 +71,7 @@ public class ORTester {
|
|||
|
||||
@Test(expected = UndefinedSymbolException.class)
|
||||
public void orShortCircuits() {
|
||||
String input = "(or t (setf x 22))";
|
||||
String input = "(or t (setq x 22))";
|
||||
|
||||
assertT(evaluateString(input));
|
||||
evaluateString("x");
|
||||
|
|
|
@ -10,11 +10,11 @@ import function.builtin.EVAL.UndefinedSymbolException;
|
|||
import sexpression.LispNumber;
|
||||
import table.*;
|
||||
|
||||
public class SETFTester {
|
||||
public class SETQTester {
|
||||
|
||||
private ExecutionContext executionContext;
|
||||
|
||||
public SETFTester() {
|
||||
public SETQTester() {
|
||||
this.executionContext = ExecutionContext.getInstance();
|
||||
}
|
||||
|
||||
|
@ -28,12 +28,6 @@ public class SETFTester {
|
|||
executionContext.clearContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setf() {
|
||||
evaluateString("(setf a 23)");
|
||||
assertSExpressionsMatch(new LispNumber("23"), evaluateString("a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setq() {
|
||||
evaluateString("(setq a 23)");
|
||||
|
@ -42,7 +36,7 @@ public class SETFTester {
|
|||
|
||||
@Test
|
||||
public void lookupDefinedSymbol() {
|
||||
evaluateString("(setf a 23)");
|
||||
evaluateString("(setq a 23)");
|
||||
assertSExpressionsMatch(new LispNumber("23"), executionContext.lookupSymbolValue("A"));
|
||||
}
|
||||
|
||||
|
@ -52,63 +46,63 @@ public class SETFTester {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void setfGlobalVariable() {
|
||||
evaluateString("(setf a 23)");
|
||||
public void setqGlobalVariable() {
|
||||
evaluateString("(setq a 23)");
|
||||
SymbolTable global = executionContext.getScope();
|
||||
executionContext.setScope(new SymbolTable(global));
|
||||
|
||||
evaluateString("(setf a 94)");
|
||||
evaluateString("(setq a 94)");
|
||||
executionContext.setScope(global);
|
||||
assertSExpressionsMatch(new LispNumber("94"), evaluateString("a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setfLocalVariable() {
|
||||
public void setqLocalVariable() {
|
||||
SymbolTable global = executionContext.getScope();
|
||||
SymbolTable local = new SymbolTable(global);
|
||||
local.put("A", new LispNumber("99"));
|
||||
executionContext.setScope(local);
|
||||
|
||||
evaluateString("(setf a 94)");
|
||||
evaluateString("(setq a 94)");
|
||||
assertSExpressionsMatch(new LispNumber("94"), evaluateString("a"));
|
||||
}
|
||||
|
||||
@Test(expected = UndefinedSymbolException.class)
|
||||
public void setfLocalVariableDefined_DoesNotSetGlobal() {
|
||||
public void setqLocalVariableDefined_DoesNotSetGlobal() {
|
||||
SymbolTable global = executionContext.getScope();
|
||||
SymbolTable local = new SymbolTable(global);
|
||||
local.put("A", new LispNumber("99"));
|
||||
executionContext.setScope(local);
|
||||
|
||||
evaluateString("(setf a 94)");
|
||||
evaluateString("(setq a 94)");
|
||||
executionContext.setScope(global);
|
||||
evaluateString("a");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setfLocalVariableUndefined_SetsGlobal() {
|
||||
public void setqLocalVariableUndefined_SetsGlobal() {
|
||||
SymbolTable global = executionContext.getScope();
|
||||
SymbolTable local = new SymbolTable(global);
|
||||
executionContext.setScope(local);
|
||||
|
||||
evaluateString("(setf a 94)");
|
||||
evaluateString("(setq a 94)");
|
||||
executionContext.setScope(global);
|
||||
assertSExpressionsMatch(new LispNumber("94"), evaluateString("a"));
|
||||
}
|
||||
|
||||
@Test(expected = BadArgumentTypeException.class)
|
||||
public void setfWithNonSymbol() {
|
||||
evaluateString("(setf 1 2)");
|
||||
public void setqWithNonSymbol() {
|
||||
evaluateString("(setq 1 2)");
|
||||
}
|
||||
|
||||
@Test(expected = TooFewArgumentsException.class)
|
||||
public void setfWithTooFewArguments() {
|
||||
evaluateString("(setf x)");
|
||||
public void setqWithTooFewArguments() {
|
||||
evaluateString("(setq x)");
|
||||
}
|
||||
|
||||
@Test(expected = TooManyArgumentsException.class)
|
||||
public void setfWithTooManyArguments() {
|
||||
evaluateString("(setf a b c)");
|
||||
public void setqWithTooManyArguments() {
|
||||
evaluateString("(setq a b c)");
|
||||
}
|
||||
|
||||
}
|
|
@ -150,7 +150,7 @@ public class LispParserTester {
|
|||
|
||||
@Test
|
||||
public void givenMultipleExpressions_CreatesCorrectSExpressions() {
|
||||
String input = "(setf x 2) x \"hi\" () 29";
|
||||
String input = "(setq x 2) x \"hi\" () 29";
|
||||
LispParser parser = createLispParser(input);
|
||||
|
||||
assertList(parser.getNextSExpression());
|
||||
|
|
Loading…
Reference in New Issue