Mercurial > audlegacy-plugins
annotate src/paranormal/libcalc/function.c @ 1175:7ae024f5d91b trunk
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
| author | nenolod |
|---|---|
| date | Fri, 08 Jun 2007 10:56:12 -0700 |
| parents | a9f1bd76a3e6 |
| children | 44d28af95a23 |
| rev | line source |
|---|---|
| 282 | 1 /* function.c -- |
| 2 * | |
| 3 * Copyright (C) 2001 Janusz Gregorczyk <jgregor@kki.net.pl> | |
| 4 * | |
| 5 * This file is part of xvs. | |
| 6 * | |
| 7 * This program is free software; you can redistribute it and/or modify | |
| 8 * it under the terms of the GNU General Public License as published by | |
| 9 * the Free Software Foundation; either version 2 of the License, or | |
| 10 * (at your option) any later version. | |
| 11 * | |
| 12 * This program is distributed in the hope that it will be useful, | |
| 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 * GNU General Public License for more details. | |
| 16 * | |
| 17 * You should have received a copy of the GNU General Public License | |
| 18 * along with this program; if not, write to the Free Software | |
| 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 20 */ | |
| 21 | |
| 22 #include <glib.h> | |
| 23 #include <math.h> | |
| 24 #include <string.h> | |
| 25 | |
| 26 #include "function.h" | |
| 27 | |
| 28 /* Function pointer type. */ | |
| 29 typedef struct { | |
| 30 char *name; | |
| 31 double (*funcptr)(ex_stack *stack); | |
| 32 } func_t; | |
| 33 | |
| 34 /* */ | |
| 35 | |
|
334
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
36 static double f_log (ex_stack *stack) { |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
37 return log (pop (stack)); |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
38 } |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
39 |
| 282 | 40 static double f_sin (ex_stack *stack) { |
| 41 return sin (pop (stack)); | |
| 42 } | |
| 43 | |
| 44 static double f_cos (ex_stack *stack) { | |
| 45 return cos (pop (stack)); | |
| 46 } | |
| 47 | |
|
334
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
48 static double f_tan (ex_stack *stack) { |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
49 return tan (pop (stack)); |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
50 } |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
51 |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
52 static double f_asin (ex_stack *stack) { |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
53 return asin (pop (stack)); |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
54 } |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
55 |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
56 static double f_acos (ex_stack *stack) { |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
57 return acos (pop (stack)); |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
58 } |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
59 |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
60 static double f_atan (ex_stack *stack) { |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
61 return atan (pop (stack)); |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
62 } |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
63 |
| 282 | 64 static double f_if (ex_stack *stack) { |
| 65 double a = pop (stack); | |
| 66 double b = pop (stack); | |
| 67 return (pop (stack) != 0.0) ? a : b; | |
| 68 } | |
| 69 | |
| 70 static double f_div (ex_stack *stack) { | |
| 71 int y = (int)pop (stack); | |
| 72 int x = (int)pop (stack); | |
| 73 return (y == 0) ? 0 : (x / y); | |
| 74 } | |
| 75 | |
|
1175
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
334
diff
changeset
|
76 static double f_rand (ex_stack *stack) { |
|
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
334
diff
changeset
|
77 return rand() % pop (stack); |
|
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
334
diff
changeset
|
78 } |
|
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
334
diff
changeset
|
79 |
| 282 | 80 /* */ |
| 81 | |
| 82 static const func_t init[] = { | |
| 83 { "sin", f_sin }, | |
| 84 { "cos", f_cos }, | |
|
334
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
85 { "tan", f_tan }, |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
86 { "asin", f_asin }, |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
87 { "acos", f_acos }, |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
88 { "atan", f_atan }, |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
89 { "log", f_log }, |
| 282 | 90 { "if", f_if }, |
| 91 { "div", f_div } | |
|
1175
7ae024f5d91b
[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents:
334
diff
changeset
|
92 { "rand", f_rand } |
| 282 | 93 }; |
| 94 | |
| 95 int function_lookup (const char *name) { | |
| 96 int i; | |
| 97 | |
| 98 for (i = 0; i < sizeof (init) / sizeof (init[0]); i++) | |
| 99 if (strcmp (init[i].name, name) == 0) | |
| 100 return i; | |
| 101 | |
| 102 g_warning ("Unknown function: %s\n", name); | |
| 103 return -1; | |
| 104 } | |
| 105 | |
| 106 void function_call (int func_id, ex_stack *stack) { | |
| 107 g_assert (func_id >= 0); | |
| 108 g_assert (func_id < sizeof (init) / sizeof (init[0])); | |
| 109 | |
| 110 push (stack, (*init[func_id].funcptr)(stack)); | |
| 111 } |
