Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

object pointer is now found

  • 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  }
39
40protected:
41  double m_balance;
42};
43
44const char Account::className[] = "Account";
45const Luna<Account>::RegType Account::Register[] =
46  {
47    {"deposit",  &Account::deposit},
48    {"withdraw", &Account::withdraw},
49    {"balance",  &Account::balance},
50    {0}
51  };
52
53class Euclid
54{
55public:
56  static const char className[];
57  static const Luna<Euclid>::RegType Register[];
58
59  Euclid(lua_State* L)
60  {}
61
62  int gcd(lua_State* L)
63  {
64    int m = static_cast<int>(lua_tonumber(L, -1));
65    int n = static_cast<int>(lua_tonumber(L, -2));
66    lua_pop(L, 2);
67
68    /* precondition: m>0 and n>0.  Let g=gcd(m,n). */
69    while( m > 0 )
70    {
71      /* invariant: gcd(m,n)=g */
72      if( n > m )
73      {
74        int t = m; m = n; n = t; /* swap */
75      }
76      /* m >= n > 0 */
77      m -= n;
78    }
79
80    lua_pushnumber(L, n);
81    return 1;
82  }
83};
84
85const char Euclid::className[] = "Euclid";
86const Luna<Euclid>::RegType Euclid::Register[] =
87  {
88    {"gcd", &Euclid::gcd},
89    {0}
90  };
91
92int main(int argc, char* argv[])
93{
94  lua_State* L = lua_open();
95  lua_baselibopen(L);
96  Luna<Account>::Register(L);
97 // Luna<Euclid>::Register(L);
98
99  lua_dofile(L, argv[1]);
100
101
102  lua_close(L);
103  return 0;
104}
Note: See TracBrowser for help on using the repository browser.