Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7820 was 7820, checked in by snellen, 18 years ago

moved old files

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