diff options
author | Alex Auvolat <alex@adnab.me> | 2017-01-26 17:43:38 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2017-01-26 17:43:38 +0100 |
commit | 96da27494f89d75f8aec0c8894440c3f7482f54b (patch) | |
tree | a239f75acf6c2c11cfa54b895653f978980064cb /src | |
parent | 809d660944cfb515e18bb7f1c1519392b4bccd53 (diff) | |
download | kogata-96da27494f89d75f8aec0c8894440c3f7482f54b.tar.gz kogata-96da27494f89d75f8aec0c8894440c3f7482f54b.zip |
More lua
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libc/stdio.c | 2 | ||||
-rw-r--r-- | src/lib/libc/stdlib.c | 5 | ||||
-rw-r--r-- | src/syslua/init.lua | 22 | ||||
-rw-r--r-- | src/syslua/test.lua | 25 |
4 files changed, 52 insertions, 2 deletions
diff --git a/src/lib/libc/stdio.c b/src/lib/libc/stdio.c index d90c30b..dcbcf83 100644 --- a/src/lib/libc/stdio.c +++ b/src/lib/libc/stdio.c @@ -238,7 +238,7 @@ int fgetc(FILE *stream) { } } char *fgets(char *s, int size, FILE *stream) { - dbg_printf("FGETS %p\n", stream); + dbg_printf("FGETS %p %d\n", stream, size); int l = 0; while (l < size - 1) { diff --git a/src/lib/libc/stdlib.c b/src/lib/libc/stdlib.c index bc543be..6b200c2 100644 --- a/src/lib/libc/stdlib.c +++ b/src/lib/libc/stdlib.c @@ -1,5 +1,6 @@ #include <stdlib.h> #include <ctype.h> +#include <string.h> #include <kogata/debug.h> @@ -75,6 +76,10 @@ double strtod(const char *nptr, char **endptr) { } char *getenv(const char *name) { + // HACK + if (!strcmp(name, "LUA_INIT")) { + return "require 'init'"; + } // TODO return 0; } diff --git a/src/syslua/init.lua b/src/syslua/init.lua new file mode 100644 index 0000000..1ee7ee6 --- /dev/null +++ b/src/syslua/init.lua @@ -0,0 +1,22 @@ +print "Lua helpers for Kogata v1" + +do + local old_tostring = tostring + function tostring(x) + if type(x) == "table" then + if next(x) == nil then + return '{}' + end + local q = '{\n ' + for k, v in pairs(x) do + if q:len() > 4 then + q = q .. ',\n ' + end + q = q .. k .. ': ' .. tostring(v):gsub('\n', '\n ') + end + return q .. '\n}' + else + return old_tostring(x) + end + end +end diff --git a/src/syslua/test.lua b/src/syslua/test.lua index 67b2852..7c9989c 100644 --- a/src/syslua/test.lua +++ b/src/syslua/test.lua @@ -1,7 +1,29 @@ print("Hello, world! (printed)") +local t = {} -return function(n) +function t.decomp(x) + local ret = {} + for i = 2, x do + while x % i == 0 do + table.insert(ret, i) + x = x // i + end + end + return ret +end + +function t.pretty(n) + for i = 1, n do + local d = t.decomp(i) + for _, v in pairs(d) do + io.stdout:write(('-'):rep(v) .. ' ') + end + io.stdout:write('\n') + end +end + +function t.guess(n) local a = math.random(n) while true do io.stdout:write("Guess? ") @@ -17,3 +39,4 @@ return function(n) end end +return t |