aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds-kbdc/builtin-functions.c
blob: 91424d4e50f30fb0e80c3f8e0a6abe46ad3bdd60 (plain) (blame)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
 * mds — A micro-display server
 * Copyright © 2014, 2015, 2016  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "builtin-functions.h"

#include "variables.h"

#include <libmdsserver/macros.h>

#include <stdlib.h>
#include <string.h>



/**
 * Define useful data for built-in function with 2 parameters
 */
#define define_2				\
  const char32_t* restrict a = *args++;		\
  const char32_t* restrict b = *args++;		\
  size_t an = string_length(a);			\
  size_t bn = string_length(b);			\
  size_t i, n = an > bn ? an : bn;		\
  char32_t* restrict rc;			\
  fail_if (xmalloc(rc, n + 1, char32_t));	\
  rc[n] = -1

/**
 * Define useful data for built-in function with 1 parameter
 */
#define define_1				\
  const char32_t* restrict a = *args++;		\
  size_t i, n = string_length(a);		\
  char32_t* restrict rc;			\
  fail_if (xmalloc(rc, n + 1, char32_t));	\
  rc[n] = -1

/**
 * Return a value, or if there was a failure somewhere, return `NULL`
 * 
 * @param  v:void*  The value to return on success
 */
#define return(v)				\
  return v;					\
 fail:						\
  return NULL;


/**
 * Definition of the built-in function add/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_add_2(const char32_t** restrict args)
{
  define_2;
  for (i = 0; i < n; i++)
    rc[i] = a[i % an] + b[i % bn];
  return(rc);
}


/**
 * Definition of the built-in function sub/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_sub_2(const char32_t** restrict args)
{
  define_2;
  for (i = 0; i < n; i++)
    rc[i] = a[i % an] - b[i % bn];
  return(rc);
}


/**
 * Definition of the built-in function mul/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_mul_2(const char32_t** restrict args)
{
  define_2;
  for (i = 0; i < n; i++)
    rc[i] = a[i % an] * b[i % bn];
  return(rc);
}


/**
 * Definition of the built-in function div/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_div_2(const char32_t** restrict args)
{
  define_2;
  for (i = 0; i < n; i++)
    rc[i] = a[i % an] / b[i % bn];
  return(rc);
}


/**
 * Definition of the built-in function mod/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_mod_2(const char32_t** restrict args)
{
  define_2;
  for (i = 0; i < n; i++)
    rc[i] = a[i % an] % b[i % bn];
  return(rc);
}


/**
 * Definition of the built-in function rsh/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_rsh_2(const char32_t** restrict args)
{
  define_2;
  for (i = 0; i < n; i++)
    rc[i] = a[i % an] >> b[i % bn];
  return(rc);
}


/**
 * Definition of the built-in function lsh/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_lsh_2(const char32_t** restrict args)
{
  define_2;
  for (i = 0; i < n; i++)
    rc[i] = a[i % an] << b[i % bn];
  return(rc);
}


/**
 * Definition of the built-in function or/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_or_2(const char32_t** restrict args)
{
  define_2;
  for (i = 0; i < n; i++)
    rc[i] = a[i % an] | b[i % bn];
  return(rc);
}


/**
 * Definition of the built-in function and/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_and_2(const char32_t** restrict args)
{
  define_2;
  for (i = 0; i < n; i++)
    rc[i] = a[i % an] & b[i % bn];
  return(rc);
}


/**
 * Definition of the built-in function xor/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_xor_2(const char32_t** restrict args)
{
  define_2;
  for (i = 0; i < n; i++)
    rc[i] = a[i % an] ^ b[i % bn];
  return(rc);
}


/**
 * Definition of the built-in function not/1
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_not_1(const char32_t** restrict args)
{
  define_1;
  for (i = 0; i < n; i++)
    rc[i] = !a[i];
  return(rc);
}


/**
 * Definition of the built-in function equals/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_equals_2(const char32_t** restrict args)
{
  define_2;
  for (i = 0; i < n; i++)
    rc[i] = a[i % an] == b[i % bn];
  return(rc);
}


/**
 * Definition of the built-in function greater/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_greater_2(const char32_t** restrict args)
{
  define_2;
  for (i = 0; i < n; i++)
    rc[i] = a[i % an] > b[i % bn];
  return(rc);
}


/**
 * Definition of the built-in function less/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_less_2(const char32_t** restrict args)
{
  define_2;
  for (i = 0; i < n; i++)
    rc[i] = a[i % an] < b[i % bn];
  return(rc);
}


/**
 * Definition of the built-in function get/2
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_get_2(const char32_t** restrict args)
{
  const char32_t* restrict a = *args++;
  const char32_t* restrict b = *args++;
  char32_t* restrict rc;
  size_t n = (size_t)*b;
  mds_kbdc_tree_t* value = variables_get((size_t)*a)->array.elements;
  while (n--)
    value = value->next;
  fail_if (rc = string_dup(value->compiled_string.string), rc == NULL);
  return(rc);
}


/**
 * Definition of the built-in function set/3
 * 
 * @param   args  The arguments passed to the function
 * @return        The return value of the function, `NULL` on error
 */
