summaryrefslogtreecommitdiff
path: root/src/hs_parse_args.c
blob: 5a14bd04b74d4a91425f678dd086b986f7b176c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>

#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<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(L, ") instead");
      lua_concat(L, 1 + (2*n_args) + (2*n_provided));
      lua_error(L);
   }
}