pibuiltin.h
Go to the documentation of this file.
1 
8 #ifndef PIBUILTIN_H
9 #define PIBUILTIN_H
10 #include "picell.h"
11 #include "pichecks.h"
12 #include "pilisp.h"
13 #include "pistack.h"
14 #include "piutils.h"
15 #include <math.h>
16 #include <stdbool.h>
17 #include <stdlib.h>
18 #include <time.h>
19 
20 // ==================== Basic inline apply ====================
21 // not usable in the interpreter: no checks! => use only in the eval
22 #if INLINE_FUNCTIONS
23 inline cell *caar(cell *c) { return c->car->car; }
24 inline cell *cddr(cell *c) { return c->cdr->cdr; }
25 inline cell *cadr(cell *c) { return c->cdr->car; }
26 inline cell *cdar(cell *c) { return c->car->cdr; }
27 inline cell *cadar(cell *c) { return c->car->cdr->car; }
28 inline cell *caddr(cell *c) { return c->cdr->cdr->car; }
29 #else
30 cell *caar(cell *c);
31 cell *cddr(cell *c);
32 cell *cadr(cell *c);
33 cell *cdar(cell *c);
34 cell *cadar(cell *c);
35 cell *caddr(cell *c);
36 #endif
37 
38 // ==================== Basic apply ====================
39 // differences from the first basic block: these functions can be called from
40 // the apply, because they do cell_remove and cell_push and check for args error
41 cell *builtin_car(cell *args);
42 cell *builtin_cdr(cell *args);
43 cell *builtin_cons(cell *args);
44 cell *builtin_atom(cell *args);
45 cell *builtin_eq(cell *args);
46 
47 // ==================== Arithmetic ====================
48 cell *addition(cell *numbers);
49 cell *subtraction(cell *numbers);
52 cell *multiplication(cell *numbers);
53 cell *division(cell *numbers);
54 
55 // ==================== Logic ====================
56 cell * or (cell * operands);
57 cell * and (cell * operands);
58 cell * not(cell * operands);
59 
60 // ==================== Comparison ====================
61 cell *greater(cell *operands);
62 cell *compare_greater_numbers(cell *first_num, cell *second_num);
63 cell *compare_greater_strings(cell *first_str, cell *second_str);
64 
65 cell *greater_eq(cell *operands);
66 cell *compare_greater_eq_numbers(cell *first_num, cell *second_num);
67 cell *compare_greater_eq_strings(cell *first_str, cell *second_str);
68 
69 cell *less(cell *operands);
70 cell *compare_less_numbers(cell *first_num, cell *second_num);
71 cell *compare_less_strings(cell *first_str, cell *second_str);
72 
73 cell *less_eq(cell *operands);
74 cell *compare_less_eq_numbers(cell *first_num, cell *second_num);
75 cell *compare_less_eq_strings(cell *first_str, cell *second_str);
76 
77 cell *integerp(cell *arg);
78 cell *symbolp(cell *arg);
79 
80 // ==================== Lists ====================
81 cell *list(cell *args);
82 cell *reverse(cell *args);
83 cell *member(cell *args);
84 cell *nth(cell *args);
85 cell *concatenate(cell *args); // ! works only with strings
86 cell *append(cell *args);
87 
88 cell *length(cell *args);
89 cell *length_string(cell *string);
91 
92 cell *subseq(cell *args); // ! works only with strings
93 cell *subseq_one_index(cell *args, int start_index);
94 cell *subseq_two_indices(cell *args, int start_index);
95 
96 // ==================== Utility ====================
97 cell *set(cell *args);
99 cell *set_add_new_value(cell *args, cell *prec);
100 
101 cell *write(cell *arg);
102 cell *load(cell *arg, cell *env);
103 cell *bye(cell *arg);
104 
105 // ==================== Macros ====================
106 cell *quote(cell *args, cell *env);
107 cell *cond(cell *arg, cell *env);
108 cell *setq(cell *args, cell *env);
109 cell *defun(cell *args, cell *env);
110 cell *let(cell *args, cell *env);
111 cell *dotimes(cell *arg, cell *env);
112 cell *map(cell *args, cell *env);
113 cell *timer(cell *arg, cell *env);
114 
115 // ==================== Pilisp special functions ====================
116 cell *compile(cell *c, cell *env); // ! the compiler needs to be loaded
117 bool should_be_compiled(cell *to_compilate);
118 cell *asm_call(cell *args, cell *env);
119 cell *mem_dump(cell *arg);
120 cell *env(cell *arg);
122 
123 // ==================== Basic Lisp functions ====================
124 // works also on lists: eq does not, but 'it's slower
125 bool total_eq(cell *c1, cell *c2);
126 
127 #if !INLINE_FUNCTIONS
128 
129 cell *car(cell *c);
130 cell *cdr(cell *c);
131 cell *cons(cell *car, cell *cdr);
132 int atom(cell *c);
133 bool eq(cell *v1, cell *v2);
134 
135 #else
136 
137 inline cell *cons(cell *car, cell *cdr) { return mk_cons(car, cdr); }
138 
139 inline int atom(cell *c) {
140  return (c == NULL) ||
141  (c->type == TYPE_SYM || c->type == TYPE_NUM || c->type == TYPE_STR ||
143  c->type == TYPE_KEYWORD);
144 }
145 
146 inline bool eq(cell *v1, cell *v2) {
147  if (!v1 || !v2)
148  return (v1 == v2);
149  if (is_num(v1) && is_num(v2))
150  return (v1->value == v2->value);
151  if (is_str(v1) && is_str(v2))
152  return (strcmp(v1->str, v2->str) == 0);
153  return (v1 == v2);
154 }
155 
156 inline cell *car(cell *c) {
157  if (c == NULL)
158  return NULL;
159 #if CHECKS
160  if (atom(c))
161  pi_lisp_error("car applied to an atom");
162 #endif
163  return c->car;
164 }
165 
166 inline cell *cdr(cell *c) {
167  if (c == NULL)
168  return NULL;
169 #if CHECKS
170  if (atom(c))
171  pi_lisp_error("cdr applied to an atom");
172 #endif
173  return c->cdr;
174 }
175 
176 #endif
177 
178 #endif // !PIBUILTIN_H
struct cell * cdr
car of the cons cell
Definition: picell.h:55
cell * length_string(cell *string)
Definition: pibuiltin.c:535
cell * defun(cell *args, cell *env)
Definition: pibuiltin.c:730
cell * builtin_eq(cell *args)
Definition: pibuiltin.c:56
cell * builtin_cdr(cell *args)
Definition: pibuiltin.c:22
cell * subseq_one_index(cell *args, int start_index)
Definition: pibuiltin.c:575
cell * cddr(cell *c)
Definition: pibuiltin.c:944
Definition: picell.h:31
cell * subtraction_two_or_more_numbers(cell *numbers)
Definition: pibuiltin.c:115
cell * compare_greater_eq_strings(cell *first_str, cell *second_str)
Definition: pibuiltin.c:308
cell * and(cell *operands)
Definition: pibuiltin.c:212
cell * append(cell *args)
Definition: pibuiltin.c:499
cell * list(cell *args)
Definition: pibuiltin.c:389
cell * division(cell *numbers)
Definition: pibuiltin.c:159
char * str
value of the num cell
Definition: picell.h:50
Definition: picell.h:28
cell * or(cell *operands)
Definition: pibuiltin.c:189
cell * map(cell *args, cell *env)
Definition: pibuiltin.c:804
cell * less_eq(cell *operands)
Definition: pibuiltin.c:338
cell * builtin_cons(cell *args)
Definition: pibuiltin.c:34
cell * subseq(cell *args)
Definition: pibuiltin.c:556
cell * member(cell *args)
Definition: pibuiltin.c:412
cell * cond(cell *arg, cell *env)
Definition: pibuiltin.c:716
cell * not(cell *operands)
Definition: pibuiltin.c:241
cell * write(cell *arg)
Definition: pibuiltin.c:658
cell * cdr(cell *c)
Definition: pibuiltin.c:982
cell * cons(cell *car, cell *cdr)
Definition: pibuiltin.c:949
cell * greater_eq(cell *operands)
Definition: pibuiltin.c:285
cell * compare_greater_numbers(cell *first_num, cell *second_num)
Definition: pibuiltin.c:277
cell * cadar(cell *c)
Definition: pibuiltin.c:947
cell * bye(cell *arg)
Definition: pibuiltin.c:701
cell * multiplication(cell *numbers)
Definition: pibuiltin.c:140
cell * nth(cell *args)
Definition: pibuiltin.c:449
cell * asm_call(cell *args, cell *env)
Definition: pibuiltin.c:890
cell * compare_less_eq_numbers(cell *first_num, cell *second_num)
Definition: pibuiltin.c:357
bool is_num(cell *c)
Definition: picell.c:526
cell * length(cell *args)
Definition: pibuiltin.c:519
cell * load(cell *arg, cell *env)
Definition: pibuiltin.c:670
cell * subtraction_invert_result(cell *numbers)
Definition: pibuiltin.c:104
unsigned char type
Definition: picell.h:45
cell * symbolp(cell *arg)
Definition: pibuiltin.c:374
cell * car(cell *c)
Definition: pibuiltin.c:972
cell * compare_less_numbers(cell *first_num, cell *second_num)
Definition: pibuiltin.c:331
cell * env(cell *arg)
Definition: pibuiltin.c:907
Basic Lisp entity.
Definition: picell.h:44
cell * concatenate(cell *args)
Definition: pibuiltin.c:479
cell * set_change_existing_value(cell *args, cell *pair)
Definition: pibuiltin.c:626
cell * let(cell *args, cell *env)
Definition: pibuiltin.c:741
Definition: picell.h:27
cell * builtin_atom(cell *args)
Definition: pibuiltin.c:43
Definition: picell.h:26
cell * greater(cell *operands)
Definition: pibuiltin.c:258
bool eq(cell *v1, cell *v2)
Definition: pibuiltin.c:958
cell * compare_less_eq_strings(cell *first_str, cell *second_str)
Definition: pibuiltin.c:360
cell * length_cons(cell *list)
Definition: pibuiltin.c:541
cell * timer(cell *arg, cell *env)
Definition: pibuiltin.c:837
cell * caddr(cell *c)
Definition: pibuiltin.c:948
cell * compare_greater_strings(cell *first_str, cell *second_str)
Definition: pibuiltin.c:281
void pi_lisp_error(char *message)
Definition: pierror.c:18
cell * subtraction(cell *numbers)
Definition: pibuiltin.c:93
Definition: picell.h:34
cell * collect_garbage_call(cell *arg)
Definition: pibuiltin.c:918
cell * compile(cell *c, cell *env)
Definition: pibuiltin.c:859
cell * mem_dump(cell *arg)
Definition: pibuiltin.c:896
cell * less(cell *operands)
Definition: pibuiltin.c:312
cell * mk_cons(cell *car, cell *cdr)
Definition: picell.c:518
cell * quote(cell *args, cell *env)
Definition: pibuiltin.c:707
struct cell * car
Definition: picell.h:54
cell * set_add_new_value(cell *args, cell *prec)
Definition: pibuiltin.c:639
bool is_str(cell *c)
Definition: picell.c:527
cell * reverse(cell *args)
Definition: pibuiltin.c:391
cell * setq(cell *args, cell *env)
Definition: pibuiltin.c:718
cell * dotimes(cell *arg, cell *env)
Definition: pibuiltin.c:769
bool should_be_compiled(cell *to_compilate)
Definition: pibuiltin.c:885
cell * integerp(cell *arg)
Definition: pibuiltin.c:364
bool total_eq(cell *c1, cell *c2)
Definition: pibuiltin.c:927
cell * subseq_two_indices(cell *args, int start_index)
Definition: pibuiltin.c:590
cell * set(cell *args)
Definition: pibuiltin.c:607
represents a builtin lisp function (e.g. car, cdr...)
Definition: picell.h:32
cell * caar(cell *c)
Definition: pibuiltin.c:943
int atom(cell *c)
Definition: pibuiltin.c:951
cell * compare_less_strings(cell *first_str, cell *second_str)
Definition: pibuiltin.c:334
cell * addition(cell *numbers)
Definition: pibuiltin.c:73
cell * cadr(cell *c)
Definition: pibuiltin.c:945
cell * builtin_car(cell *args)
Definition: pibuiltin.c:9
cell * cdar(cell *c)
Definition: pibuiltin.c:946
cell * compare_greater_eq_numbers(cell *first_num, cell *second_num)
Definition: pibuiltin.c:304
int value
Definition: picell.h:49