Revert "Avoid unnecessary referencing in O_multi()."
[mpsl.git] / mpsl_c.c
blob721ddb1805b4ca753ee258fa03978be13a9e3773
1 /*
3 MPSL - Minimum Profit Scripting Language
4 Copyright (C) 2003/2010 Angel Ortega <angel@triptico.com>
6 mpsl_c.c - Minimum Profit Scripting Language Core
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <wchar.h>
30 #include <math.h>
31 #include "mpdm.h"
32 #include "mpsl.h"
35 /** data **/
37 /* global abort flag */
38 int mpsl_abort = 0;
40 /* temporary storage for the local symbol table */
41 static mpdm_t local_symtbl = NULL;
43 /* temporary storage for the opcode table
44 (only usable while compiling) */
45 mpdm_t mpsl_opcodes = NULL;
47 /* flag to mark if an execution is from inside constant folding */
48 static int in_constant_folding = 0;
50 /* pointer to a trap function */
51 static mpdm_t mpsl_trap_func = NULL;
54 /** code **/
56 /**
57 * mpsl_is_true - Tests if a value is true.
58 * @v: the value
60 * If @v is a valid MPSL 'false' value (NULL, "" or the "0" string),
61 * returns zero, or nonzero otherwise.
63 int mpsl_is_true(mpdm_t v)
65 /* if value is NULL, it's false */
66 if (v == NULL)
67 return 0;
69 /* if it's a printable string... */
70 if (v->flags & MPDM_STRING) {
71 wchar_t * ptr = (wchar_t *)v->data;
73 /* ... and it's "" or the "0" string, it's false */
74 if(*ptr == L'\0' || (*ptr == L'0' && *(ptr + 1) == L'\0'))
75 return 0;
78 /* any other case is true */
79 return 1;
83 /**
84 * mpsl_boolean - Returns 'true' or 'false' MPSL stock values.
85 * @b: boolean selector
87 * Returns MPSL's 'false' or 'true' values depending on the value in @b.
89 mpdm_t mpsl_boolean(int b)
91 return b ? mpdm_hget_s(mpdm_root(), L"TRUE") : NULL;
95 static mpdm_t find_local_symtbl(mpdm_t s, mpdm_t l)
96 /* finds the local symbol table hash that holds l */
98 int n;
99 mpdm_t v = NULL;
101 /* no local symbol table? nothing to find */
102 if(l == NULL)
103 return NULL;
105 /* if s is multiple, take just the first element */
106 if(MPDM_IS_ARRAY(s))
107 s = mpdm_aget(s, 0);
109 /* travel the local symbol table trying to find it */
110 for (n = mpdm_size(l) - 1; n >= 0; n--) {
111 mpdm_t h = mpdm_aget(l, n);
113 if (mpdm_exists(h, s)) {
114 v = h;
115 break;
119 return v;
123 static void set_local_symbols(mpdm_t s, mpdm_t v, mpdm_t l)
124 /* sets (or creates) a list of local symbols with a list of values */
126 mpdm_t h;
128 if (l == NULL)
129 return;
131 /* gets the top local variable frame */
132 h = mpdm_aget(l, -1);
134 if (MPDM_IS_ARRAY(s) || MPDM_IS_ARRAY(v)) {
135 int n;
136 mpdm_t a;
138 for (n = 0; n < mpdm_size(s); n++)
139 mpdm_hset(h, mpdm_aget(s, n), mpdm_aget(v, n));
141 /* store the rest of arguments into _ */
142 a = MPDM_A(0);
144 for (; n < mpdm_size(v); n++)
145 mpdm_push(a, mpdm_aget(v, n));
147 mpdm_hset_s(h, L"_", a);
149 else
150 mpdm_hset(h, s, v);
154 static mpdm_t get_symbol(mpdm_t s, mpdm_t l)
155 /* gets a symbol from a local symbol table, or the global */
157 return mpdm_sget(find_local_symtbl(s, l), s);
161 static mpdm_t set_symbol(mpdm_t s, mpdm_t v, mpdm_t l)
162 /* sets a symbol in a local symbol table, or the global */
164 mpdm_sset(find_local_symtbl(s, l), s, v);
165 return v;
170 * mpsl_set_symbol - Sets value to a symbol.
171 * @s: symbol name
172 * @v: value
174 * Assigns the value @v to the @s symbol. If the value exists as
175 * a local symbol, it's assigned to it; otherwise, it's set as a global
176 * symbol (and created if it does not exist).
178 * This function is only meant to be executed from inside an MPSL
179 * program; from outside, it's exactly the same as calling mpdm_sset()
180 * (as the local symbol table won't exist).
182 mpdm_t mpsl_set_symbol(mpdm_t s, mpdm_t v)
184 return set_symbol(s, v, local_symtbl);
189 * mpsl_get_symbol - Gets the value of a symbol.
190 * @s: symbol name
192 * Gets the value of a symbol. The symbol can be local or global
193 * (if the symbol exists in both tables, the local value will be returned).
195 * This function is only meant to be executed from inside an MPSL
196 * program; from outside, it's exactly the same as calling mpdm_sget()
197 * (as the local symbol table won't exist).
199 mpdm_t mpsl_get_symbol(mpdm_t s)
201 return get_symbol(s, local_symtbl);
206 * mpsl_error - Generates an error.
207 * @err: the error message
209 * Generates an error. The @err error message is stored in the ERROR
210 * mpsl variable and the mpsl_abort global flag is set, so no further
211 * mpsl code can be executed until reset.
213 mpdm_t mpsl_error(mpdm_t err)
215 /* abort further execution */
216 mpsl_abort = 1;
218 /* set the error */
219 return mpdm_hset_s(mpdm_root(), L"ERROR", err);
222 /** opcodes **/
224 #define O_TYPE static mpdm_t
225 #define O_ARGS mpdm_t c, mpdm_t a, mpdm_t l, int * f
227 O_TYPE mpsl_exec_i(O_ARGS);
229 #define C(n) mpdm_aget(c, n)
230 #define C0 C(0)
231 #define C1 C(1)
233 #define M(n) mpsl_exec_i(C(n), a, l, f)
234 #define M1 M(1)
235 #define M2 M(2)
236 #define M3 M(3)
238 #define R(x) mpdm_rval(x)
239 #define I(x) mpdm_ival(x)
241 #define RM1 mpdm_rval(M(1))
242 #define RM2 mpdm_rval(M(2))
243 #define IM1 mpdm_ival(M(1))
244 #define IM2 mpdm_ival(M(2))
246 #define GET(m) get_symbol(m, l)
247 #define SET(m, v) set_symbol(m, v, l)
248 #define BOOL mpsl_boolean
249 #define ISTRU mpsl_is_true
251 #define RF(v) mpdm_ref(v)
252 #define UF(v) mpdm_unref(v)
254 O_TYPE O_literal(O_ARGS) { return mpdm_clone(C1); }
255 O_TYPE O_multi(O_ARGS) { mpdm_t v = RF(M1); if (!*f) v = M2; else UF(v); return v; }
256 O_TYPE O_imulti(O_ARGS) { mpdm_t v = RF(M1); if (!*f) M2; return UF(v); }
257 O_TYPE O_symval(O_ARGS) { return GET(M1); }
258 O_TYPE O_assign(O_ARGS) { mpdm_t v = RF(M1); mpdm_t r = SET(v, M2); UF(v); return r; }
259 O_TYPE O_if(O_ARGS) { return ISTRU(M1) ? M2 : M3; }
260 O_TYPE O_local(O_ARGS) { set_local_symbols(M1, NULL, l); return NULL; }
261 O_TYPE O_uminus(O_ARGS) { return MPDM_R(-RM1); }
262 O_TYPE O_add(O_ARGS) { return MPDM_R(RM1 + RM2); }
263 O_TYPE O_sub(O_ARGS) { return MPDM_R(RM1 - RM2); }
264 O_TYPE O_mul(O_ARGS) { return MPDM_R(RM1 * RM2); }
265 O_TYPE O_div(O_ARGS) { return MPDM_R(RM1 / RM2); }
266 O_TYPE O_mod(O_ARGS) { return MPDM_I(IM1 % IM2); }
267 O_TYPE O_not(O_ARGS) { return BOOL(! ISTRU(M1)); }
268 O_TYPE O_and(O_ARGS) { mpdm_t r = M1; return ISTRU(r) ? M2 : r; }
269 O_TYPE O_or(O_ARGS) { mpdm_t r = M1; return ISTRU(r) ? r : M2; }
270 O_TYPE O_bitand(O_ARGS) { return MPDM_I(IM1 & IM2); }
271 O_TYPE O_bitor(O_ARGS) { return MPDM_I(IM1 | IM2); }
272 O_TYPE O_bitxor(O_ARGS) { return MPDM_I(IM1 ^ IM2); }
273 O_TYPE O_shl(O_ARGS) { return MPDM_I(IM1 << IM2); }
274 O_TYPE O_shr(O_ARGS) { return MPDM_I(IM1 >> IM2); }
275 O_TYPE O_pow(O_ARGS) { return MPDM_R(pow(RM1, RM2)); }
276 O_TYPE O_numlt(O_ARGS) { return BOOL(RM1 < RM2); }
277 O_TYPE O_numle(O_ARGS) { return BOOL(RM1 <= RM2); }
278 O_TYPE O_numgt(O_ARGS) { return BOOL(RM1 > RM2); }
279 O_TYPE O_numge(O_ARGS) { return BOOL(RM1 >= RM2); }
280 O_TYPE O_strcat(O_ARGS) { mpdm_t v = RF(M1); mpdm_t r = mpdm_strcat(v, M2); UF(v); return r; }
281 O_TYPE O_streq(O_ARGS) { mpdm_t v = RF(M1); mpdm_t r = BOOL(mpdm_cmp(v, M2) == 0); UF(v); return r; }
282 O_TYPE O_numeq(O_ARGS) { mpdm_t v1 = RF(M1); mpdm_t v2 = M2; UF(v1); return BOOL((v1 == NULL || v2 == NULL) ? (v1 == v2) : (R(v1) == R(v2))); }
283 O_TYPE O_break(O_ARGS) { *f = 1; return NULL; }
284 O_TYPE O_return(O_ARGS) { mpdm_t v = M1; *f = -1; return v; }
286 O_TYPE O_execsym(O_ARGS)
287 /* executes the value of a symbol */
289 mpdm_t s, v, r = NULL;
291 /* gets the symbol name */
292 s = RF(M1);
294 /* gets the symbol value */
295 v = GET(s);
297 if (!MPDM_IS_EXEC(v)) {
298 /* not found or NULL value? error */
299 mpdm_t t;
300 char tmp[128];
302 t = mpdm_join_s(L".", s);
303 t = MPDM_2MBS((wchar_t *) t->data);
305 snprintf(tmp, sizeof(tmp), "Undefined function %s()",
306 (char *)t->data);
308 mpsl_error(MPDM_MBS(tmp));
310 else {
311 /* save current local symbol table */
312 mpdm_t t = local_symtbl;
314 /* substitute with this one */
315 local_symtbl = l;
317 /* execute */
318 r = mpdm_exec(v, M2);
320 /* and get back to the original one */
321 local_symtbl = t;
324 UF(s);
326 return r;
330 O_TYPE O_while(O_ARGS)
331 /* while loop */
333 mpdm_t r = NULL;
335 while (! *f && ISTRU(M1)) {
336 UF(r);
337 r = RF(M2);
340 if (*f == 1)
341 *f = 0;
343 return UF(r);
347 O_TYPE O_foreach(O_ARGS)
348 /* foreach loop */
350 mpdm_t s = RF(M1);
351 mpdm_t v = RF(M2);
352 mpdm_t r = NULL;
353 int n;
355 for (n = 0; n < mpdm_size(v) && ! *f; n++) {
356 SET(s, mpdm_aget(v, n));
357 UF(r);
358 r = RF(M3);
361 if (*f == 1)
362 *f = 0;
364 UF(s); UF(v);
366 return UF(r);
370 O_TYPE O_range(O_ARGS)
371 /* build list from range of two numeric values */
373 double n;
374 double v1 = RM1;
375 double v2 = RM2;
376 mpdm_t ret = MPDM_A(0);
378 if (v1 < v2)
379 for (n = v1; n <= v2; n++)
380 mpdm_push(ret, MPDM_R(n));
381 else
382 for (n = v1; n >= v2; n--)
383 mpdm_push(ret, MPDM_R(n));
385 return ret;
389 O_TYPE O_list(O_ARGS)
390 /* build list from instructions */
392 mpdm_t ret = RF(mpdm_size(c) == 2 ? MPDM_A(0) : M(2));
394 mpdm_push(ret, M(1));
395 return UF(ret);
399 O_TYPE O_ilist(O_ARGS)
400 /* build and inverse list from instructions */
402 mpdm_t ret = RF(mpdm_size(c) == 2 ? MPDM_A(0) : M(2));
404 mpdm_ins(ret, M(1), 0);
405 return UF(ret);
409 O_TYPE O_hash(O_ARGS)
410 /* build hash from instructions */
412 mpdm_t k, v;
413 mpdm_t ret = RF(mpdm_size(c) == 3 ? MPDM_H(0) : M(3));
415 k = RF(M(1)); v = RF(M(2));
416 mpdm_hset(ret, UF(k), UF(v));
417 return UF(ret);
421 O_TYPE generic_frame(O_ARGS)
422 /* runs an instruction under a frame */
424 mpdm_t ret;
426 /* if l is NULL (usually for subroutine frames),
427 create a new array for holding local symbol tables */
428 if (l == NULL)
429 l = MPDM_A(0);
431 RF(l);
433 /* create a new local symbol table */
434 mpdm_push(l, MPDM_H(0));
436 /* creates the arguments (if any) as local variables */
437 set_local_symbols(M2, a, l);
439 /* execute instruction */
440 ret = M1;
442 UF(l);
444 /* destroy the local symbol table */
445 mpdm_pop(l);
447 return ret;
451 O_TYPE O_blkframe(O_ARGS)
452 /* runs an instruction under a block frame */
454 return generic_frame(c, a, l, f);
458 O_TYPE O_subframe(O_ARGS)
459 /* runs an instruction inside a subroutine frame */
461 /* don't propagate the local symbol table,
462 triggering a new subroutine frame */
463 return generic_frame(c, a, NULL, f);
467 static struct mpsl_op_s {
468 wchar_t * name;
469 int foldable;
470 mpdm_t (* func)(O_ARGS);
471 } op_table[] = {
472 { L"LITERAL", 0, O_literal }, /* *must* be the zeroth */
473 { L"MULTI", 0, O_multi },
474 { L"IMULTI", 0, O_imulti },
475 { L"SYMVAL", 0, O_symval },
476 { L"ASSIGN", 0, O_assign },
477 { L"EXECSYM", 0, O_execsym },
478 { L"IF", 0, O_if },
479 { L"WHILE", 0, O_while },
480 { L"FOREACH", 0, O_foreach },
481 { L"SUBFRAME", 0, O_subframe },
482 { L"BLKFRAME", 0, O_blkframe },
483 { L"BREAK", 0, O_break },
484 { L"RETURN", 0, O_return },
485 { L"LOCAL", 0, O_local },
486 { L"LIST", 1, O_list },
487 { L"ILIST", 1, O_ilist },
488 { L"HASH", 1, O_hash },
489 { L"RANGE", 1, O_range },
490 { L"UMINUS", 1, O_uminus },
491 { L"ADD", 1, O_add },
492 { L"SUB", 1, O_sub },
493 { L"MUL", 1, O_mul },
494 { L"DIV", 1, O_div },
495 { L"MOD", 1, O_mod },
496 { L"NOT", 1, O_not },
497 { L"AND", 1, O_and },
498 { L"OR", 1, O_or },
499 { L"NUMEQ", 1, O_numeq },
500 { L"NUMLT", 1, O_numlt },
501 { L"NUMLE", 1, O_numle },
502 { L"NUMGT", 1, O_numgt },
503 { L"NUMGE", 1, O_numge },
504 { L"STRCAT", 1, O_strcat },
505 { L"STREQ", 1, O_streq },
506 { L"BITAND", 1, O_bitand },
507 { L"BITOR", 1, O_bitor },
508 { L"BITXOR", 1, O_bitxor },
509 { L"SHL", 1, O_shl },
510 { L"SHR", 1, O_shr },
511 { L"POW", 1, O_pow },
512 { NULL, 0, NULL }
516 O_TYPE mpsl_exec_i(O_ARGS)
517 /* Executes one MPSL instruction in the MPSL virtual machine. Called
518 from mpsl_exec_p() (which holds the flow control status variable) */
520 struct mpsl_op_s * o;
521 mpdm_t ret = NULL;
523 /* if aborted or NULL, do nothing */
524 if (mpsl_abort || c == NULL)
525 return NULL;
527 /* sweep some values */
528 if (!in_constant_folding)
529 mpdm_sweep(0);
531 /* gets the opcode */
532 o = &op_table[mpdm_ival(C0)];
534 /* blindly call it, or crash */
535 ret = o->func(c, a, l, f);
537 if (mpsl_trap_func != NULL && !in_constant_folding) {
538 mpdm_t f = mpsl_trap_func;
540 mpsl_trap_func = NULL;
541 mpdm_exec_3(f, c, a, ret);
542 mpsl_trap_func = f;
545 return ret;
549 mpdm_t mpsl_exec_p(mpdm_t c, mpdm_t a)
550 /* executes an MPSL instruction stream */
552 int f = 0;
554 /* execute first instruction with a new flow control variable */
555 return mpsl_exec_i(c, a, local_symtbl, &f);
559 static mpdm_t constant_fold(mpdm_t i)
560 /* tries to fold complex but constant expressions into a literal */
562 int n;
564 /* get the number opcode */
565 n = mpdm_ival(mpdm_aget(i, 0));
567 if (op_table[n].foldable) {
568 /* test if all arguments are literal (opcode 0) */
569 for (n = 1; n < mpdm_size(i); n++) {
570 mpdm_t t = mpdm_aget(i, n);
572 /* if it's not LITERAL, abort immediately */
573 if (mpdm_ival(mpdm_aget(t, 0)) != 0)
574 return i;
577 in_constant_folding = 1;
579 /* execute the instruction and convert to LITERAL */
580 i = mpsl_exec_p(i, NULL);
581 i = mpsl_mkins(L"LITERAL", 1, i, NULL, NULL);
583 in_constant_folding = 0;
586 return i;
590 mpdm_t mpsl_mkins(wchar_t * opcode, int args, mpdm_t a1, mpdm_t a2, mpdm_t a3)
591 /* creates an instruction */
593 mpdm_t o;
594 mpdm_t v;
596 v = MPDM_A(args + 1);
598 /* inserts the opcode */
599 o = mpdm_hget_s(mpsl_opcodes, opcode);
600 mpdm_aset(v, o, 0);
602 switch (args) {
603 case 3: mpdm_aset(v, a3, 3);
604 case 2: mpdm_aset(v, a2, 2);
605 case 1: mpdm_aset(v, a1, 1);
608 v = constant_fold(v);
610 return v;
614 mpdm_t mpsl_build_opcodes(void)
615 /* builds the table of opcodes */
617 int n;
618 mpdm_t r = MPDM_H(0);
620 for (n = 0; op_table[n].name != NULL; n++) {
621 mpdm_t v = MPDM_LS(op_table[n].name);
623 mpdm_set_ival(v, n);
625 /* keys and values are the same */
626 mpdm_hset(r, v, v);
629 return r;
634 * mpsl_trap - Install a trapping function.
635 * @trap_func: The trapping MPSL code
637 * Installs a trapping function. The function is an MPSL
638 * executable value receiving 3 arguments: the code stream,
639 * the arguments and the return value of the executed code.
641 * Returns the previous trapping function.
643 mpdm_t mpsl_trap(mpdm_t trap_func)
645 mpdm_t r = mpdm_unref(mpsl_trap_func);
646 mpsl_trap_func = mpdm_ref(trap_func);
647 return r;
652 * mpsl_argv - Fills the ARGV global array.
653 * @argc: number of arguments
654 * @argv: array of string values
656 * Fills the ARGV global MPSL array with an array of arguments. These
657 * are usually the ones sent to main().
659 void mpsl_argv(int argc, char * argv[])
661 int n;
662 mpdm_t ARGV;
664 /* create the ARGV array */
665 ARGV = MPDM_A(0);
667 for (n = 0; n < argc; n++)
668 mpdm_push(ARGV, MPDM_MBS(argv[n]));
670 mpdm_hset_s(mpdm_root(), L"ARGV", ARGV);
674 /* in mpsl_f.c */
675 mpdm_t mpsl_build_funcs(void);
679 * mpsl_startup - Initializes MPSL.
681 * Initializes the Minimum Profit Scripting Language. Returns 0 if
682 * everything went OK.
684 int mpsl_startup(void)
686 mpdm_t r;
687 mpdm_t m;
689 /* startup MPDM */
690 mpdm_startup();
692 r = mpdm_root();
694 /* creates INC, unless already defined */
695 if (mpdm_hget_s(r, L"INC") == NULL)
696 mpdm_hset_s(r, L"INC", MPDM_A(0));
698 /* the TRUE value */
699 mpdm_hset_s(r, L"TRUE", MPDM_I(1));
701 /* standard file descriptors */
702 mpdm_hset_s(r, L"STDIN", MPDM_F(stdin));
703 mpdm_hset_s(r, L"STDOUT", MPDM_F(stdout));
704 mpdm_hset_s(r, L"STDERR", MPDM_F(stderr));
706 /* home and application directories */
707 mpdm_hset_s(r, L"HOMEDIR", mpdm_home_dir());
708 mpdm_hset_s(r, L"APPDIR", mpdm_app_dir());
710 /* fill now the MPSL hash */
711 m = MPDM_H(0);
712 mpdm_hset_s(r, L"MPSL", m);
714 /* store things there */
715 mpdm_hset_s(m, L"VERSION", MPDM_MBS(VERSION));
716 mpdm_hset_s(m, L"OPCODE", mpsl_build_opcodes());
717 mpdm_hset_s(m, L"LC", MPDM_H(0));
718 mpdm_hset_s(m, L"CORE", mpsl_build_funcs());
720 mpdm_dump_1 = mpsl_dump_1;
722 return 0;
727 * mpsl_shutdown - Shuts down MPSL.
729 * Shuts down MPSL. No MPSL functions should be used from now on.
731 void mpsl_shutdown(void)
733 mpdm_shutdown();