Fuck tables

This commit is contained in:
2024-08-06 11:01:44 -04:00
parent 1e9af84c8b
commit f675c0612d
2 changed files with 126 additions and 10 deletions

View File

@@ -1,4 +1,9 @@
#include <lua.hpp>
#include <map>
#include <variant>
#include <cstdint>
#include <format>
#include "jlog/jlog.hpp"
#pragma once
@@ -24,15 +29,113 @@ public:
void Pull(lua_State *l, int index) override { value = luaL_checknumber(l, index); };
};
//template<typename ... Args>
class LuaCFunction : public LuaValue<lua_CFunction> {
const char *name;
public:
LuaCFunction() : Base() {};
LuaCFunction(lua_CFunction lcf) : LuaValue<lua_CFunction> (lcf) {};
// This is for programmers not the lua engine
// The lua engine can fuck off
// Also you need to make sure to set it
const char* Name() { return name; };
void Name(const char* n) { this->name = n; };
void Push(lua_State *l) { lua_pushcfunction(l, value); lua_setglobal(l, name); };
// I don't know how to check if we're pulling a valid cfunction back in yet.
// I assume if you're using this you're either a dumb ass or have a damn good reason to.
void Pull(lua_State *l, int index) { value = lua_tocfunction(l, index); };
};
class LuaThread : public LuaValue<lua_State*>
{
public:
LuaThread() : Base() {};
LuaThread(lua_State* value) : Base(value) {};
void Push(lua_State *l) override { /* IDK */ };
void Pull(lua_State *l, int index) override { /* IDK */ };
};
class LuaBoolean : public LuaValue<bool>
{
public:
LuaBoolean() : Base() {};
LuaBoolean(bool value) : Base(value) {};
void Push(lua_State *l) override { lua_pushboolean(l, value); };
void Pull(lua_State *l, int index) override { luaL_checktype(l, index, LUA_TBOOLEAN); }
};
template <typename... Types>
class LuaReturnTuple : public std::tuple<Types...>
{
public:
typedef std::tuple<Types...> Base;
};
//using LuaAny = std::variant<LuaNil, LuaNumber, LuaInt, LuaBoolean, LuaTable, LuaThread>;
using LuaAny = std::variant<LuaNumber>;
/*
class LuaTable : public LuaValue<std::map<LuaAny, LuaAny>> {
public:
using MapBase = std::map<LuaAny, LuaAny>;
using ImplBase = LuaValue<MapBase>;
LuaTable() : ImplBase () {};
LuaTable(MapBase t) : ImplBase(t) {};
void Push(lua_State *l) {
for (auto const& [key, val] : this->value) {
DEBUG(std::format("K {} V {}", std::get<LuaValue>(key), val));
}
};
void Pull(lua_State *l, int index) {};
template <typename T = LuaAny, typename K>
T Get(K key) const;
};
*/
// I fucking hate this SHIT
class LuaTable : public LuaValue<std::map<LuaAny, LuaAny>> {
public:
using MapBase = std::map<LuaAny, LuaAny>;
using ImplBase = LuaValue<MapBase>;
LuaTable() : ImplBase () {};
LuaTable(MapBase t) : ImplBase(t) {};
void Push(lua_State *l) {
for (auto const& [key, val] : this->value) {
//DEBUG(std::format("K {} V {}", std::get<auto>(key), val));
std::visit([](auto&& arg){ DEBUG(std::format("K {}", arg.Value())); }, key);
std::visit([](auto&& arg){ DEBUG(std::format("V {}", arg.Value())); }, val);
}
};
void Pull(lua_State *l, int index) {};
template <typename T = LuaAny, typename K>
T Get(K key) const;
};
class LuaEngine {
public:
lua_State *L;
LuaEngine() { L = luaL_newstate(); luaL_openlibs(L); };
~LuaEngine() { lua_close(L); }; // Don't assume this is totally correct. The idea is upon deallocation we close the lua state.
template<typename Val>
void Push(Val lv) { lv.Push(L); };
template<typename Val>
Val Pull(int index) { Val v; v.Pull(L, index); return v; };
bool LoadString(const char *str) { return luaL_loadstring(L, str); };
int Pcall(int nargs, int nresults, int msgh) { return lua_pcall(L, nargs, nresults, msgh); };
const char *ToString(int index) { return lua_tostring(L, index); };
void Pop(int n) { return lua_pop(L, n); };
};

View File

@@ -18,6 +18,8 @@ static int l_sin (lua_State *L) {
return 1;
}
int main() {
char buff[256];
int error;
@@ -26,17 +28,28 @@ int main() {
LuaEngine LE = LuaEngine();
lua_pushcfunction(LE.L, l_sin);
lua_setglobal(LE.L, "mysin");
//lua_pushcfunction(LE.L, l_sin);
//lua_setglobal(LE.L, "mysin");
LuaCFunction lcf = LuaCFunction(&l_sin);
lcf.Name("mysin");
LE.Push(lcf);
while(fgets(buff, sizeof(buff), stdin) != NULL) {
error = LE.LoadString(buff) || lua_pcall(LE.L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s\n", lua_tostring(LE.L, -1));
lua_pop(LE.L, 1);
}
//std::map<LuaAny, LuaAny> m;
//m.emplace(LuaNumber(1), LuaNumber(20));
//if (LuaNumber(1) == LuaNumber(2)) {
// exit(1);
//}
//LuaTable lt = m;
//LE.Push(lt);
while(fgets(buff, sizeof(buff), stdin) != NULL) {
error = LE.LoadString(buff) || LE.Pcall(0,0,0);
if (error) {
fprintf(stderr, "%s\n", LE.ToString(-1));
LE.Pop(1);
}
}
lua_close(LE.L);
//lua_close(LE.L);
return 0;
}