|
@@ -346,6 +346,9 @@ lval* builtin(lval* a, char* func) {
|
346
|
346
|
if (strcmp("tail", func) == 0) {return builtin_tail(a);}
|
347
|
347
|
if (strcmp("join", func) == 0) {return builtin_join(a);}
|
348
|
348
|
if (strcmp("eval", func) == 0) {return builtin_eval(a);}
|
|
349
|
+ if (strcmp("cons", func) == 0) {return builtin_cons(a);}
|
|
350
|
+ if (strcmp("len", func) == 0) {return builtin_len(a);}
|
|
351
|
+ if (strcmp("init", func) == 0) {return builtin_init(a);}
|
349
|
352
|
|
350
|
353
|
/* Check for operators */
|
351
|
354
|
if (strstr("+-*/%^", func) ||
|
|
@@ -542,6 +545,69 @@ lval* lval_join(lval* x, lval* y) {
|
542
|
545
|
}
|
543
|
546
|
|
544
|
547
|
|
|
548
|
+lval* builtin_cons(lval* a) {
|
|
549
|
+
|
|
550
|
+ /* Check error conditions */
|
|
551
|
+ LASSERT(a, a->count == 2 ,
|
|
552
|
+ "Function 'cons' takes exactly 2 arguments!");
|
|
553
|
+
|
|
554
|
+ LASSERT(a, a->cell[1]->type == LVAL_QEXPR,
|
|
555
|
+ "Function 'cons' passed incorrect type!");
|
|
556
|
+
|
|
557
|
+ lval* v = lval_qexpr();
|
|
558
|
+ v = lval_add(v, lval_pop(a, 0));
|
|
559
|
+
|
|
560
|
+ lval* y = lval_pop(a, 0);
|
|
561
|
+
|
|
562
|
+ while (y->count) {
|
|
563
|
+ v = lval_add(v, lval_pop(y, 0));
|
|
564
|
+ }
|
|
565
|
+
|
|
566
|
+ lval_del(a);
|
|
567
|
+ lval_del(y);
|
|
568
|
+ return v;
|
|
569
|
+
|
|
570
|
+}
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+lval* builtin_len(lval* a) {
|
|
574
|
+
|
|
575
|
+ /* Check error conditions */
|
|
576
|
+ LASSERT(a, a->count == 1,
|
|
577
|
+ "Function 'len' passed too many arguments!");
|
|
578
|
+
|
|
579
|
+ LASSERT(a, a->cell[0]->type == LVAL_QEXPR,
|
|
580
|
+ "Function 'len' passed incorrect type!");
|
|
581
|
+
|
|
582
|
+ lval* v = lval_num(a->cell[0]->count);
|
|
583
|
+ return v;
|
|
584
|
+
|
|
585
|
+}
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+lval* builtin_init(lval* a) {
|
|
589
|
+
|
|
590
|
+ /* Check error conditions */
|
|
591
|
+ LASSERT(a, a->count == 1,
|
|
592
|
+ "Function 'init' passed too many arguments!");
|
|
593
|
+
|
|
594
|
+ LASSERT(a, a->cell[0]->type == LVAL_QEXPR,
|
|
595
|
+ "Function 'init' passed incorrect type!");
|
|
596
|
+
|
|
597
|
+ LASSERT(a, a->cell[0]->count != 0,
|
|
598
|
+ "Function 'init' passed {}!");
|
|
599
|
+
|
|
600
|
+ /* Otherwise take first argument */
|
|
601
|
+ lval* v = lval_take(a, 0);
|
|
602
|
+
|
|
603
|
+ /* Delete last element and return */
|
|
604
|
+ lval_del(lval_pop(v, v->count-1));
|
|
605
|
+
|
|
606
|
+ return v;
|
|
607
|
+
|
|
608
|
+}
|
|
609
|
+
|
|
610
|
+
|
545
|
611
|
long min(long x, long y) {
|
546
|
612
|
if (x <= y) {
|
547
|
613
|
return x;
|