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
|
/*
* Copyright 2007-2012 Adrian Thurston <thurston@complang.org>
*/
/* This file is part of Colm.
*
* Colm 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.
*
* Colm 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 Colm; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <colm/pool.h>
#include <colm/pdarun.h>
#include <colm/bytecode.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
Str *string_prefix( Program *prg, Str *str, long len )
{
Head *head = stringAllocFull( prg, str->value->data, len );
return (Str*)constructString( prg, head );
}
Str *string_suffix( Program *prg, Str *str, long pos )
{
long len = str->value->length - pos;
Head *head = stringAllocFull( prg, str->value->data + pos, len );
return (Str*)constructString( prg, head );
}
Tree *constructString( Program *prg, Head *s )
{
Str *str = (Str*) treeAllocate( prg );
str->id = LEL_ID_STR;
str->value = s;
return (Tree*)str;
}
/*
* In this system strings are not null terminated. Often strings come from a
* parse, in which case the string is just a pointer into the the data string.
* A block in a parsed stream can house many tokens and there is no room for
* nulls.
*/
Head *stringCopy( Program *prg, Head *head )
{
Head *result = 0;
if ( head != 0 ) {
if ( (char*)(head+1) == head->data )
result = stringAllocFull( prg, head->data, head->length );
else
result = colm_string_alloc_pointer( prg, head->data, head->length );
if ( head->location != 0 ) {
result->location = locationAllocate( prg );
result->location->name = head->location->name;
result->location->line = head->location->line;
result->location->column = head->location->column;
result->location->byte = head->location->byte;
}
}
return result;
}
void stringFree( Program *prg, Head *head )
{
if ( head != 0 ) {
if ( head->location != 0 )
locationFree( prg, head->location );
if ( (char*)(head+1) == head->data ) {
/* Full string allocation. */
free( head );
}
else {
/* Just a string head. */
headFree( prg, head );
}
}
}
const char *stringData( Head *head )
{
if ( head == 0 )
return 0;
return head->data;
}
long stringLength( Head *head )
{
if ( head == 0 )
return 0;
return head->length;
}
void stringShorten( Head *head, long newlen )
{
assert( newlen <= head->length );
head->length = newlen;
}
Head *initStrSpace( long length )
{
/* Find the length and allocate the space for the shared string. */
Head *head = (Head*) malloc( sizeof(Head) + length );
/* Init the header. */
head->data = (char*)(head+1);
head->length = length;
head->location = 0;
/* Save the pointer to the data. */
return head;
}
/* Create from a c-style string. */
Head *stringAllocFull( Program *prg, const char *data, long length )
{
/* Init space for the data. */
Head *head = initStrSpace( length );
/* Copy in the data. */
memcpy( (head+1), data, length );
return head;
}
/* Create from a c-style string. */
Head *colm_string_alloc_pointer( Program *prg, const char *data, long length )
{
/* Find the length and allocate the space for the shared string. */
Head *head = headAllocate( prg );
/* Init the header. */
head->data = data;
head->length = length;
return head;
}
Head *concatStr( Head *s1, Head *s2 )
{
long s1Len = s1->length;
long s2Len = s2->length;
/* Init space for the data. */
Head *head = initStrSpace( s1Len + s2Len );
/* Copy in the data. */
memcpy( (head+1), s1->data, s1Len );
memcpy( (char*)(head+1) + s1Len, s2->data, s2Len );
return head;
}
Head *stringToUpper( Head *s )
{
/* Init space for the data. */
long len = s->length;
Head *head = initStrSpace( len );
/* Copy in the data. */
const char *src = s->data;
char *dst = (char*)(head+1);
int i;
for ( i = 0; i < len; i++ )
*dst++ = toupper( *src++ );
return head;
}
Head *stringToLower( Head *s )
{
/* Init space for the data. */
long len = s->length;
Head *head = initStrSpace( len );
/* Copy in the data. */
const char *src = s->data;
char *dst = (char*)(head+1);
int i;
for ( i = 0; i < len; i++ )
*dst++ = tolower( *src++ );
return head;
}
/* Compare two strings. If identical returns 1, otherwise 0. */
word_t cmpString( Head *s1, Head *s2 )
{
if ( s1->length < s2->length )
return -1;
else if ( s1->length > s2->length )
return 1;
else {
char *d1 = (char*)(s1->data);
char *d2 = (char*)(s2->data);
return memcmp( d1, d2, s1->length );
}
}
word_t strAtoi( Head *str )
{
/* FIXME: need to implement this by hand. There is no null terminator. */
char *nulled = (char*)malloc( str->length + 1 );
memcpy( nulled, str->data, str->length );
nulled[str->length] = 0;
int res = atoi( nulled );
free( nulled );
return res;
}
word_t strAtoo( Head *str )
{
/* FIXME: need to implement this by hand. There is no null terminator. */
char *nulled = (char*)malloc( str->length + 1 );
memcpy( nulled, str->data, str->length );
nulled[str->length] = 0;
int res = strtol( nulled, 0, 8 );
free( nulled );
return res;
}
Head *intToStr( Program *prg, word_t i )
{
char data[20];
sprintf( data, "%ld", i );
return stringAllocFull( prg, data, strlen(data) );
}
word_t strUord16( Head *head )
{
uchar *data = (uchar*)(head->data);
ulong res;
res = (ulong)data[1];
res |= ((ulong)data[0]) << 8;
return res;
}
word_t strUord8( Head *head )
{
uchar *data = (uchar*)(head->data);
ulong res = (ulong)data[0];
return res;
}
Head *makeLiteral( Program *prg, long offset )
{
return colm_string_alloc_pointer( prg,
prg->rtd->litdata[offset],
prg->rtd->litlen[offset] );
}
Head *stringSprintf( Program *prg, Str *format, long integer )
{
Head *formatHead = format->value;
long written = snprintf( 0, 0, stringData(formatHead), integer );
Head *head = initStrSpace( written+1 );
written = snprintf( (char*)head->data, written+1, stringData(formatHead), integer );
head->length -= 1;
return head;
}
|