From db81b925d776103326128bf629cbdda576a223e7 Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 16 Apr 2022 11:55:09 -0500 Subject: move 3rd-party librarys into libs/ and add built-in honeysuckle --- libs/honeysuckle/src/hs_parse_args.c | 149 +++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 libs/honeysuckle/src/hs_parse_args.c (limited to 'libs/honeysuckle/src/hs_parse_args.c') diff --git a/libs/honeysuckle/src/hs_parse_args.c b/libs/honeysuckle/src/hs_parse_args.c new file mode 100644 index 0000000..d6d3b3c --- /dev/null +++ b/libs/honeysuckle/src/hs_parse_args.c @@ -0,0 +1,149 @@ +#include + +#include "honeysuckle.h" + +static bool check_parse(lua_State *L, int index, struct hs_arg *expected) +{ + switch(expected->type) { + case HS_BOOL: + if (!lua_isboolean(L, index)) + return false; + *(expected->ptr.boolean) = lua_toboolean(L, index); + return true; + + case HS_INT: + if (!lua_isnumber(L, index)) + return false; + *(expected->ptr.integer) = lua_tointeger(L, index); + return true; + + case HS_NUM: + if (!lua_isnumber(L, index)) + return false; + *(expected->ptr.number) = lua_tonumber(L, index); + return true; + + case HS_STR: + if (!lua_isstring(L, index) || lua_isnumber(L, index)) + return false; + *(expected->ptr.string) = (char *) lua_tostring(L, index); + return true; + + case HS_TBL: + if (!lua_istable(L, index)) + return false; + *(expected->ptr.stack_index) = index; + return true; + + case HS_FUNC: + if (!lua_isfunction(L, index)) + return false; + *(expected->ptr.stack_index) = index; + return true; + + case HS_CFUNC: + if (!lua_iscfunction(L, index)) + return false; + *(expected->ptr.function) = lua_tocfunction(L, index); + return true; + + case HS_USER: + if (!lua_isuserdata(L, index)) + return false; + *(expected->ptr.userdata) = lua_touserdata(L, index); + return true; + + case HS_LIGHT: + if (!lua_islightuserdata(L, index)) + return false; + *(expected->ptr.userdata) = lua_touserdata(L, index); + return true; + + case HS_NIL: + if (!lua_isnil(L, index)) + return false; + *(expected->ptr.stack_index) = index; + + case HS_ANY: + *(expected->ptr.stack_index) = index; + return true; + + default: + return false; + } +} + + +static bool try_parse_args(lua_State *L, int n_args, struct hs_arg *arguments) +{ + int args_provided = lua_gettop(L); + if (args_provided != n_args) + return false; + + for (int i=0; i