-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlualexer.c
43 lines (33 loc) · 890 Bytes
/
lualexer.c
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
37
38
39
40
41
42
43
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include "lexer.h"
static int lua_tokenize(lua_State* L) {
const char* input = luaL_checkstring(L, 1);
token_t** tokens = tokenize(input);
lua_newtable(L);
for (int i = 0; tokens[i] != NULL; i++) {
lua_newtable(L);
lua_pushstring(L, "type");
lua_pushstring(L, token_to_str(tokens[i]->type));
lua_settable(L, -3);
lua_pushstring(L, "value");
lua_pushstring(L, tokens[i]->value);
lua_settable(L, -3);
lua_rawseti(L, -2, i + 1);
free(tokens[i]->value);
free(tokens[i]);
}
free(tokens);
return 1;
}
static const struct luaL_Reg lualexer[] = {
{ "tokenize", lua_tokenize },
{NULL, NULL}
};
int luaopen_lualexer(lua_State *L) {
luaL_newlib(L, lualexer);
return 1;
}