blob: 8120feb8bb3680b1ca44fb0ccd91b2d91c8af720 (
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
97
98
99
|
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "ast.h"
#include "util.h"
void ast_free(AST *ast){
assert(ast);
switch(ast->type){
case AST_LIST:
assert(ast->l.len>=0);
for(int i=0;i<ast->l.len;i++){
assert(ast->l.nodes[i]);
ast_free(ast->l.nodes[i]);
}
break;
case AST_WORD:
assert(ast->w.word);
free(ast->w.word);
break;
case AST_NUMBER:
case AST_SYMBOL:
break;
default:
assert(false);
}
free(ast);
}
AST* ast_copy(const AST *ast){
assert(ast);
switch(ast->type){
case AST_LIST:{
assert(ast->l.len>=0);
assert(ast->l.nodes);
AST **nodes=malloc(ast->l.len,AST*);
for(int i=0;i<ast->l.len;i++)nodes[i]=ast_copy(ast->l.nodes[i]);
AST *l=ast_list(ast->l.len,nodes);
l->l.quoted=ast->l.quoted;
return l;
}
case AST_WORD:
assert(ast->w.word);
return ast_word(copystring(ast->w.word));
case AST_NUMBER:
return ast_number(ast->n.num);
case AST_SYMBOL:{
assert(ast->s.name);
AST *sym=ast_symbol(ast->s.name);
sym->s.symid=ast->s.symid;
return sym;
}
default:
assert(false);
}
}
AST* ast_list(int len,AST **nodes){
assert(len>=0);
assert(nodes);
AST *ast=malloc(1,AST);
ast->l.len=len;
ast->l.nodes=malloc(len,AST*);
memcpy(ast->l.nodes,nodes,len*sizeof(AST*));
ast->l.quoted=false;
return ast;
}
AST* ast_word(char *word){
assert(word);
AST *ast=malloc(1,AST);
ast->w.word=word;
return ast;
}
AST* ast_number(double num){
AST *ast=malloc(1,AST);
ast->n.num=num;
return ast;
}
AST* ast_symbol(char *name){
assert(name);
AST *ast=malloc(1,AST);
ast->s.name=name;
ast->s.symid=-1;
return ast;
}
|