Simplified O_uminus().
[mpsl.git] / mpsl.y
blobc01ec0e85e7dceeaf8b240c552658cfde8351e50
1 %{
2 /*
4 MPSL - Minimum Profit Scripting Language
5 Copyright (C) 2003/2010 Angel Ortega <angel@triptico.com>
7 mpsl.y - Minimum Profit Scripting Language YACC parser
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 http://www.triptico.com
27 #include <stdio.h>
28 #include <string.h>
29 #include <wchar.h>
30 #include "mpdm.h"
31 #include "mpsl.h"
33 /** data **/
35 /* the bytecode being generated */
36 static mpdm_t mpsl_bytecode = NULL;
38 /* pointer to source code being compiled */
39 extern wchar_t *mpsl_next_char;
41 /* pointer to file being compiled */
42 extern FILE *mpsl_file;
44 /* line number */
45 extern int mpsl_line;
47 /* compiled filename (for errors) */
48 static char *mpsl_filename = NULL;
50 /* cached value MPSL.OPCODE */
51 extern mpdm_t mpsl_opcodes;
53 /* cached value MPSL.LC */
54 extern mpdm_t mpsl_lc;
57 /** code **/
59 int yylex(void);
60 void yyerror(char * s);
62 #define INS0(o) mpsl_mkins(o, 0, NULL, NULL, NULL)
63 #define INS1(o,a1) mpsl_mkins(o, 1, a1, NULL, NULL)
64 #define INS2(o,a1,a2) mpsl_mkins(o, 2, a1, a2, NULL)
65 #define INS3(o,a1,a2,a3) mpsl_mkins(o, 3, a1, a2, a3)
67 static mpdm_t mpsl_x(mpdm_t a1, mpdm_t a2, int sf)
68 /* creates an executable value with the MPSL executor as the first
69 argument and a compiled stream as the second */
71 return MPDM_X2(mpsl_exec_p,
72 mpsl_mkins(sf ? L"SUBFRAME" : L"BLKFRAME",
73 a2 == NULL ? 1 : 2, a1, a2, NULL));
77 static void compiler_warning(char *str)
79 fprintf(stderr, "WARNING: %s.\n", str);
85 %union {
86 mpdm_t v; /* a simple value */
87 mpdm_t ins; /* an 'instruction': [ opcode, args ] */
90 %token <v> NULLV INTEGER REAL STRING SYMBOL LITERAL
91 %token WHILE IF SUB FOREACH LOCAL BREAK RETURN
92 %nonassoc IFI
93 %nonassoc ELSE
95 %left BOOLAND BOOLOR
96 %left INC DEC IADD ISUB IMUL IDIV IMOD IBITAND IBITOR IBITXOR ISHR ISHL
97 %left '!'
98 %left STRCAT STREQ NUMEQ STRNE NUMNE NUMGE NUMLE HASHPAIR RANGE '>''<' INVCALL
99 %left BITAND
100 %left BITOR BITXOR
101 %left SHL SHR
102 %left '+' '-'
103 %left '*' '/' MOD POW
104 %nonassoc UMINUS
106 %type <ins> stmt expr sym_list stmt_list list hash compsym
110 program:
111 function { ; }
114 function:
115 function stmt_list {
116 mpsl_bytecode = $2;
118 | /* NULL */
121 stmt:
122 ';' {
123 /* null instruction */
124 $$ = INS0(L"MULTI");
126 | expr ';' {
127 /* expression, as is */
128 $$ = $1;
131 | WHILE '(' expr ')' stmt
133 /* while loop */
134 $$ = INS2(L"WHILE", $3, $5);
136 | IF '(' expr ')' stmt %prec IFI
138 /* if - then construction */
139 $$ = INS2(L"IF", $3, $5);
141 | IF '(' expr ')' stmt ELSE stmt
143 /* if - then - else construction */
144 $$ = INS3(L"IF", $3, $5, $7);
147 | SUB compsym '{' stmt_list '}'
149 /* subroutine definition,
150 without arguments */
151 $$ = INS2(L"ASSIGN", $2,
152 INS1(L"LITERAL",
153 mpsl_x($4, NULL, 1)));
156 | SUB compsym '(' ')' '{' stmt_list '}'
158 /* subroutine definition,
159 without arguments (second
160 syntax, including parens) */
161 $$ = INS2(L"ASSIGN", $2,
162 INS1(L"LITERAL",
163 mpsl_x($6, NULL, 1)));
166 | SUB compsym '(' sym_list ')' '{' stmt_list '}'
168 /* subroutine definition,
169 with arguments */
170 $$ = INS2(L"ASSIGN", $2,
171 INS1(L"LITERAL",
172 mpsl_x($7, $4, 1)));
175 | FOREACH '(' compsym ',' expr ')' stmt
177 /* foreach construction */
178 /* a block frame is created, the iterator
179 created as local, and the foreach executed */
180 $$ = INS1(L"BLKFRAME",
181 INS2(L"MULTI",
182 INS1(L"LOCAL", $3),
183 INS3(L"FOREACH", $3, $5, $7)
187 | FOREACH '(' LOCAL compsym ',' expr ')' stmt
189 compiler_warning("useless use of local in foreach loop");
191 $$ = INS1(L"BLKFRAME",
192 INS2(L"MULTI",
193 INS1(L"LOCAL", $4),
194 INS3(L"FOREACH", $4, $6, $8)
199 | '{' stmt_list '}' {
200 /* block of instructions,
201 with local symbol table */
202 $$ = INS1(L"BLKFRAME", $2);
205 | LOCAL sym_list ';' {
206 /* local symbol creation */
207 $$ = INS1(L"LOCAL", $2);
209 | LOCAL SYMBOL '=' expr ';'
211 /* contraction; local symbol
212 creation and assignation */
213 $$ = INS2(L"MULTI",
214 INS1(L"LOCAL",
215 INS1(L"LITERAL", $2)),
216 INS2(L"ASSIGN",
217 INS1(L"LITERAL", $2),$4)
220 | BREAK ';' {
221 /* break (exit from loop) */
222 $$ = INS0(L"BREAK");
224 | RETURN expr ';' {
225 /* return from subroutine */
226 $$ = INS1(L"RETURN", $2);
228 | RETURN ';' {
229 /* return from subroutine (void) */
230 $$ = INS0(L"RETURN");
234 stmt_list:
235 stmt { $$ = $1; }
236 | stmt_list stmt {
237 /* sequence of instructions */
238 $$ = INS2(L"MULTI", $1, $2);
242 list:
243 expr {
244 $$ = INS1(L"LIST", $1);
246 | list ',' expr {
247 /* build list from list of
248 instructions */
249 $$ = INS2(L"LIST", $3, $1);
253 sym_list:
254 SYMBOL {
255 $$ = INS1(L"LIST",
256 INS1(L"LITERAL", $1));
258 | sym_list ',' SYMBOL {
259 /* comma-separated list of symbols */
260 $$ = INS2(L"LIST",
261 INS1(L"LITERAL", $3), $1);
265 hash:
266 expr HASHPAIR expr {
267 $$ = INS2(L"HASH", $1, $3);
269 | hash ',' expr HASHPAIR expr
271 /* build hash from list of
272 instructions */
273 $$ = INS3(L"HASH", $3, $5, $1);
277 compsym:
278 SYMBOL {
279 $$ = INS1(L"LIST",
280 INS1(L"LITERAL", $1));
282 | compsym '.' INTEGER {
283 /* a.5 compound symbol */
284 $$ = INS2(L"LIST",
285 INS1(L"LITERAL", $3), $1);
287 | compsym '.' SYMBOL {
288 /* a.b compound symbol */
289 $$ = INS2(L"LIST",
290 INS1(L"LITERAL", $3), $1);
292 | compsym '[' expr ']' {
293 /* a["b"] or a[5] compound symbol */
294 $$ = INS2(L"LIST", $3, $1);
298 expr:
299 INTEGER {
300 /* literal integer */
301 $$ = INS1(L"LITERAL", $1);
303 | STRING {
304 /* literal string */
305 $$ = INS1(L"LITERAL", $1);
307 | REAL {
308 /* literal real number */
309 $$ = INS1(L"LITERAL", $1);
311 | compsym {
312 /* compound symbol */
313 $$ = INS1(L"SYMVAL", $1);
315 | NULLV {
316 /* NULL value */
317 $$ = INS1(L"LITERAL", NULL);
320 | '-' expr %prec UMINUS {
321 /* unary minus */
322 $$ = INS1(L"UMINUS", $2);
325 /* math operations */
326 | expr '+' expr { $$ = INS2(L"ADD", $1, $3); }
327 | expr '-' expr { $$ = INS2(L"SUB", $1, $3); }
328 | expr '*' expr { $$ = INS2(L"MUL", $1, $3); }
329 | expr '/' expr { $$ = INS2(L"DIV", $1, $3); }
330 | expr MOD expr { $$ = INS2(L"MOD", $1, $3); }
331 | expr POW expr { $$ = INS2(L"POW", $1, $3); }
333 /* bit operations */
334 | expr BITAND expr { $$ = INS2(L"BITAND", $1, $3); }
335 | expr BITOR expr { $$ = INS2(L"BITOR", $1, $3); }
336 | expr BITXOR expr { $$ = INS2(L"BITXOR", $1, $3); }
337 | expr SHL expr { $$ = INS2(L"SHL", $1, $3); }
338 | expr SHR expr { $$ = INS2(L"SHR", $1, $3); }
340 /* increment and decrement (prefix) */
341 | INC compsym { $$ = INS2(L"ASSIGN", $2,
342 INS2(L"ADD",
343 INS1(L"SYMVAL", $2),
344 INS1(L"LITERAL", MPDM_I(1))
348 | DEC compsym { $$ = INS2(L"ASSIGN", $2,
349 INS2(L"SUB",
350 INS1(L"SYMVAL", $2),
351 INS1(L"LITERAL", MPDM_I(1))
356 /* increment and decrement (suffix) */
357 | compsym INC { $$ = INS2(L"IMULTI",
358 INS1(L"SYMVAL", $1),
359 INS2(L"ASSIGN", $1,
360 INS2(L"ADD",
361 INS1(L"SYMVAL", $1),
362 INS1(L"LITERAL", MPDM_I(1))
367 | compsym DEC { $$ = INS2(L"IMULTI",
368 INS1(L"SYMVAL", $1),
369 INS2(L"ASSIGN", $1,
370 INS2(L"SUB",
371 INS1(L"SYMVAL", $1),
372 INS1(L"LITERAL", MPDM_I(1))
378 /* immediate math operations */
379 | compsym IADD expr { $$ = INS2(L"ASSIGN", $1,
380 INS2(L"ADD",
381 INS1(L"SYMVAL", $1),
385 | compsym ISUB expr { $$ = INS2(L"ASSIGN", $1,
386 INS2(L"SUB",
387 INS1(L"SYMVAL", $1),
391 | compsym IMUL expr { $$ = INS2(L"ASSIGN", $1,
392 INS2(L"MUL",
393 INS1(L"SYMVAL", $1),
397 | compsym IDIV expr { $$ = INS2(L"ASSIGN", $1,
398 INS2(L"DIV",
399 INS1(L"SYMVAL", $1),
403 | compsym IMOD expr { $$ = INS2(L"ASSIGN", $1,
404 INS2(L"MOD",
405 INS1(L"SYMVAL", $1),
409 | compsym IBITAND expr { $$ = INS2(L"ASSIGN", $1,
410 INS2(L"BITAND",
411 INS1(L"SYMVAL", $1),
415 | compsym IBITOR expr { $$ = INS2(L"ASSIGN", $1,
416 INS2(L"BITOR",
417 INS1(L"SYMVAL", $1),
421 | compsym IBITXOR expr { $$ = INS2(L"ASSIGN", $1,
422 INS2(L"BITXOR",
423 INS1(L"SYMVAL", $1),
427 | compsym ISHL expr { $$ = INS2(L"ASSIGN", $1,
428 INS2(L"SHL",
429 INS1(L"SYMVAL", $1),
433 | compsym ISHR expr { $$ = INS2(L"ASSIGN", $1,
434 INS2(L"SHR",
435 INS1(L"SYMVAL", $1),
440 | '!' expr {
441 /* boolean not */
442 $$ = INS1(L"NOT", $2);
444 | expr '<' expr {
445 /* bool less than */
446 $$ = INS2(L"NUMLT", $1, $3);
448 | expr '>' expr {
449 /* bool greater than */
450 $$ = INS2(L"NUMGT", $1, $3);
452 | expr NUMLE expr {
453 /* bool less or equal than */
454 $$ = INS2(L"NUMLE", $1, $3);
456 | expr NUMGE expr {
457 /* bool greater or equal than */
458 $$ = INS2(L"NUMGE", $1, $3);
460 | expr NUMEQ expr {
461 /* bool numeric equal */
462 $$ = INS2(L"NUMEQ", $1, $3);
464 | expr NUMNE expr {
465 /* bool numeric non-equal */
466 $$ = INS1(L"NOT",
467 INS2(L"NUMEQ", $1, $3));
470 | expr STRCAT expr {
471 /* string concatenation */
472 $$ = INS2(L"STRCAT", $1, $3);
474 | expr STREQ expr {
475 /* bool string equal */
476 $$ = INS2(L"STREQ", $1, $3);
478 | expr STRNE expr {
479 /* bool string non-equal */
480 $$ = INS1(L"NOT",
481 INS2(L"STREQ", $1, $3));
484 | expr BOOLAND expr {
485 /* boolean and */
486 $$ = INS2(L"AND", $1, $3);
488 | expr BOOLOR expr {
489 /* boolean or */
490 $$ = INS2(L"OR", $1, $3);
493 | SUB '{' stmt_list '}' {
494 /* anonymous subroutine (without args) */
495 $$ = INS1(L"LITERAL", mpsl_x($3, NULL, 0));
498 | SUB '(' sym_list ')' '{' stmt_list '}'
500 /* anonymous subroutine (with args) */
501 $$ = INS1(L"LITERAL", mpsl_x($6, $3, 0));
504 | '(' expr ')' {
505 /* parenthesized expression */
506 $$ = $2;
509 | '[' ']' {
510 /* empty list */
511 $$ = INS1(L"LITERAL", MPDM_A(0));
513 | '[' list ']' {
514 /* non-empty list */
515 $$ = $2;
517 | '[' expr RANGE expr ']'
519 /* build range from expressions */
520 $$ = INS2(L"RANGE", $2, $4);
523 | '{' '}' {
524 /* empty hash */
525 $$ = INS1(L"LITERAL", MPDM_H(0));
527 | '{' hash '}' {
528 /* non-empty hash */
529 $$ = $2;
532 | compsym '(' ')' {
533 /* function call (without args) */
534 $$ = INS1(L"EXECSYM", $1);
536 | compsym '(' list ')' {
537 /* function call (with args) */
538 $$ = INS2(L"EXECSYM", $1, $3);
540 | expr INVCALL compsym '(' ')' {
541 /* function call with only an inverse argument */
542 $$ = INS2(L"EXECSYM", $3, INS1(L"ILIST", $1));
544 | expr INVCALL compsym '(' list ')' {
545 /* function call with inverse argument and other ones */
546 $$ = INS2(L"EXECSYM", $3, INS2(L"ILIST", $1, $5));
548 | compsym '=' expr {
549 /* simple assignation */
550 $$ = INS2(L"ASSIGN", $1, $3);
557 void yyerror(char * s)
559 char tmp[1024];
561 snprintf(tmp, sizeof(tmp), "%s in %s, line %d",
562 s, mpsl_filename, mpsl_line + 1);
564 mpsl_error(MPDM_MBS(tmp));
568 static FILE * inc_fopen(const char *filename, mpdm_t inc)
569 /* loads filename, searching in INC if not directly accesible */
571 FILE * f = NULL;
572 char tmp[1024];
573 int n;
575 /* loop through INC, prepending each path
576 to the filename */
577 for (n = 0; n < mpdm_size(inc); n++) {
578 mpdm_t v = mpdm_aget(inc, n);
580 v = mpdm_ref(MPDM_2MBS(v->data));
581 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)v->data, filename);
582 mpdm_unref(v);
584 if ((f = fopen(tmp, "r")) != NULL)
585 break;
588 return f;
592 static mpdm_t do_parse(const char *filename, wchar_t *code, FILE *file)
593 /* calls yyparse() after doing some initialisations, and returns
594 the compiled code as an executable value */
596 mpdm_t v;
597 mpdm_t x = NULL;
599 /* first line */
600 mpsl_line = 0;
602 /* reset last bytecode */
603 mpsl_bytecode = NULL;
605 /* set globals */
606 mpsl_next_char = code;
607 mpsl_file = file;
609 if (mpsl_filename != NULL)
610 free(mpsl_filename);
612 mpsl_filename = strdup(filename);
614 /* cache some values */
615 v = mpdm_hget_s(mpdm_root(), L"MPSL");
616 mpsl_opcodes = mpdm_hget_s(v, L"OPCODE");
617 mpsl_lc = mpdm_hget_s(v, L"LC");
619 /* compile! */
620 if (yyparse() == 0 && mpsl_bytecode != NULL)
621 x = mpsl_x(mpsl_bytecode, NULL, 1);
623 /* clean back cached values */
624 mpsl_opcodes = NULL;
625 mpsl_lc = NULL;
627 return x;
632 * mpsl_compile - Compiles a string of MPSL code.
633 * @code: A value containing a string of MPSL code
635 * Compiles a string of MPSL code and returns an mpdm value executable
636 * by mpdm_exec(). If there is a syntax (or other type) error, NULL
637 * is returned instead.
639 mpdm_t mpsl_compile(mpdm_t code)
641 mpdm_t x = NULL;
643 mpdm_ref(code);
644 x = do_parse("<INLINE>", (wchar_t *) code->data, NULL);
645 mpdm_unref(code);
647 return x;
652 * mpsl_compile_file - Compiles a file of MPSL code.
653 * @file: File stream or file name.
654 * @inc: search path for source files.
656 * Compiles a source file of MPSL code and returns an mpdm value
657 * executable by mpdm_exec(). If @file is an MPSL file descriptor,
658 * it's read as is and compiled; otherwise, it's assumed to be a
659 * file name, that will be searched for in any of the paths defined
660 * in the @inc array. If the file cannot be found
661 * or there is any other error, NULL is returned instead.
663 mpdm_t mpsl_compile_file(mpdm_t file, mpdm_t inc)
665 mpdm_t w;
666 mpdm_t x = NULL;
667 FILE *f = NULL;
668 const char *filename = NULL;
670 mpdm_ref(file);
671 mpdm_ref(inc);
673 if ((f = mpdm_get_filehandle(file)) != NULL) {
674 filename = "<FILE>";
675 w = file;
677 else {
678 mpdm_t v;
680 /* it's a filename; open it */
681 v = mpdm_ref(MPDM_2MBS(file->data));
683 filename = v->data;
685 if ((f = inc_fopen(filename, inc)) == NULL) {
686 char tmp[128];
688 snprintf(tmp, sizeof(tmp) - 1,
689 "File '%s' not found in INC",
690 filename);
691 mpsl_error(MPDM_MBS(tmp));
694 mpdm_unref(v);
696 w = MPDM_F(f);
699 if (w != NULL) {
700 x = do_parse(filename, NULL, f);
701 mpdm_close(w);
704 mpdm_unref(inc);
705 mpdm_unref(file);
707 return x;
712 * mpsl_eval - Evaluates MSPL code.
713 * @code: A value containing a string of MPSL code, or executable code
714 * @args: optional arguments for @code
715 * @ctxt: context for @code
717 * Evaluates a piece of code. The @code can be a string containing MPSL source
718 * code (that will be compiled) or a direct executable value. If the compilation
719 * or the execution gives an error, the ERROR variable will be set to a printable
720 * value and NULL returned. Otherwise, the exit value from the code is returned
721 * and ERROR set to NULL. The abort flag is reset on exit.
723 mpdm_t mpsl_eval(mpdm_t code, mpdm_t args, mpdm_t ctxt)
725 mpdm_t cs, r;
727 /* reset error */
728 mpsl_error(NULL);
729 mpsl_abort = 0;
731 mpdm_ref(code);
733 /* if code is not executable, try to compile */
734 if (!MPDM_IS_EXEC(code)) {
735 mpdm_t c;
737 /* get the eval cache */
738 if ((c = mpdm_hget_s(mpdm_root(), L"__EVAL__")) == NULL)
739 c = mpdm_hset_s(mpdm_root(), L"__EVAL__", MPDM_H(0));
741 /* this code still not compiled? do it */
742 if ((cs = mpdm_hget(c, code)) == NULL)
743 cs = mpdm_hset(c, code, mpsl_compile(code));
745 else
746 cs = code;
748 /* execute, if possible */
749 if (MPDM_IS_EXEC(cs))
750 r = mpdm_exec(cs, args, ctxt);
751 else
752 r = NULL;
754 /* reset the abort flag */
755 mpsl_abort = 0;
757 mpdm_unref(code);
759 return r;