annotate dmeval.c @ 700:6d96d5bf9653

Add output file stream argument to dm_print_optree().
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 16 Apr 2013 21:29:37 +0300
parents f6cf172a6ef7
children e8224750f576
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 #include "dmeval.h"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 #include <stdio.h>
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 #include <math.h>
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 #define DM_MAX_ID_LEN 16
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 #define DM_MAX_BUF 512
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #define DM_STACK_SIZE 512
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
9
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
10 /* Function definitions
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
11 */
663
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
12 static DMValue func_int_clip(DMValue *v)
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
13 {
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
14 return (*v < -1.0f) ? -1.0f : ((*v > 1.0f) ? 1.0f : *v);
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
15 }
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
16
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
17
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
18 static DMValue func_sin(DMValue *v)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 {
663
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
20 return sin(*v);
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
21 }
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
22
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
23
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
24 static DMValue func_cos(DMValue *v)
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
25 {
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
26 return cos(*v);
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
27 }
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
28
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
29
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
30 static DMValue func_pow(DMValue *v)
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
31 {
05eb24a608f0 Some minor touches to evaluator code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
32 return pow(v[0], v[1]);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 /* Some basic functions
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 static const DMEvalId dm_eval_basic[] =
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 {
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
40 { "sin", ID_FUNC, 1, func_sin, NULL, 0 },
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
41 { "cos", ID_FUNC, 1, func_cos, NULL, 0 },
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
42 { "clip", ID_FUNC, 1, func_int_clip, NULL, 0 },
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
43 { "pow", ID_FUNC, 2, func_pow, NULL, 0 },
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
44
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
45 { "pi", ID_CVAR, 0, NULL, NULL, DM_PI },
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 };
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 static const int ndm_eval_basic = sizeof(dm_eval_basic) / sizeof(dm_eval_basic[0]);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 void dm_eval_err_v(DMEvalContext *ev, const char *fmt, va_list ap)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 #if 1
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 char *tmp = dm_strdup_vprintf(fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 ev->err = TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 if (ev->errStr != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 ev->errStr = dm_strdup_printf("%s%s", ev->errStr, tmp);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 dmFree(tmp);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 ev->errStr = tmp;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 #else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 vfprintf(stdout, fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 #endif
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 void dm_eval_err(DMEvalContext *ev, const char *fmt, ...)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 va_list ap;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 va_start(ap, fmt);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 dm_eval_err_v(ev, fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 va_end(ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 DMEvalId *dm_eval_find_id(DMEvalContext *ev, const char *name)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 int i;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 if (ev->ids == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 for (i = 0; i < ev->nids; i++)
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
87 {
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 if (strcmp(ev->ids[i].name, name) == 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 return &(ev->ids[i]);
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
90 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 static DMEvalId *dm_eval_add_id(DMEvalContext *ev, const char *name, const int type)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 DMEvalId *id = dm_eval_find_id(ev, name);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 if (id != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 ev->ids = dmRealloc(ev->ids, sizeof(DMEvalId) * (ev->nids + 1));
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 if (ev->ids == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 dm_eval_err(ev, "Could not reallocate eval ids array (#%d). Fatal error.\n", ev->nids + 1);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 id = &(ev->ids[ev->nids]);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 ev->nids++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 id->name = dm_strdup(name);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 id->type = type;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 id->func = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 id->var = NULL;
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
116
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 return id;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 DMEvalId *dm_eval_add_var(DMEvalContext *ev, const char *name, DMValue *var)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 DMEvalId *id = dm_eval_add_id(ev, name, ID_VAR);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 if (id == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 id->var = var;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 return id;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
132 DMEvalId *dm_eval_add_const(DMEvalContext *ev, const char *name, DMValue value)
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
133 {
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
134 DMEvalId *id = dm_eval_add_id(ev, name, ID_CVAR);
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
135 if (id == NULL)
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
136 return NULL;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
137
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
138 id->cvalue = value;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
139 return id;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
140 }
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
141
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
142
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
143 DMEvalId *dm_eval_add_func(DMEvalContext *ev, const char *name, DMValue (*func)(DMValue *), int nargs)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 DMEvalId *id = dm_eval_add_id(ev, name, ID_VAR);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 if (id == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
149 id->func = func;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
150 id->nargs = nargs;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 return id;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 DMEvalContext *dm_eval_new(void)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 int i;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 DMEvalContext *ev = dmCalloc(1, sizeof(DMEvalContext));
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 if (ev == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 for (i = 0; i < ndm_eval_basic; i++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 const DMEvalId *id = &dm_eval_basic[i];
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 DMEvalId *nid = dm_eval_add_id(ev, id->name, id->type);
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
167 nid->nargs = id->nargs;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
168 nid->func = id->func;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
169 nid->var = id->var;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
170 nid->cvalue = id->cvalue;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 return ev;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
176
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 void dm_eval_free(DMEvalNode *node)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 while (node != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 DMEvalNode *next = node->next;
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
182 int i;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
184 for (i = 0; i < DM_MAX_ARGS; i++)
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
185 {
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
186 dm_eval_free(node->args[i]);
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
187 node->args[i] = NULL;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
188 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
190 dm_eval_free(node->subexpr);
687
f3d2b19dbbb7 Set node->subexpr to NULL in free, as we are paranoidly cargo-culting a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 686
diff changeset
191 node->subexpr = NULL;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 dmFree(node);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 node = next;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 void dm_eval_clear_err(DMEvalContext *ev)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 if (ev == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 return;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 dmFree(ev->errStr);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 ev->err = FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205 ev->errStr = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 void dm_eval_close(DMEvalContext *ev)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 int i;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 if (ev == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 return;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 for (i = 0; i < ev->nids; i++)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 dmFree(ev->ids[i].name);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219 dmFree(ev->ids);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 dm_eval_clear_err(ev);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 dmFree(ev);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 static DMEvalNode *dm_eval_pop_node(DMEvalNode **list)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 DMEvalNode *node;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 if (*list == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 node = (*list)->prev;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234 if (*list != node)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 node->prev->next = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 (*list)->prev = node->prev;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 *list = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 return node;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245
679
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
246 static DMEvalNode *dm_eval_peek_node(DMEvalNode **list)
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
247 {
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
248 if (*list == NULL)
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
249 return NULL;
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
250
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
251 return (*list)->prev;
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
252 }
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
253
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
254
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 static DMEvalNode *dm_eval_insert_node(DMEvalNode **list, DMEvalNode *node)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 if (*list != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 node->prev = (*list)->prev;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 (*list)->prev->next = node;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 (*list)->prev = node;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 *list = node;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 node->prev = *list;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 node->next = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270 return node;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273
699
f6cf172a6ef7 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 697
diff changeset
274 static DMEvalNode *dm_eval_push_node(DMEvalNode **list, const DMEvalNode *src)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275 {
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
276 DMEvalNode *tmp, *node = dmCalloc(1, sizeof(DMEvalNode));
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
277 int i;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
278
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 if (node == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281
664
57fc2ec4efdc Some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 663
diff changeset
282 node->op = src->op;
57fc2ec4efdc Some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 663
diff changeset
283 node->val = src->val;
57fc2ec4efdc Some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 663
diff changeset
284 node->id = src->id;
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
285 node->ok = src->ok;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
286
696
de664bfc7509 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 694
diff changeset
287 if (src->op == OP_SUBEXPR)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
288 {
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
289 for (tmp = src->subexpr; tmp != NULL; tmp = tmp->next)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
290 dm_eval_push_node(&(node->subexpr), tmp);
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
291 }
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
292
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
293 if (src->op == OP_FUNC)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
294 {
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
295 for (i = 0; i < src->id->nargs; i++)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
296 {
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
297 for (tmp = src->args[i]; tmp != NULL; tmp = tmp->next)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
298 dm_eval_push_node(&(node->args[i]), tmp);
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
299 }
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
300 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302 return dm_eval_insert_node(list, node);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 static DMEvalNode *dm_eval_add_node(DMEvalNode **list, const int op)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
308 DMEvalNode *node = dmCalloc(1, sizeof(DMEvalNode));
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309 if (node == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 node->op = op;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 return dm_eval_insert_node(list, node);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318 enum
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 PARSE_NONE = 0x0000,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 PARSE_START = 0x1000,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 PARSE_END = 0x2000,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 PARSE_ERROR = 0x8000,
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325 PARSE_IDENT = 0x0001,
684
dae4db2512c4 Remove PARSE_WS, it's not used anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
326 PARSE_CONST = 0x0002,
dae4db2512c4 Remove PARSE_WS, it's not used anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
327 PARSE_OPER = 0x0004, // All operators
dae4db2512c4 Remove PARSE_WS, it's not used anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
328 PARSE_OPER2 = 0x0008, // '-' only
dae4db2512c4 Remove PARSE_WS, it's not used anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
329 PARSE_SUBEXPR = 0x0010,
dae4db2512c4 Remove PARSE_WS, it's not used anymore.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
330 PARSE_ARGS = 0x0020,
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
331
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
332 PARSE_NORMAL = PARSE_CONST | PARSE_IDENT | PARSE_SUBEXPR | PARSE_OPER2,
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 };
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 #define DM_CHECK(x) { if (mode & PARSE_ ## x ) { if (str[0]) strcat(str, " or "); strcat(str, # x ); } }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 static char *dm_get_mode(int mode)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 char str[128] = "";
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341 DM_CHECK(START);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 DM_CHECK(END);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 DM_CHECK(IDENT);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344 DM_CHECK(CONST);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345 DM_CHECK(OPER);
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
346 DM_CHECK(OPER2);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
347 DM_CHECK(SUBEXPR);
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
348 DM_CHECK(ARGS);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350 return dm_strdup(str);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354 static void dm_set_mode(DMEvalContext *ev, const int mode)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355 {
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
356 if (mode != PARSE_ERROR &&
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
357 mode != PARSE_START &&
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 ev->expect != PARSE_NONE &&
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 (mode & ev->expect) == 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 char *tmp1 = dm_get_mode(ev->expect),
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 *tmp2 = dm_get_mode(mode);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 dm_eval_err(ev, "Expected [%s], got %s.\n", tmp1, tmp2);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 dmFree(tmp1);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 dmFree(tmp2);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369 ev->prev = ev->mode;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 ev->mode = mode;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 static int dm_eval_parse_expr_do(DMEvalContext *ev, DMEvalNode **list, char **str, int depth)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376 char *c = *str;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377 char tmpStr[DM_MAX_BUF + 2];
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
378 int tmpStrLen = 0, argIndex;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379 DMEvalNode *node = NULL, *func = NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 BOOL first = FALSE, decimal = FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
382 ev->expect = PARSE_NORMAL;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 ev->mode = PARSE_START;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 while (ev->mode != PARSE_ERROR && ev->mode != PARSE_END)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386 switch (ev->mode)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388 case PARSE_SUBEXPR:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 char *tmp = c + 1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
392 ev->expect = PARSE_NORMAL;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
394 if ((node = dm_eval_add_node(list, OP_SUBEXPR)) == NULL)
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
395 dm_set_mode(ev, PARSE_ERROR);
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
396 else
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
397 if (dm_eval_parse_expr_do(ev, &(node->subexpr), &tmp, depth + 1) != 0)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 {
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
399 dm_eval_err(ev, "Subexpression starting at '%s' contained errors.\n", c);
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
400 dm_set_mode(ev, PARSE_ERROR);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401 }
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
402
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 if (ev->mode != PARSE_ERROR)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405 dm_set_mode(ev, PARSE_START);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406 ev->expect = PARSE_OPER | PARSE_END;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407 c = tmp;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 break;
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
411
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
412 case PARSE_ARGS:
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
413 {
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
414 char *tmp = c + 1;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
415 for (argIndex = 0; argIndex < func->id->nargs; argIndex++)
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
416 {
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
417 if (dm_eval_parse_expr_do(ev, &(func->args[argIndex]), &tmp, depth + 1) != 0)
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
418 {
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
419 dm_eval_err(ev, "Function argument subexpression starting at '%s' contained errors.\n", c);
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
420 dm_set_mode(ev, PARSE_ERROR);
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
421 }
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
422 }
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
423
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
424 func = NULL;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
425
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
426 if (ev->mode != PARSE_ERROR)
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
427 {
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
428 dm_set_mode(ev, PARSE_START);
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
429 ev->expect = PARSE_OPER | PARSE_END;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
430 c = tmp;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
431 }
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
432 }
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
433 break;
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
434
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
435 case PARSE_START:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436 // Start
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437 if (*c == 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438 dm_set_mode(ev, PARSE_END);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
440 // Skip whitespace
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441 else if (isspace(*c))
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
442 c++;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
444 else if (*c == ')' || *c == ',')
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 if (depth > 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447 dm_set_mode(ev, PARSE_END);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450 dm_eval_err(ev, "Invalid nesting near '%s' (depth %d).\n", c, depth);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 dm_set_mode(ev, PARSE_ERROR);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453 c++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454 }
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
455
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 else if (*c == '(')
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
457 dm_set_mode(ev, func != NULL ? PARSE_ARGS : PARSE_SUBEXPR);
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
458
671
e5e56d16597e And another fix to parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 670
diff changeset
459 else if (*c == '-')
672
75e9af630e18 And another fix.
Matti Hamalainen <ccr@tnsp.org>
parents: 671
diff changeset
460 dm_set_mode(ev, (ev->prev == PARSE_START || ev->prev == PARSE_OPER) ? PARSE_OPER2 : PARSE_OPER);
671
e5e56d16597e And another fix to parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 670
diff changeset
461
e5e56d16597e And another fix to parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 670
diff changeset
462 else if (strchr("+*/<>%&|!^", *c))
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463 dm_set_mode(ev, PARSE_OPER);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 else if (isdigit(*c) || *c == '.')
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
466 dm_set_mode(ev, PARSE_CONST);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
467
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
468 else if (isalpha(*c) || *c == '_')
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469 dm_set_mode(ev, PARSE_IDENT);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
471 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
472 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473 dm_eval_err(ev, "Syntax error near '%s' (depth %d).\n", c, depth);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
474 dm_set_mode(ev, PARSE_ERROR);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
475 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
477 first = TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
479
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
480 case PARSE_CONST:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481 if (first)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
482 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
483 first = FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
484 decimal = FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
485 tmpStrLen = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
487 if (isdigit(*c) || *c == '-' || *c == '+' || *c == '.')
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 if (*c == '.')
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490 decimal = TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491 tmpStr[tmpStrLen++] = *c++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
493 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
495 dm_eval_err(ev, "Invalid constant expression near '%s'.\n", c);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496 dm_set_mode(ev, PARSE_ERROR);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
497 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501 if (isdigit(*c) || (*c == '.' && !decimal))
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503 if (*c == '.')
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 decimal = TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505 tmpStr[tmpStrLen++] = *c++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
508 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
509 tmpStr[tmpStrLen] = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
511 if ((node = dm_eval_add_node(list, OP_CONST)) == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
513 dm_set_mode(ev, PARSE_ERROR);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514 }
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
515 else
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
516 {
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
517 node->val = atof(tmpStr);
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
518 dm_set_mode(ev, PARSE_START);
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
519 ev->expect = PARSE_OPER | PARSE_END;
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
520 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
521 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
523 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525 case PARSE_OPER:
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
526 case PARSE_OPER2:
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528 int op = OP_NONE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 switch (*c)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532 case '+': op = OP_ADD; c++; break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
533 case '-': op = OP_SUB; c++; break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
534 case '*': op = OP_MUL; c++; break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
535 case '/': op = OP_DIV; c++; break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
536 case '%': op = OP_MOD; c++; break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
537 case '&': op = OP_AND; c++; break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
538 case '|': op = OP_OR; c++; break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539 case '>':
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540 if (c[1] == '>')
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542 c += 2;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
543 op = OP_RSHIFT;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
545 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547 dm_eval_err(ev, "Syntax error near '%s'.\n", c);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548 dm_set_mode(ev, PARSE_ERROR);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552 case '<':
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553 if (c[1] == '<')
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
554 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
555 c += 2;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
556 op = OP_LSHIFT;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560 dm_eval_err(ev, "Syntax error near '%s'.\n", c);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 dm_set_mode(ev, PARSE_ERROR);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565 default:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 dm_eval_err(ev, "Unknown operator '%c'.\n", *c);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567 dm_set_mode(ev, PARSE_ERROR);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
568 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
569
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
570 if (op != OP_NONE)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571 {
676
030b2a8e04d5 Improve parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 675
diff changeset
572 if (ev->mode == PARSE_OPER2 && op == OP_SUB &&
030b2a8e04d5 Improve parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 675
diff changeset
573 (node = dm_eval_add_node(list, op)) != NULL)
030b2a8e04d5 Improve parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 675
diff changeset
574 {
030b2a8e04d5 Improve parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 675
diff changeset
575 ev->expect = PARSE_NORMAL;
030b2a8e04d5 Improve parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 675
diff changeset
576 dm_set_mode(ev, PARSE_START);
030b2a8e04d5 Improve parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 675
diff changeset
577 }
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
578 else
676
030b2a8e04d5 Improve parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 675
diff changeset
579 if (ev->mode == PARSE_OPER &&
030b2a8e04d5 Improve parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 675
diff changeset
580 (node = dm_eval_add_node(list, op)) != NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
581 {
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
582 ev->expect = PARSE_NORMAL | PARSE_OPER2;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
583 dm_set_mode(ev, PARSE_START);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
584 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
585 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
586 dm_set_mode(ev, PARSE_ERROR);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
587 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
588 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
589 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
590
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
591 case PARSE_IDENT:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
592 if (isalnum(*c) || *c == '_')
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
594 if (first)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
596 tmpStrLen = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
597 first = FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
598 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
599
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
600 if (tmpStrLen < DM_MAX_ID_LEN)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
601 tmpStr[tmpStrLen++] = *c++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
602 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
603 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
604 tmpStr[tmpStrLen] = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605 dm_eval_err(ev, "Identifier too long! ('%s')\n", tmpStr);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
606 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
608 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
609 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
610 tmpStr[tmpStrLen] = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
611 DMEvalId *id = dm_eval_find_id(ev, tmpStr);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
612 if (id != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
613 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
614 if ((node = dm_eval_add_node(list, id->type == ID_FUNC ? OP_FUNC : OP_VAR)) != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
615 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
616 node->id = id;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
617 if (id->type == ID_FUNC)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
618 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
619 func = node;
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
620 ev->expect = PARSE_ARGS;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
621 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
622 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
623 ev->expect = PARSE_END | PARSE_OPER;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
624
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
625 dm_set_mode(ev, PARSE_START);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
626 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
627 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
628 dm_set_mode(ev, PARSE_ERROR);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
629 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
630 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
632 dm_eval_err(ev, "No such identifier '%s'.\n", tmpStr);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
633 dm_set_mode(ev, PARSE_ERROR);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
634 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
635 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
636 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
637 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
638
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
639 *str = c;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
640
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
641 return (ev->mode == PARSE_ERROR) ? -1 : 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
642 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
643
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
644
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
645 int dm_eval_parse_expr(DMEvalContext *ev, char *expr, DMEvalNode **result)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
646 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
647 int ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
648
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
649 if (ev == NULL || result == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
650 return -1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
651
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
652 ev->prev = PARSE_START;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
653 ret = dm_eval_parse_expr_do(ev, result, &expr, 0);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
654
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
655 return ret;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
656 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
657
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
658
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
659 static const char *dm_oper_names[OP_NOPERS] =
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
660 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
661 "NONE",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
662 "+",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
663 "-",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
664 "*",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
665 "/",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
666 "%",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
667
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
668 "<<",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
669 ">>",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
670
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
671 "&",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
672 "|",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
673 "^",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
674
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
675 "FUNC",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
676 "VAR",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
677 "SUBEXPR",
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
678 "CONST"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
679 };
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
680
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
681
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
682 static void dm_print_optree_do(FILE *out, DMEvalContext *ev, DMEvalNode *node, const int level)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
683 {
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
684 int i;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
685 while (node != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
686 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
687 switch (node->op)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
688 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
689 case OP_FUNC:
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
690 fprintf(out, "%s(", node->id != NULL ? node->id->name : "?ERROR");
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
691 for (i = 0; i < node->id->nargs; i++)
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
692 {
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
693 dm_print_optree_do(out, ev, node->args[i], level + 1);
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
694 if (i < node->id->nargs - 1)
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
695 fprintf(out, ",");
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
696 }
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
697 fprintf(out, ")");
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
698 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
699
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
700 case OP_VAR:
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
701 fprintf(out, "%s", node->id != NULL ? node->id->name : "?ERROR");
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
702 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
703
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
704 case OP_CONST:
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
705 fprintf(out, "%.1f", node->val);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
706 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
707
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
708 case OP_SUBEXPR:
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
709 fprintf(out, "(");
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
710 if (node->subexpr != NULL)
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
711 dm_print_optree_do(out, ev, node->subexpr, level + 1);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
712 else
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
713 fprintf(out, "?ERROR");
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
714 fprintf(out, ")");
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
715 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
716
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
717 default:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
718 if (node->op > 0 && node->op < OP_NOPERS)
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
719 fprintf(out, "%s", dm_oper_names[node->op]);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
720 else
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
721 fprintf(out, "ERROR!");
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
722 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
723 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
724 node = node->next;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
725 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
726 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
727
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
728
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
729 void dm_print_optree(FILE *out, DMEvalContext *ev, DMEvalNode *node)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
730 {
700
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
731 dm_print_optree_do(out, ev, node, 0);
6d96d5bf9653 Add output file stream argument to dm_print_optree().
Matti Hamalainen <ccr@tnsp.org>
parents: 699
diff changeset
732 fprintf(out, "\n");
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
733 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
734
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
735
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
736 /*
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
737 operator precedence:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
738 HIGHEST
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
739 "()"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
740
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
741 "*"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
742 "/"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
743 "%"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
744
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
745 "+"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
746 "-"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
747
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
748 "<<"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
749 ">>"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
750
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
751 "&"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
752 "^"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
753 "|"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
754 LOWEST
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
755 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
756
699
f6cf172a6ef7 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 697
diff changeset
757 static int dm_eval_reorder_pass0(DMEvalContext *ev, const DMEvalNode *node, DMEvalNode **result)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
758 {
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
759 DMEvalNode *tmp, *sub;
679
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
760 int i, res;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
761
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
762 for (; node != NULL; node = node->next)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
763 switch (node->op)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
764 {
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
765 case OP_SUB:
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
766 if ((tmp = dm_eval_peek_node(result)) == NULL || tmp->op <= OP_FUNC)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
767 {
679
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
768 // Add subexpression node
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
769 if ((sub = dm_eval_add_node(result, OP_SUBEXPR)) == NULL)
679
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
770 return -3;
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
771
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
772 // Add this operator into subexpression
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
773 dm_eval_add_node(&(sub->subexpr), node->op);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
774
679
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
775 // Next node
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
776 node = node->next;
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
777 if (node == NULL)
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
778 return -72;
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
779
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
780 dm_eval_push_node(&(sub->subexpr), node);
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
781 }
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
782 else
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
783 if (dm_eval_push_node(result, node) == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
784 return -32;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
785
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
786 break;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
787
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
788 case OP_FUNC:
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
789 if ((tmp = dm_eval_push_node(result, node)) == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
790 return -1;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
791
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
792 for (i = 0; i < node->id->nargs; i++)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
793 {
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
794 if ((res = dm_eval_reorder(ev, node->args[i], &(tmp->args[i]))) != 0)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
795 return res;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
796 }
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
797 break;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
798
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
799 case OP_SUBEXPR:
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
800 if ((tmp = dm_eval_push_node(result, node)) == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
801 return -1;
679
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
802
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
803 if ((res = dm_eval_reorder(ev, node->subexpr, &(tmp->subexpr))) != 0)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
804 return res;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
805 break;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
806
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
807 default:
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
808 if (dm_eval_push_node(result, node) == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
809 return -32;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
810 break;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
811 }
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
812
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
813 return 0;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
814 }
679
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
815
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
816
699
f6cf172a6ef7 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 697
diff changeset
817 static int dm_eval_reorder_pass1(DMEvalContext *ev, const DMEvalNode *node, DMEvalNode **result)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
818 {
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
819 DMEvalNode *tmp, *sub;
679
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
820
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
821 for (; node != NULL; node = node->next)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
822 switch (node->op)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
823 {
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
824 case OP_MUL:
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
825 case OP_DIV:
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
826 case OP_MOD:
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
827 if (node->ok)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
828 {
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
829 if (dm_eval_push_node(result, node) == NULL)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
830 return -32;
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
831 break;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
832 }
679
1dc7bd5f99f7 Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 678
diff changeset
833
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
834 // Pop previous node, f.e. 5*3 -> 5
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
835 if ((tmp = dm_eval_pop_node(result)) == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
836 return -24;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
837
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
838 // Add subexpression node
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
839 if ((sub = dm_eval_add_node(result, OP_SUBEXPR)) == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
840 return -3;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
841
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
842 // Add popped node into subexpression
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
843 dm_eval_push_node(&(sub->subexpr), tmp);
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
844
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
845 // Add this operator into subexpression
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
846 DMEvalNode *foo = dm_eval_add_node(&(sub->subexpr), node->op);
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
847 foo->ok = TRUE;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
848
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
849 // Next node
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
850 node = node->next;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
851 if (node == NULL)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
852 return -72;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
853
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
854 dm_eval_push_node(&(sub->subexpr), node);
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
855 break;
692
391310cfba26 Fix & precedence vs +.
Matti Hamalainen <ccr@tnsp.org>
parents: 688
diff changeset
856
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
857 default:
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
858 if (dm_eval_push_node(result, node) == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
859 return -32;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
860 break;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
861 }
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
862
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
863 return 0;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
864 }
692
391310cfba26 Fix & precedence vs +.
Matti Hamalainen <ccr@tnsp.org>
parents: 688
diff changeset
865
699
f6cf172a6ef7 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 697
diff changeset
866 static int dm_eval_reorder_pass2(DMEvalContext *ev, const DMEvalNode *node, DMEvalNode **result)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
867 {
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
868 DMEvalNode *tmp, *sub;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
869
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
870 for (; node != NULL; node = node->next)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
871 switch (node->op)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
872 {
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
873 case OP_SUB:
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
874 case OP_ADD:
694
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
875 if (node->ok)
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
876 {
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
877 if (dm_eval_push_node(result, node) == NULL)
694
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
878 return -32;
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
879 break;
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
880 }
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
881
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
882 // Pop previous node, f.e. 5*3 -> 5
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
883 if ((tmp = dm_eval_pop_node(result)) == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
884 return -24;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
885
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
886 // Add subexpression node
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
887 if ((sub = dm_eval_add_node(result, OP_SUBEXPR)) == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
888 return -3;
692
391310cfba26 Fix & precedence vs +.
Matti Hamalainen <ccr@tnsp.org>
parents: 688
diff changeset
889
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
890 // Add popped node into subexpression
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
891 dm_eval_push_node(&(sub->subexpr), tmp);
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
892
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
893 // Add this operator into subexpression
694
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
894 DMEvalNode *foo = dm_eval_add_node(&(sub->subexpr), node->op);
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
895 foo->ok = TRUE;
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
896
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
897 // Next node
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
898 node = node->next;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
899 if (node == NULL)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
900 return -72;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
901
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
902 dm_eval_push_node(&(sub->subexpr), node);
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
903 break;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
904
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
905 default:
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
906 if (dm_eval_push_node(result, node) == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
907 return -32;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
908 break;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
909 }
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
910
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
911 return 0;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
912 }
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
913
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
914
699
f6cf172a6ef7 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 697
diff changeset
915 static int dm_eval_reorder_pass3(DMEvalContext *ev, const DMEvalNode *node, DMEvalNode **result)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
916 {
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
917 DMEvalNode *tmp, *sub;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
918 int res;
692
391310cfba26 Fix & precedence vs +.
Matti Hamalainen <ccr@tnsp.org>
parents: 688
diff changeset
919
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
920 while (node != NULL)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
921 switch (node->op)
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
922 {
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
923 case OP_LSHIFT:
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
924 case OP_RSHIFT:
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
925 // Bitshift operator precedence is .. different
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
926 if (dm_eval_push_node(result, node) == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
927 return -32;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
928
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
929 if ((sub = dm_eval_add_node(result, OP_SUBEXPR)) == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
930 return -3;
692
391310cfba26 Fix & precedence vs +.
Matti Hamalainen <ccr@tnsp.org>
parents: 688
diff changeset
931
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
932 if (node->next == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
933 return -14;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
934
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
935 if ((res = dm_eval_reorder_pass3(ev, node->next, &(sub->subexpr))) != 0)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
936 return res;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
937
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
938 node = NULL;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
939 break;
692
391310cfba26 Fix & precedence vs +.
Matti Hamalainen <ccr@tnsp.org>
parents: 688
diff changeset
940
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
941 default:
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
942 if (dm_eval_push_node(result, node) == NULL)
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
943 return -32;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
944
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
945 node = node->next;
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
946 break;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
947 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
948
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
949 return 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
950 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
951
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
952
680
4422a1880859 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
953 int dm_eval_reorder(DMEvalContext *ev, DMEvalNode *node, DMEvalNode **result)
4422a1880859 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
954 {
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
955 DMEvalNode *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
680
4422a1880859 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
956 int res;
4422a1880859 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
957
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
958 res = dm_eval_reorder_pass0(ev, node, &tmp1);
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
959
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
960 if (res != 0)
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
961 {
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
962 dm_eval_free(tmp1);
680
4422a1880859 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
963 return res;
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
964 }
680
4422a1880859 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
965
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
966 res = dm_eval_reorder_pass1(ev, tmp1, &tmp2);
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
967 dm_eval_free(tmp1);
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
968
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
969 if (res != 0)
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
970 {
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
971 dm_eval_free(tmp2);
692
391310cfba26 Fix & precedence vs +.
Matti Hamalainen <ccr@tnsp.org>
parents: 688
diff changeset
972 return res;
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
973 }
692
391310cfba26 Fix & precedence vs +.
Matti Hamalainen <ccr@tnsp.org>
parents: 688
diff changeset
974
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
975 res = dm_eval_reorder_pass2(ev, tmp2, &tmp3);
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
976 dm_eval_free(tmp2);
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
977 if (res != 0)
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
978 {
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
979 dm_eval_free(tmp3);
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
980 return res;
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
981 }
693
837ad9dcc348 More work on evaluator reordering pass.
Matti Hamalainen <ccr@tnsp.org>
parents: 692
diff changeset
982
697
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
983 res = dm_eval_reorder_pass3(ev, tmp3, result);
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
984 dm_eval_free(tmp3);
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
985
48321a711bec Work towards plugging memory leaks.
Matti Hamalainen <ccr@tnsp.org>
parents: 696
diff changeset
986 return res;
680
4422a1880859 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
987 }
4422a1880859 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
988
4422a1880859 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 679
diff changeset
989
699
f6cf172a6ef7 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 697
diff changeset
990 static int dm_eval_get(DMEvalContext *ev, const DMEvalNode *node, DMValue *result)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
991 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
992 if (node == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
993 return -32;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
994
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
995 switch (node->op)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
996 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
997 case OP_VAR:
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
998 if (node->id->type == ID_VAR)
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
999 *result = *(node->id->var);
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
1000 else
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
1001 *result = node->id->cvalue;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1002 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1003
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1004 case OP_CONST:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1005 *result = node->val;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1006 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1007
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1008 case OP_SUBEXPR:
678
4a08ce0997bc Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 677
diff changeset
1009 return dm_eval_exec(ev, node->subexpr, result);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1010
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1011 case OP_FUNC:
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
1012 {
678
4a08ce0997bc Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 677
diff changeset
1013 DMValue tmp[DM_MAX_ARGS];
4a08ce0997bc Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 677
diff changeset
1014 int i;
4a08ce0997bc Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 677
diff changeset
1015 for (i = 0; i < node->id->nargs; i++)
4a08ce0997bc Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 677
diff changeset
1016 {
4a08ce0997bc Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 677
diff changeset
1017 if (dm_eval_exec(ev, node->args[i], &tmp[i]) != 0)
4a08ce0997bc Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 677
diff changeset
1018 return -1;
4a08ce0997bc Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 677
diff changeset
1019 }
4a08ce0997bc Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 677
diff changeset
1020
4a08ce0997bc Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 677
diff changeset
1021 *result = node->id->func(tmp);
670
0d37fe455b86 More work on evaluator.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
1022 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1023 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1024
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1025 default:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1026 return -16;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1027 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1028
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1029 return 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1030 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1031
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1032
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1033 int dm_eval_exec(DMEvalContext *ev, DMEvalNode *tree, DMValue *presult)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1034 {
677
4b7cccaa62c0 Set result variables to zero.
Matti Hamalainen <ccr@tnsp.org>
parents: 676
diff changeset
1035 DMValue result = 0;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1036 DMEvalNode *node = tree;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1037
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1038 while (node != NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1039 {
677
4b7cccaa62c0 Set result variables to zero.
Matti Hamalainen <ccr@tnsp.org>
parents: 676
diff changeset
1040 DMValue tmp = 0;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1041
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1042 switch (node->op)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1043 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1044 case OP_LSHIFT:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1045 case OP_RSHIFT:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1046
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1047 case OP_AND:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1048 case OP_XOR:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1049 case OP_OR:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1050
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1051 case OP_MUL:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1052 case OP_DIV:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1053 case OP_MOD:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1054 if (dm_eval_get(ev, node->next, &tmp) != 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1055 return -6;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1056
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1057 switch (node->op)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1058 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1059 case OP_DIV:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1060 if (tmp == 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1061 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1062 dm_eval_err(ev, "Division by zero.\n");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1063 return -1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1064 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1065 result /= tmp;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1066 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1067
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1068 case OP_MUL: result *= tmp; break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1069
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1070 case OP_MOD:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1071 if (tmp == 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1072 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1073 dm_eval_err(ev, "Division by zero.\n");
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1074 return -1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1075 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1076 result = DMCONVTYPE result % DMCONVTYPE tmp;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1077 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1078
694
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
1079 case OP_LSHIFT:
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
1080 if (tmp > 31)
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
1081 dm_eval_err(ev, "Left shift count >= width of type");
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
1082 result = DMCONVTYPE result << DMCONVTYPE tmp; break;
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
1083
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
1084 case OP_RSHIFT:
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
1085 if (tmp > 31)
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
1086 dm_eval_err(ev, "Right shift count >= width of type");
b8c8cf55c761 Improve reordering some more, produce less nesting.
Matti Hamalainen <ccr@tnsp.org>
parents: 693
diff changeset
1087 result = DMCONVTYPE result >> DMCONVTYPE tmp; break;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1088
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1089 case OP_AND: result = DMCONVTYPE result & DMCONVTYPE tmp; break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1090 case OP_OR: result = DMCONVTYPE result | DMCONVTYPE tmp; break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1091 case OP_XOR: result = DMCONVTYPE result ^ DMCONVTYPE tmp; break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1092 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1093
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1094 node = node->next;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1095 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1096
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1097 case OP_ADD:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1098 case OP_SUB:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1099 if (dm_eval_get(ev, node->next, &tmp) != 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1100 return -6;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1101
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1102 switch (node->op)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1103 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1104 case OP_ADD: result += tmp; break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1105 case OP_SUB: result -= tmp; break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1106 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1107
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1108 node = node->next;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1109 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1110
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1111 default:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1112 if (dm_eval_get(ev, node, &result) != 0)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1113 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1114 dm_eval_err(ev, "Invalid opcode %d in node %p.\n", node->op, node);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1115 return -4;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1116 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1117 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1118 node = node->next;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1119 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1120
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1121 *presult = result;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1122 return 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1123 }