Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/lib/script_engine/account.cc @ 7702

Last change on this file since 7702 was 7702, checked in by snellen, 19 years ago

new concept

  • Property svn:executable set to *
File size: 1.8 KB
Line 
1#include "luna.h"
2/*extern "C" {
3#include <lualib.h>
4}
5*/
6
7class Account
8{
9public:
10  static const char className[];
11  static const Luna<Account>::RegType Register[];
12
13  Account(lua_State* L)
14  {
15    /* constructor table at top of stack */
16    lua_pushstring(L, "balance");
17    lua_gettable(L, -2);
18    m_balance = lua_tonumber(L, -1);
19    lua_pop(L, 2); /* pop constructor table and balance */
20  }
21
22  int deposit(lua_State* L)
23  {
24    m_balance += lua_tonumber(L, -1);
25    lua_pop(L, 1);
26    return 0;
27  }
28  int withdraw(lua_State* L)
29  {
30    m_balance -= lua_tonumber(L, -1);
31    lua_pop(L, 1);
32    return 0;
33  }
34  int balance(lua_State* L)
35  {
36    lua_pushnumber(L, m_balance);
37    return 1;
38  }
39protected:
40  double m_balance;
41};
42
43const char Account::className[] = "Account";
44const Luna<Account>::RegType Account::Register[] =
45  {
46    {"deposit",  &Account::deposit},
47    {"withdraw", &Account::withdraw},
48    {"balance",  &Account::balance},
49    {0}
50  };
51
52class Euclid
53{
54public:
55  static const char className[];
56  static const Luna<Euclid>::RegType Register[];
57
58  Euclid(lua_State* L)
59  {}
60
61  int gcd(lua_State* L)
62  {
63    int m = static_cast<int>(lua_tonumber(L, -1));
64    int n = static_cast<int>(lua_tonumber(L, -2));
65    lua_pop(L, 2);
66
67    /* precondition: m>0 and n>0.  Let g=gcd(m,n). */
68    while( m > 0 )
69    {
70      /* invariant: gcd(m,n)=g */
71      if( n > m )
72      {
73        int t = m; m = n; n = t; /* swap */
74      }
75      /* m >= n > 0 */
76      m -= n;
77    }
78
79    lua_pushnumber(L, n);
80    return 1;
81  }
82};
83
84const char Euclid::className[] = "Euclid";
85const Luna<Euclid>::RegType Euclid::Register[] =
86  {
87    {"gcd", &Euclid::gcd},
88    {0}
89  };
90
91int main(int argc, char* argv[])
92{
93  lua_State* L = lua_open();
94  lua_baselibopen(L);
95
96  Luna<Account>::Register(L);
97  Luna<Euclid>::Register(L);
98
99  lua_dofile(L, argv[1]);
100
101  lua_close(L);
102  return 0;
103}
Note: See TracBrowser for help on using the repository browser.