Commits

Armin Rigo  committed 9f744d6

pair?, cons

  • Participants
  • Parent commits 723c1bf

Comments (0)

Files changed (2)

     return res;
 }
 
+DuObject *du_cons(DuObject *cons, DuObject *locals)
+{
+    if (cons == Du_None || _DuCons_NEXT(cons) == Du_None ||
+        _DuCons_NEXT(_DuCons_NEXT(cons)) != Du_None)
+        Du_FatalError("cons: expected two arguments");
+
+    DuObject *obj1 = Du_Eval(_DuCons_CAR(cons), locals);
+    DuObject *obj2 = Du_Eval(_DuCons_CAR(_DuCons_NEXT(cons)), locals);
+    DuObject *res = DuCons_New(obj1, obj2);
+    Du_DECREF(obj1);
+    Du_DECREF(obj2);
+    return res;
+}
+
 DuObject *du_not(DuObject *cons, DuObject *locals)
 {
     if (cons == Du_None || _DuCons_NEXT(cons) != Du_None)
     return DuInt_FromInt(res != NULL);
 }
 
+DuObject *du_pair(DuObject *cons, DuObject *locals)
+{
+    if (cons == Du_None || _DuCons_NEXT(cons) != Du_None)
+        Du_FatalError("pair?: expected one argument");
+    DuObject *obj = Du_Eval(_DuCons_CAR(cons), locals);
+    int res = DuCons_Check(obj);
+    Du_DECREF(obj);
+    return DuInt_FromInt(res != 0);
+}
+
 DuObject *du_assert(DuObject *cons, DuObject *locals)
 {
     if (cons == Du_None || _DuCons_NEXT(cons) != Du_None)
     DuFrame_SetBuiltinMacro(Du_Globals, "defun", du_defun);
     DuFrame_SetBuiltinMacro(Du_Globals, "car", du_car);
     DuFrame_SetBuiltinMacro(Du_Globals, "cdr", du_cdr);
+    DuFrame_SetBuiltinMacro(Du_Globals, "cons", du_cons);
     DuFrame_SetBuiltinMacro(Du_Globals, "not", du_not);
     DuFrame_SetBuiltinMacro(Du_Globals, "transaction", du_transaction);
     DuFrame_SetBuiltinMacro(Du_Globals, "sleepms", du_sleepms);
     DuFrame_SetBuiltinMacro(Du_Globals, "defined?", du_defined);
+    DuFrame_SetBuiltinMacro(Du_Globals, "pair?", du_pair);
     DuFrame_SetBuiltinMacro(Du_Globals, "assert", du_assert);
     DuFrame_SetSymbolStr(Du_Globals, "None", Du_None);
 }

File test/test_cons.py

     assert run("(print ())") == "None\n"
     assert run("(print None)") == "None\n"
     assert run("(print (quote (1 2 3)))") == "( 1 2 3 )\n"
+    assert run("(print (cons 1 2))") == "( 1 . 2 )\n"
+
+def test_pair():
+    assert run("(print (pair? 1))") == "0\n"
+    assert run("(print (pair? (cons 1 2)))") == "1\n"
+    assert run("(setq x (cons 1 2)) (print (pair? x))") == "1\n"
+    assert run("(setq x 42) (print (pair? x))") == "0\n"
 
 def test_car_cdr():
     assert run("(print (car (quote (2 3))))") == "2\n"