-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex28.1.c
More file actions
36 lines (30 loc) · 731 Bytes
/
ex28.1.c
File metadata and controls
36 lines (30 loc) · 731 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <string.h>
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
int main(int argc, char *argv[]) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
if (luaL_dofile(L, "ex28.1.lua")) {
fprintf(stderr, "Failed loading lua code!\n");
return 0;
}
for (int i = 0; i < 10; ++i) {
lua_getglobal(L, "f");
lua_pushnumber(L, i);
if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
fprintf(stderr, "error running function 'f': %s", lua_tostring(L, -1));
lua_pop(L, 1);
continue;
}
int j = lua_tointeger(L, -1);
lua_pop(L, 1);
for (int k = 0; k < j; ++k) {
fprintf(stdout, "*");
}
fprintf(stdout, "\n");
}
lua_close(L);
return 0;
}