Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
| 1 | #include <stdio.h> |
| 2 | |
| 3 | extern "C" { |
| 4 | #include "lua.h" |
| 5 | #include "lualib.h" |
| 6 | #include "lauxlib.h" |
| 7 | } |
| 8 | |
| 9 | /* the Lua interpreter */ |
| 10 | lua_State* L; |
| 11 | |
| 12 | static int average(lua_State *L) |
| 13 | { |
| 14 | /* get number of arguments */ |
| 15 | int n = lua_gettop(L); |
| 16 | int sum = 0; |
| 17 | int i; |
| 18 | |
| 19 | /* loop through each argument */ |
| 20 | for (i = 1; i <= n; i++) |
| 21 | { |
| 22 | /* total the arguments */ |
| 23 | sum += lua_tonumber(L, i); |
| 24 | } |
| 25 | |
| 26 | /* push the average */ |
| 27 | lua_pushnumber(L, sum / n); |
| 28 | |
| 29 | /* push the sum */ |
| 30 | lua_pushnumber(L, sum); |
| 31 | |
| 32 | /* return the number of results */ |
| 33 | return 2; |
| 34 | } |
| 35 | |
| 36 | int main ( int argc, char *argv[] ) |
| 37 | { |
| 38 | /* initialize Lua */ |
| 39 | L = lua_open(); |
| 40 | |
| 41 | /* load Lua base libraries */ |
| 42 | luaL_openlibs(L); |
| 43 | |
| 44 | /* register our function */ |
| 45 | lua_register(L, "average", average); |
| 46 | |
| 47 | /* run the script */ |
| 48 | luaL_dofile(L, "avg.lua"); |
| 49 | |
| 50 | /* cleanup Lua */ |
| 51 | lua_close(L); |
| 52 | |
| 53 | /* pause */ |
| 54 | printf( "Press enter to exit..." ); |
| 55 | getchar(); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 |
Branches:
master
