Updated WHILE opcode documentation.
[mpsl.git] / mpsl_c.c
blobf52b2216f47f99c21cf9ee5c8357b7d6588f9cbc
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 opcode table
41 (only usable while compiling) */
42 mpdm_t mpsl_opcodes = NULL;
44 /* pointer to a trap function */
45 static mpdm_t mpsl_trap_func = NULL;
48 /** code **/
50 /**
51 * mpsl_is_true - Tests if a value is true.
52 * @v: the value
54 * If @v is a valid MPSL 'false' value (NULL, "" or the "0" string),
55 * returns zero, or nonzero otherwise.
57 int mpsl_is_true(mpdm_t v)
59 /* if value is NULL, it's false */
60 if (v == NULL)
61 return 0;
63 /* if it's a printable string... */
64 if (v->flags & MPDM_STRING) {
65 wchar_t *ptr = (wchar_t *) v->data;
67 /* ... and it's "" or the "0" string, it's false */
68 if (*ptr == L'\0' || (*ptr == L'0' && *(ptr + 1) == L'\0'))
69 return 0;
72 /* any other case is true */
73 return 1;
77 /**
78 * mpsl_boolean - Returns 'true' or 'false' MPSL stock values.
79 * @b: boolean selector
81 * Returns MPSL's 'false' or 'true' values depending on the value in @b.
83 mpdm_t mpsl_boolean(int b)
85 return b ? mpdm_hget_s(mpdm_root(), L"TRUE") : NULL;
89 static mpdm_t find_local_symtbl(mpdm_t s, mpdm_t l)
90 /* finds the local symbol table hash that holds l */
92 int n;
93 mpdm_t v = NULL;
95 /* no local symbol table? nothing to find */
96 if (l == NULL)
97 return NULL;
99 /* if s is multiple, take just the first element */
100 if (MPDM_IS_ARRAY(s))
101 s = mpdm_aget(s, 0);
103 /* travel the local symbol table trying to find it */
104 for (n = mpdm_size(l) - 1; n >= 0; n--) {
105 mpdm_t h = mpdm_aget(l, n);
107 if (mpdm_exists(h, s)) {
108 v = h;
109 break;
113 return v;
117 static void set_local_symbols(mpdm_t s, mpdm_t v, mpdm_t l)
118 /* sets (or creates) a list of local symbols with a list of values */
120 if (l != NULL) {
121 mpdm_t h;
123 mpdm_ref(s);
124 mpdm_ref(v);
125 mpdm_ref(l);
127 /* gets the top local variable frame */
128 h = mpdm_aget(l, -1);
130 if (MPDM_IS_ARRAY(s) || MPDM_IS_ARRAY(v)) {
131 int n;
132 mpdm_t a;
134 for (n = 0; n < mpdm_size(s); n++)
135 mpdm_hset(h, mpdm_aget(s, n), mpdm_aget(v, n));
137 /* store the rest of arguments into _ */
138 a = mpdm_hset_s(h, L"_", MPDM_A(0));
140 for (; n < mpdm_size(v); n++)
141 mpdm_push(a, mpdm_aget(v, n));
143 else
144 mpdm_hset(h, s, v);
146 mpdm_unref(l);
147 mpdm_unref(v);
148 mpdm_unref(s);
154 * mpsl_set_symbol - Sets value to a symbol.
155 * @s: symbol name
156 * @v: value
157 * @l: local symbol table
159 * Assigns the value @v to the @s symbol. If the value exists as
160 * a local symbol, it's assigned to it; otherwise, it's set as a global
161 * symbol (and created if it does not exist).
163 mpdm_t mpsl_set_symbol(mpdm_t s, mpdm_t v, mpdm_t l)
165 mpdm_t r;
167 mpdm_ref(s);
168 mpdm_ref(v);
169 mpdm_ref(l);
171 r = mpdm_sset(find_local_symtbl(s, l), s, v);
173 mpdm_unref(l);
174 mpdm_unref(v);
175 mpdm_unref(s);
177 return r;
182 * mpsl_get_symbol - Gets the value of a symbol.
183 * @s: symbol name
184 * @l: local symbol table
186 * Gets the value of a symbol. The symbol can be local or global
187 * (if the symbol exists in both tables, the local value will be returned).
189 mpdm_t mpsl_get_symbol(mpdm_t s, mpdm_t l)
191 mpdm_t r;
193 mpdm_ref(s);
194 mpdm_ref(l);
196 r = mpdm_sget(find_local_symtbl(s, l), s);
198 mpdm_unref(l);
199 mpdm_unref(s);
201 return r;
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);
223 /** opcode macro helpers **/
225 #define O_TYPE static mpdm_t
226 #define O_ARGS mpdm_t c, mpdm_t a, mpdm_t l, int * f
228 O_TYPE mpsl_exec_i(O_ARGS);
230 #define C(n) mpdm_aget(c, n)
231 #define C0 C(0)
232 #define C1 C(1)
234 #define M(n) mpsl_exec_i(C(n), a, l, f)
235 #define M1 M(1)
236 #define M2 M(2)
237 #define M3 M(3)
238 #define M4 M(4)
240 #define R(x) mpdm_rval(x)
241 #define I(x) mpdm_ival(x)
243 #define RM1 mpdm_rval(M(1))
244 #define RM2 mpdm_rval(M(2))
245 #define IM1 mpdm_ival(M(1))
246 #define IM2 mpdm_ival(M(2))
248 #define GET(m) mpsl_get_symbol(m, l)
249 #define SET(m, v) mpsl_set_symbol(m, v, l)
250 #define BOOL mpsl_boolean
252 #define RF(v) mpdm_ref(v)
253 #define UF(v) v = mpdm_unref(v)
254 #define UFND(v) mpdm_unrefnd(v)
256 static int is_true_uf(mpdm_t v)
258 int r;
260 RF(v);
261 r = mpsl_is_true(v);
262 UF(v);
264 return r;
267 /** opcodes **/
269 O_TYPE O_literal(O_ARGS)
271 return mpdm_clone(C1);
274 O_TYPE O_multi(O_ARGS)
276 mpdm_t v = M1;
278 if (!*f) {
279 mpdm_void(v);
280 v = M2;
283 return v;
286 O_TYPE O_imulti(O_ARGS)
288 mpdm_t v = RF(M1);
290 if (!*f)
291 mpdm_void(M2);
293 return UFND(v);
296 O_TYPE O_symval(O_ARGS)
298 return GET(M1);
301 O_TYPE O_assign(O_ARGS)
303 return SET(M1, M2);
306 O_TYPE O_if(O_ARGS)
308 return is_true_uf(M1) ? M2 : M3;
311 O_TYPE O_local(O_ARGS)
313 set_local_symbols(M1, NULL, l);
315 return NULL;
318 O_TYPE O_uminus(O_ARGS)
320 return MPDM_R(-mpdm_rval(M1));
323 O_TYPE O_add(O_ARGS)
325 return MPDM_R(RM1 + RM2);
328 O_TYPE O_sub(O_ARGS)
330 return MPDM_R(RM1 - RM2);
333 O_TYPE O_mul(O_ARGS)
335 return MPDM_R(RM1 * RM2);
338 O_TYPE O_div(O_ARGS)
340 return MPDM_R(RM1 / RM2);
343 O_TYPE O_mod(O_ARGS)
345 return MPDM_I(IM1 % IM2);
348 O_TYPE O_not(O_ARGS)
350 return BOOL(!is_true_uf(M1));
353 O_TYPE O_and(O_ARGS)
355 mpdm_t v = M1;
356 mpdm_t r;
358 if (mpsl_is_true(v)) {
359 mpdm_void(v);
360 r = M2;
362 else
363 r = v;
365 return r;
368 O_TYPE O_or(O_ARGS)
370 mpdm_t v = M1;
371 mpdm_t r;
373 if (!mpsl_is_true(v)) {
374 mpdm_void(v);
375 r = M2;
377 else
378 r = v;
380 return r;
383 O_TYPE O_bitand(O_ARGS)
385 return MPDM_I(IM1 & IM2);
388 O_TYPE O_bitor(O_ARGS)
390 return MPDM_I(IM1 | IM2);
393 O_TYPE O_bitxor(O_ARGS)
395 return MPDM_I(IM1 ^ IM2);
398 O_TYPE O_shl(O_ARGS)
400 return MPDM_I(IM1 << IM2);
403 O_TYPE O_shr(O_ARGS)
405 return MPDM_I(IM1 >> IM2);
408 O_TYPE O_pow(O_ARGS)
410 return MPDM_R(pow(RM1, RM2));
413 O_TYPE O_numlt(O_ARGS)
415 return BOOL(RM1 < RM2);
418 O_TYPE O_numle(O_ARGS)
420 return BOOL(RM1 <= RM2);
423 O_TYPE O_numgt(O_ARGS)
425 return BOOL(RM1 > RM2);
428 O_TYPE O_numge(O_ARGS)
430 return BOOL(RM1 >= RM2);
433 O_TYPE O_strcat(O_ARGS)
435 return mpdm_strcat(M1, M2);
438 O_TYPE O_streq(O_ARGS)
440 return BOOL(mpdm_cmp(M1, M2) == 0);
443 O_TYPE O_numeq(O_ARGS)
445 mpdm_t v1 = RF(M1);
446 mpdm_t v2 = RF(M2);
448 mpdm_t r = BOOL((v1 == NULL || v2 == NULL) ?
449 (v1 == v2) : (R(v1) == R(v2))
452 UF(v2);
453 UF(v1);
455 return r;
458 O_TYPE O_break(O_ARGS)
460 *f = 1;
462 return NULL;
465 O_TYPE O_return(O_ARGS)
467 mpdm_t v = M1;
469 *f = -1;
471 return v;
474 O_TYPE execsym(O_ARGS, int th)
476 mpdm_t s, v, r = NULL;
478 /* gets the symbol name */
479 s = RF(M1);
481 /* gets the symbol value */
482 v = GET(s);
484 if (!MPDM_IS_EXEC(v)) {
485 /* not found or NULL value? error */
486 mpdm_t t, w;
487 char tmp[128];
489 w = RF(mpdm_join_s(s, L"."));
490 t = RF(MPDM_2MBS((wchar_t *) w->data));
492 snprintf(tmp, sizeof(tmp), "Undefined function %s()",
493 (char *) t->data);
495 mpsl_error(MPDM_MBS(tmp));
497 UF(w);
498 UF(t);
500 else {
501 /* execute */
502 r = th ? mpdm_exec_thread(v, M2, l) : mpdm_exec(v, M2, l);
505 UF(s);
507 return r;
511 O_TYPE O_execsym(O_ARGS)
512 /* executes the value of a symbol */
514 return execsym(c, a, l, f, 0);
518 O_TYPE O_threadsym(O_ARGS)
519 /* executes the value of a symbol in a new thread */
521 return execsym(c, a, l, f, 1);
525 O_TYPE O_while(O_ARGS)
526 /* while/for loop */
528 mpdm_t r = NULL;
530 for (mpdm_void(M3); !*f && is_true_uf(M1); mpdm_void(M4)) {
531 UF(r);
532 r = RF(M2);
535 if (*f == 1)
536 *f = 0;
538 return UFND(r);
542 O_TYPE O_foreach(O_ARGS)
543 /* foreach loop */
545 mpdm_t s = RF(M1);
546 mpdm_t v = RF(M2);
547 mpdm_t r = NULL;
548 int n;
550 for (n = 0; n < mpdm_size(v) && !*f; n++) {
551 SET(s, mpdm_aget(v, n));
552 UF(r);
553 r = RF(M3);
556 if (*f == 1)
557 *f = 0;
559 UF(s);
560 UF(v);
562 return UFND(r);
566 O_TYPE O_range(O_ARGS)
567 /* build list from range of two numeric values */
569 double n;
570 double v1 = RM1;
571 double v2 = RM2;
572 mpdm_t r = RF(MPDM_A(0));
574 if (v1 < v2)
575 for (n = v1; n <= v2; n++)
576 mpdm_push(r, MPDM_R(n));
577 else
578 for (n = v1; n >= v2; n--)
579 mpdm_push(r, MPDM_R(n));
581 UFND(r);
583 return r;
587 O_TYPE O_list(O_ARGS)
588 /* build list from instructions */
590 mpdm_t ret = RF(mpdm_size(c) == 2 ? MPDM_A(0) : M(2));
592 mpdm_push(ret, M(1));
594 return UFND(ret);
598 O_TYPE O_ilist(O_ARGS)
599 /* build and inverse list from instructions */
601 mpdm_t ret = RF(mpdm_size(c) == 2 ? MPDM_A(0) : M(2));
603 mpdm_ins(ret, M(1), 0);
605 return UFND(ret);
609 O_TYPE O_hash(O_ARGS)
610 /* build hash from instructions */
612 mpdm_t ret = RF(mpdm_size(c) == 3 ? MPDM_H(0) : M(3));
614 mpdm_hset(ret, M1, M2);
616 return UFND(ret);
620 O_TYPE O_blkframe(O_ARGS)
621 /* runs an instruction under a block frame */
623 mpdm_t ret;
625 /* no context? create one */
626 if (l == NULL)
627 l = MPDM_A(0);
629 RF(l);
631 /* create a new local symbol table */
632 mpdm_push(l, MPDM_H(0));
634 /* creates the arguments (if any) as local variables */
635 set_local_symbols(M2, a, l);
637 /* execute instruction */
638 ret = RF(M1);
640 /* destroy the local symbol table */
641 mpdm_adel(l, -1);
643 UF(l);
645 return UFND(ret);
649 O_TYPE O_subframe(O_ARGS)
650 /* runs an instruction inside a subroutine frame */
652 /* like a block frame, but with its own symbol table */
653 return O_blkframe(c, a, MPDM_A(0), f);
657 static struct mpsl_op_s {
658 wchar_t *name;
659 int foldable;
660 mpdm_t(*func) (O_ARGS);
661 } op_table[] = {
662 { L"LITERAL", 0, O_literal }, /* *must* be the zeroth */
663 { L"MULTI", 0, O_multi },
664 { L"IMULTI", 0, O_imulti },
665 { L"SYMVAL", 0, O_symval },
666 { L"ASSIGN", 0, O_assign },
667 { L"EXECSYM", 0, O_execsym },
668 { L"THREADSYM", 0, O_threadsym },
669 { L"IF", 0, O_if },
670 { L"WHILE", 0, O_while },
671 { L"FOREACH", 0, O_foreach },
672 { L"SUBFRAME", 0, O_subframe },
673 { L"BLKFRAME", 0, O_blkframe },
674 { L"BREAK", 0, O_break },
675 { L"RETURN", 0, O_return },
676 { L"LOCAL", 0, O_local },
677 { L"LIST", 1, O_list },
678 { L"ILIST", 1, O_ilist },
679 { L"HASH", 1, O_hash },
680 { L"RANGE", 1, O_range },
681 { L"UMINUS", 1, O_uminus },
682 { L"ADD", 1, O_add },
683 { L"SUB", 1, O_sub },
684 { L"MUL", 1, O_mul },
685 { L"DIV", 1, O_div },
686 { L"MOD", 1, O_mod },
687 { L"NOT", 1, O_not },
688 { L"AND", 1, O_and },
689 { L"OR", 1, O_or },
690 { L"NUMEQ", 1, O_numeq },
691 { L"NUMLT", 1, O_numlt },
692 { L"NUMLE", 1, O_numle },
693 { L"NUMGT", 1, O_numgt },
694 { L"NUMGE", 1, O_numge },
695 { L"STRCAT", 1, O_strcat },
696 { L"STREQ", 1, O_streq },
697 { L"BITAND", 1, O_bitand },
698 { L"BITOR", 1, O_bitor },
699 { L"BITXOR", 1, O_bitxor },
700 { L"SHL", 1, O_shl },
701 { L"SHR", 1, O_shr },
702 { L"POW", 1, O_pow },
703 { NULL, 0, NULL }
707 O_TYPE mpsl_exec_i(O_ARGS)
708 /* Executes one MPSL instruction in the MPSL virtual machine. Called
709 from mpsl_exec_p() (which holds the flow control status variable) */
711 mpdm_t ret = NULL;
713 mpdm_ref(c);
714 mpdm_ref(a);
715 mpdm_ref(l);
717 /* if aborted or NULL, do nothing */
718 if (!mpsl_abort && c != NULL) {
719 /* gets the opcode and calls it */
720 ret = op_table[mpdm_ival(C0)].func(c, a, l, f);
722 if (mpsl_trap_func != NULL) {
723 mpdm_t f = mpsl_trap_func;
725 mpdm_ref(ret);
727 mpsl_trap_func = NULL;
728 mpdm_exec_3(f, c, a, ret, l);
729 mpsl_trap_func = f;
731 mpdm_unrefnd(ret);
735 mpdm_unref(l);
736 mpdm_unref(a);
737 mpdm_unref(c);
739 return ret;
743 mpdm_t mpsl_exec_p(mpdm_t c, mpdm_t a, mpdm_t ctxt)
744 /* executes an MPSL instruction stream */
746 int f = 0;
748 /* execute first instruction with a new flow control variable */
749 return mpsl_exec_i(c, a, ctxt, &f);
753 static mpdm_t constant_fold(mpdm_t i)
754 /* tries to fold complex but constant expressions into a literal */
756 int n;
757 mpdm_t v;
759 /* get the number opcode */
760 n = mpdm_ival(mpdm_aget(i, 0));
762 if (op_table[n].foldable) {
763 /* test if all arguments are literal (opcode 0) */
764 for (n = 1; n < mpdm_size(i); n++) {
765 mpdm_t t = mpdm_aget(i, n);
767 /* if it's not LITERAL, abort immediately */
768 if (mpdm_ival(mpdm_aget(t, 0)) != 0)
769 return i;
772 /* execute the instruction and convert to LITERAL */
773 v = mpsl_exec_p(i, NULL, NULL);
774 i = mpsl_mkins(L"LITERAL", 1, v, NULL, NULL, NULL);
777 return i;
781 mpdm_t mpsl_mkins(wchar_t * opcode, int args, mpdm_t a1, mpdm_t a2,
782 mpdm_t a3, mpdm_t a4)
783 /* creates an instruction */
785 mpdm_t o;
786 mpdm_t v;
788 v = MPDM_A(args + 1);
789 mpdm_ref(v);
791 /* inserts the opcode */
792 o = mpdm_hget_s(mpsl_opcodes, opcode);
793 mpdm_aset(v, o, 0);
795 switch (args) {
796 case 4: mpdm_aset(v, a4, 4); /* no break */
797 case 3: mpdm_aset(v, a3, 3); /* no break */
798 case 2: mpdm_aset(v, a2, 2); /* no break */
799 case 1: mpdm_aset(v, a1, 1); /* no break */
802 mpdm_unrefnd(v);
804 v = constant_fold(v);
806 return v;
810 mpdm_t mpsl_build_opcodes(void)
811 /* builds the table of opcodes */
813 int n;
814 mpdm_t r = MPDM_H(0);
816 mpdm_ref(r);
818 for (n = 0; op_table[n].name != NULL; n++) {
819 mpdm_t v = MPDM_LS(op_table[n].name);
821 mpdm_set_ival(v, n);
823 /* keys and values are the same */
824 mpdm_hset(r, v, v);
827 mpdm_unrefnd(r);
829 return r;
834 * mpsl_trap - Install a trapping function.
835 * @trap_func: The trapping MPSL code
837 * Installs a trapping function. The function is an MPSL
838 * executable value receiving 3 arguments: the code stream,
839 * the arguments and the return value of the executed code.
841 * Returns NULL (previous versions returned the previous
842 * trapping function).
844 mpdm_t mpsl_trap(mpdm_t trap_func)
846 mpdm_ref(trap_func);
847 mpdm_unref(mpsl_trap_func);
848 mpsl_trap_func = trap_func;
850 return NULL;
855 * mpsl_argv - Fills the ARGV global array.
856 * @argc: number of arguments
857 * @argv: array of string values
859 * Fills the ARGV global MPSL array with an array of arguments. These
860 * are usually the ones sent to main().
862 void mpsl_argv(int argc, char *argv[])
864 int n;
865 mpdm_t ARGV;
867 /* create the ARGV array */
868 ARGV = mpdm_hset_s(mpdm_root(), L"ARGV", MPDM_A(0));
870 for (n = 0; n < argc; n++)
871 mpdm_push(ARGV, MPDM_MBS(argv[n]));
875 /* in mpsl_f.c */
876 mpdm_t mpsl_build_funcs(void);
880 * mpsl_startup - Initializes MPSL.
882 * Initializes the Minimum Profit Scripting Language. Returns 0 if
883 * everything went OK.
885 int mpsl_startup(void)
887 mpdm_t r;
888 mpdm_t m;
890 /* startup MPDM */
891 mpdm_startup();
893 r = mpdm_root();
895 /* creates INC, unless already defined */
896 if (mpdm_hget_s(r, L"INC") == NULL)
897 mpdm_hset_s(r, L"INC", MPDM_A(0));
899 /* the TRUE value */
900 mpdm_hset_s(r, L"TRUE", MPDM_I(1));
902 /* standard file descriptors */
903 mpdm_hset_s(r, L"STDIN", MPDM_F(stdin));
904 mpdm_hset_s(r, L"STDOUT", MPDM_F(stdout));
905 mpdm_hset_s(r, L"STDERR", MPDM_F(stderr));
907 /* home and application directories */
908 mpdm_hset_s(r, L"HOMEDIR", mpdm_home_dir());
909 mpdm_hset_s(r, L"APPDIR", mpdm_app_dir());
911 /* fill now the MPSL hash */
912 m = MPDM_H(0);
913 mpdm_hset_s(r, L"MPSL", m);
915 /* store things there */
916 mpdm_hset_s(m, L"VERSION", MPDM_MBS(VERSION));
917 mpdm_hset_s(m, L"OPCODE", mpsl_build_opcodes());
918 mpdm_hset_s(m, L"LC", MPDM_H(0));
919 mpdm_hset_s(m, L"CORE", mpsl_build_funcs());
921 mpdm_dump_1 = mpsl_dump_1;
923 return 0;
928 * mpsl_shutdown - Shuts down MPSL.
930 * Shuts down MPSL. No MPSL functions should be used from now on.
932 void mpsl_shutdown(void)
934 mpdm_shutdown();