Join

join函数是我们需要定义的最后一个函数。

它需要多个参数,其结构看起来更像先前定义的builtin_op。首先确保所有的参数都是Q-表达式,然后将它们拼接起来。所以我们需要定义lval_join函数,它将y中元素依次弹出并添加进x中,然后将y删除,返回x

  1. lval* builtin_join(lval* a) {
  2. for (int i = 0; i < a->count; i++) {
  3. LASSERT(a, a->cell[i]->type == LVAL_QEXPR,
  4. "Function 'join' passed incorrect type.");
  5. }
  6. lval* x = lval_pop(a, 0);
  7. while (a->count) {
  8. x = lval_join(x, lval_pop(a, 0));
  9. }
  10. lval_del(a);
  11. return x;
  12. }
  13. lval* lval_join(lval* x, lval* y) {
  14. /* For each cell in 'y' add it to 'x' */
  15. while (y->count) {
  16. x = lval_add(x, lval_pop(y, 0));
  17. }
  18. /* Delete the empty 'y' and return 'x' */
  19. lval_del(y);
  20. return x;
  21. }