summaryrefslogtreecommitdiff
path: root/src/tibexpr.c
blob: 2f122d260dc3da469b42eb3f8aab36b265ffd38b (plain)
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
/*
 *  libtib - Read, write, and evaluate TI BASIC programs
 *  Copyright (C) 2015-2017 Delwink, LLC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, version 3 only.
 *
 *  This program 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <string.h>

#include "tiberr.h"
#include "tibexpr.h"
#include "tibchar.h"
#include "tibeval.h"

#define BUFFER_BLOCK_SIZE 16

int
tib_expr_init(struct tib_expr *self)
{
	self->data = malloc(BUFFER_BLOCK_SIZE * sizeof(int));
	if (!self->data)
		return TIB_EALLOC;

	self->bufsize = BUFFER_BLOCK_SIZE;
	self->len = 0;

	return 0;
}

void
tib_expr_destroy(struct tib_expr *self)
{
	if (self->bufsize)
	{
		free(self->data);

		self->data = NULL;
		self->bufsize = 0;
		self->len = 0;
	}
}

int
tib_exprcpy(struct tib_expr *dest, const struct tib_expr *src)
{
	dest->len = 0;
	return tib_exprcat(dest, src);
}

int
tib_exprcat(struct tib_expr *dest, const struct tib_expr *src)
{
	int rc;

	if (!dest->bufsize)
	{
		rc = tib_expr_init(dest);
		if (rc)
			return rc;
	}

	int i;
	tib_expr_foreach(src, i)
	{
		rc = tib_expr_push(dest, src->data[i]);
		if (rc)
		{
			tib_expr_destroy(dest);
			return rc;
		}
	}

	return 0;
}

char *
tib_expr_tostr_f(const struct tib_expr *self, const char *(*get_special)(int))
{
	if (!self->data)
	{
		tib_errno = TIB_ENULLPTR;
		return NULL;
	}

	int i, len = 1;
	tib_expr_foreach(self, i)
	{
		const char *special = get_special(self->data[i]);
		if (special)
			len += strlen(special);
		else
			++len;
	}

	char *out = malloc(len * sizeof(char));
	if (!out)
	{
		tib_errno = TIB_EALLOC;
		return NULL;
	}

	int bump = 0;
	tib_expr_foreach(self, i)
	{
		const char *special = get_special(self->data[i]);
		if (special)
		{
			out[i + bump] = '\0';
			strcat(out, special);
			bump += strlen(special) - 1;
		}
		else
		{
			out[i + bump] = self->data[i];
		}
	}

	out[i + bump] = '\0';
	return out;
}

char *
tib_expr_tostr(const struct tib_expr *self)
{
	return tib_expr_tostr_f(self, tib_special_char_text);
}

int
tib_expr_parse_complex(const struct tib_expr *self, gsl_complex *out)
{
	if (!tib_eval_isnum(self))
		return TIB_ESYNTAX;

	const int len = self->len;
	char s[len + 1];

	for (int i = 0; i < len; ++i)
		s[i] = self->data[i];

	s[len] = '\0';

	char *i_start = NULL;
	if (contains_i(self))
	{
		int num_operators = sign_count(self);
		for (int i = 0; i < len; ++i)
		{
			int c = self->data[i];

			if ('i' == c)
			{
				i_start = s;
				break;
			}
			else if (is_sign_operator(c) && --num_operators == 0)
			{
				i_start = &s[i];
				break;
			}
		}
	}

	GSL_SET_REAL(out, s == i_start ? 0 : strtod(s, NULL));

	if (i_start)
	{
		if (is_sign_operator(i_start[0]) && 'i' == i_start[1])
			i_start[1] = '1';
		else if ('i' == i_start[0])
			i_start[0] = '1';
	}

	GSL_SET_IMAG(out, i_start ? strtod(i_start, NULL) : 0);

	return 0;
}

int
tib_expr_delete(struct tib_expr *self, int i)
{
	if (i > self->len)
		return TIB_EINDEX;

	--self->len;

	for (; i < self->len; ++i)
		self->data[i] = self->data[i + 1];

	return 0;
}

int
tib_expr_insert(struct tib_expr *self, int i, int c)
{
	if (i > ++self->len)
	{
		--self->len;
		return TIB_EINDEX;
	}

	if (!self->bufsize)
	{
		struct tib_expr temp = { .bufsize = 0 };
		--self->len;

		int rc = tib_exprcpy(&temp, self);
		if (rc)
			return rc;

		*self = temp;
		++self->len;
	}
	else if (self->len > self->bufsize)
	{
		if (16384 == self->bufsize)
		{
			return TIB_EALLOC;
		}
		else
		{
			int *old = self->data;
			self->bufsize *= 2;

			self->data = realloc(self->data,
					self->bufsize * sizeof(int));
			if (!self->data)
			{
				self->bufsize /= 2;
				--self->len;
				self->data = old;
				return TIB_EALLOC;
			}
		}
	}

	for (int j = self->len - 1; j > i; --j)
		self->data[j] = self->data[j - 1];

	self->data[i] = c;
	return 0;
}

int
tib_expr_push(struct tib_expr *self, int c)
{
	return tib_expr_insert(self, self->len, c);
}

int
tib_expr_indexof(const struct tib_expr *self, int c)
{
	for (int i = 0; i < self->len; ++i)
		if (c == self->data[i])
			return i;

	return -1;
}

int
tib_expr_indexof_r(const struct tib_expr *self, int c)
{
	for (int i = self->len - 1; i >= 0; --i)
		if (c == self->data[i])
			return i;

	return -1;
}

int
tib_subexpr(struct tib_expr *dest, const struct tib_expr *src, int beg,
	int end)
{
	if (end > src->len || end < beg)
		return TIB_EINDEX;

	dest->len = end - beg;
	dest->bufsize = 0;
	dest->data = &src->data[beg];

	return 0;
}