source: golgotha/src/test/lisp/functions.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 1.9 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "main/main.hh"
10#include "init/init.hh"
11#include "lisp/lisp.hh"
12#include "lisp/li_init.hh"
13
14li_object *add(li_object *o, li_environment *env)
15{
16  int int_sum=0, return_float=0;
17  float float_sum=0;
18
19  while (o)     // while there are still more parameters in the list
20  {
21    // li_car get's the data for this node in the parameter list
22    // i.e. o->data()
23    // li_eval evaluates the object if it's not already a number
24    li_object *item=li_eval(li_car(o, env), env);
25
26
27
28    if (item->type()==LI_FLOAT || return_float)   // adding floats or ints?   
29    {
30      return_float=1;
31      float_sum += li_get_float(item, env);
32    }
33    else
34      int_sum += li_get_int(item,env);
35     
36    o=li_cdr(o,env);      // go to the next parameter
37  }
38
39  if (return_float)
40    return new li_float(float_sum);
41  else
42    return new li_int(int_sum);
43}
44
45
46li_object *sub(li_object *o, li_environment *env)
47{
48  float x1=li_get_float(li_eval(li_first(o,env), env),env);
49  float x2=li_get_float(li_eval(li_second(o,env), env),env);
50
51  return new li_float(x1-x2);
52}
53
54
55// this method of adding functions is the automatic way.  sub will be added during i4_init
56// through c++ magic.
57li_automatic_add_function(sub, "sub");
58
59void i4_main(w32 argc, i4_const_str *argv)
60{
61  i4_init();
62 
63  // this is the manual way to add functions
64  li_add_function("add", add);
65
66  li_load("functions.scm");  // loads & evaluates from current directory
67
68  i4_uninit();
69}
70
Note: See TracBrowser for help on using the repository browser.