blob: eefbc45fdb98e6e5ebfe75d44f0282b0951eca33 (
plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#include "./include/ast.h"
#include <stdio.h>
#include <stdlib.h>
/* A very... lightweight version of "inheritance" */
ast_t *init_ast(int type) {
ast_t *a = (ast_t *)malloc(sizeof(ast_t));
a->type = type;
a->car = NULL;
a->cdr = NULL;
a->string_value = NULL;
a->int_value = 0;
a->float_value = 0;
a->bool_value = false;
return a;
}
ast_t *init_ast_string(char *value) {
ast_t *a = init_ast(AST_STRING);
a->string_value = value;
return a;
}
ast_t *init_ast_int(int value) {
ast_t *a = init_ast(AST_INT);
a->int_value = value;
return a;
}
ast_t *init_ast_float(double value) {
ast_t *a = init_ast(AST_FLOAT);
a->float_value = value;
return a;
}
ast_t *init_ast_pair(ast_t *car, ast_t *cdr) {
ast_t *a = init_ast(AST_PAIR);
a->car = car;
a->cdr = cdr;
return a;
}
ast_t *init_ast_bool(bool value) {
ast_t *a = init_ast(AST_BOOL);
a->bool_value = value;
return a;
}
ast_t *init_ast_symbol(char *value) {
ast_t *a = init_ast(AST_SYMBOL);
a->string_value = value;
return a;
}
ast_t *init_ast_function(ast_t *car, ast_t *cdr) {
ast_t *a = init_ast(AST_FUNCTION);
a->car = car;
a->cdr = cdr;
return a;
}
ast_t *init_ast_root(ast_t **subnodes, int size) {
ast_t *a = init_ast(AST_ROOT);
a->subnodes = subnodes;
a->root_size = size;
return a;
}
void ast_type_print(ast_t *e) {
if (e->type == AST_FUNCTION) {
printf("Function\n");
} else if (e->type == AST_INT) {
printf("Integer\n");
} else if (e->type == AST_FLOAT) {
printf("Float\n");
} else if (e->type == AST_BOOL) {
printf("Bool\n");
} else if (e->type == AST_SYMBOL) {
printf("Symbol\n");
} else if (e->type == AST_PAIR) {
printf("Pair\n");
} else if (e->type == AST_STRING) {
printf("String\n");
} else if (e->type == AST_ROOT) {
printf("Root Node\n");
}
}
bool is_proper_list(ast_t *e) {
ast_t *cur = e;
while (cur->cdr != NULL) {
cur = cur->cdr;
}
if (cur->type == AST_PAIR && cur->car == NULL)
return true;
return false;
}
|