| [7702] | 1 | #include "luna.h" |
|---|
| 2 | /*extern "C" { |
|---|
| 3 | #include <lualib.h> |
|---|
| 4 | } |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | class Account |
|---|
| 8 | { |
|---|
| 9 | public: |
|---|
| 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 | { |
|---|
| [7815] | 24 | std::cout<<"deposit called"<<std::endl; |
|---|
| [7702] | 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 | } |
|---|
| [7808] | 40 | |
|---|
| [7702] | 41 | protected: |
|---|
| 42 | double m_balance; |
|---|
| 43 | }; |
|---|
| 44 | |
|---|
| 45 | const char Account::className[] = "Account"; |
|---|
| 46 | const Luna<Account>::RegType Account::Register[] = |
|---|
| 47 | { |
|---|
| 48 | {"deposit", &Account::deposit}, |
|---|
| 49 | {"withdraw", &Account::withdraw}, |
|---|
| 50 | {"balance", &Account::balance}, |
|---|
| 51 | {0} |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | class Euclid |
|---|
| 55 | { |
|---|
| 56 | public: |
|---|
| 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 | |
|---|
| 86 | const char Euclid::className[] = "Euclid"; |
|---|
| 87 | const Luna<Euclid>::RegType Euclid::Register[] = |
|---|
| 88 | { |
|---|
| 89 | {"gcd", &Euclid::gcd}, |
|---|
| 90 | {0} |
|---|
| 91 | }; |
|---|
| 92 | |
|---|
| 93 | int main(int argc, char* argv[]) |
|---|
| 94 | { |
|---|
| 95 | lua_State* L = lua_open(); |
|---|
| 96 | lua_baselibopen(L); |
|---|
| 97 | Luna<Account>::Register(L); |
|---|
| [7808] | 98 | // Luna<Euclid>::Register(L); |
|---|
| [7702] | 99 | |
|---|
| 100 | lua_dofile(L, argv[1]); |
|---|
| 101 | |
|---|
| [7808] | 102 | |
|---|
| [7702] | 103 | lua_close(L); |
|---|
| 104 | return 0; |
|---|
| 105 | } |
|---|