2017-05-06 14:12:46 +02:00
|
|
|
/*
|
|
|
|
* ell.c: an experimental little language
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017, Přemysl Janouch <p.janouch@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-05-06 13:27:02 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
2017-05-18 14:03:47 +02:00
|
|
|
#include <setjmp.h>
|
2017-05-06 13:27:02 +02:00
|
|
|
|
|
|
|
#if defined __GNUC__
|
|
|
|
#define ATTRIBUTE_PRINTF(x, y) __attribute__ ((format (printf, x, y)))
|
|
|
|
#else // ! __GNUC__
|
|
|
|
#define ATTRIBUTE_PRINTF(x, y)
|
|
|
|
#endif // ! __GNUC__
|
|
|
|
|
|
|
|
#define N_ELEMENTS(a) (sizeof (a) / sizeof ((a)[0]))
|
|
|
|
|
|
|
|
// --- Utilities ---------------------------------------------------------------
|
|
|
|
|
|
|
|
static char *format (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
|
|
|
|
|
|
|
|
static char *
|
2017-05-17 21:29:07 +02:00
|
|
|
vformat (const char *format, va_list ap) {
|
2017-05-06 13:27:02 +02:00
|
|
|
va_list aq;
|
|
|
|
va_copy (aq, ap);
|
|
|
|
int size = vsnprintf (NULL, 0, format, aq);
|
|
|
|
va_end (aq);
|
|
|
|
if (size < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2017-05-20 21:03:04 +02:00
|
|
|
char *buf = malloc (size + 1);
|
|
|
|
if (buf && vsnprintf (buf, size + 1, format, ap) < 0) {
|
|
|
|
free (buf);
|
2017-05-06 13:27:02 +02:00
|
|
|
return NULL;
|
2017-05-20 21:03:04 +02:00
|
|
|
}
|
|
|
|
return buf;
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
format (const char *format, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, format);
|
2017-05-17 21:29:07 +02:00
|
|
|
char *result = vformat (format, ap);
|
2017-05-06 13:27:02 +02:00
|
|
|
va_end (ap);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Generic buffer ----------------------------------------------------------
|
|
|
|
|
|
|
|
struct buffer {
|
|
|
|
char *s; ///< Buffer data
|
2017-05-24 19:32:18 +02:00
|
|
|
size_t alloc, len; ///< Number of bytes allocated and used
|
2017-05-06 13:27:02 +02:00
|
|
|
bool memory_failure; ///< Memory allocation failed
|
|
|
|
};
|
|
|
|
|
|
|
|
#define BUFFER_INITIALIZER { NULL, 0, 0, false }
|
|
|
|
|
|
|
|
static bool
|
|
|
|
buffer_append (struct buffer *self, const void *s, size_t n) {
|
|
|
|
if (self->memory_failure)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!self->s)
|
|
|
|
self->s = malloc (self->alloc = 8);
|
|
|
|
while (self->len + n > self->alloc)
|
|
|
|
self->s = realloc (self->s, self->alloc <<= 1);
|
|
|
|
|
|
|
|
if (!self->s) {
|
|
|
|
self->memory_failure = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy (self->s + self->len, s, n);
|
|
|
|
self->len += n;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static bool
|
|
|
|
buffer_append_c (struct buffer *self, char c) {
|
|
|
|
return buffer_append (self, &c, 1);
|
|
|
|
}
|
|
|
|
|
2017-05-24 19:32:18 +02:00
|
|
|
// --- Data items --------------------------------------------------------------
|
2017-05-06 13:27:02 +02:00
|
|
|
|
|
|
|
enum item_type { ITEM_STRING, ITEM_LIST };
|
|
|
|
|
|
|
|
struct item {
|
|
|
|
enum item_type type; ///< The type of this object
|
|
|
|
struct item *next; ///< Next item on the list/stack
|
|
|
|
|
|
|
|
struct item *head; ///< The head of the list
|
2017-05-24 19:32:18 +02:00
|
|
|
size_t len; ///< Length of "value" (sans '\0')
|
2017-05-06 13:27:02 +02:00
|
|
|
char value[]; ///< The null-terminated string value
|
|
|
|
};
|
|
|
|
|
|
|
|
static void item_free_list (struct item *);
|
|
|
|
static struct item *new_clone_list (const struct item *);
|
|
|
|
|
|
|
|
static void
|
|
|
|
item_free (struct item *item) {
|
2017-05-24 19:32:18 +02:00
|
|
|
item_free_list (item->head);
|
2017-05-06 13:27:02 +02:00
|
|
|
free (item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
item_free_list (struct item *item) {
|
|
|
|
while (item) {
|
|
|
|
struct item *link = item;
|
|
|
|
item = item->next;
|
|
|
|
item_free (link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct item *
|
|
|
|
new_clone (const struct item *item) {
|
2017-05-24 19:32:18 +02:00
|
|
|
size_t size = sizeof *item + item->len + 1;
|
2017-05-06 13:27:02 +02:00
|
|
|
struct item *clone = malloc (size);
|
|
|
|
if (!clone)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memcpy (clone, item, size);
|
2017-05-24 19:32:18 +02:00
|
|
|
if (clone->head && !(clone->head = new_clone_list (clone->head))) {
|
|
|
|
free (clone);
|
|
|
|
return NULL;
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
clone->next = NULL;
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct item *
|
|
|
|
new_clone_list (const struct item *item) {
|
2017-05-19 21:56:10 +02:00
|
|
|
struct item *head = NULL;
|
2017-05-06 13:27:02 +02:00
|
|
|
for (struct item **out = &head; item; item = item->next) {
|
2017-05-19 21:56:10 +02:00
|
|
|
if (!(*out = new_clone (item))) {
|
2017-05-06 13:27:02 +02:00
|
|
|
item_free_list (head);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-05-19 21:56:10 +02:00
|
|
|
out = &(*out)->next;
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct item *
|
2017-05-20 21:03:04 +02:00
|
|
|
new_string (const char *s, size_t len) {
|
2017-05-06 13:27:02 +02:00
|
|
|
struct item *item = calloc (1, sizeof *item + len + 1);
|
|
|
|
if (!item)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
item->type = ITEM_STRING;
|
|
|
|
item->len = len;
|
|
|
|
memcpy (item->value, s, len);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct item *
|
|
|
|
new_list (struct item *head) {
|
2017-05-18 19:45:56 +02:00
|
|
|
struct item *item = calloc (1, sizeof *item + 1);
|
2017-05-19 19:22:44 +02:00
|
|
|
if (!item) {
|
|
|
|
item_free_list (head);
|
2017-05-06 13:27:02 +02:00
|
|
|
return NULL;
|
2017-05-19 19:22:44 +02:00
|
|
|
}
|
2017-05-06 13:27:02 +02:00
|
|
|
|
|
|
|
item->type = ITEM_LIST;
|
|
|
|
item->head = head;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Lexer -------------------------------------------------------------------
|
|
|
|
|
2017-05-19 09:19:35 +02:00
|
|
|
enum token { T_ABORT, T_LPAREN, T_RPAREN, T_LBRACKET, T_RBRACKET,
|
|
|
|
T_LBRACE, T_RBRACE, T_STRING, T_NEWLINE, T_AT };
|
2017-05-06 13:27:02 +02:00
|
|
|
|
2017-05-19 21:56:10 +02:00
|
|
|
static const char *token_names[] = {
|
|
|
|
[T_ABORT] = "end of input",
|
|
|
|
[T_LPAREN] = "left parenthesis",
|
|
|
|
[T_RPAREN] = "right parenthesis",
|
|
|
|
[T_LBRACKET] = "left bracket",
|
|
|
|
[T_RBRACKET] = "right bracket",
|
|
|
|
[T_LBRACE] = "left brace",
|
|
|
|
[T_RBRACE] = "right brace",
|
|
|
|
[T_STRING] = "string",
|
|
|
|
[T_NEWLINE] = "newline",
|
|
|
|
[T_AT] = "at symbol",
|
|
|
|
};
|
2017-05-06 13:27:02 +02:00
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
struct lexer {
|
2017-05-24 20:47:55 +02:00
|
|
|
const unsigned char *p; ///< Current position in input
|
2017-05-06 13:27:02 +02:00
|
|
|
size_t len; ///< How many bytes of input are left
|
2017-05-17 23:34:58 +02:00
|
|
|
unsigned line, column; ///< Current line and column
|
2017-05-17 21:29:07 +02:00
|
|
|
struct buffer string; ///< Parsed string value
|
2017-05-06 13:27:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
lexer_init (struct lexer *self, const char *p, size_t len) {
|
2017-05-24 20:47:55 +02:00
|
|
|
*self = (struct lexer) { .p = (const unsigned char *) p, .len = len };
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lexer_free (struct lexer *self) {
|
2017-05-17 21:29:07 +02:00
|
|
|
free (self->string.s);
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
lexer_advance (struct lexer *self) {
|
|
|
|
int c = *self->p++;
|
|
|
|
if (c == '\n') {
|
|
|
|
self->column = 0;
|
|
|
|
self->line++;
|
|
|
|
} else
|
|
|
|
self->column++;
|
|
|
|
|
|
|
|
self->len--;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-05-17 21:29:07 +02:00
|
|
|
lexer_hexa_escape (struct lexer *self, struct buffer *output) {
|
2017-05-24 20:03:17 +02:00
|
|
|
const char *alphabet = "0123456789abcdef", *h, *l;
|
|
|
|
if (!self->len || !(h = strchr (alphabet, tolower (lexer_advance (self))))
|
|
|
|
|| !self->len || !(l = strchr (alphabet, tolower (lexer_advance (self)))))
|
2017-05-06 13:27:02 +02:00
|
|
|
return false;
|
|
|
|
|
2017-05-24 20:03:17 +02:00
|
|
|
buffer_append_c (output, (h - alphabet) << 4 | (l - alphabet));
|
2017-05-06 13:27:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-24 20:03:17 +02:00
|
|
|
enum { LEXER_STRING_QUOTE = '\'', LEXER_ESCAPE = '\\', LEXER_COMMENT = '#' };
|
|
|
|
static bool lexer_is_whitespace (int c) { return !c || c == ' ' || c == '\t'; }
|
|
|
|
|
|
|
|
static unsigned char lexer_escapes[256] = {
|
|
|
|
[LEXER_STRING_QUOTE] = LEXER_STRING_QUOTE, [LEXER_ESCAPE] = LEXER_ESCAPE,
|
|
|
|
['a'] = '\a', ['b'] = '\b', ['n'] = '\n', ['r'] = '\r', ['t'] = '\t',
|
|
|
|
};
|
|
|
|
|
2017-05-19 09:48:53 +02:00
|
|
|
static const char *
|
|
|
|
lexer_escape_sequence (struct lexer *self, struct buffer *output) {
|
2017-05-18 20:45:46 +02:00
|
|
|
if (!self->len)
|
2017-05-19 09:48:53 +02:00
|
|
|
return "premature end of escape sequence";
|
2017-05-06 13:27:02 +02:00
|
|
|
|
2017-05-24 20:47:55 +02:00
|
|
|
int c = lexer_advance (self);
|
2017-05-24 20:03:17 +02:00
|
|
|
if (c == 'x') {
|
2017-05-06 13:27:02 +02:00
|
|
|
if (lexer_hexa_escape (self, output))
|
2017-05-19 09:48:53 +02:00
|
|
|
return NULL;
|
|
|
|
return "invalid hexadecimal escape";
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
2017-05-24 20:03:17 +02:00
|
|
|
if (!(c = lexer_escapes[c]))
|
|
|
|
return "unknown escape sequence";
|
2017-05-06 13:27:02 +02:00
|
|
|
|
2017-05-17 21:29:07 +02:00
|
|
|
buffer_append_c (output, c);
|
2017-05-19 09:48:53 +02:00
|
|
|
return NULL;
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 09:48:53 +02:00
|
|
|
static const char *
|
|
|
|
lexer_string (struct lexer *self, struct buffer *output) {
|
2017-05-24 20:47:55 +02:00
|
|
|
int c;
|
2017-05-19 09:48:53 +02:00
|
|
|
const char *e = NULL;
|
2017-05-06 13:27:02 +02:00
|
|
|
while (self->len) {
|
2017-05-24 20:03:17 +02:00
|
|
|
if ((c = lexer_advance (self)) == LEXER_STRING_QUOTE)
|
2017-05-19 09:48:53 +02:00
|
|
|
return NULL;
|
2017-05-24 20:03:17 +02:00
|
|
|
if (c != LEXER_ESCAPE)
|
2017-05-17 21:29:07 +02:00
|
|
|
buffer_append_c (output, c);
|
2017-05-19 09:48:53 +02:00
|
|
|
else if ((e = lexer_escape_sequence (self, output)))
|
|
|
|
return e;
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
2017-05-19 09:48:53 +02:00
|
|
|
return "premature end of string";
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 20:03:17 +02:00
|
|
|
static enum token lexer_tokens[256] = {
|
|
|
|
['('] = T_LPAREN, [')'] = T_RPAREN, ['['] = T_LBRACKET, [']'] = T_RBRACKET,
|
|
|
|
['{'] = T_LBRACE, ['}'] = T_RBRACE, [';'] = T_NEWLINE, ['\n'] = T_NEWLINE,
|
|
|
|
['@'] = T_AT, [LEXER_STRING_QUOTE] = T_STRING,
|
|
|
|
};
|
|
|
|
|
2017-05-06 13:27:02 +02:00
|
|
|
static enum token
|
2017-05-19 09:39:57 +02:00
|
|
|
lexer_next (struct lexer *self, const char **e) {
|
2017-05-24 20:03:17 +02:00
|
|
|
while (self->len && lexer_is_whitespace (*self->p))
|
2017-05-06 13:27:02 +02:00
|
|
|
lexer_advance (self);
|
|
|
|
if (!self->len)
|
|
|
|
return T_ABORT;
|
|
|
|
|
2017-05-17 21:29:07 +02:00
|
|
|
free (self->string.s);
|
|
|
|
self->string = (struct buffer) BUFFER_INITIALIZER;
|
|
|
|
|
2017-05-24 20:47:55 +02:00
|
|
|
int c = lexer_advance (self);
|
2017-05-24 20:03:17 +02:00
|
|
|
if (c == LEXER_COMMENT) {
|
2017-05-06 13:27:02 +02:00
|
|
|
while (self->len)
|
|
|
|
if (lexer_advance (self) == '\n')
|
|
|
|
return T_NEWLINE;
|
|
|
|
return T_ABORT;
|
2017-05-24 20:03:17 +02:00
|
|
|
}
|
2017-05-06 13:27:02 +02:00
|
|
|
|
2017-05-24 20:03:17 +02:00
|
|
|
enum token token = lexer_tokens[c];
|
|
|
|
if (!token) {
|
|
|
|
buffer_append_c (&self->string, c);
|
|
|
|
while (self->len && !lexer_is_whitespace (*self->p)
|
2017-05-24 20:47:55 +02:00
|
|
|
&& !lexer_tokens[*self->p])
|
2017-05-24 20:03:17 +02:00
|
|
|
buffer_append_c (&self->string, lexer_advance (self));
|
2017-05-06 13:27:02 +02:00
|
|
|
return T_STRING;
|
|
|
|
}
|
2017-05-24 20:03:17 +02:00
|
|
|
if (token == T_STRING
|
|
|
|
&& (*e = lexer_string (self, &self->string)))
|
|
|
|
return T_ABORT;
|
|
|
|
return token;
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 09:39:57 +02:00
|
|
|
static char *lexer_errorf (struct lexer *self, const char *fmt, ...)
|
|
|
|
ATTRIBUTE_PRINTF (2, 3);
|
|
|
|
|
|
|
|
static char *
|
|
|
|
lexer_errorf (struct lexer *self, const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
char *description = vformat (fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
if (!description)
|
|
|
|
return NULL;
|
|
|
|
|
2017-05-24 20:03:17 +02:00
|
|
|
char *e = format ("at or before line %u, column %u: %s",
|
2017-05-19 09:39:57 +02:00
|
|
|
self->line + 1, self->column + 1, description);
|
|
|
|
free (description);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-05-06 13:27:02 +02:00
|
|
|
// --- Parsing -----------------------------------------------------------------
|
|
|
|
|
2017-05-18 14:29:53 +02:00
|
|
|
static void
|
|
|
|
print_string (const char *s) {
|
|
|
|
putc ('\'', stdout);
|
|
|
|
for (; *s; s++)
|
|
|
|
if (*s == '\n') printf ("\\n");
|
|
|
|
else if (*s == '\\') putc ('\\', stdout);
|
|
|
|
else putc (*s, stdout);
|
|
|
|
putc ('\'', stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_tree (struct item *tree, int level) {
|
|
|
|
// TODO: also re-add syntax sugar
|
|
|
|
for (struct item *iter = tree; iter; iter = iter->next) {
|
|
|
|
if (iter != tree)
|
|
|
|
printf ("%*s", level, "");
|
|
|
|
if (iter->type == ITEM_STRING) {
|
|
|
|
print_string (iter->value);
|
2017-05-21 17:11:16 +02:00
|
|
|
} else if (iter->head
|
|
|
|
&& iter->head->type == ITEM_STRING
|
2017-05-18 14:29:53 +02:00
|
|
|
&& !strcmp (iter->head->value, "list")) {
|
|
|
|
printf ("[");
|
|
|
|
print_tree (iter->head->next, level + 1);
|
|
|
|
printf ("]");
|
|
|
|
} else {
|
|
|
|
printf ("(");
|
|
|
|
print_tree (iter->head, level + 1);
|
|
|
|
printf (")");
|
|
|
|
}
|
|
|
|
if (iter->next)
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2017-05-19 21:56:10 +02:00
|
|
|
struct parser {
|
2017-05-18 14:03:47 +02:00
|
|
|
struct lexer lexer; ///< Tokenizer
|
|
|
|
char *error; ///< Tokenizer error
|
|
|
|
enum token token; ///< Current token in the lexer
|
|
|
|
bool replace_token; ///< Replace the token
|
2017-05-19 19:22:44 +02:00
|
|
|
bool memory_failure; ///< Memory allocation failed
|
2017-05-18 14:03:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
parser_init (struct parser *self, const char *script, size_t len) {
|
|
|
|
memset (self, 0, sizeof *self);
|
|
|
|
lexer_init (&self->lexer, script, len);
|
|
|
|
|
|
|
|
// As reading in tokens may cause exceptions, we wait for the first peek()
|
|
|
|
// to replace the initial T_ABORT.
|
|
|
|
self->replace_token = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parser_free (struct parser *self) {
|
|
|
|
lexer_free (&self->lexer);
|
|
|
|
if (self->error)
|
|
|
|
free (self->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum token
|
|
|
|
parser_peek (struct parser *self, jmp_buf out) {
|
2017-05-19 09:19:35 +02:00
|
|
|
if (self->replace_token) {
|
2017-05-19 09:39:57 +02:00
|
|
|
const char *e = NULL;
|
|
|
|
self->token = lexer_next (&self->lexer, &e);
|
|
|
|
if (e) {
|
2017-05-19 19:22:44 +02:00
|
|
|
self->memory_failure =
|
|
|
|
!(self->error = lexer_errorf (&self->lexer, "%s", e));
|
2017-05-18 14:03:47 +02:00
|
|
|
longjmp (out, 1);
|
2017-05-19 09:39:57 +02:00
|
|
|
}
|
2017-05-20 15:38:06 +02:00
|
|
|
if (self->token == T_STRING && self->lexer.string.memory_failure)
|
2017-05-19 19:22:44 +02:00
|
|
|
longjmp (out, 1);
|
2017-05-18 14:03:47 +02:00
|
|
|
self->replace_token = false;
|
|
|
|
}
|
|
|
|
return self->token;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
parser_accept (struct parser *self, enum token token, jmp_buf out) {
|
|
|
|
return self->replace_token = (parser_peek (self, out) == token);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parser_expect (struct parser *self, enum token token, jmp_buf out) {
|
|
|
|
if (parser_accept (self, token, out))
|
|
|
|
return;
|
|
|
|
|
2017-05-19 19:22:44 +02:00
|
|
|
self->memory_failure = !(self->error = lexer_errorf (&self->lexer,
|
|
|
|
"unexpected `%s', expected `%s'",
|
2017-05-19 21:56:10 +02:00
|
|
|
token_names[self->token], token_names[token]));
|
2017-05-18 14:03:47 +02:00
|
|
|
longjmp (out, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
// We don't need no generator, but a few macros will come in handy.
|
|
|
|
// From time to time C just doesn't have the right features.
|
|
|
|
|
|
|
|
#define PEEK() parser_peek (self, err)
|
|
|
|
#define ACCEPT(token) parser_accept (self, token, err)
|
|
|
|
#define EXPECT(token) parser_expect (self, token, err)
|
|
|
|
#define SKIP_NL() do {} while (ACCEPT (T_NEWLINE))
|
|
|
|
|
2017-05-19 19:22:44 +02:00
|
|
|
static struct item *
|
|
|
|
parser_check (struct parser *self, struct item *item, jmp_buf out) {
|
|
|
|
if (!item) {
|
|
|
|
self->memory_failure = true;
|
|
|
|
longjmp (out, 1);
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
2017-05-18 14:03:47 +02:00
|
|
|
|
2017-05-19 19:22:44 +02:00
|
|
|
// Beware that this jumps to the "out" buffer directly
|
|
|
|
#define CHECK(item) parser_check (self, (item), out)
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2017-05-18 14:03:47 +02:00
|
|
|
|
2017-05-06 13:27:02 +02:00
|
|
|
static struct item *
|
2017-05-18 14:03:47 +02:00
|
|
|
parse_prefix_list (struct item *list, const char *name) {
|
2017-05-19 19:22:44 +02:00
|
|
|
struct item *prefix;
|
|
|
|
if (!(prefix = new_string (name, strlen (name)))) {
|
|
|
|
item_free_list (list);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-05-18 14:03:47 +02:00
|
|
|
prefix->next = list;
|
|
|
|
return new_list (prefix);
|
|
|
|
}
|
2017-05-17 23:34:58 +02:00
|
|
|
|
2017-05-19 19:22:44 +02:00
|
|
|
static struct item * parse_line (struct parser *self, jmp_buf out);
|
|
|
|
|
2017-05-18 14:03:47 +02:00
|
|
|
static struct item *
|
|
|
|
parse_item (struct parser *self, jmp_buf out) {
|
|
|
|
jmp_buf err;
|
2017-05-19 19:22:44 +02:00
|
|
|
struct item *volatile result = NULL, *volatile *tail = &result;
|
2017-05-18 14:03:47 +02:00
|
|
|
if (setjmp (err)) {
|
|
|
|
item_free_list (result);
|
|
|
|
longjmp (out, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
SKIP_NL ();
|
|
|
|
if (ACCEPT (T_STRING))
|
2017-05-19 19:22:44 +02:00
|
|
|
return CHECK (new_string
|
|
|
|
(self->lexer.string.s, self->lexer.string.len));
|
2017-05-18 14:03:47 +02:00
|
|
|
if (ACCEPT (T_AT)) {
|
|
|
|
result = parse_item (self, out);
|
2017-05-19 19:22:44 +02:00
|
|
|
return CHECK (parse_prefix_list (result, "set"));
|
2017-05-18 14:03:47 +02:00
|
|
|
}
|
|
|
|
if (ACCEPT (T_LPAREN)) {
|
|
|
|
while (!ACCEPT (T_RPAREN)) {
|
2017-05-19 19:22:44 +02:00
|
|
|
tail = &(*tail = parse_item (self, err))->next;
|
2017-05-18 14:03:47 +02:00
|
|
|
SKIP_NL ();
|
2017-05-17 23:34:58 +02:00
|
|
|
}
|
2017-05-19 19:22:44 +02:00
|
|
|
return CHECK (new_list (result));
|
2017-05-17 23:34:58 +02:00
|
|
|
}
|
2017-05-18 14:03:47 +02:00
|
|
|
if (ACCEPT (T_LBRACKET)) {
|
|
|
|
while (!ACCEPT (T_RBRACKET)) {
|
2017-05-19 19:22:44 +02:00
|
|
|
tail = &(*tail = parse_item (self, err))->next;
|
2017-05-18 14:03:47 +02:00
|
|
|
SKIP_NL ();
|
|
|
|
}
|
2017-05-19 19:22:44 +02:00
|
|
|
return CHECK (parse_prefix_list (result, "list"));
|
2017-05-18 14:03:47 +02:00
|
|
|
}
|
|
|
|
if (ACCEPT (T_LBRACE)) {
|
|
|
|
while ((*tail = parse_line (self, err)))
|
|
|
|
tail = &(*tail)->next;
|
|
|
|
EXPECT (T_RBRACE);
|
2017-05-19 19:22:44 +02:00
|
|
|
result = CHECK (new_list (result));
|
|
|
|
return CHECK (parse_prefix_list (result, "quote"));
|
2017-05-18 14:03:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 19:22:44 +02:00
|
|
|
self->memory_failure = !(self->error = lexer_errorf (&self->lexer,
|
2017-05-19 21:56:10 +02:00
|
|
|
"unexpected `%s', expected a value", token_names[self->token]));
|
2017-05-18 14:03:47 +02:00
|
|
|
longjmp (out, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct item *
|
|
|
|
parse_line (struct parser *self, jmp_buf out) {
|
|
|
|
jmp_buf err;
|
2017-05-19 19:22:44 +02:00
|
|
|
struct item *volatile result = NULL, *volatile *tail = &result;
|
2017-05-18 14:03:47 +02:00
|
|
|
if (setjmp (err)) {
|
|
|
|
item_free_list (result);
|
|
|
|
longjmp (out, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (PEEK () != T_RBRACE && PEEK () != T_ABORT) {
|
2017-05-19 18:47:35 +02:00
|
|
|
if (!ACCEPT (T_NEWLINE)) {
|
2017-05-19 19:22:44 +02:00
|
|
|
tail = &(*tail = parse_item (self, err))->next;
|
2017-05-19 18:47:35 +02:00
|
|
|
} else if (result) {
|
2017-05-19 19:22:44 +02:00
|
|
|
return CHECK (new_list (result));
|
2017-05-18 14:03:47 +02:00
|
|
|
}
|
2017-05-17 23:34:58 +02:00
|
|
|
}
|
2017-05-18 14:03:47 +02:00
|
|
|
if (result)
|
2017-05-19 19:22:44 +02:00
|
|
|
return CHECK (new_list (result));
|
2017-05-17 21:29:07 +02:00
|
|
|
return NULL;
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:03:47 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
#undef PEEK
|
|
|
|
#undef ACCEPT
|
|
|
|
#undef EXPECT
|
|
|
|
#undef SKIP_NL
|
2017-05-19 19:22:44 +02:00
|
|
|
#undef CHECK
|
2017-05-18 14:03:47 +02:00
|
|
|
|
|
|
|
static struct item *
|
2017-05-19 20:35:32 +02:00
|
|
|
parser_run (struct parser *self, const char **e) {
|
2017-05-18 14:03:47 +02:00
|
|
|
jmp_buf err;
|
2017-05-19 19:22:44 +02:00
|
|
|
struct item *volatile result = NULL, *volatile *tail = &result;
|
2017-05-18 14:03:47 +02:00
|
|
|
if (setjmp (err)) {
|
|
|
|
item_free_list (result);
|
2017-05-20 15:38:06 +02:00
|
|
|
*e = self->error;
|
|
|
|
if (self->memory_failure || self->lexer.string.memory_failure)
|
|
|
|
*e = "memory allocation failure";
|
2017-05-18 14:03:47 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-05-19 20:35:32 +02:00
|
|
|
while ((*tail = parse_line (self, err)))
|
2017-05-18 14:03:47 +02:00
|
|
|
tail = &(*tail)->next;
|
2017-05-19 20:35:32 +02:00
|
|
|
parser_expect (self, T_ABORT, err);
|
2017-05-19 18:32:28 +02:00
|
|
|
return result;
|
2017-05-18 14:03:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-06 13:27:02 +02:00
|
|
|
// --- Runtime -----------------------------------------------------------------
|
|
|
|
|
|
|
|
struct context {
|
2017-05-18 14:03:47 +02:00
|
|
|
struct item *variables; ///< List of variables
|
2017-05-21 09:47:24 +02:00
|
|
|
struct native_fn *native; ///< Maps strings to C functions
|
2017-05-21 17:47:19 +02:00
|
|
|
struct item *arguments; ///< Arguments to last executed block
|
2017-05-06 13:27:02 +02:00
|
|
|
|
|
|
|
char *error; ///< Error information
|
|
|
|
bool error_is_fatal; ///< Whether the error can be catched
|
|
|
|
bool memory_failure; ///< Memory allocation failure
|
|
|
|
|
|
|
|
void *user_data; ///< User data
|
|
|
|
};
|
|
|
|
|
2017-05-21 09:47:24 +02:00
|
|
|
typedef bool (*handler_fn) (struct context *, struct item *, struct item **);
|
|
|
|
|
|
|
|
struct native_fn {
|
|
|
|
struct native_fn *next; ///< The next link in the chain
|
|
|
|
handler_fn handler; ///< Internal C handler, or NULL
|
|
|
|
char name[]; ///< The name of the function
|
|
|
|
};
|
|
|
|
|
2017-05-06 13:27:02 +02:00
|
|
|
static void
|
|
|
|
context_init (struct context *ctx) {
|
2017-05-17 23:22:43 +02:00
|
|
|
memset (ctx, 0, sizeof *ctx);
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
context_free (struct context *ctx) {
|
2017-05-21 09:47:24 +02:00
|
|
|
struct native_fn *next, *iter;
|
|
|
|
for (iter = ctx->native; iter; iter = next) {
|
|
|
|
next = iter->next;
|
|
|
|
free (iter);
|
|
|
|
}
|
2017-05-18 14:03:47 +02:00
|
|
|
item_free_list (ctx->variables);
|
2017-05-21 17:47:19 +02:00
|
|
|
item_free_list (ctx->arguments);
|
2017-05-06 13:27:02 +02:00
|
|
|
free (ctx->error);
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:06 +02:00
|
|
|
static bool
|
|
|
|
check (struct context *ctx, struct item *item) {
|
|
|
|
return !(ctx->memory_failure |= !item);
|
|
|
|
}
|
|
|
|
|
2017-05-18 19:47:10 +02:00
|
|
|
static struct item *
|
|
|
|
get (struct context *ctx, const char *name) {
|
|
|
|
for (struct item *iter = ctx->variables; iter; iter = iter->next)
|
|
|
|
if (!strcmp (iter->head->value, name))
|
|
|
|
return iter->head->next;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
set (struct context *ctx, const char *name, struct item *value) {
|
2017-05-21 09:03:57 +02:00
|
|
|
struct item **p;
|
|
|
|
for (p = &ctx->variables; *p; p = &(*p)->next)
|
|
|
|
if (!strcmp ((*p)->head->value, name)) {
|
|
|
|
struct item *tmp = *p;
|
|
|
|
*p = (*p)->next;
|
|
|
|
item_free (tmp);
|
2017-05-18 19:47:10 +02:00
|
|
|
break;
|
2017-05-21 09:03:57 +02:00
|
|
|
}
|
|
|
|
if (!value)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
struct item *key, *pair;
|
2017-05-20 15:38:06 +02:00
|
|
|
if (!check (ctx, (key = new_string (name, strlen (name))))
|
2017-05-21 09:03:57 +02:00
|
|
|
|| !check (ctx, (pair = new_list (key)))) {
|
|
|
|
item_free_list (value);
|
2017-05-18 19:47:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-05-21 09:03:57 +02:00
|
|
|
key->next = value;
|
2017-05-20 15:38:06 +02:00
|
|
|
pair->next = ctx->variables;
|
|
|
|
ctx->variables = pair;
|
|
|
|
return true;
|
2017-05-18 19:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2017-05-21 09:47:24 +02:00
|
|
|
static struct native_fn *
|
|
|
|
native_find (struct context *ctx, const char *name) {
|
|
|
|
for (struct native_fn *fn = ctx->native; fn; fn = fn->next)
|
|
|
|
if (!strcmp (fn->name, name))
|
|
|
|
return fn;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
native_register (struct context *ctx, const char *name, handler_fn handler) {
|
|
|
|
struct native_fn *fn = native_find (ctx, name);
|
|
|
|
if (!fn) {
|
|
|
|
if (!(fn = calloc (1, sizeof *fn + strlen (name) + 1)))
|
|
|
|
return false;
|
|
|
|
strcpy (fn->name, name);
|
|
|
|
fn->next = ctx->native;
|
|
|
|
ctx->native = fn;
|
|
|
|
}
|
|
|
|
fn->handler = handler;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2017-05-06 13:27:02 +02:00
|
|
|
static bool
|
|
|
|
set_error (struct context *ctx, const char *format, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, format);
|
2017-05-20 15:38:06 +02:00
|
|
|
free (ctx->error);
|
|
|
|
if (!(ctx->error = vformat (format, ap)))
|
2017-05-06 13:27:02 +02:00
|
|
|
ctx->memory_failure = true;
|
2017-05-20 15:38:06 +02:00
|
|
|
va_end (ap);
|
2017-05-06 13:27:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-21 17:57:31 +02:00
|
|
|
static bool
|
|
|
|
can_modify_error (struct context *ctx) {
|
|
|
|
// In that case, `error' is NULL and there's nothing else to do anyway.
|
|
|
|
// Errors starting with an underscore are exceptions and would not work
|
|
|
|
// with stack traces generated this way.
|
|
|
|
return !ctx->memory_failure && ctx->error[0] != '_';
|
|
|
|
}
|
|
|
|
|
2017-05-18 19:47:10 +02:00
|
|
|
static bool
|
2017-05-21 17:47:19 +02:00
|
|
|
assign_arguments (struct context *ctx, struct item *names) {
|
|
|
|
struct item *arg = ctx->arguments;
|
2017-05-18 19:47:10 +02:00
|
|
|
for (; names; names = names->next) {
|
2017-05-21 09:03:57 +02:00
|
|
|
if (names->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "argument names must be strings");
|
|
|
|
|
2017-05-21 17:47:19 +02:00
|
|
|
struct item *value = NULL;
|
|
|
|
if (arg && !check (ctx, (value = new_clone (arg))))
|
2017-05-21 09:03:57 +02:00
|
|
|
return false;
|
2017-05-18 20:45:46 +02:00
|
|
|
if (!set (ctx, names->value, value))
|
|
|
|
return false;
|
2017-05-21 17:47:19 +02:00
|
|
|
if (arg)
|
|
|
|
arg = arg->next;
|
2017-05-18 19:47:10 +02:00
|
|
|
}
|
|
|
|
return true;
|
2017-05-18 18:01:57 +02:00
|
|
|
}
|
|
|
|
|
2017-05-18 23:08:55 +02:00
|
|
|
static bool execute_statement (struct context *, struct item *, struct item **);
|
2017-05-18 19:47:10 +02:00
|
|
|
static bool execute (struct context *ctx, struct item *body, struct item **);
|
2017-05-06 13:27:02 +02:00
|
|
|
|
2017-05-18 23:08:55 +02:00
|
|
|
static bool
|
2017-05-21 18:32:28 +02:00
|
|
|
execute_args (struct context *ctx, struct item *args) {
|
2017-05-21 17:57:31 +02:00
|
|
|
size_t i = 0;
|
2017-05-21 18:32:28 +02:00
|
|
|
struct item *res = NULL, **out = &res;
|
2017-05-20 15:38:06 +02:00
|
|
|
for (; args; args = args->next) {
|
2017-05-18 23:08:55 +02:00
|
|
|
struct item *evaluated = NULL;
|
2017-05-21 08:27:47 +02:00
|
|
|
// Arguments should not evaporate, default to a nil value
|
2017-05-21 17:57:31 +02:00
|
|
|
if (!execute_statement (ctx, args, &evaluated)
|
|
|
|
|| (!evaluated && !check (ctx, (evaluated = new_list (NULL)))))
|
|
|
|
goto error;
|
2017-05-21 08:27:47 +02:00
|
|
|
item_free_list (evaluated->next);
|
|
|
|
evaluated->next = NULL;
|
2017-05-21 18:32:28 +02:00
|
|
|
out = &(*out = evaluated)->next;
|
2017-05-21 17:57:31 +02:00
|
|
|
i++;
|
2017-05-18 23:08:55 +02:00
|
|
|
}
|
2017-05-21 18:32:28 +02:00
|
|
|
item_free_list (ctx->arguments);
|
|
|
|
ctx->arguments = res;
|
2017-05-18 23:08:55 +02:00
|
|
|
return true;
|
2017-05-21 18:32:28 +02:00
|
|
|
|
2017-05-21 17:57:31 +02:00
|
|
|
error:
|
|
|
|
// Once the code flows like this, at least make some use of it
|
|
|
|
if (can_modify_error (ctx)) {
|
|
|
|
char *tmp = ctx->error;
|
|
|
|
ctx->error = NULL;
|
|
|
|
set_error (ctx, "(argument %zu) -> %s", i, tmp);
|
|
|
|
free (tmp);
|
|
|
|
}
|
2017-05-21 18:32:28 +02:00
|
|
|
item_free_list (res);
|
2017-05-21 17:57:31 +02:00
|
|
|
return false;
|
2017-05-18 23:08:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-05-21 17:20:15 +02:00
|
|
|
execute_native (struct context *ctx, const char *name, struct item *args,
|
|
|
|
struct item **result) {
|
|
|
|
struct native_fn *fn = native_find (ctx, name);
|
|
|
|
if (!fn)
|
|
|
|
return set_error (ctx, "unknown function");
|
2017-05-21 18:32:28 +02:00
|
|
|
if (!execute_args (ctx, args))
|
|
|
|
return false;
|
2017-05-21 17:20:15 +02:00
|
|
|
|
2017-05-21 18:32:28 +02:00
|
|
|
// "ctx->arguments" is for assign_arguments() only
|
|
|
|
args = ctx->arguments;
|
|
|
|
ctx->arguments = NULL;
|
|
|
|
bool ok = fn->handler (ctx, args, result);
|
|
|
|
item_free_list (args);
|
2017-05-21 17:20:15 +02:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
execute_resolved (struct context *ctx, struct item *body, struct item *args,
|
|
|
|
struct item **result) {
|
|
|
|
// Resolving names ecursively could be pretty fatal, let's not do that
|
|
|
|
if (body->type == ITEM_STRING)
|
|
|
|
return check (ctx, (*result = new_clone (body)));
|
2017-05-21 18:32:28 +02:00
|
|
|
return execute_args (ctx, args)
|
|
|
|
&& execute (ctx, body->head, result);
|
2017-05-18 23:08:55 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:06 +02:00
|
|
|
static bool
|
2017-05-21 17:20:15 +02:00
|
|
|
execute_item (struct context *ctx, struct item *body, struct item **result) {
|
|
|
|
struct item *args = body->next;
|
|
|
|
if (body->type == ITEM_STRING) {
|
|
|
|
const char *name = body->value;
|
|
|
|
// TODO: these could be just regular handlers, only top priority
|
|
|
|
if (!strcmp (name, "quote"))
|
|
|
|
return !args || check (ctx, (*result = new_clone_list (args)));
|
|
|
|
if (!strcmp (name, "arg"))
|
2017-05-21 17:47:19 +02:00
|
|
|
return assign_arguments (ctx, args);
|
2017-05-21 17:20:15 +02:00
|
|
|
if ((body = get (ctx, name)))
|
|
|
|
return execute_resolved (ctx, body, args, result);
|
|
|
|
return execute_native (ctx, name, args, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// When someone tries to call a block directly, we must evaluate it;
|
|
|
|
// e.g. something like `{ choose [@f1 @f2 @f3] } arg1 arg2 arg3`.
|
|
|
|
struct item *evaluated = NULL;
|
|
|
|
if (!execute_statement (ctx, body, &evaluated))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// It might a bit confusing that this doesn't evaluate arguments
|
|
|
|
// but neither does "quote" and there's nothing to do here
|
|
|
|
if (!evaluated)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
bool success = execute_resolved (ctx, evaluated, args, result);
|
|
|
|
item_free_list (evaluated);
|
|
|
|
return success;
|
2017-05-20 15:38:06 +02:00
|
|
|
}
|
|
|
|
|
2017-05-06 13:27:02 +02:00
|
|
|
static bool
|
2017-05-18 19:47:10 +02:00
|
|
|
execute_statement
|
|
|
|
(struct context *ctx, struct item *statement, struct item **result) {
|
2017-05-20 15:38:06 +02:00
|
|
|
if (statement->type == ITEM_STRING)
|
|
|
|
return check (ctx, (*result = new_clone (statement)));
|
2017-05-18 19:47:10 +02:00
|
|
|
|
2017-05-21 08:27:47 +02:00
|
|
|
// Executing a nil value results in no value. It's not very different from
|
|
|
|
// calling a block that returns no value--it's for our callers to resolve.
|
2017-05-21 17:20:15 +02:00
|
|
|
if (!statement->head
|
|
|
|
|| execute_item (ctx, statement->head, result))
|
2017-05-06 13:27:02 +02:00
|
|
|
return true;
|
|
|
|
|
2017-05-21 08:28:45 +02:00
|
|
|
item_free_list (*result);
|
|
|
|
*result = NULL;
|
|
|
|
|
2017-05-21 17:20:15 +02:00
|
|
|
const char *name = "(block)";
|
|
|
|
if (statement->head->type == ITEM_STRING)
|
|
|
|
name = statement->head->value;
|
|
|
|
|
2017-05-21 17:57:31 +02:00
|
|
|
if (can_modify_error (ctx)) {
|
2017-05-20 15:38:06 +02:00
|
|
|
char *tmp = ctx->error;
|
2017-05-21 13:05:54 +02:00
|
|
|
ctx->error = NULL;
|
2017-05-20 15:38:06 +02:00
|
|
|
set_error (ctx, "%s -> %s", name, tmp);
|
|
|
|
free (tmp);
|
|
|
|
}
|
2017-05-06 13:27:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-18 18:01:57 +02:00
|
|
|
// Execute a block and return whatever the last statement returned
|
2017-05-18 19:47:10 +02:00
|
|
|
static bool
|
|
|
|
execute (struct context *ctx, struct item *body, struct item **result) {
|
|
|
|
for (; body; body = body->next) {
|
|
|
|
item_free_list (*result);
|
|
|
|
*result = NULL;
|
|
|
|
if (!execute_statement (ctx, body, result))
|
|
|
|
return false;
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
2017-05-18 19:47:10 +02:00
|
|
|
return true;
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Runtime library ---------------------------------------------------------
|
|
|
|
|
2017-05-18 19:47:10 +02:00
|
|
|
#define defn(name) static bool name \
|
|
|
|
(struct context *ctx, struct item *args, struct item **result)
|
2017-05-06 13:27:02 +02:00
|
|
|
|
2017-05-21 17:47:19 +02:00
|
|
|
static bool
|
|
|
|
set_single_argument (struct context *ctx, struct item *item) {
|
|
|
|
struct item *single;
|
|
|
|
if (!check (ctx, (single = new_clone (item))))
|
|
|
|
return false;
|
|
|
|
item_free_list (ctx->arguments);
|
|
|
|
ctx->arguments = single;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define E_BREAK "_break"
|
|
|
|
|
|
|
|
static bool
|
|
|
|
eat_error (struct context *ctx, const char *name) {
|
|
|
|
if (!ctx->error || strcmp (ctx->error, name))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
free (ctx->error);
|
|
|
|
ctx->error = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-21 10:03:29 +02:00
|
|
|
static struct item *
|
2017-05-21 10:33:04 +02:00
|
|
|
new_number (double n) {
|
|
|
|
char *s;
|
|
|
|
if (!(s = format ("%f", n)))
|
2017-05-21 10:03:29 +02:00
|
|
|
return NULL;
|
|
|
|
|
2017-05-21 10:33:04 +02:00
|
|
|
char *p = strchr (s, 0);
|
|
|
|
while (--p > s && *p == '0')
|
|
|
|
*p = 0;
|
|
|
|
if (*p == '.')
|
|
|
|
*p = 0;
|
|
|
|
|
2017-05-21 10:03:29 +02:00
|
|
|
struct item *item = new_string (s, strlen (s));
|
|
|
|
free (s);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2017-05-06 13:27:02 +02:00
|
|
|
static bool
|
2017-05-20 21:24:11 +02:00
|
|
|
truthy (struct item *item) {
|
2017-05-24 20:54:37 +02:00
|
|
|
return item && (item->head || item->len);
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-21 10:39:39 +02:00
|
|
|
static struct item * new_boolean (bool b) { return new_string ("1", b); }
|
|
|
|
|
2017-05-21 09:38:18 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2017-05-18 19:47:10 +02:00
|
|
|
defn (fn_set) {
|
|
|
|
struct item *name = args;
|
|
|
|
if (!name || name->type != ITEM_STRING)
|
2017-05-20 15:38:06 +02:00
|
|
|
return set_error (ctx, "first argument must be string");
|
2017-05-18 19:47:10 +02:00
|
|
|
|
|
|
|
struct item *value;
|
|
|
|
if ((value = name->next))
|
2017-05-21 09:03:57 +02:00
|
|
|
return check (ctx, (value = new_clone (value)))
|
|
|
|
&& set (ctx, name->value, value);
|
2017-05-18 19:47:10 +02:00
|
|
|
|
2017-05-20 15:46:52 +02:00
|
|
|
// We return an empty list for a nil value
|
|
|
|
if (!(value = get (ctx, name->value)))
|
|
|
|
return check (ctx, (*result = new_list (NULL)));
|
|
|
|
return check (ctx, (*result = new_clone (value)));
|
2017-05-20 15:38:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defn (fn_list) {
|
|
|
|
struct item *values = NULL;
|
|
|
|
if (args && !check (ctx, (values = new_clone_list (args))))
|
|
|
|
return false;
|
|
|
|
return check (ctx, (*result = new_list (values)));
|
2017-05-18 19:47:10 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 21:24:11 +02:00
|
|
|
defn (fn_if) {
|
|
|
|
struct item *cond, *body, *keyword;
|
|
|
|
for (cond = args; ; cond = keyword->next) {
|
|
|
|
if (!cond)
|
|
|
|
return set_error (ctx, "missing condition");
|
|
|
|
if (!(body = cond->next))
|
|
|
|
return set_error (ctx, "missing body");
|
|
|
|
|
|
|
|
struct item *res = NULL;
|
|
|
|
if (!execute_statement (ctx, cond, &res))
|
|
|
|
return false;
|
|
|
|
bool match = truthy (res);
|
|
|
|
item_free_list (res);
|
|
|
|
if (match)
|
|
|
|
return execute_statement (ctx, body, result);
|
|
|
|
|
|
|
|
if (!(keyword = body->next))
|
|
|
|
break;
|
|
|
|
if (keyword->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "expected keyword, got list");
|
|
|
|
|
|
|
|
if (!strcmp (keyword->value, "else")) {
|
|
|
|
if (!(body = keyword->next))
|
|
|
|
return set_error (ctx, "missing body");
|
|
|
|
return execute_statement (ctx, body, result);
|
|
|
|
}
|
|
|
|
if (strcmp (keyword->value, "elif"))
|
|
|
|
return set_error (ctx, "invalid keyword: %s", keyword->value);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
defn (fn_for) {
|
|
|
|
struct item *list = args, *body;
|
|
|
|
if (!list || list->type != ITEM_LIST)
|
|
|
|
return set_error (ctx, "first argument must be a list");
|
|
|
|
if (!(body = list->next) || body->type != ITEM_LIST)
|
|
|
|
return set_error (ctx, "second argument must be a function");
|
|
|
|
|
|
|
|
(void) result;
|
2017-05-21 00:23:51 +02:00
|
|
|
for (struct item *v = list->head; v; v = v->next) {
|
2017-05-20 21:24:11 +02:00
|
|
|
struct item *res = NULL;
|
2017-05-21 17:47:19 +02:00
|
|
|
bool ok = set_single_argument (ctx, v)
|
2017-05-21 00:23:51 +02:00
|
|
|
&& execute (ctx, body->head, &res);
|
2017-05-20 21:24:11 +02:00
|
|
|
item_free_list (res);
|
2017-05-21 09:38:18 +02:00
|
|
|
if (eat_error (ctx, E_BREAK))
|
|
|
|
break;
|
2017-05-20 21:24:11 +02:00
|
|
|
if (!ok)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-21 09:38:18 +02:00
|
|
|
defn (fn_break) {
|
|
|
|
(void) args; (void) result; return set_error (ctx, E_BREAK);
|
|
|
|
}
|
|
|
|
|
2017-05-20 21:24:11 +02:00
|
|
|
defn (fn_map) {
|
|
|
|
struct item *body = args, *values;
|
|
|
|
if (!body || body->type != ITEM_LIST)
|
|
|
|
return set_error (ctx, "first argument must be a function");
|
|
|
|
if (!(values = body->next) || values->type != ITEM_LIST)
|
|
|
|
return set_error (ctx, "second argument must be a list");
|
|
|
|
|
|
|
|
struct item *res = NULL, **out = &res;
|
|
|
|
for (struct item *v = values->head; v; v = v->next) {
|
2017-05-21 17:47:19 +02:00
|
|
|
if (!set_single_argument (ctx, v)
|
2017-05-21 00:23:51 +02:00
|
|
|
|| !execute (ctx, body->head, out)) {
|
2017-05-20 21:24:11 +02:00
|
|
|
item_free_list (res);
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-21 00:23:51 +02:00
|
|
|
while (*out)
|
2017-05-20 21:24:11 +02:00
|
|
|
out = &(*out)->next;
|
|
|
|
}
|
|
|
|
return check (ctx, (*result = new_list (res)));
|
|
|
|
}
|
|
|
|
|
2017-05-17 23:22:43 +02:00
|
|
|
defn (fn_print) {
|
2017-05-18 19:47:10 +02:00
|
|
|
(void) result;
|
2017-05-20 15:38:06 +02:00
|
|
|
for (; args; args = args->next) {
|
|
|
|
if (args->type != ITEM_STRING)
|
|
|
|
// TODO: print lists as their parsable representation
|
|
|
|
return set_error (ctx, "cannot print lists");
|
|
|
|
if (fwrite (args->value, 1, args->len, stdout) != args->len)
|
|
|
|
return set_error (ctx, "write failed: %s", strerror (errno));
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
defn (fn_concatenate) {
|
2017-05-18 19:47:10 +02:00
|
|
|
struct buffer buf = BUFFER_INITIALIZER;
|
2017-05-20 15:38:06 +02:00
|
|
|
for (; args; args = args->next) {
|
|
|
|
if (args->type != ITEM_STRING) {
|
|
|
|
free (buf.s);
|
|
|
|
return set_error (ctx, "cannot concatenate lists");
|
|
|
|
}
|
2017-05-18 19:47:10 +02:00
|
|
|
buffer_append (&buf, args->value, args->len);
|
|
|
|
}
|
2017-05-20 15:38:06 +02:00
|
|
|
buffer_append_c (&buf, '\0');
|
|
|
|
|
2017-05-20 21:38:08 +02:00
|
|
|
bool ok = !(ctx->memory_failure |= buf.memory_failure)
|
|
|
|
&& check (ctx, (*result = new_string (buf.s, buf.len - 1)));
|
2017-05-18 19:47:10 +02:00
|
|
|
free (buf.s);
|
2017-05-20 15:38:06 +02:00
|
|
|
return ok;
|
2017-05-06 13:27:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-21 10:03:29 +02:00
|
|
|
defn (fn_system) {
|
|
|
|
struct item *command = args;
|
|
|
|
if (!command || command->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "first argument must be string");
|
|
|
|
if (command->next)
|
|
|
|
return set_error (ctx, "cannot deal with multiple arguments");
|
2017-05-21 10:33:04 +02:00
|
|
|
return check (ctx, (*result = new_number (system (command->value))));
|
2017-05-21 10:03:29 +02:00
|
|
|
}
|
|
|
|
|
2017-05-21 10:24:14 +02:00
|
|
|
defn (fn_plus) {
|
|
|
|
double res = 0.0;
|
|
|
|
for (; args; args = args->next) {
|
|
|
|
if (args->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "arguments must be strings");
|
|
|
|
res += strtod (args->value, NULL);
|
|
|
|
}
|
2017-05-21 10:33:04 +02:00
|
|
|
return check (ctx, (*result = new_number (res)));
|
2017-05-21 10:24:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defn (fn_minus) {
|
2017-05-21 10:33:04 +02:00
|
|
|
double res = 0.0;
|
2017-05-21 10:24:14 +02:00
|
|
|
for (; args; args = args->next) {
|
|
|
|
if (args->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "arguments must be strings");
|
|
|
|
res -= strtod (args->value, NULL);
|
|
|
|
}
|
2017-05-21 10:33:04 +02:00
|
|
|
return check (ctx, (*result = new_number (res)));
|
2017-05-21 10:24:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defn (fn_multiply) {
|
|
|
|
double res = 1.0;
|
|
|
|
for (; args; args = args->next) {
|
|
|
|
if (args->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "arguments must be strings");
|
|
|
|
res *= strtod (args->value, NULL);
|
|
|
|
}
|
2017-05-21 10:33:04 +02:00
|
|
|
return check (ctx, (*result = new_number (res)));
|
2017-05-21 10:24:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defn (fn_divide) {
|
|
|
|
if (!args || args->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "first argument must be string");
|
|
|
|
double res = strtod (args->value, NULL), x;
|
|
|
|
for (args = args->next; args; args = args->next) {
|
|
|
|
if (args->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "arguments must be strings");
|
|
|
|
if (!(x = strtod (args->value, NULL)))
|
|
|
|
return set_error (ctx, "division by zero");
|
|
|
|
res /= x;
|
|
|
|
}
|
2017-05-21 10:33:04 +02:00
|
|
|
return check (ctx, (*result = new_number (res)));
|
2017-05-21 10:24:14 +02:00
|
|
|
}
|
|
|
|
|
2017-05-21 10:39:39 +02:00
|
|
|
defn (fn_not) {
|
|
|
|
if (!args)
|
|
|
|
return set_error (ctx, "missing argument");
|
|
|
|
return check (ctx, (*result = new_boolean (!truthy (args))));
|
|
|
|
}
|
|
|
|
|
2017-05-21 12:07:21 +02:00
|
|
|
// TODO: "and" and "or" should be short-circuiting special forms
|
2017-05-21 10:39:39 +02:00
|
|
|
defn (fn_and) {
|
|
|
|
bool res = true;
|
|
|
|
for (; args; args = args->next)
|
|
|
|
res &= truthy (args);
|
|
|
|
return check (ctx, (*result = new_boolean (res)));
|
|
|
|
}
|
|
|
|
|
|
|
|
defn (fn_or) {
|
|
|
|
bool res = false;
|
|
|
|
for (; args; args = args->next)
|
|
|
|
res |= truthy (args);
|
|
|
|
return check (ctx, (*result = new_boolean (res)));
|
|
|
|
}
|
|
|
|
|
2017-05-21 11:26:59 +02:00
|
|
|
defn (fn_eq) {
|
|
|
|
struct item *etalon = args;
|
|
|
|
if (!etalon || etalon->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "first argument must be string");
|
|
|
|
bool res = true;
|
|
|
|
for (args = etalon->next; args; args = args->next) {
|
|
|
|
if (args->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "arguments must be strings");
|
|
|
|
if (!(res &= !strcmp (etalon->value, args->value)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return check (ctx, (*result = new_boolean (res)));
|
|
|
|
}
|
|
|
|
|
|
|
|
defn (fn_lt) {
|
|
|
|
struct item *etalon = args;
|
|
|
|
if (!etalon || etalon->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "first argument must be string");
|
|
|
|
bool res = true;
|
|
|
|
for (args = etalon->next; args; args = args->next) {
|
|
|
|
if (args->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "arguments must be strings");
|
|
|
|
if (!(res &= strcmp (etalon->value, args->value) < 0))
|
|
|
|
break;
|
|
|
|
etalon = args;
|
|
|
|
}
|
|
|
|
return check (ctx, (*result = new_boolean (res)));
|
|
|
|
}
|
|
|
|
|
2017-05-21 12:14:43 +02:00
|
|
|
defn (fn_equals) {
|
|
|
|
struct item *etalon = args;
|
|
|
|
if (!etalon || etalon->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "first argument must be string");
|
|
|
|
bool res = true;
|
|
|
|
for (args = etalon->next; args; args = args->next) {
|
|
|
|
if (args->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "arguments must be strings");
|
|
|
|
if (!(res &= strtod (etalon->value, NULL)
|
|
|
|
== strtod (args->value, NULL)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return check (ctx, (*result = new_boolean (res)));
|
|
|
|
}
|
|
|
|
|
|
|
|
defn (fn_less) {
|
|
|
|
struct item *etalon = args;
|
|
|
|
if (!etalon || etalon->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "first argument must be string");
|
|
|
|
bool res = true;
|
|
|
|
for (args = etalon->next; args; args = args->next) {
|
|
|
|
if (args->type != ITEM_STRING)
|
|
|
|
return set_error (ctx, "arguments must be strings");
|
|
|
|
if (!(res &= strtod (etalon->value, NULL) < strtod (args->value, NULL)))
|
|
|
|
break;
|
|
|
|
etalon = args;
|
|
|
|
}
|
|
|
|
return check (ctx, (*result = new_boolean (res)));
|
|
|
|
}
|
|
|
|
|
2017-05-20 21:24:11 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
static bool
|
|
|
|
init_runtime_library (struct context *ctx) {
|
|
|
|
struct {
|
|
|
|
const char *name; ///< Name of the function
|
|
|
|
const char *definition; ///< The defining script
|
|
|
|
} functions[] = {
|
2017-05-21 18:48:21 +02:00
|
|
|
{ "unless", "arg _cond _body; if (not (@_cond)) @_body" },
|
2017-05-23 17:54:53 +02:00
|
|
|
{ "filter", "arg _body _list; map {"
|
|
|
|
"arg _item; if (@_body @_item) { @_item } } @_list" },
|
2017-05-21 12:07:21 +02:00
|
|
|
// TODO: we should be able to apply them to all arguments
|
2017-05-21 12:46:21 +02:00
|
|
|
{ "ne?", "arg _ne1 _ne2; not (eq? @_ne1 @_ne2)" },
|
|
|
|
{ "ge?", "arg _ge1 _ge2; not (lt? @_ge1 @_ge2)" },
|
2017-05-21 12:07:21 +02:00
|
|
|
{ "le?", "arg _le1 _le2; ge? @_le2 @_le1" },
|
|
|
|
{ "gt?", "arg _gt1 _gt2; lt? @_gt2 @_gt1" },
|
2017-05-21 12:46:21 +02:00
|
|
|
{ "<>", "arg _<>1 _<>2; not (= @_<>1 @_<>2)" },
|
|
|
|
{ ">=", "arg _>=1 _>=2; not (< @_>=1 @_>=2)" },
|
2017-05-21 12:14:43 +02:00
|
|
|
{ "<=", "arg _<=1 _<=2; >= @_<=2 @_<=1" },
|
|
|
|
{ ">", "arg _>1 _>2; < @_>2 @_>1" },
|
2017-05-20 21:24:11 +02:00
|
|
|
};
|
|
|
|
|
2017-05-21 09:47:24 +02:00
|
|
|
bool ok = true;
|
2017-05-20 21:24:11 +02:00
|
|
|
for (size_t i = 0; i < N_ELEMENTS (functions); i++) {
|
|
|
|
struct parser parser;
|
|
|
|
parser_init (&parser,
|
|
|
|
functions[i].definition, strlen (functions[i].definition));
|
|
|
|
const char *e = NULL;
|
|
|
|
struct item *body = parser_run (&parser, &e);
|
|
|
|
if (e) {
|
|
|
|
printf ("error parsing internal function `%s': %s\n",
|
|
|
|
functions[i].name, e);
|
|
|
|
ok = false;
|
2017-05-21 09:03:57 +02:00
|
|
|
} else if (!check (ctx, (body = new_list (body)))
|
|
|
|
|| !set (ctx, functions[i].name, body)) {
|
|
|
|
ok = false;
|
2017-05-20 21:24:11 +02:00
|
|
|
} else
|
2017-05-21 09:03:57 +02:00
|
|
|
body = NULL;
|
2017-05-20 21:24:11 +02:00
|
|
|
item_free_list (body);
|
|
|
|
parser_free (&parser);
|
|
|
|
}
|
2017-05-21 09:47:24 +02:00
|
|
|
|
|
|
|
return ok
|
|
|
|
&& native_register (ctx, "set", fn_set)
|
|
|
|
&& native_register (ctx, "list", fn_list)
|
|
|
|
&& native_register (ctx, "if", fn_if)
|
|
|
|
&& native_register (ctx, "for", fn_for)
|
|
|
|
&& native_register (ctx, "break", fn_break)
|
|
|
|
&& native_register (ctx, "map", fn_map)
|
|
|
|
&& native_register (ctx, "print", fn_print)
|
2017-05-21 10:03:29 +02:00
|
|
|
&& native_register (ctx, "..", fn_concatenate)
|
2017-05-21 10:24:14 +02:00
|
|
|
&& native_register (ctx, "system", fn_system)
|
|
|
|
&& native_register (ctx, "+", fn_plus)
|
|
|
|
&& native_register (ctx, "-", fn_minus)
|
|
|
|
&& native_register (ctx, "*", fn_multiply)
|
2017-05-21 10:39:39 +02:00
|
|
|
&& native_register (ctx, "/", fn_divide)
|
|
|
|
&& native_register (ctx, "not", fn_not)
|
|
|
|
&& native_register (ctx, "and", fn_and)
|
2017-05-21 11:26:59 +02:00
|
|
|
&& native_register (ctx, "or", fn_or)
|
|
|
|
&& native_register (ctx, "eq?", fn_eq)
|
2017-05-21 12:14:43 +02:00
|
|
|
&& native_register (ctx, "lt?", fn_lt)
|
|
|
|
&& native_register (ctx, "=", fn_equals)
|
|
|
|
&& native_register (ctx, "<", fn_less);
|
2017-05-20 21:24:11 +02:00
|
|
|
}
|