summaryrefslogtreecommitdiff
path: root/src/honeysuckle/hs_parse_args.c
blob: d6d3b3c7386241b2dc7b83bf246a1a6cc47f7a75 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#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);
   }
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

static void hs_overloaded_error(lua_State *L, va_list args)
{
   lua_pushstring(L, "hs_overloaded failed");
   lua_error(L);
}


int hs_parse_overloaded_(lua_State *L, ...)
{
   va_list args, args_error;
   va_start(args, L);
   va_copy(args_error, args);

   int choice = 0;

   while(true) {
      int n_args = va_arg(args, int);
      if (n_args == -1)
	 break;
      else {
	 struct hs_arg *arguments = va_arg(args, struct hs_arg *);
	 if (try_parse_args(L, n_args, arguments))
	    return choice;
      }
      choice++;
   }

   hs_overloaded_error(L, args_error);
   
   va_end(args);
   va_end(args_error);
}