static char32_t* builtin_function_set_3(const char32_t** restrict args)
{
  const char32_t* restrict a = *args++;
  const char32_t* restrict b = *args++;
  const char32_t* restrict c = *args++;
  char32_t* restrict rc;
  size_t n = (size_t)*b;
  mds_kbdc_tree_t* value = variables_get((size_t)*a)->array.elements;
  while (n--)
    value = value->next;
  free(value->compiled_string.string);
  value->compiled_string.string = string_dup(c);
  fail_if (value->compiled_string.string == NULL);
  fail_if (rc = string_dup(c), rc == NULL);
  return(rc);
}


#undef define_1
#undef define_2
#undef return


/**
 * Check whether a function is a builtin function
 * 
 * @param   name       The name of the function
 * @param   arg_count  The number of arguments to pass to the function
 * @return             Whether the described function is a builtin function
 */
int builtin_function_defined(const char* restrict name, size_t arg_count)
{
  size_t i;
  static const char* const BUILTIN_FUNCTIONS_2[] =
    {
      "add", "sub", "mul", "div", "mod", "rsh", "lsh", "or",
      "and", "xor", "equals", "greater", "less", "get", NULL
    };
  
  if (arg_count == 3)
    return !strcmp(name, "set");
  else if (arg_count == 1)
    return !strcmp(name, "not");
  else if (arg_count == 2)
    for (i = 0; BUILTIN_FUNCTIONS_2[i]; i++)
      if (!strcmp(name, BUILTIN_FUNCTIONS_2[i]))
	return 1;
  
  return 0;
}


/**
 * Invoke a builtin function
 * 
 * The function will abort if an non-builtin function is addressed
 * 
 * Before invoking set/3 or get/2, please check the arguments,
 * please also check that either all or none of the arguments
 * are empty string for any of the other builtin functions
 * 
 * @param   name       The name of the function
 * @param   arg_count  The number of arguments to pass to the function
 * @param   args       The arguments to pass
 * @return             The return value of the function, `NULL` on error
 */
char32_t* builtin_function_invoke(const char* restrict name, size_t arg_count, const char32_t** restrict args)
{
#define t(f)  do { fail_if (rc = builtin_function_##f(args), rc == NULL); return rc; } while (0)
  char32_t* rc;
  
  if ((arg_count == 3) && !strcmp(name, "set"))  t (set_3);
  if ((arg_count == 1) && !strcmp(name, "not"))  t (not_1);
  
  if (arg_count != 2)
    abort();
  
  if (!strcmp(name, "add"))      t (add_2);
  if (!strcmp(name, "sub"))      t (sub_2);
  if (!strcmp(name, "mul"))      t (mul_2);
  if (!strcmp(name, "div"))      t (div_2);
  if (!strcmp(name, "mod"))      t (mod_2);
  if (!strcmp(name, "rsh"))      t (rsh_2);
  if (!strcmp(name, "lsh"))      t (lsh_2);
  if (!strcmp(name, "or"))       t (or_2);
  if (!strcmp(name, "and"))      t (and_2);
  if (!strcmp(name, "xor"))      t (xor_2);
  if (!strcmp(name, "equals"))   t (equals_2);
  if (!strcmp(name, "greater"))  t (greater_2);
  if (!strcmp(name, "less"))     t (less_2);
  if (!strcmp(name, "get"))      t (get_2);
  
  abort();
 fail:
  return NULL;
#undef t
}