-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasy_node.cpp
More file actions
319 lines (237 loc) · 5.78 KB
/
Copy patheasy_node.cpp
File metadata and controls
319 lines (237 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
extern "C" {
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "easy_code.h"
}
#include "easy.hpp"
#include "easy_node.hpp"
node *elist=NULL;
node *begn=NULL;
node *current=NULL;
extern const char * debug_type (int dtype);
extern bool subBlock;
fpos_t myftell (FILE *fp);
//======================================================================
// What follows is a bunch of routines for a doubly linked list.
// As this was started in C, rather than C++, I didn't use the STL
// templates. I wanted some specific functionality and my
// C++ was a bit rusty. I didn't feel like debugging this over.
node * lftNode(node * n) {
return n->lft;
}
node * rghtNode(node * n) {
return n->rght;
}
//---------------------------------
// copy nodes - make a copy of the content of a set of nodes
//
node * copyNodes (node * n) {
node * f=NULL;
node * c;
do {
c=(node *) malloc(sizeof(node));
c->dtype=n->dtype;
c->value=n->value;
c->str=n->str;
c->lft=n->lft;
c->rght=n->rght;
if (f==NULL) {
f=c; //remember first link
}
n=n->rght;
}
while (n!=NULL);
f->lft=NULL; // first link goes to NULL
return f;
}
// is list empty
int isEmpty() {
return (elist==NULL);
}
int listLength() {
int length=0;
node *current;
for (current=elist;current!=NULL;current=current->lft) {
length++;
}
return length;
}
void replaceNode(node *n, int type, float v, char * s) {
n->dtype=type;
n->value=v;
n->str=s;
}
//------------------------------------------
// We don't free the contents of nodes because they
// can be reused in macros, etc and this would lead
// to a memory nightmare. Trivial memory leak, well,
// depends on how you think about it.
//
void emptyList() {
begn=NULL;
elist=NULL;
// printf("NC\n");
}
//------------------------
int isCommand(node * n) {
if ((n->dtype>1)&&(n->dtype>100)) {
return 1;
}
return 0;
}
//------------------------
int countArgs(node * n) {
int count=0;
node *ptr=n;
while ((ptr!=NULL)&&(!isCommand(ptr))) {
count++;
ptr = ptr->lft;
}
return count;
}
void displayNode(node *ptr) {
printf("NODE: %s... %f,%s\n",debug_type(ptr->dtype),ptr->value,ptr->str);
}
void displayBackward() {
printf("COMMANDS BACKWARD:\n");
//start from the beginning
node *ptr = elist;
//navigate till the end of the list
int i=0;
while(ptr != NULL) {
i++;
printf("%d (%s,%f,%s,%d)\n",i,debug_type(ptr->dtype),ptr->value,ptr->str,ptr->filled);
ptr = ptr->lft;
}
}
//display the list from begn to first
void displayForward() {
printf("COMMANDS FORWARD:\n");
//start from the begn
node *ptr = begn;
//navigate till the start of the list
int i=0;
while(ptr != NULL) {
i++;
//print data
printf("%d (%s,%f,%s,%d)\n",i,debug_type(ptr->dtype),ptr->value,ptr->str,ptr->filled);
//move to lft item
ptr = ptr ->rght;
}
}
//-----------------------------------
// inserts a list of nodes from another list (a)
// in the middle of this list (n)
//
// node we are at gets replaced (this is for macros)
//
// yes, this kind of nasty and there
// are 5 scenarios to be checked for
// 1) variable is the only item on list
// 2) variable is at start of list
// 3) variable is at end of list
// 4) variable is in the middle of list
// 5) recursive variables
void insertNodes(node *to, node*from) {
node * z;
node *lhs=to->lft;
// node *eolist;
node *eocopy;
node * copy=copyNodes(from);
for (z=copy;z->rght!=NULL;z=z->rght) {
}
eocopy=z;
// (the 'to' node gets replaced and patched over)
copy->lft=lhs;
if (lhs!=NULL) {
lhs->rght=copy;
}
else { // start of list edge case
begn=copy;
}
// end of list: patch it
if (eocopy->rght==NULL) { // if it is at end of line
elist=eocopy;
}
}
//insert link at the first location --------------------
void push(int dtype, float value, char * str) {
// printf("push debug: %d %f\n",dtype,value);
//create a link
node *link = (node*) malloc(sizeof(node));
link->dtype = dtype;
link->value = value;
link->str = str;
link->filled = false;
link->lft=NULL;
link->rght=NULL;
if(isEmpty()) {
//make it the begn link
begn = link;
link->rght=NULL;
} else {
//update first rght link
elist->rght = link;
}
//point it to old first link
link->lft = elist;
//point first to new first link
elist = link;
}
// //insert link at the begn location
// void shift(FILE *fp, int dtype, float value, char * str) {
// fpos_t posi;
// //create a link
// struct node *link = (struct node*) malloc(sizeof(struct node));
// link->dtype = dtype;
// link->value = value;
// link->str = str;
// link->filled = false;
// if(isEmpty()) {
// //make it the begn link
// begn = link;
// } else {
// //make link a new begn link
// begn->lft = link;
// //mark old begn node as rght of new link
// link->rght = begn;
// }
// //point begn to new begn node
// begn = link;
// }
//delete first item
node* pop() {
//save reference to first link
node *tempLink = elist;
//if only one link
if(elist->lft == NULL){
begn = NULL;
} else {
elist->lft->rght = NULL;
}
elist = elist->lft;
//return the deleted link
return tempLink;
}
//delete link at the begn location
node* unshift() {
//save reference to begn link
node *tempLink = begn;
//if only one link
if(elist->lft == NULL) {
elist = NULL;
} else {
begn->rght->lft = NULL;
}
begn = begn->rght;
//return the deleted link
return tempLink;
}
// flags a node for later deletion
void zeroNode(node * n) {
if (n->str!=NULL) free(n->str);
n->str=NULL;
n->dtype=NOOP;
n->value=0;
}