comparison src/paranormal-ng/libcalc/function.c @ 2078:1fa3c8cd366a

paranormal-ng: a GL visualiser. lots to do, most stuff won't work, but hey, this will do cool stuff too soon
author William Pitcock <nenolod@atheme.org>
date Mon, 15 Oct 2007 06:20:13 -0500
parents
children f1b6f1b2cdb3
comparison
equal deleted inserted replaced
2077:e5b639ab62b0 2078:1fa3c8cd366a
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
36 static double f_log (ex_stack *stack) {
37 return log (pop (stack));
38 }
39
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
48 static double f_tan (ex_stack *stack) {
49 return tan (pop (stack));
50 }
51
52 static double f_asin (ex_stack *stack) {
53 return asin (pop (stack));
54 }
55
56 static double f_acos (ex_stack *stack) {
57 return acos (pop (stack));
58 }
59
60 static double f_atan (ex_stack *stack) {
61 return atan (pop (stack));
62 }
63
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 static double f_rand (ex_stack *stack) {
77 return g_random_double_range((double) pop(stack), (double) pop(stack));
78 }
79
80 /* */
81
82 static const func_t init[] = {
83 { "sin", f_sin },
84 { "cos", f_cos },
85 { "tan", f_tan },
86 { "asin", f_asin },
87 { "acos", f_acos },
88 { "atan", f_atan },
89 { "log", f_log },
90 { "if", f_if },
91 { "div", f_div },
92 { "rand", f_rand }
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 }