aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config.c28
-rw-r--r--src/config.h25
-rw-r--r--src/io.c55
-rw-r--r--src/io.h3
-rw-r--r--src/reg.c5
-rw-r--r--src/table.c18
-rw-r--r--src/table.h2
7 files changed, 129 insertions, 7 deletions
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}
+};
diff --git a/src/io.c b/src/io.c
index 6d07a44..9b84804 100644
--- a/src/io.c
+++ b/src/io.c
@@ -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;
+}
diff --git a/src/io.h b/src/io.h
index 7cbca95..d32d313 100644
--- a/src/io.h
+++ b/src/io.h
@@ -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}
};
diff --git a/src/reg.c b/src/reg.c
index e443cae..10c9e33 100644
--- a/src/reg.c
+++ b/src/reg.c
@@ -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]