summaryrefslogtreecommitdiffabout
path: root/lib/forlangrm.y
blob: e735072febb3d61ef9afc6237a95bc6560f824cf (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
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
%{
/* This file is part of Eclat.
   Copyright (C) 2012-2014 Sergey Poznyakoff.
 
   Eclat 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 3, or (at your option)
   any later version.
 
   Eclat 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 Eclat.  If not, see <http://www.gnu.org/licenses/>. */

#include "libeclat.h"
#include <grecs.h>
#include <grecs-locus.h>
#include "forlangrm.h"
#include "forlan.h"
#include <string.h>

int yylex();	
static int yyerror(char *);
union forlan_node *forlan_parse_tree;
size_t forlan_variable_count;
static struct grecs_symtab *forlan_symtab;

static size_t find_variable(const char *name);
static union forlan_node *create_comp_node(union forlan_node *root,
					   struct grecs_list *arglist,
					   int wildcards);

struct path_component {
	char *id;
	char *label;
};

%}
%error-verbose
%locations

%union {
	char *string;
	union forlan_node *node;
	struct grecs_list *list;
	struct {
		struct path_component *comp;
		int wildcard;
	} comp;
	struct {
		int wildcards;
		struct grecs_list *list;
	} complist;
};

%token <string> STRING IDENT
%token LAST IF ELSE LET FOR IN BREAK CONTINUE STOP

%left OR
%left AND
%nonassoc EQ NE
%left NOT

%type <node> stmt stmt_cond stmt_expr stmt_blk cond bool node funcall arg rval
%type <node> stmt_let stmt_for stmt_ctrl stmt_asgn
%type <list> stmtlist arglist
%type <string> string
%type <complist> complist
%type <comp> comp

%%
input     : stmtlist
            {
		    forlan_parse_tree = forlan_stmt_from_list($1);
	    }
          ;

stmtlist  : stmt
            {
		    $$ = forlan_stmt_list();
		    grecs_list_append($$, $1);
	    }
          | stmtlist stmt
	    {
		    grecs_list_append($1, $2);
		    $$ = $1;
	    }
          ;

stmt      : stmt_cond
          | stmt_expr
          | stmt_blk
          | stmt_let
          | stmt_asgn
          | stmt_for
          | stmt_ctrl
          ;

stmt_blk  : '{' stmtlist '}'
            {
		    $$ = forlan_stmt_from_list($2);
	    }
          ;

stmt_cond : IF cond stmt
            {
		    $$ = forlan_node_create(forlan_type_cond);
		    $$->cond.expr = $2;
		    $$->cond.iftrue = $3;
		    $$->cond.iffalse = NULL;
	    }
          | IF cond stmt ELSE stmt
            {
		    $$ = forlan_node_create(forlan_type_cond);
		    $$->cond.expr = $2;
		    $$->cond.iftrue = $3;
		    $$->cond.iffalse = $5;
	    }
          ;

cond      : bool
          ;

bool      : node
            {
		    $$ = forlan_node_create(forlan_type_expr);
		    $$->expr.opcode = forlan_opcode_node;
		    $$->expr.arg[0] = $1;
	    }
          | bool AND bool
            {
		    $$ = forlan_node_create(forlan_type_expr);
		    $$->expr.opcode = forlan_opcode_and;
		    $$->expr.arg[0] = $1;
		    $$->expr.arg[1] = $3;
	    }
          | bool OR bool
            {
		    $$ = forlan_node_create(forlan_type_expr);
		    $$->expr.opcode = forlan_opcode_or;
		    $$->expr.arg[0] = $1;
		    $$->expr.arg[1] = $3;
	    }
          | NOT bool
            {
		    $$ = forlan_node_create(forlan_type_expr);
		    $$->expr.opcode = forlan_opcode_not;
		    $$->expr.arg[0] = $2;
	    }
          | node EQ node
            {
		    $$ = forlan_node_create(forlan_type_expr);
		    $$->expr.opcode = forlan_opcode_eq;
		    $$->expr.arg[0] = $1;
		    $$->expr.arg[1] = $3;
	    }
          | node NE node
            {
		    $$ = forlan_node_create(forlan_type_expr);
		    $$->expr.opcode = forlan_opcode_ne;
		    $$->expr.arg[0] = $1;
		    $$->expr.arg[1] = $3;
	    }
          | '(' bool ')'
            {
		    $$ = $2;
	    }
          ;

node      : rval
          | rval '.' complist
            {
		    $$ = create_comp_node($1, $3.list, $3.wildcards);
            }
          | '.' complist
            {
		    $$ = create_comp_node(NULL, $2.list, $2.wildcards);
	    }
          | '.'
            {
		    $$ = forlan_node_create(forlan_type_comp);
	    }
          ;

complist  : comp
            {
		    $$.list = grecs_list_create();
		    grecs_list_append($$.list, $1.comp);
		    $$.wildcards = $1.wildcard;
	    }
          | complist '.' comp
	    {
		    grecs_list_append($1.list, $3.comp);
		    $$ = $1;
		    $$.wildcards |= $3.wildcard;
	    }
          ;

comp      : IDENT
            {
		    $$.comp = grecs_zalloc(sizeof(*$$.comp));
		    $$.comp->id = $1;
		    $$.wildcard = 0;
	    }
          | IDENT '[' string ']'
            {
		    $$.comp = grecs_zalloc(sizeof(*$$.comp));
		    $$.comp->id = $1;
		    $$.comp->label = $3;
		    $$.wildcard = 0;
	    }
          | '%'
	    {
		    $$.comp = grecs_zalloc(sizeof(*$$.comp));
		    $$.comp->id = grecs_strdup("%");
		    $$.wildcard = 1;
	    }
	  | '*'
	    {
		    $$.comp = grecs_zalloc(sizeof(*$$.comp));
		    $$.comp->id = grecs_strdup("*");
		    $$.wildcard = 1;
	    }
          ;

string    : IDENT
          | STRING
          ;

funcall   : IDENT '(' ')'
            {
		    struct forlan_function *fp =
			    forlan_find_function($1);
		    if (!fp) {
			    grecs_error(&@1, 0,
					"call to unknown function \"%s\"",
					$1);
			    YYERROR;
		    }

		    if (fp->minargs != 0) {
			    grecs_error(&@1, 0,
					"not enough arguments in call to \"%s\"",
					$1);
			    YYERROR;
		    }
		    
		    $$ = forlan_node_create(forlan_type_func);
		    $$->func.fp = fp;
		    $$->func.args = NULL;
	    }
          | IDENT '(' arglist ')'
            {
		    struct forlan_function *fp =
			    forlan_find_function($1);
		    if (!fp) {
			    grecs_error(&@1, 0,
					"call to unknown function \"%s\"",
					$1);
			    YYERROR;
		    }

		    if ($3->count < fp->minargs) {
			    grecs_error(&@1, 0,
					"not enough arguments in call to \"%s\"",
					$1);
			    YYERROR;
		    }
		    if (fp->maxargs >= 0 && $3->count > fp->maxargs) {
			    grecs_error(&@1, 0,
					"too many arguments in call to \"%s\"",
					$1);
			    YYERROR;
		    }

		    /* FIXME: Check data types */
		    
		    $$ = forlan_node_create(forlan_type_func);
		    $$->func.fp = fp;
		    $$->func.args = $3;
	    }
          ;

rval      : funcall
          | LAST
            {
		    $$ = forlan_node_create(forlan_type_last);
	    }
          | STRING
            {
		    $$ = forlan_node_create(forlan_type_lit);
		    $$->lit.string = $1;
	    }
          | IDENT
            {
		    $$ = forlan_node_create(forlan_type_var);
		    $$->var.idx = find_variable($1);
//		    free($1);
	    }
          ;

arglist   : arg
            {
		    $$ = forlan_stmt_list();
		    grecs_list_append($$, $1);
	    }
          | arglist ',' arg
	    {
		    grecs_list_append($1, $3);
		    $$ = $1;
	    }
          ;

arg       : node
          ;

stmt_expr : funcall ';'
          ;

stmt_let  : LET IDENT ';'
            {
		    find_variable($2);
	    }
          | LET IDENT '=' node ';'
            {
		    $$ = forlan_node_create(forlan_type_asgn);
		    $$->asgn.idx = find_variable($2);
		    $$->asgn.node = $4;
	    }
          | LET IDENT '=' funcall ';'
            {
		    $$ = forlan_node_create(forlan_type_asgn);
		    $$->asgn.idx = find_variable($2);
		    $$->asgn.node = $4;
	    }
          ;

stmt_for  : FOR '(' IDENT IN node ')' stmt
            {
		    $$ = forlan_node_create(forlan_type_loop);
		    $$->loop.idx = find_variable($3);
		    $$->loop.node = $5;
		    $$->loop.stmt = $7;
	    }
          ;


stmt_ctrl : BREAK ';'
            {
		    $$ = forlan_node_create(forlan_type_break);
	    }
          | CONTINUE ';'
            {
		    $$ = forlan_node_create(forlan_type_continue);
	    }
          | STOP ';' 
            {
		    $$ = forlan_node_create(forlan_type_stop);
	    }
          ;

stmt_asgn : IDENT '=' node ';'
            {
		    $$ = forlan_node_create(forlan_type_asgn);
		    $$->asgn.idx = find_variable($1);
		    $$->asgn.node = $3;
	    }		    
          ;
%%
static int
yyerror(char *s)
{
	grecs_error(&yylloc, 0, "%s", s);
	return 0;
}
	
int
forlan_parser()
{
	yydebug = debug_level(forlan_dbg) >= FORLAN_DBG_GRAM;
	return yyparse();
}

forlan_eval_env_t
forlan_parse_buffer(const char *input, size_t length,
		    struct grecs_locus_point *pt)
{
	int rc;
	forlan_eval_env_t env = NULL;
	
	forlan_lex_from_buffer(input, length, pt);
	rc = forlan_parser();
	forlan_lex_end();
	grecs_symtab_free(forlan_symtab);
	forlan_symtab = NULL;

	if (rc == 0)
		env = forlan_create_environment(forlan_parse_tree,
						forlan_variable_count);

	return env;
}

forlan_eval_env_t
forlan_parse_file(FILE *fp, struct grecs_locus_point *pt)
{
	int rc;
	forlan_eval_env_t env = NULL;
	
	forlan_lex_from_file(fp, pt);
	rc = forlan_parser();
	forlan_lex_end();
	grecs_symtab_free(forlan_symtab);
	forlan_symtab = NULL;

	if (rc == 0)
		env = forlan_create_environment(forlan_parse_tree,
						forlan_variable_count);

	return env;
}

static size_t
find_variable(const char *name)
{
	struct forlan_variable key;
	struct forlan_variable *ent;
	int install = 1;

	if (!forlan_symtab) {
		forlan_symtab = grecs_symtab_create_default(
			              sizeof(struct forlan_variable));
		if (!forlan_symtab)
			grecs_alloc_die();
		forlan_variable_count = 0;
	}
  
	key.name = (char*) name;
	ent = grecs_symtab_lookup_or_install(forlan_symtab, &key, &install);
	if (!ent)
		grecs_alloc_die();
	if (install)
		ent->idx = forlan_variable_count++;
	return ent->idx;
}

/* Optimize the content of "comp" node.
   
   The grecs_match_buf_create function compresses contiguous occurrences
   of the "*" wildcard into a single one, shifting the rest of elements in
   argv and labelv into the freed slots.  This can result in changing the
   number of arguments.  We create a buffer to let it do its job and update
   the argc stored in the node, so that subsequent calls to
   grecs_match_list_create get the correct number of elements.
 */
static void
optimize_comp(union forlan_node *p)
{
	grecs_match_buf_t mb;

	mb = grecs_match_buf_create(p->comp.argc, p->comp.argv, p->comp.labelv);
	p->comp.argc = grecs_match_buf_get_args(mb, NULL);
	free(mb);
}

static union forlan_node *
create_comp_node(union forlan_node *root, struct grecs_list *arglist,
		 int wildcards)
{
	union forlan_node *ret;
	int i;
	struct grecs_list_entry *ep;
		    
	ret = forlan_node_create(forlan_type_comp);
	ret->comp.root = root;
	ret->comp.wildcards = wildcards;
	ret->comp.argc = arglist->count;
	ret->comp.argv = grecs_calloc(arglist->count,
				      sizeof(ret->comp.argv[0]));
	ret->comp.labelv = grecs_calloc(arglist->count,
					sizeof(ret->comp.labelv[0]));
	for (i = 0, ep = arglist->head; ep; i++, ep = ep->next) {
		struct path_component *comp = ep->data;
		struct grecs_value *val;

		ret->comp.argv[i] = comp->id;
		if (comp->label) {
			val = grecs_malloc(sizeof(*val));
			val->type = GRECS_TYPE_STRING;
			val->v.string = comp->label;
		} else
			val = NULL;
		ret->comp.labelv[i] = val;
	}
	grecs_list_free(arglist);
	optimize_comp(ret);
	return ret;
}

Return to:

Send suggestions and report system problems to the System administrator.