-
Notifications
You must be signed in to change notification settings - Fork 12
/
parser.c
645 lines (626 loc) · 16.3 KB
/
parser.c
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <math.h>
#include <stdint.h>
#include "citrine.h"
char* ctr_cparse_current_program;
ctr_source_map* ctr_source_map_head;
int ctr_source_mapping;
/**
* CTRParserEmitErrorUnexpected
*
* Emits a parser error and adds the file and
* position where the error has occurred.
* Optionally you can pass a hint to this method to
* add some details to the error message.
*/
void ctr_cparse_emit_error_unexpected( int t, char* hint )
{
char* flowMessage = ctr_heap_allocate( 500 );
char* errorMessage = ctr_heap_allocate( 250 );
char* hintMessage = ctr_heap_allocate( 250 );
char* message = ctr_clex_tok_describe( t );
snprintf(errorMessage, 250, CTR_ERR_SYNTAX, message, ctr_cparse_current_program, ctr_clex_line_number+1);
if (hint) {
snprintf(hintMessage, 250, "%s", hint);
} else {
snprintf(hintMessage, 250, "%s", "");
}
snprintf(flowMessage, 500, "%s%s", errorMessage, hintMessage);
CtrStdFlow = ctr_error(flowMessage, 0);
ctr_heap_free(errorMessage);
ctr_heap_free(hintMessage);
ctr_heap_free(flowMessage);
}
/**
* CTRParserCreateNode
*
* Creates a parser node and adds it to the source map.
*/
ctr_tnode* ctr_cparse_create_node( int type ){
ctr_tnode* node = (ctr_tnode*) ctr_heap_allocate_tracked( sizeof( ctr_tnode ) );
if (ctr_source_mapping) {
ctr_source_map* m = (ctr_source_map*) ctr_heap_allocate_tracked( sizeof( ctr_source_map ) );
m->line = ctr_clex_line_number;
m->node = node;
if (ctr_source_map_head) {
m->next = ctr_source_map_head;
ctr_source_map_head = m;
} else {
ctr_source_map_head = m;
}
}
return node;
}
/**
* CTRParserMessage
*
* Creates the AST nodes for sending a message.
*
* - precedence mode 0: no argument (allows processing of unary message, binary message and keyword message)
* - precedence mode 1: as argument of keyword message (allows processing of unary message and binary message)
* - precedence mode 2: as argument of binary message (only allows processing of unary message)
*/
ctr_tnode* ctr_cparse_message(int mode) {
long msgpartlen; /* length of part of message string */
ctr_tnode* m;
int t;
char* s;
char* msg;
ctr_tlistitem* li;
ctr_tlistitem* curlistitem;
int lookAhead;
int isBin;
int first;
ctr_size ulen;
t = ctr_clex_tok();
msgpartlen = ctr_clex_tok_value_length();
if ((msgpartlen) > 255) {
ctr_cparse_emit_error_unexpected( t, CTR_ERR_LONG );
return NULL;
}
m = ctr_cparse_create_node( CTR_AST_NODE );
m->type = -1;
s = ctr_clex_tok_value();
msg = ctr_heap_allocate_tracked( 255 * sizeof( char ) );
memcpy(msg, s, msgpartlen);
ulen = ctr_getutf8len(msg, msgpartlen);
lookAhead = ctr_clex_tok(); ctr_clex_putback();
isBin = (ulen == 1 && lookAhead != CTR_TOKEN_COLON);
if (mode == 2 && isBin) {
ctr_clex_putback();
return m;
}
if (isBin) {
m->type = CTR_AST_NODE_BINMESSAGE;
m->value = msg;
m->vlen = msgpartlen;
li = (ctr_tlistitem*) ctr_heap_allocate_tracked( sizeof(ctr_tlistitem) );
li->node = ctr_cparse_expr(2);
if (li->node == NULL) return NULL;
m->nodes = li;
return m;
}
lookAhead = ctr_clex_tok(); ctr_clex_putback();
if (lookAhead == CTR_TOKEN_COLON) {
if (mode > 0) {
ctr_clex_putback();
return m;
}
*(msg + msgpartlen) = ctr_clex_param_prefix_char;
msgpartlen += 1;
if ((msgpartlen) > 255) {
ctr_cparse_emit_error_unexpected( t, CTR_ERR_LONG );
}
m->type = CTR_AST_NODE_KWMESSAGE;
t = ctr_clex_tok();
first = 1;
while(1) {
li = (ctr_tlistitem*) ctr_heap_allocate_tracked( sizeof(ctr_tlistitem) );
li->node = ctr_cparse_expr(1);
if (li->node == NULL) {
return NULL;
}
if (first) {
m->nodes = li;
curlistitem = m->nodes;
first = 0;
} else {
curlistitem->next = li;
curlistitem = li;
}
t = ctr_clex_tok();
if (t == CTR_TOKEN_DOT) break;
if (t == CTR_TOKEN_FIN) break;
if (t == CTR_TOKEN_CHAIN) break;
if (t == CTR_TOKEN_PARCLOSE) break;
if (t == CTR_TOKEN_REF) {
long l = ctr_clex_tok_value_length();
if ((msgpartlen + l) > 255) {
ctr_cparse_emit_error_unexpected( t, CTR_ERR_LONG );
return NULL;
}
memcpy( (msg+msgpartlen), ctr_clex_tok_value(), l);
msgpartlen = msgpartlen + l;
*(msg + msgpartlen) = ctr_clex_param_prefix_char;
msgpartlen ++;
t = ctr_clex_tok();
if (t != CTR_TOKEN_COLON) {
ctr_cparse_emit_error_unexpected( t, CTR_ERR_EXP_COLON );
}
}
}
ctr_clex_putback(); /* not a colon so put back */
m->value = msg;
m->vlen = msgpartlen;
} else {
m->type = CTR_AST_NODE_UNAMESSAGE;
m->value = msg;
m->vlen = msgpartlen;
}
return m;
}
/**
* CTRParserMessages
*
* Manages the creation of nodes to send a message, uses CTRParserMessage
* to create the actual nodes.
*/
ctr_tlistitem* ctr_cparse_messages(ctr_tnode* r, int mode) {
int t = ctr_clex_tok();
ctr_tlistitem* pli = NULL;
ctr_tlistitem* li = NULL;
ctr_tlistitem* fli = NULL;
int first = 1;
ctr_tnode* node = NULL;
/* explicit chaining (,) only allowed for keyword message: Console write: 3 factorial, write: 3 factorial is not possible otherwise. */
while ((t == CTR_TOKEN_REF || (t == CTR_TOKEN_CHAIN && node && node->type == CTR_AST_NODE_KWMESSAGE))) {
if (t == CTR_TOKEN_CHAIN) {
t = ctr_clex_tok();
if (t != CTR_TOKEN_REF) {
ctr_cparse_emit_error_unexpected( t, CTR_ERR_EXP_MSG );
return NULL;
}
}
li = (ctr_tlistitem*) ctr_heap_allocate_tracked( sizeof(ctr_tlistitem) );
ctr_clex_putback();
node = ctr_cparse_message(mode);
if (node == NULL) return NULL;
if (node->type == -1) {
if (first) {
li->node = node;
return li;
}
ctr_clex_tok();
break;
}
li->node = node;
if (first) {
first = 0;
pli = li;
fli = li;
} else {
pli->next = li;
pli = li;
}
t = ctr_clex_tok();
}
ctr_clex_putback();
if (first) {
ctr_cparse_emit_error_unexpected( t, CTR_ERR_EXP_MSG );
fli = NULL;
}
return fli;
}
/**
* CTRParserPOpen
*
* Generates a set of nested nodes.
*/
ctr_tnode* ctr_cparse_popen() {
ctr_tnode* r;
ctr_tlistitem* li;
int t;
ctr_clex_tok();
r = ctr_cparse_create_node( CTR_AST_NODE );
r->type = CTR_AST_NODE_NESTED;
li = (ctr_tlistitem*) ctr_heap_allocate_tracked( sizeof(ctr_tlistitem) );
r->nodes = li;
li->node = ctr_cparse_expr(0);
if (li->node == NULL) return NULL;
t = ctr_clex_tok();
if (t != CTR_TOKEN_PARCLOSE) {
ctr_cparse_emit_error_unexpected( t, CTR_ERR_EXP_PCLS );
return NULL;
}
return r;
}
/**
* CTRParserBlock
*
* Generates a set of AST nodes to represent a block of code.
*/
ctr_tnode* ctr_cparse_block() {
ctr_tnode* r;
ctr_tlistitem* codeBlockPart1;
ctr_tlistitem* codeBlockPart2;
ctr_tnode* paramList;
ctr_tnode* codeList;
ctr_tlistitem* previousListItem;
ctr_tlistitem* previousCodeListItem;
int t;
int first;
ctr_clex_tok();
r = ctr_cparse_create_node( CTR_AST_NODE );
r->type = CTR_AST_NODE_CODEBLOCK;
codeBlockPart1 = (ctr_tlistitem*) ctr_heap_allocate_tracked( sizeof(ctr_tlistitem) );
codeBlockPart2 = (ctr_tlistitem*) ctr_heap_allocate_tracked( sizeof(ctr_tlistitem) );
r->nodes = codeBlockPart1;
paramList = ctr_cparse_create_node( CTR_AST_NODE );
codeList = ctr_cparse_create_node( CTR_AST_NODE );
codeBlockPart1->node = paramList;
codeBlockPart2->node = codeList;
codeBlockPart1->next = codeBlockPart2;
codeBlockPart2->next = NULL; /* a codeblock has fixed nodes: instr and param, just 2 */
paramList->type = CTR_AST_NODE_PARAMLIST;
codeList->type = CTR_AST_NODE_INSTRLIST;
paramList->value = NULL;
paramList->vlen = 0;
paramList->nodes = NULL;
codeList->type = CTR_AST_NODE_INSTRLIST;
codeList->value = NULL;
codeList->vlen = 0;
codeList->nodes = NULL;
t = ctr_clex_tok();
if (t == CTR_TOKEN_BLOCKCLOSE) {
ctr_cparse_emit_error_unexpected( t, NULL );
return NULL;
}
first = 1;
while(t == CTR_TOKEN_COLON) {
/* okay we have new parameter, load it */
t = ctr_clex_tok();
ctr_tlistitem* paramListItem = (ctr_tlistitem*) ctr_heap_allocate_tracked( sizeof(ctr_tlistitem) );
ctr_tnode* paramItem = ctr_cparse_create_node( CTR_AST_NODE );
long l = ctr_clex_tok_value_length();
paramItem->type = CTR_AST_NODE_PARAMETER;
paramItem->value = ctr_heap_allocate_tracked( sizeof( char ) * l );
memcpy(paramItem->value, ctr_clex_tok_value(), l);
paramItem->vlen = l;
paramListItem->node = paramItem;
if (first) {
paramList->nodes = paramListItem;
previousListItem = paramListItem;
first = 0;
} else {
previousListItem->next = paramListItem;
previousListItem = paramListItem;
}
t = ctr_clex_tok();
}
first = 1;
while((first || t == CTR_TOKEN_DOT)) {
ctr_tlistitem* codeListItem;
ctr_tnode* codeNode;
if (first) {
ctr_clex_putback();
}
t = ctr_clex_tok();
if (t == CTR_TOKEN_BLOCKCLOSE) break;
ctr_clex_putback();
codeListItem = (ctr_tlistitem*) ctr_heap_allocate_tracked( sizeof(ctr_tlistitem) );
codeNode = ctr_cparse_create_node( CTR_AST_NODE );
if (t == CTR_TOKEN_RET) {
codeNode = ctr_cparse_ret();
} else {
codeNode = ctr_cparse_expr(0);
}
if (codeNode == NULL) return NULL;
codeListItem->node = codeNode;
if (first) {
codeList->nodes = codeListItem;
previousCodeListItem = codeListItem;
first = 0;
} else {
previousCodeListItem->next = codeListItem;
previousCodeListItem = codeListItem;
}
t = ctr_clex_tok();
if (t != CTR_TOKEN_DOT) {
ctr_cparse_emit_error_unexpected( t, CTR_ERR_EXP_DOT );
return NULL;
}
}
return r;
}
/**
* CTRParserReference
*
* Generates the nodes to respresent a variable or property.
*/
ctr_tnode* ctr_cparse_ref() {
ctr_tnode* r;
char* tmp;
ctr_clex_tok();
r = ctr_cparse_create_node( CTR_AST_NODE );
r->type = CTR_AST_NODE_REFERENCE;
r->vlen = ctr_clex_tok_value_length();
tmp = ctr_clex_tok_value();
if (strncmp(ctr_clex_keyword_my_icon, tmp, ctr_clex_keyword_my_icon_len)==0 && r->vlen == ctr_clex_keyword_my_icon_len) {
int t = ctr_clex_tok();
if (t != CTR_TOKEN_REF) {
ctr_cparse_emit_error_unexpected( t, CTR_ERR_EXP_KEY );
return NULL;
}
tmp = ctr_clex_tok_value();
r->modifier = 1;
r->vlen = ctr_clex_tok_value_length();
}
if (
(strncmp(">>", tmp, 2)==0 && r->vlen == 2) ||
(strncmp(ctr_clex_keyword_var_icon, tmp, ctr_clex_keyword_var_icon_len)==0 && r->vlen == ctr_clex_keyword_var_icon_len)
) {
int t = ctr_clex_tok();
if (t != CTR_TOKEN_REF) {
ctr_cparse_emit_error_unexpected( t, CTR_ERR_EXP_VAR );
return NULL;
}
tmp = ctr_clex_tok_value();
r->modifier = 2;
r->vlen = ctr_clex_tok_value_length();
}
r->value = ctr_heap_allocate_tracked( r->vlen );
memcpy(r->value, tmp, r->vlen);
return r;
}
/**
* CTRParserString
*
* Generates a node to represent a string.
*/
ctr_tnode* ctr_cparse_string() {
ctr_tnode* r;
char* n;
ctr_size vlen;
ctr_clex_tok();
r = ctr_cparse_create_node( CTR_AST_NODE );
r->type = CTR_AST_NODE_LTRSTRING;
n = ctr_clex_readstr();
vlen = ctr_clex_tok_value_length();
r->value = ctr_heap_allocate_tracked( sizeof( char ) * vlen );
memcpy(r->value, n, vlen);
r->vlen = vlen;
ctr_heap_free(n);
return r;
}
/**
* CTRParserNumber
*
* Generates a node to represent a number.
*/
ctr_tnode* ctr_cparse_number() {
char* n;
ctr_tnode* r;
long l;
ctr_clex_tok();
r = ctr_cparse_create_node( CTR_AST_NODE );
r->type = CTR_AST_NODE_LTRNUM;
n = ctr_clex_tok_value();
l = ctr_clex_tok_value_length();
r->value = ctr_heap_allocate_tracked( sizeof( char ) * l );
memcpy(r->value, n, l);
r->vlen = l;
return r;
}
/**
* CTRParserReceiver
*
* Generates a node to represent a receiver (of a message).
*/
ctr_tnode* ctr_cparse_receiver() {
int t;
t = ctr_clex_tok();
ctr_clex_putback();
switch(t){
case CTR_TOKEN_NUMBER:
return ctr_cparse_number();
case CTR_TOKEN_QUOTE:
return ctr_cparse_string();
case CTR_TOKEN_REF:
return ctr_cparse_ref();
case CTR_TOKEN_BLOCKOPEN:
return ctr_cparse_block();
case CTR_TOKEN_PAROPEN:
return ctr_cparse_popen();
default:
/* This function always exits, so return a dummy value. */
ctr_cparse_emit_error_unexpected( t, CTR_ERR_EXP_RCP );
return NULL;
}
}
/**
* CTRParserAssignment
*
* Generates a node to represent an assignment.
*/
ctr_tnode* ctr_cparse_assignment(ctr_tnode* r) {
ctr_tnode* a;
ctr_tlistitem* li;
ctr_tlistitem* liAssignExpr;
ctr_clex_tok();
a = ctr_cparse_create_node( CTR_AST_NODE );
li = (ctr_tlistitem*) ctr_heap_allocate_tracked( sizeof(ctr_tlistitem) );
liAssignExpr = (ctr_tlistitem*) ctr_heap_allocate_tracked( sizeof(ctr_tlistitem) );
a->type = CTR_AST_NODE_EXPRASSIGNMENT;
a->nodes = li;
li->node = r;
liAssignExpr->node = ctr_cparse_expr(0);
if (liAssignExpr->node == NULL) return NULL;
li->next = liAssignExpr;
return a;
}
/**
* CTRParserExpression
*
* Generates a set of nodes to represent an expression.
*/
ctr_tnode* ctr_cparse_expr(int mode) {
ctr_tnode* r;
ctr_tnode* e;
int t2;
int t3;
ctr_tlistitem* nodes;
ctr_tlistitem* rli;
r = ctr_cparse_receiver();
if (r == NULL) return NULL;
t2 = ctr_clex_tok();
t3 = ctr_clex_tok();
ctr_clex_putback();
ctr_clex_putback();
/* new part of message begins, end of argument */
if (mode == 1 && t2 == CTR_TOKEN_REF && t3 == CTR_TOKEN_COLON ) {
return r;
}
/* user tries to put colon directly after recipient */
if ( t2 == CTR_TOKEN_COLON ) {
ctr_cparse_emit_error_unexpected( t2, CTR_ERR_EXP_MSG2 );
return NULL;
}
if ( t2 == CTR_TOKEN_BLOCKCLOSE ) {
ctr_cparse_emit_error_unexpected( t2, NULL );
return NULL;
}
if ( t2 == CTR_TOKEN_ASSIGNMENT ) {
if ( r->type != CTR_AST_NODE_REFERENCE ) {
ctr_cparse_emit_error_unexpected( t2, CTR_ERR_INV_LAS );
return NULL;
}
e = ctr_cparse_assignment(r);
} else if (
t2 != CTR_TOKEN_DOT &&
t2 != CTR_TOKEN_PARCLOSE &&
t2 != CTR_TOKEN_CHAIN &&
t2 != CTR_TOKEN_FIN
) {
e = ctr_cparse_create_node( CTR_AST_NODE );
e->type = CTR_AST_NODE_EXPRMESSAGE;
nodes = ctr_cparse_messages(r, mode);
if (nodes == NULL) {
return NULL;
}
if (nodes->node->type == -1) {
ctr_clex_tok();
ctr_clex_putback();
return r; /* no messages, then just return receiver (might be in case of argument). */
}
rli = (ctr_tlistitem*) ctr_heap_allocate_tracked( sizeof(ctr_tlistitem) );
rli->node = r;
rli->next = nodes;
e->nodes = rli;
} else {
return r;
}
return e;
}
/**
* CTRParserReturn
*
* Generates a node to represent a return from a block of code.
*/
ctr_tnode* ctr_cparse_ret() {
ctr_tlistitem* li;
ctr_tnode* r;
ctr_clex_tok();
r = ctr_cparse_create_node( CTR_AST_NODE );
r->type = CTR_AST_NODE_RETURNFROMBLOCK;
li = (ctr_tlistitem*) ctr_heap_allocate_tracked( sizeof(ctr_tlistitem) );
r->nodes = li;
li->node = ctr_cparse_expr(0);
if (li->node == NULL) return NULL;
return r;
}
/**
* CTRParserFin
*
* Generates a node to represent the end of a program.
*/
ctr_tnode* ctr_cparse_fin() {
ctr_tnode* f;
ctr_clex_tok();
f = ctr_cparse_create_node( CTR_AST_NODE );
f->type = CTR_AST_NODE_ENDOFPROGRAM;
return f;
}
/**
* CTRParserStatement
*
* Generates a set of nodes representing a statement.
*/
ctr_tlistitem* ctr_cparse_statement() {
ctr_tlistitem* li = (ctr_tlistitem*) ctr_heap_allocate_tracked( sizeof(ctr_tlistitem) );
int t = ctr_clex_tok();
ctr_clex_putback();
if (t == CTR_TOKEN_FIN) {
li->node = ctr_cparse_fin();
return li;
} else if (t == CTR_TOKEN_RET) {
li->node = ctr_cparse_ret();
if (li->node == NULL) return NULL;
} else {
li->node = ctr_cparse_expr(0);
if (li->node == NULL) return NULL;
}
t = ctr_clex_tok();
if (t != CTR_TOKEN_DOT) {
ctr_cparse_emit_error_unexpected( t, CTR_ERR_EXP_DOT );
return NULL;
}
return li;
}
/**
* CTRParserProgram
*
* Generates the nodes to represent the entire program
* as an Abstract Syntax Tree (AST).
*/
ctr_tnode* ctr_cparse_program() {
ctr_tnode* program = ctr_cparse_create_node( CTR_AST_PROGRAM );
ctr_tlistitem* pli;
int first = 1;
while(1) {
ctr_tlistitem* li = ctr_cparse_statement();
if (li == NULL) {
return NULL;
}
if (first) {
first = 0;
program->nodes = li;
} else {
pli->next = li;
}
if (li->node->type == CTR_AST_NODE_ENDOFPROGRAM) break;
pli = li;
}
return program;
}
/**
* CTRParserStart
*
* Begins the parsing stage of a program.
*/
ctr_tnode* ctr_cparse_parse(char* prg, char* pathString) {
ctr_tnode* program;
ctr_clex_load(prg);
ctr_cparse_current_program = pathString;
program = ctr_cparse_program();
if (program == NULL) {
return NULL;
}
program->value = pathString;
program->vlen = strlen(pathString);
program->type = CTR_AST_NODE_PROGRAM;
return program;
}