File: parsetree.cpp

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (353 lines) | stat: -rw-r--r-- 7,354 bytes parent folder | download
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
===========================================================================
Copyright (C) 2025 the OpenMoHAA team

This file is part of OpenMoHAA source code.

OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

// parsetree.cpp: Abstract Syntax Layer for Lexer/Parser

#include "parsetree.h"
#include "../fgame/gamecvars.h"
#include "../qcommon/mem_tempalloc.h"

MEM_TempAlloc parsetree_allocator;
yyparsedata   parsedata;
sval_u        node_none = {0};

char *str_replace(char *orig, const char *rep, const char *with)
{
    char  *result;    // the return string
    char  *ins;       // the next insert point
    char  *tmp;       // varies
    size_t len_rep;   // length of rep
    size_t len_with;  // length of with
    size_t len_front; // distance between rep and end of last rep
    int    count;     // number of replacements

    if (!orig) {
        return NULL;
    }
    if (!rep) {
        rep = "";
    }
    len_rep = strlen(rep);
    if (!with) {
        with = "";
    }
    len_with = strlen(with);

    ins = orig;
    for (count = 0; (tmp = strstr(ins, rep)) != nullptr; ++count) {
        ins = tmp + len_rep;
    }

    // first time through the loop, all the variable are set correctly
    // from here on,
    //    tmp points to the end of the result string
    //    ins points to the next occurrence of rep in orig
    //    orig points to the remainder of orig after "end of rep"
    tmp = result = (char *)parsetree_allocator.Alloc(strlen(orig) + (len_with - len_rep) * count + 1);

    if (!result) {
        return NULL;
    }

    while (count--) {
        ins       = strstr(orig, rep);
        len_front = ins - orig;
        tmp       = strncpy(tmp, orig, len_front) + len_front;
        tmp       = strcpy(tmp, with) + len_with;
        orig += len_front + len_rep; // move to next "end of rep"
    }
    strcpy(tmp, orig);
    return result;
}

void parsetree_freeall()
{
    parsetree_allocator.FreeAll();

    if (g_showopcodes->integer) {
        gi.DPrintf("%d bytes freed\n", parsedata.total_length);
    }
}

void parsetree_init()
{
    parsedata.total_length = 0;
}

size_t parsetree_length()
{
    return parsedata.total_length;
}

#if 0
char* parsetree_string(const char* string)
{
	//char *pszString = ( char * )parsetree_allocator.Alloc( strlen( string ) + 1 );
	//strcpy( pszString, string );

	char* buffer = str_replace((char*)string, "\\\"", "\"");

	if (buffer)
	{
		char* ptr = buffer;

		if (ptr[0] == '"')
		{
			ptr++;
		}

		int len = strlen(buffer);

		if (buffer[len - 1] == '"')
		{
			buffer[len - 1] = 0;
		}

		buffer = ptr;
	}

	return buffer;
}
#endif

extern size_t yyleng;
extern size_t prev_yyleng;

char *parsetree_malloc(size_t s)
{
    parsedata.total_length += s;
    return (char *)parsetree_allocator.Alloc(s);
}

sval_u append_lists(sval_u val1, sval_u val2)
{
    val1.node[1].node[1] = val2.node[0];
    val1.node[1]         = val2.node[1];

    return val1;
}

sval_u append_node(sval_u val1, sval_u val2)
{
    sval_u *node;

    node = (sval_u *)parsetree_malloc(sizeof(sval_t[2]));

    node[1].node = NULL;
    node[0]      = val2;

    val1.node[1].node[1].node = node;
    val1.node[1].node         = node;

    return val1;
}

sval_u prepend_node(sval_u val1, sval_u val2)
{
    sval_u *node;

    node = (sval_u *)parsetree_malloc(sizeof(sval_t[2]));

    node[0] = val1;
    node[1] = val2;

    val2.node = node;

    return val2;
}

sval_u linked_list_end(sval_u val)
{
    sval_u *node;
    sval_u  end;

    node = (sval_u *)parsetree_malloc(sizeof(sval_t[2]));

    node[0]      = val;
    node[1].node = NULL;

    end.node = (sval_u *)parsetree_malloc(sizeof(sval_t[2]));

    end.node[0].node = node;
    end.node[1].node = node;

    return end;
}

sval_u node1_(int val1)
{
    sval_u val;

    val.intValue = val1;

    return val;
}

sval_u node1b(int val1)
{
    sval_u val;

    val.byteValue = val1;

    return val;
}

sval_u node_pos(unsigned int pos)
{
    sval_u val;

    val.sourcePosValue = pos;

    return val;
}

sval_u node_string(char *text)
{
    sval_u val;

    val.stringValue = text;

    return val;
}

sval_u node0(int type)
{
    sval_u val;

    if (type == ENUM_NOP) {
        // memory optimization
        val.node = &node_none;
    } else {
        val.node = (sval_u *)parsetree_malloc(sizeof(sval_u));

        val.node[0].node = NULL;
        val.node[0].type = type;
    }

    return val;
}

sval_u node1(int type, sval_u val1)
{
    sval_u val;

    val.node = (sval_u *)parsetree_malloc(sizeof(sval_u[2]));

    val.node[0].type = type;
    val.node[1]      = val1;

    return val;
}

sval_u node2(int type, sval_u val1, sval_u val2)
{
    sval_u val;

    assert(type != ENUM_NOP);

    val.node = (sval_u *)parsetree_malloc(sizeof(sval_t[3]));

    val.node[0].type = type;
    val.node[1]      = val1;
    val.node[2]      = val2;

    return val;
}

sval_u node3(int type, sval_u val1, sval_u val2, sval_u val3)
{
    sval_u val;

    assert(type != ENUM_NOP);

    val.node = (sval_u *)parsetree_malloc(sizeof(sval_t[4]));

    val.node[0].type = type;
    val.node[1]      = val1;
    val.node[2]      = val2;
    val.node[3]      = val3;

    return val;
}

sval_u node4(int type, sval_u val1, sval_u val2, sval_u val3, sval_u val4)
{
    sval_u val;

    assert(type != ENUM_NOP);

    val.node = (sval_u *)parsetree_malloc(sizeof(sval_t[5]));

    val.node[0].type = type;
    val.node[1]      = val1;
    val.node[2]      = val2;
    val.node[3]      = val3;
    val.node[4]      = val4;

    return val;
}

sval_u node5(int type, sval_u val1, sval_u val2, sval_u val3, sval_u val4, sval_u val5)
{
    sval_u val;

    assert(type != ENUM_NOP);

    val.node = (sval_u *)parsetree_malloc(sizeof(sval_t[6]));

    val.node[0].type = type;
    val.node[1]      = val1;
    val.node[2]      = val2;
    val.node[3]      = val3;
    val.node[4]      = val4;
    val.node[5]      = val5;

    return val;
}

sval_u node6(int type, sval_u val1, sval_u val2, sval_u val3, sval_u val4, sval_u val5, sval_u val6)
{
    sval_u val;

    assert(type != ENUM_NOP);

    val.node = (sval_u *)parsetree_malloc(sizeof(sval_t[7]));

    val.node[0].type = type;
    val.node[1]      = val1;
    val.node[2]      = val2;
    val.node[3]      = val3;
    val.node[4]      = val4;
    val.node[5]      = val5;
    val.node[6]      = val6;

    return val;
}

sval_u node_listener(sval_u val1, sval_u val2)
{
    if (!str::icmp(val1.stringValue, "self")) {
        return node2(ENUM_listener, node1_(method_self), val2);
    } else {
        return node2(ENUM_string, val1, val2);
    }
}