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)
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
)
269 O_TYPE
O_literal(O_ARGS
)
271 return mpdm_clone(C1
);
274 O_TYPE
O_multi(O_ARGS
)
286 O_TYPE
O_imulti(O_ARGS
)
296 O_TYPE
O_symval(O_ARGS
)
301 O_TYPE
O_assign(O_ARGS
)
308 return is_true_uf(M1
) ? M2
: M3
;
311 O_TYPE
O_local(O_ARGS
)
313 set_local_symbols(M1
, NULL
, l
);
318 O_TYPE
O_uminus(O_ARGS
)
320 return MPDM_R(-mpdm_rval(M1
));
325 return MPDM_R(RM1
+ RM2
);
330 return MPDM_R(RM1
- RM2
);
335 return MPDM_R(RM1
* RM2
);
340 return MPDM_R(RM1
/ RM2
);
345 return MPDM_I(IM1
% IM2
);
350 return BOOL(!is_true_uf(M1
));
358 if (mpsl_is_true(v
)) {
373 if (!mpsl_is_true(v
)) {
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
);
400 return MPDM_I(IM1
<< IM2
);
405 return MPDM_I(IM1
>> IM2
);
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
)
448 mpdm_t r
= BOOL((v1
== NULL
|| v2
== NULL
) ?
449 (v1
== v2
) : (R(v1
) == R(v2
))
458 O_TYPE
O_break(O_ARGS
)
465 O_TYPE
O_return(O_ARGS
)
474 O_TYPE
execsym(O_ARGS
, int th
)
476 mpdm_t s
, v
, r
= NULL
;
478 /* gets the symbol name */
481 /* gets the symbol value */
484 if (!MPDM_IS_EXEC(v
)) {
485 /* not found or NULL value? error */
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()",
495 mpsl_error(MPDM_MBS(tmp
));
502 r
= th
? mpdm_exec_thread(v
, M2
, l
) : mpdm_exec(v
, M2
, l
);
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
)
530 for (mpdm_void(M3
); !*f
&& is_true_uf(M1
); mpdm_void(M4
)) {
542 O_TYPE
O_foreach(O_ARGS
)
550 for (n
= 0; n
< mpdm_size(v
) && !*f
; n
++) {
551 SET(s
, mpdm_aget(v
, n
));
566 O_TYPE
O_range(O_ARGS
)
567 /* build list from range of two numeric values */
572 mpdm_t r
= RF(MPDM_A(0));
575 for (n
= v1
; n
<= v2
; n
++)
576 mpdm_push(r
, MPDM_R(n
));
578 for (n
= v1
; n
>= v2
; n
--)
579 mpdm_push(r
, MPDM_R(n
));
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));
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);
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
);
620 O_TYPE
O_blkframe(O_ARGS
)
621 /* runs an instruction under a block frame */
625 /* no context? create one */
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 */
640 /* destroy the local symbol table */
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
{
660 mpdm_t(*func
) (O_ARGS
);
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
},
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
},
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
},
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) */
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
;
727 mpsl_trap_func
= NULL
;
728 mpdm_exec_3(f
, c
, a
, ret
, l
);
743 mpdm_t
mpsl_exec_p(mpdm_t c
, mpdm_t a
, mpdm_t ctxt
)
744 /* executes an MPSL instruction stream */
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 */
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)
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
);
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 */
788 v
= MPDM_A(args
+ 1);
791 /* inserts the opcode */
792 o
= mpdm_hget_s(mpsl_opcodes
, opcode
);
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 */
804 v
= constant_fold(v
);
810 mpdm_t
mpsl_build_opcodes(void)
811 /* builds the table of opcodes */
814 mpdm_t r
= MPDM_H(0);
818 for (n
= 0; op_table
[n
].name
!= NULL
; n
++) {
819 mpdm_t v
= MPDM_LS(op_table
[n
].name
);
823 /* keys and values are the same */
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
)
847 mpdm_unref(mpsl_trap_func
);
848 mpsl_trap_func
= trap_func
;
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
[])
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
]));
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)
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));
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 */
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
;
928 * mpsl_shutdown - Shuts down MPSL.
930 * Shuts down MPSL. No MPSL functions should be used from now on.
932 void mpsl_shutdown(void)