diff options
| author | ame <[email protected]> | 2023-12-18 13:55:13 -0600 |
|---|---|---|
| committer | ame <[email protected]> | 2023-12-18 13:55:13 -0600 |
| commit | 9a4d5574a89c4a7842211238d8b9b30b905cc056 (patch) | |
| tree | 17506097f415d4bddcdf3566d3ce0de08ab9a377 | |
| parent | 9607c2fff47e764efba9ec0e12291d9368ae9073 (diff) | |
config and io functions
| -rw-r--r-- | readme.md | 2 | ||||
| -rw-r--r-- | src/config.c | 28 | ||||
| -rw-r--r-- | src/config.h | 25 | ||||
| -rw-r--r-- | src/io.c | 55 | ||||
| -rw-r--r-- | src/io.h | 3 | ||||
| -rw-r--r-- | src/reg.c | 5 | ||||
| -rw-r--r-- | src/table.c | 18 | ||||
| -rw-r--r-- | src/table.h | 2 | ||||
| -rw-r--r-- | tt.lua | 9 |
9 files changed, 139 insertions, 8 deletions
@@ -1,5 +1,5 @@ -build with `clang -shared src/*.c src/*/*.c -o llib.so` +build with `clang -shared src/{*.c,*/*.c} -o llib.so -fPIC` useage and docs coming soon:3 diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..46beca7 --- /dev/null +++ b/src/config.c @@ -0,0 +1,28 @@ +#include "config.h" +#include <string.h> + +int _print_type = 0; +int _max_depth = 2; +int _start_nl_at = 3; + +int get_config_map(const char* key){ + for(int i = 0; config_map[i].key != NULL; i++){ + if(strcmp(config_map[i].key,key) == 0) return i; + } + return -1; +} + +int l_set(lua_State* L) { + luaL_checktype(L, 1, LUA_TTABLE); + get_config_map("print_type"); + lua_pushnil(L); + for(;lua_next(L,1) != 0;){ + int ind = get_config_map(lua_tostring(L,-2)); + + if(ind != -1) { + *config_map[ind].value = lua_tonumber(L,-1); + } + lua_pop(L,1); + } + return 0; +} diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..1d45cad --- /dev/null +++ b/src/config.h @@ -0,0 +1,25 @@ +#include "lua.h" + + +extern int _print_type; +extern int _max_depth; +extern int _start_nl_at; + +struct str_to_int { + const char* key; + int* value; +}; + +static struct str_to_int config_map[] = { + {"print_type", &_print_type}, + {"max_depth", &_max_depth}, + {"start_nl_at", &_start_nl_at}, + {NULL,NULL} +}; + +int l_set(lua_State*); + +static const luaL_Reg config_function_list [] = { + {"set",l_set}, + {NULL,NULL} +}; @@ -1,9 +1,9 @@ #include "io.h" #include "stdlib.h" #include "stdio.h" -#include "string.h" +#include "config.h" #include "stdint.h" -#include "unistd.h" +#include "table.h" int l_readfile(lua_State* L){ size_t len; @@ -75,3 +75,54 @@ int l_error(lua_State* L){ return 0; } +void print_indentation(int i){ + for(int z = 0; z < i; z++) printf(" "); +} + +void i_pprint(lua_State* L, int indent){ + int last_idx = lua_gettop(L); + const char* type = lua_typename(L,lua_type(L,-1)); + int t = lua_type(L,-1); + switch(lua_type(L,-1)){ + case LUA_TTABLE: + print_indentation(indent); + if(indent >= _max_depth && _max_depth >= 0) {printf("{"color_gray"..."color_reset"}"); break;} + int skip = i_len(L,last_idx) < _start_nl_at; + printf("{"); + if(!skip) printf("\n"); + lua_pushnil(L); + for(;lua_next(L,last_idx) != 0;){ + if(lua_type(L,-2) == LUA_TSTRING){ + print_indentation(indent); + printf(" %s"color_gray":"color_reset, lua_tostring(L,-2)); + } + i_pprint(L,indent+1); + printf(","); + if(!skip) printf("\n"); + + lua_pop(L,1); + } + + print_indentation(indent); + printf("}"); + break; + case LUA_TSTRING: + print_indentation(indent); + printf("\"%s\"", lua_tostring(L,-1)); + break; + default: + print_indentation(indent); + printf(color_yellow"%s"color_reset, lua_tostring(L,-1)); + } + + if(_print_type){ + if(lua_istable(L,last_idx)) printf(color_gray" : <%s:%lu>"color_reset,type,i_len(L,last_idx)); + else printf(color_gray" : <%s>"color_reset,type); + } + +} +int l_pprint(lua_State* L){ + i_pprint(L,0); + printf("\n"); + return 0; +} @@ -23,6 +23,7 @@ int l_debug(lua_State*); int l_log(lua_State*); int l_warn(lua_State*); int l_error(lua_State*); +int l_pprint(lua_State*); static const luaL_Reg io_function_list [] = { {"readfile",l_readfile}, @@ -30,6 +31,6 @@ static const luaL_Reg io_function_list [] = { {"log",l_log}, {"warn",l_warn}, {"error",l_error}, - + {"pprint",l_pprint}, {NULL,NULL} }; @@ -1,6 +1,7 @@ #include "lua.h" #include "table.h" #include "crypto.h" +#include "config.h" #include "io.h" #include "math.h" @@ -23,6 +24,10 @@ int luaopen_llib(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, math_function_list); lua_setfield(L, 2, "math"); + + lua_newtable(L); + luaL_register(L, NULL, config_function_list); + lua_setfield(L, 2, "config"); //make llib global lua_setglobal(L, "llib"); return 1; diff --git a/src/table.c b/src/table.c index 4e99cf1..6c3686a 100644 --- a/src/table.c +++ b/src/table.c @@ -1,10 +1,20 @@ #include "table.h" #include <stdlib.h> #include <string.h> - +#include <stdint.h> + +uint64_t i_len(lua_State* L, int pos){ + uint64_t i = 0; + lua_pushnil(L); + for(;lua_next(L,pos) != 0;){ + i += 1; + lua_pop(L,1); + } + return i; +} int l_len(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - lua_pushnumber(L,lua_objlen(L,1)); + luaL_checktype(L, 1, LUA_TTABLE); + lua_pushnumber(L,i_len(L,1)); return 1; } @@ -178,7 +188,7 @@ int l_split(lua_State* L){ size_t split_len = 0; char* input = (char*)luaL_checklstring(L, 1, &input_len); char* split = (char*)luaL_checklstring(L, 2, &split_len); - size_t table_len = 0; + size_t table_len = 1; lua_newtable(L); size_t current_len = 0; diff --git a/src/table.h b/src/table.h index bf27ffc..5b85e9a 100644 --- a/src/table.h +++ b/src/table.h @@ -2,8 +2,10 @@ #include "i_util.h" #include "i_common.h" #include "sort.h" +#include <stdint.h> void i_shuffle(double*, size_t); +uint64_t i_len(lua_State*,int); int l_len(lua_State*); //[double+int] -> i int l_reverse(lua_State*); //[double+int] -> arr[N] @@ -0,0 +1,9 @@ +require "llib" + +local s = llib.array.split("0 5 3 10"," ") +local b = {a=5} +llib.config.set({max_depth=-1}); +llib.io.pprint({5,{"what"},{55,55,33,2,5,5,5},{{55,5,8},9},3,{5,{meow=3}},{meow={5,5}}}) +--llib.io.pprint(llib.array.len(b)) + + |
