annotate src/paranormal/libcalc/execute.c @ 2190:36b9f3e6cbcf

demac: added stub for update_song_tuple() for debugging purposes
author Eugene Zagidullin <e.asphyx@gmail.com>
date Tue, 27 Nov 2007 18:03:20 +0300
parents 3e160f6c04d2
children f1b6f1b2cdb3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
1 /* execute.c -- execute precompiled expression expr
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
2 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
3 * Copyright (C) 2001 Janusz Gregorczyk <jgregor@kki.net.pl>
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
4 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
5 * This file is part of xvs.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
6 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
9 * the Free Software Foundation; either version 2 of the License, or
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
10 * (at your option) any later version.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
11 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful,
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
15 * GNU General Public License for more details.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
16 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
18 * along with this program; if not, write to the Free Software
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
20 */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
21
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
22 #include <glib.h>
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
23 #include <math.h>
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
24
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
25 #include "execute.h"
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
26 #include "function.h"
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
27
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
28 /* Execution stack. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
29
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
30 gboolean check_stack (ex_stack *stack, int depth) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
31 if (stack->sp < depth) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
32 g_warning ("Stack error");
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
33 return FALSE;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
34 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
35
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
36 return TRUE;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
37 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
38
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
39 void push (ex_stack *stack, double value) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
40 g_assert (stack);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
41
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
42 if (stack->sp < STACK_DEPTH) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
43 stack->value[stack->sp++] = value;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
44 } else {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
45 g_warning ("Stack overflow");
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
46 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
47 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
48
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
49 double pop (ex_stack *stack) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
50 g_assert (stack);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
51
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
52 if (stack->sp > 0) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
53 return stack->value[--stack->sp];
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
54 } else {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
55 g_warning ("Stack error (stack empty)");
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
56 return 0.0;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
57 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
58 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
59
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
60 /* */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
61
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
62 void expr_execute (expression_t *expr, symbol_dict_t *dict) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
63 char op, *str = expr->data->str;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
64 ex_stack stack = { 0, { 0.0 }};
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
65
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
66 while ((op = *str++)) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
67 switch (op) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
68 case 'l': /* Load a variable. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
69 push (&stack, dict->variables[load_int (str)].value);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
70 str += sizeof (int);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
71 break;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
72
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
73 case 's': /* Store to a variable. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
74 dict->variables[load_int (str)].value = pop (&stack);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
75 str += sizeof (int);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
76 break;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
77
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
78 case 'f': /* Call a function. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
79 function_call (load_int (str), &stack);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
80 str += sizeof (int);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
81 break;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
82
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
83 case 'c': /* Load a constant. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
84 push (&stack, load_double (str));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
85 str += sizeof (double);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
86 break;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
87
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
88 case 'n': /* Do a negation. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
89 push (&stack, -pop (&stack));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
90 break;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
91
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
92 case '+': /* Do an addition. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
93 push (&stack, pop (&stack) + pop (&stack));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
94 break;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
95 case '-': /* Do a subtraction. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
96 push (&stack, pop (&stack) - pop (&stack));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
97 break;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
98 case '*': /* Do a multiplication. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
99 push (&stack, pop (&stack) * pop (&stack));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
100 break;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
101 case '/': /* Do a division. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
102 if (check_stack (&stack, 2)) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
103 double y = stack.value[stack.sp - 2] / stack.value[stack.sp - 1];
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
104 stack.sp -= 2;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
105 push (&stack, y);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
106 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
107 break;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
108 case '^': /* Do an exponentiation. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
109 if (check_stack (&stack, 2)) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
110 double y = pow (stack.value[stack.sp - 2], stack.value[stack.sp - 1]);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
111 stack.sp -= 2;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
112 push (&stack, y);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
113 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
114 break;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
115
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
116 default:
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
117 g_warning ("Invalid opcode: %c", op);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
118 return;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
119 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
120 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
121 }