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
37 /* global abort flag */
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
;
51 * mpsl_is_true - Tests if a value is true.
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 */
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'))
72 /* any other case is true */
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 */
95 /* no local symbol table? nothing to find */
99 /* if s is multiple, take just the first element */
100 if (MPDM_IS_ARRAY(s
))
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
)) {
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 */
127 /* gets the top local variable frame */
128 h
= mpdm_aget(l
, -1);
130 if (MPDM_IS_ARRAY(s
) || MPDM_IS_ARRAY(v
)) {
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
));
154 * mpsl_set_symbol - Sets value to a symbol.
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
)
171 r
= mpdm_sset(find_local_symtbl(s
, l
), s
, v
);
182 * mpsl_get_symbol - Gets the value of a symbol.
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
)
196 r
= mpdm_sget(find_local_symtbl(s
, l
), s
);
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 */
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)
234 #define M(n) mpsl_exec_i(C(n), a, l, f)
239 #define R(x) mpdm_rval(x)
240 #define I(x) mpdm_ival(x)
242 #define RM1 mpdm_rval(M(1))
243 #define RM2 mpdm_rval(M(2))
244 #define IM1 mpdm_ival(M(1))
245 #define IM2 mpdm_ival(M(2))
247 #define GET(m) mpsl_get_symbol(m, l)
248 #define SET(m, v) mpsl_set_symbol(m, v, l)
249 #define BOOL mpsl_boolean
251 #define RF(v) mpdm_ref(v)
252 #define UF(v) v = mpdm_unref(v)
253 #define UFND(v) mpdm_unrefnd(v)
255 static int is_true_uf(mpdm_t v
)
268 O_TYPE
O_literal(O_ARGS
)
270 return mpdm_clone(C1
);
273 O_TYPE
O_multi(O_ARGS
)
285 O_TYPE
O_imulti(O_ARGS
)
295 O_TYPE
O_symval(O_ARGS
)
300 O_TYPE
O_assign(O_ARGS
)
307 return is_true_uf(M1
) ? M2
: M3
;
310 O_TYPE
O_local(O_ARGS
)
312 set_local_symbols(M1
, NULL
, l
);
317 O_TYPE
O_uminus(O_ARGS
)
319 return MPDM_R(-mpdm_rval(M1
));
324 return MPDM_R(RM1
+ RM2
);
329 return MPDM_R(RM1
- RM2
);
334 return MPDM_R(RM1
* RM2
);
339 return MPDM_R(RM1
/ RM2
);
344 return MPDM_I(IM1
% IM2
);
349 return BOOL(!is_true_uf(M1
));
357 if (mpsl_is_true(v
)) {
372 if (!mpsl_is_true(v
)) {
382 O_TYPE
O_bitand(O_ARGS
)
384 return MPDM_I(IM1
& IM2
);
387 O_TYPE
O_bitor(O_ARGS
)
389 return MPDM_I(IM1
| IM2
);
392 O_TYPE
O_bitxor(O_ARGS
)
394 return MPDM_I(IM1
^ IM2
);
399 return MPDM_I(IM1
<< IM2
);
404 return MPDM_I(IM1
>> IM2
);
409 return MPDM_R(pow(RM1
, RM2
));
412 O_TYPE
O_numlt(O_ARGS
)
414 return BOOL(RM1
< RM2
);
417 O_TYPE
O_numle(O_ARGS
)
419 return BOOL(RM1
<= RM2
);
422 O_TYPE
O_numgt(O_ARGS
)
424 return BOOL(RM1
> RM2
);
427 O_TYPE
O_numge(O_ARGS
)
429 return BOOL(RM1
>= RM2
);
432 O_TYPE
O_strcat(O_ARGS
)
434 return mpdm_strcat(M1
, M2
);
437 O_TYPE
O_streq(O_ARGS
)
439 return BOOL(mpdm_cmp(M1
, M2
) == 0);
442 O_TYPE
O_numeq(O_ARGS
)
447 mpdm_t r
= BOOL((v1
== NULL
|| v2
== NULL
) ?
448 (v1
== v2
) : (R(v1
) == R(v2
))
457 O_TYPE
O_break(O_ARGS
)
464 O_TYPE
O_return(O_ARGS
)
473 O_TYPE
execsym(O_ARGS
, int th
)
475 mpdm_t s
, v
, r
= NULL
;
477 /* gets the symbol name */
480 /* gets the symbol value */
483 if (!MPDM_IS_EXEC(v
)) {
484 /* not found or NULL value? error */
488 w
= RF(mpdm_join_s(L
".", s
));
489 t
= RF(MPDM_2MBS((wchar_t *) w
->data
));
491 snprintf(tmp
, sizeof(tmp
), "Undefined function %s()",
494 mpsl_error(MPDM_MBS(tmp
));
501 r
= th
? mpdm_exec_thread(v
, M2
, l
) : mpdm_exec(v
, M2
, l
);
510 O_TYPE
O_execsym(O_ARGS
)
511 /* executes the value of a symbol */
513 return execsym(c
, a
, l
, f
, 0);
517 O_TYPE
O_threadsym(O_ARGS
)
518 /* executes the value of a symbol in a new thread */
520 return execsym(c
, a
, l
, f
, 1);
524 O_TYPE
O_while(O_ARGS
)
529 while (!*f
&& is_true_uf(M1
)) {
541 O_TYPE
O_foreach(O_ARGS
)
549 for (n
= 0; n
< mpdm_size(v
) && !*f
; n
++) {
550 SET(s
, mpdm_aget(v
, n
));
565 O_TYPE
O_range(O_ARGS
)
566 /* build list from range of two numeric values */
571 mpdm_t r
= RF(MPDM_A(0));
574 for (n
= v1
; n
<= v2
; n
++)
575 mpdm_push(r
, MPDM_R(n
));
577 for (n
= v1
; n
>= v2
; n
--)
578 mpdm_push(r
, MPDM_R(n
));
586 O_TYPE
O_list(O_ARGS
)
587 /* build list from instructions */
589 mpdm_t ret
= RF(mpdm_size(c
) == 2 ? MPDM_A(0) : M(2));
591 mpdm_push(ret
, M(1));
597 O_TYPE
O_ilist(O_ARGS
)
598 /* build and inverse list from instructions */
600 mpdm_t ret
= RF(mpdm_size(c
) == 2 ? MPDM_A(0) : M(2));
602 mpdm_ins(ret
, M(1), 0);
608 O_TYPE
O_hash(O_ARGS
)
609 /* build hash from instructions */
611 mpdm_t ret
= RF(mpdm_size(c
) == 3 ? MPDM_H(0) : M(3));
613 mpdm_hset(ret
, M1
, M2
);
619 O_TYPE
O_blkframe(O_ARGS
)
620 /* runs an instruction under a block frame */
624 /* no context? create one */
630 /* create a new local symbol table */
631 mpdm_push(l
, MPDM_H(0));
633 /* creates the arguments (if any) as local variables */
634 set_local_symbols(M2
, a
, l
);
636 /* execute instruction */
639 /* destroy the local symbol table */
648 O_TYPE
O_subframe(O_ARGS
)
649 /* runs an instruction inside a subroutine frame */
651 /* like a block frame, but with its own symbol table */
652 return O_blkframe(c
, a
, MPDM_A(0), f
);
656 static struct mpsl_op_s
{
659 mpdm_t(*func
) (O_ARGS
);
661 { L
"LITERAL", 0, O_literal
}, /* *must* be the zeroth */
662 { L
"MULTI", 0, O_multi
},
663 { L
"IMULTI", 0, O_imulti
},
664 { L
"SYMVAL", 0, O_symval
},
665 { L
"ASSIGN", 0, O_assign
},
666 { L
"EXECSYM", 0, O_execsym
},
667 { L
"THREADSYM", 0, O_threadsym
},
669 { L
"WHILE", 0, O_while
},
670 { L
"FOREACH", 0, O_foreach
},
671 { L
"SUBFRAME", 0, O_subframe
},
672 { L
"BLKFRAME", 0, O_blkframe
},
673 { L
"BREAK", 0, O_break
},
674 { L
"RETURN", 0, O_return
},
675 { L
"LOCAL", 0, O_local
},
676 { L
"LIST", 1, O_list
},
677 { L
"ILIST", 1, O_ilist
},
678 { L
"HASH", 1, O_hash
},
679 { L
"RANGE", 1, O_range
},
680 { L
"UMINUS", 1, O_uminus
},
681 { L
"ADD", 1, O_add
},
682 { L
"SUB", 1, O_sub
},
683 { L
"MUL", 1, O_mul
},
684 { L
"DIV", 1, O_div
},
685 { L
"MOD", 1, O_mod
},
686 { L
"NOT", 1, O_not
},
687 { L
"AND", 1, O_and
},
689 { L
"NUMEQ", 1, O_numeq
},
690 { L
"NUMLT", 1, O_numlt
},
691 { L
"NUMLE", 1, O_numle
},
692 { L
"NUMGT", 1, O_numgt
},
693 { L
"NUMGE", 1, O_numge
},
694 { L
"STRCAT", 1, O_strcat
},
695 { L
"STREQ", 1, O_streq
},
696 { L
"BITAND", 1, O_bitand
},
697 { L
"BITOR", 1, O_bitor
},
698 { L
"BITXOR", 1, O_bitxor
},
699 { L
"SHL", 1, O_shl
},
700 { L
"SHR", 1, O_shr
},
701 { L
"POW", 1, O_pow
},
706 O_TYPE
mpsl_exec_i(O_ARGS
)
707 /* Executes one MPSL instruction in the MPSL virtual machine. Called
708 from mpsl_exec_p() (which holds the flow control status variable) */
716 /* if aborted or NULL, do nothing */
717 if (!mpsl_abort
&& c
!= NULL
) {
718 /* gets the opcode and calls it */
719 ret
= op_table
[mpdm_ival(C0
)].func(c
, a
, l
, f
);
721 if (mpsl_trap_func
!= NULL
) {
722 mpdm_t f
= mpsl_trap_func
;
726 mpsl_trap_func
= NULL
;
727 mpdm_exec_3(f
, c
, a
, ret
, l
);
742 mpdm_t
mpsl_exec_p(mpdm_t c
, mpdm_t a
, mpdm_t ctxt
)
743 /* executes an MPSL instruction stream */
747 /* execute first instruction with a new flow control variable */
748 return mpsl_exec_i(c
, a
, ctxt
, &f
);
752 static mpdm_t
constant_fold(mpdm_t i
)
753 /* tries to fold complex but constant expressions into a literal */
758 /* get the number opcode */
759 n
= mpdm_ival(mpdm_aget(i
, 0));
761 if (op_table
[n
].foldable
) {
762 /* test if all arguments are literal (opcode 0) */
763 for (n
= 1; n
< mpdm_size(i
); n
++) {
764 mpdm_t t
= mpdm_aget(i
, n
);
766 /* if it's not LITERAL, abort immediately */
767 if (mpdm_ival(mpdm_aget(t
, 0)) != 0)
771 /* execute the instruction and convert to LITERAL */
772 v
= mpsl_exec_p(i
, NULL
, NULL
);
773 i
= mpsl_mkins(L
"LITERAL", 1, v
, NULL
, NULL
);
780 mpdm_t
mpsl_mkins(wchar_t * opcode
, int args
, mpdm_t a1
, mpdm_t a2
,
782 /* creates an instruction */
787 v
= MPDM_A(args
+ 1);
790 /* inserts the opcode */
791 o
= mpdm_hget_s(mpsl_opcodes
, opcode
);
795 case 3: mpdm_aset(v
, a3
, 3); /* no break */
796 case 2: mpdm_aset(v
, a2
, 2); /* no break */
797 case 1: mpdm_aset(v
, a1
, 1); /* no break */
802 v
= constant_fold(v
);
808 mpdm_t
mpsl_build_opcodes(void)
809 /* builds the table of opcodes */
812 mpdm_t r
= MPDM_H(0);
816 for (n
= 0; op_table
[n
].name
!= NULL
; n
++) {
817 mpdm_t v
= MPDM_LS(op_table
[n
].name
);
821 /* keys and values are the same */
832 * mpsl_trap - Install a trapping function.
833 * @trap_func: The trapping MPSL code
835 * Installs a trapping function. The function is an MPSL
836 * executable value receiving 3 arguments: the code stream,
837 * the arguments and the return value of the executed code.
839 * Returns NULL (previous versions returned the previous
840 * trapping function).
842 mpdm_t
mpsl_trap(mpdm_t trap_func
)
845 mpdm_unref(mpsl_trap_func
);
846 mpsl_trap_func
= trap_func
;
853 * mpsl_argv - Fills the ARGV global array.
854 * @argc: number of arguments
855 * @argv: array of string values
857 * Fills the ARGV global MPSL array with an array of arguments. These
858 * are usually the ones sent to main().
860 void mpsl_argv(int argc
, char *argv
[])
865 /* create the ARGV array */
866 ARGV
= mpdm_hset_s(mpdm_root(), L
"ARGV", MPDM_A(0));
868 for (n
= 0; n
< argc
; n
++)
869 mpdm_push(ARGV
, MPDM_MBS(argv
[n
]));
874 mpdm_t
mpsl_build_funcs(void);
878 * mpsl_startup - Initializes MPSL.
880 * Initializes the Minimum Profit Scripting Language. Returns 0 if
881 * everything went OK.
883 int mpsl_startup(void)
893 /* creates INC, unless already defined */
894 if (mpdm_hget_s(r
, L
"INC") == NULL
)
895 mpdm_hset_s(r
, L
"INC", MPDM_A(0));
898 mpdm_hset_s(r
, L
"TRUE", MPDM_I(1));
900 /* standard file descriptors */
901 mpdm_hset_s(r
, L
"STDIN", MPDM_F(stdin
));
902 mpdm_hset_s(r
, L
"STDOUT", MPDM_F(stdout
));
903 mpdm_hset_s(r
, L
"STDERR", MPDM_F(stderr
));
905 /* home and application directories */
906 mpdm_hset_s(r
, L
"HOMEDIR", mpdm_home_dir());
907 mpdm_hset_s(r
, L
"APPDIR", mpdm_app_dir());
909 /* fill now the MPSL hash */
911 mpdm_hset_s(r
, L
"MPSL", m
);
913 /* store things there */
914 mpdm_hset_s(m
, L
"VERSION", MPDM_MBS(VERSION
));
915 mpdm_hset_s(m
, L
"OPCODE", mpsl_build_opcodes());
916 mpdm_hset_s(m
, L
"LC", MPDM_H(0));
917 mpdm_hset_s(m
, L
"CORE", mpsl_build_funcs());
919 mpdm_dump_1
= mpsl_dump_1
;
926 * mpsl_shutdown - Shuts down MPSL.
928 * Shuts down MPSL. No MPSL functions should be used from now on.
930 void mpsl_shutdown(void)