-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.h
58 lines (51 loc) · 1.24 KB
/
types.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef TYPES_H
#define TYPES_H
typedef enum { LIST, NUMBER, SYMBOL, CONS, FUNCTION } ExprType;
struct LispExpr;
typedef GENERIC_LIST(struct LispExpr*) LispList;
struct LispFunc;
typedef struct LispExpr {
ExprType type;
union {
LispList* list;
char* symbol;
float number;
struct { struct LispExpr* first; struct LispExpr* second; } cons;
struct LispFunc* func;
} data;
} LispExpr;
typedef struct LispFunc {
char* name;
CharList* params;
LispExpr* body;
} LispFunc;
void print_expr(LispExpr* e) {
void _print(LispExpr* e, int level) {
for (int i = 0; i < level; i++) {
fprintf(stderr, " ");
}
switch (e->type) {
case LIST:
fprintf(stderr, "<LIST %d>\n", e->data.list->size);
int n = e->data.list->size;
for (int j = 0; j < n; j++) {
_print(e->data.list->data[j], level+1);
}
fprintf(stderr, "\n");
break;
case SYMBOL:
fprintf(stderr, "<SYMBOL %s>\n", e->data.symbol);
break;
case NUMBER:
fprintf(stderr, "<NUMBER %.1f>\n", e->data.number);
break;
case CONS:
fprintf(stderr, "<CONS>\n");
_print(e->data.cons.first, level+1);
_print(e->data.cons.second, level+1);
break;
}
}
_print(e, 0);
}
#endif