-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.c
83 lines (73 loc) · 1.94 KB
/
errors.c
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
#include "errors.h"
#include <string.h>
devilval* devilval_num(long x) {
devilval* v = malloc(sizeof(devilval));
v->type = DEVILVAL_NUM;
v->data.l = x;
return v;
}
devilval* devilval_dec(double x) {
devilval* v = malloc(sizeof(devilval));
v->type = DEVILVAL_DEC;
v->data.dec = x;
return v;
}
devilval* devilval_err(char* m) {
devilval* v = malloc(sizeof(devilval));
v->type = DEVILVAL_ERR;
v->err = malloc(strlen(m) + 1);
strcpy(v->err, m);
return v;
}
devilval* devilval_sym(char* s) {
devilval* v = malloc(sizeof(devilval));
v->type = DEVILVAL_SYM;
v->sym = malloc(strlen(s) + 1);
strcpy(v->sym, s);
return v;
}
devilval* devilval_sexpr(void) {
devilval* v = malloc(sizeof(devilval));
v->type = DEVILVAL_SEXPR;
v->count = 0;
v->cell = NULL;
return v;
}
void devilval_del(devilval* v) {
switch (v->type) {
case DEVILVAL_NUM: break;
case DEVILVAL_DEC: break;
case DEVILVAL_ERR: free(v->err); break;
case DEVILVAL_SYM: free(v->sym); break;
case DEVILVAL_SEXPR:
for (int i = 0; i < v->count; i++) {
devilval_del(v->cell[i]);
}
free(v->cell);
break;
}
free(v);
}
void devilval_print(devilval* v) {
switch (v->type) {
case DEVILVAL_NUM: printf("%li", v->data.l); break;
case DEVILVAL_DEC: printf("%f", v->data.dec); break;
case DEVILVAL_ERR: printf("Error: %s", v->err); break;
case DEVILVAL_SYM: printf("%s", v->sym); break;
case DEVILVAL_SEXPR: devilval_expr_print(v, '(', ')'); break;
}
}
void devilval_println(devilval* v) {
devilval_print(v);
putchar('\n');
}
void devilval_expr_print(devilval* v, char open, char close) {
putchar(open);
for (int i = 0; i < v->count; i++) {
devilval_print(v->cell[i]);
if (i != (v->count - 1)) {
putchar(' ');
}
}
putchar(close);
}