summaryrefslogtreecommitdiff
path: root/src/hs_parse_args.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/hs_parse_args.c')
-rw-r--r--src/hs_parse_args.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/hs_parse_args.c b/src/hs_parse_args.c
new file mode 100644
index 0000000..6fb8f31
--- /dev/null
+++ b/src/hs_parse_args.c
@@ -0,0 +1,111 @@
+#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))
+ 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<n_args; i++) {
+ bool success = check_parse(L, i+1, arguments + i);
+ if (!success)
+ return false;
+ }
+ return true;
+}
+
+
+void hs_parse_args_(lua_State *L, int n_args, struct hs_arg *arguments)
+{
+ bool success = try_parse_args(L, n_args, arguments);
+ if (!success) {
+ lua_pushstring(L, "expected arguments of the form (");
+ for (int i=0; i<n_args; i++) {
+ lua_pushstring(L, hs_type_to_string(arguments[i].type));
+ lua_pushstring(L, ", ");
+ }
+ lua_pop(L, 1);
+ lua_pushstring(L, "); received (");
+ const int n_provided = lua_gettop(L);
+ for (int i=0; i<n_provided; i++) {
+ lua_pushstring(L, lua_typename(L, lua_type(L, i+1)));
+ lua_pushstring(L, ", ");
+ }
+ lua_pop(L, 1);
+ lua_pushstring(") instead");
+ lua_concat(L, 1 + (2*n_args) + (2*n_provided));
+ lua_error(L);
+ }
+}