comparison src/paranormal/libcalc/function.c @ 282:3e160f6c04d2 trunk

[svn] - add libcalc from xvs and link to build
author nenolod
date Mon, 20 Nov 2006 13:26:09 -0800
parents
children a9f1bd76a3e6
comparison
equal deleted inserted replaced
281:65c4d8591b4a 282:3e160f6c04d2
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_sin (ex_stack *stack) {
37 return sin (pop (stack));
38 }
39
40 static double f_cos (ex_stack *stack) {
41 return cos (pop (stack));
42 }
43
44 static double f_if (ex_stack *stack) {
45 double a = pop (stack);
46 double b = pop (stack);
47 return (pop (stack) != 0.0) ? a : b;
48 }
49
50 static double f_div (ex_stack *stack) {
51 int y = (int)pop (stack);
52 int x = (int)pop (stack);
53 return (y == 0) ? 0 : (x / y);
54 }
55
56 /* */
57
58 static const func_t init[] = {
59 { "sin", f_sin },
60 { "cos", f_cos },
61 { "if", f_if },
62 { "div", f_div }
63 };
64
65 int function_lookup (const char *name) {
66 int i;
67
68 for (i = 0; i < sizeof (init) / sizeof (init[0]); i++)
69 if (strcmp (init[i].name, name) == 0)
70 return i;
71
72 g_warning ("Unknown function: %s\n", name);
73 return -1;
74 }
75
76 void function_call (int func_id, ex_stack *stack) {
77 g_assert (func_id >= 0);
78 g_assert (func_id < sizeof (init) / sizeof (init[0]));
79
80 push (stack, (*init[func_id].funcptr)(stack));
81 }