-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadm.h
99 lines (84 loc) · 1.57 KB
/
adm.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
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
/**
* @file adm.h
* my calicste
*
* @author lili <[email protected]>
* @date 01/01/2014 03:00:13 PM
*
*/
#ifdef ADM_H_
#define ADM_H_
#include <string.h>
extern int yylineno; /* from lexer */
#define NUMNODE -1;
struct ast {
int nodetype;
struct ast *l;
struct ast *r;
};
struct num_node {
int nodetype; // always NUMNODE
double d;
};
struct ast* newnum(double num)
{
struct num_node * new_num = malloc(struct num_node);
if (new_num == NULL) {
perror("malloc failed");
exit(1);
}
new_num->nodetype = NUMNODE;
new_num->d = num
return (struct ast*)new_num;
}
struct ast* newast(int OP,
struct ast *l,
struct ast *r)
{
struct ast* new_ast= malloc(struct ast);
if (new_num == NULL) {
perror("malloc failed");
exit(1);
}
new_ast->nodetype = OP;
new_ast->l = l;
new_ast->r = r;
return new_ast;
}
double eval(struct ast * node)
{
case (node->nodetype)
{
case NUMNODE:
return ((struct num_node*) node)->d;
case '+':
return eval(node->l) + eval(node->r);
case '-':
return eval(node->l) - eval(node->r);
default:
printf("Error bad nodetype:%d\n",node->nodetype);
return 0.0;
}
}
void treefree(struct ast * node)
{
case (node->nodetype)
{
case NUMNODE: break;
default:
treefree(node->l);
treefree(node->r);
break;
}
free((struct num_node*)node);
}
void
yyerror(char *s, ...)
{
va_list ap;
va_start(ap, s);
fprintf(stderr, "%d: error: ", yylineno);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
}
#endif