Mercurial > audlegacy-plugins
annotate src/paranormal/libcalc/function.c @ 334:a9f1bd76a3e6 trunk
[svn] - apply_xform(): check for NULL vfield (broken scripts)
- add tan(), asin(), acos(), atan(), log() to script engine.
| author | nenolod |
|---|---|
| date | Tue, 05 Dec 2006 03:40:04 -0800 |
| parents | 3e160f6c04d2 |
| children | 7ae024f5d91b |
| 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 | |
| 76 /* */ | |
| 77 | |
| 78 static const func_t init[] = { | |
| 79 { "sin", f_sin }, | |
| 80 { "cos", f_cos }, | |
|
334
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
81 { "tan", f_tan }, |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
82 { "asin", f_asin }, |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
83 { "acos", f_acos }, |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
84 { "atan", f_atan }, |
|
a9f1bd76a3e6
[svn] - apply_xform(): check for NULL vfield (broken scripts)
nenolod
parents:
282
diff
changeset
|
85 { "log", f_log }, |
| 282 | 86 { "if", f_if }, |
| 87 { "div", f_div } | |
| 88 }; | |
| 89 | |
| 90 int function_lookup (const char *name) { | |
| 91 int i; | |
| 92 | |
| 93 for (i = 0; i < sizeof (init) / sizeof (init[0]); i++) | |
| 94 if (strcmp (init[i].name, name) == 0) | |
| 95 return i; | |
| 96 | |
| 97 g_warning ("Unknown function: %s\n", name); | |
| 98 return -1; | |
| 99 } | |
| 100 | |
| 101 void function_call (int func_id, ex_stack *stack) { | |
| 102 g_assert (func_id >= 0); | |
| 103 g_assert (func_id < sizeof (init) / sizeof (init[0])); | |
| 104 | |
| 105 push (stack, (*init[func_id].funcptr)(stack)); | |
| 106 } |
