summaryrefslogtreecommitdiff
path: root/src/tibchar.c
blob: ec9b3ee717aec72ff4c50baf51a43ed5ea3b2948 (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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
 *  libtib - Read, write, and evaluate TI BASIC programs
 *  Copyright (C) 2015-2016 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 <pfxtree.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include "tibchar.h"
#include "tiberr.h"

static PrefixTree *keywords = NULL;

const char *
tib_special_char_text (int c)
{
  switch (c)
    {
    case TIB_CHAR_AND:
      return " And ";

    case TIB_CHAR_ANS:
      return "Ans";

    case TIB_CHAR_AXESOFF:
      return "AxesOff";

    case TIB_CHAR_CLEARDRAW:
      return "ClrDraw";

    case TIB_CHAR_CLEARHOME:
      return "ClrHome";

    case TIB_CHAR_CLRLIST:
      return "ClrList";

    case TIB_CHAR_COS:
      return "cos(";

    case TIB_CHAR_DEGREE:
      return "*(180/pi)";

    case TIB_CHAR_DELVAR:
      return "DelVar";

    case TIB_CHAR_DIFFERENT:
      return "~";

    case TIB_CHAR_DIM:
      return "Dim(";

    case TIB_CHAR_DISP:
      return "Disp(";

    case TIB_CHAR_ELSE:
      return "Else";

    case TIB_CHAR_END:
      return "End";

    case TIB_CHAR_EPOW10:
      return "*10^";

    case TIB_CHAR_FILL:
      return "Fill(";

    case TIB_CHAR_FOR:
      return "For(";

    case TIB_CHAR_GETKEY:
      return "GetKey";

    case TIB_CHAR_GOTO:
      return "Goto ";

    case TIB_CHAR_IF:
      return "If ";

    case TIB_CHAR_INPUT:
      return "Input ";

    case TIB_CHAR_INT:
      return "int(";

    case TIB_CHAR_L1:
      return "L\\1";

    case TIB_CHAR_L2:
      return "L\\2";

    case TIB_CHAR_L3:
      return "L\\3";

    case TIB_CHAR_L4:
      return "L\\4";

    case TIB_CHAR_L5:
      return "L\\5";

    case TIB_CHAR_L6:
      return "L\\6";

    case TIB_CHAR_L7:
      return "L\\7";

    case TIB_CHAR_L8:
      return "L\\8";

    case TIB_CHAR_L9:
      return "L\\9";

    case TIB_CHAR_LABEL:
      return "Lbl ";

    case TIB_CHAR_LINE:
      return "Line(";

    case TIB_CHAR_MATA:
      return "[[A]]";

    case TIB_CHAR_MATB:
      return "[[B]]";

    case TIB_CHAR_MATC:
      return "[[C]]";

    case TIB_CHAR_MATD:
      return "[[D]]";

    case TIB_CHAR_MATE:
      return "[[E]]";

    case TIB_CHAR_MATF:
      return "[[F]]";

    case TIB_CHAR_MATG:
      return "[[G]]";

    case TIB_CHAR_MATH:
      return "[[H]]";

    case TIB_CHAR_MATI:
      return "[[I]]";

    case TIB_CHAR_MENU:
      return "Menu(";

    case TIB_CHAR_NOT:
      return "Not(";

    case TIB_CHAR_OR:
      return " Or ";

    case TIB_CHAR_OUTPUT:
      return "Output(";

    case TIB_CHAR_PAUSE:
      return "Pause ";

    case TIB_CHAR_PI:
      return "pi";

    case TIB_CHAR_PIC1:
      return "Pic1";

    case TIB_CHAR_PIXEL_TEST:
      return "pxl-Test(";

    case TIB_CHAR_RAND:
      return "RAND";

    case TIB_CHAR_RANDINT:
      return "RandInt(";

    case TIB_CHAR_RECALLPIC:
      return "RecallPic ";

    case TIB_CHAR_REPEAT:
      return "Repeat ";

    case TIB_CHAR_RETURN:
      return "Return ";

    case TIB_CHAR_ROUND:
      return "Round(";

    case TIB_CHAR_SIN:
      return "sin(";

    case TIB_CHAR_STO:
      return "$";

    case TIB_CHAR_STOP:
      return "Stop ";

    case TIB_CHAR_STOREPIC:
      return "StorePic ";

    case TIB_CHAR_TAN:
      return "tan(";

    case TIB_CHAR_TEXT:
      return "Text(";

    case TIB_CHAR_THEN:
      return "Then";

    case TIB_CHAR_THETA:
      return "Theta";

    case TIB_CHAR_WHILE:
      return "While ";

    case TIB_CHAR_XMIN:
      return "Xmin";

    case TIB_CHAR_XMAX:
      return "Xmax";

    case TIB_CHAR_XSCL:
      return "Xscl";

    case TIB_CHAR_YMIN:
      return "Ymin";

    case TIB_CHAR_YMAX:
      return "Ymax";

    case TIB_CHAR_YSCL:
      return "Yscl";

    default:
      return NULL;
    }
}

static int
load_range (int beg, int end)
{
  int rc = 0, i;
  for (i = beg; i <= end; ++i)
    {
      const char *trans = tib_special_char_text (i);
      if (NULL == trans)
        continue;

      rc = pt_add (keywords, trans, i);
      if (rc)
        return rc;
    }

  return rc;
}

static int
tokenize (struct tib_expr *expr, char *beg)
{
  int rc;
  char *orig = beg;
  size_t len = strlen (beg);

  rc = tib_expr_init (expr);
  if (rc)
    return rc;

  while (beg < orig + len)
    {
      char temp;
      char *end = strchr (beg, '(');

      if (end)
        {
          temp = *(++end);
          *end = '\0';

          const PrefixTree *t = pt_search (keywords, beg);
          *end = temp;

          if (t)
            {
              rc = tib_expr_push (expr, pt_data (t));
              if (rc)
                goto fail;

              beg = end;
              continue;
            }
        }

      bool found = false;
      for (end = beg + 1; end <= orig + len; ++end)
        {
          temp = *end;
          *end = '\0';

          const PrefixTree *t = pt_search (keywords, beg);
          if (t)
            {
              rc = tib_expr_push (expr, pt_data (t));
              if (rc)
                goto fail;

              beg = end;
              found = true;
            }

          *end = temp;
          if (found)
            break;
        }

      if (!found)
        {
          rc = tib_expr_push (expr, *beg);
          if (rc)
            goto fail;

          ++beg;
        }
    }

  return rc;

 fail:
  tib_expr_destroy (expr);
  return rc;
}

int
tib_encode_str (struct tib_expr *expr, const char *s)
{
  int rc = 0;
  char *buf, *beg;
  size_t line_len = strlen (s);

  buf = malloc ((line_len + 1) * sizeof (char));
  if (!buf)
    return TIB_EALLOC;

  strcpy (buf, s);
  beg = buf;
  expr->len = 0;

  while (beg < buf + line_len)
    {
      char *end = strchr (beg, '"');
      if (end)
        *end = '\0';

      struct tib_expr part;
      rc = tokenize (&part, beg);
      if (rc)
        break;

      rc = tib_exprcat (expr, &part);
      tib_expr_destroy (&part);
      if (rc)
        break;

      if (!end)
        break;

      beg = end + 1;
      end = strchr (beg, '"');

      if (!end)
        end = buf + line_len;

      rc = tib_expr_push (expr, '"');
      if (rc)
        break;

      for (; beg < end; ++beg)
        {
          rc = tib_expr_push (expr, *beg);
          if (rc)
            goto end;
        }

      if (*end)
        {
          rc = tib_expr_push (expr, *end);
          if (rc)
            break;
        }

      beg = end + 1;
    }

 end:
  if (rc)
    tib_expr_destroy (expr);

  free (buf);
  return rc;
}

int
tib_keyword_init ()
{
  if (keywords)
    return 0;

  keywords = pt_new ();
  if (NULL == keywords)
    return TIB_EALLOC;

  int rc = load_range (TIB_FIRST_CHAR, TIB_LAST_CHAR);
  if (rc)
    tib_keyword_free ();

  return rc;
}

void
tib_keyword_free ()
{
  if (keywords)
    {
      pt_free (keywords);
      keywords = NULL;
    }
}