ell/repl.c

117 lines
3.1 KiB
C
Raw Normal View History

2017-05-21 13:01:23 +02:00
/*
* repl.c: test REPL
*
* 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.
*
*/
#include "ell.c"
#include <readline/readline.h>
#include <readline/history.h>
2017-05-21 13:22:24 +02:00
static void
2017-05-30 02:43:42 +02:00
run (struct ell *ell, struct ell_v *program) {
struct ell_v *result = NULL;
(void) ell_eval_block (ell, program, NULL, &result);
ell_free_seq (program);
2017-05-21 13:22:24 +02:00
2017-05-30 02:43:42 +02:00
const char *failure = ell->error;
if (ell->memory_failure)
2017-05-21 13:22:24 +02:00
failure = "memory allocation failure";
if (failure) {
printf ("\x1b[31m%s: %s\x1b[0m\n", "runtime error", failure);
2017-05-30 02:43:42 +02:00
free (ell->error);
ell->error = NULL;
ell->memory_failure = false;
2017-05-21 13:22:24 +02:00
} else {
2017-05-30 02:43:42 +02:00
ell_print_seq (result);
2017-05-21 13:22:24 +02:00
putchar ('\n');
2017-05-30 02:43:42 +02:00
ell_free_seq (result);
2017-05-21 13:22:24 +02:00
}
}
2017-05-23 18:08:00 +02:00
static int
init_readline (void) {
rl_variable_bind ("blink-matching-paren", "on");
rl_bind_key (TAB, rl_named_function ("possible-completions"));
2017-05-23 18:08:00 +02:00
return 0;
}
2017-05-30 02:43:42 +02:00
static struct ell ell;
static char **
complete (const char *text, int start, int end) {
2017-05-25 13:52:13 +02:00
(void) start;
(void) end;
// Don't iterate over filenames and stuff
rl_attempted_completion_over = true;
static char *buf[128];
size_t n = 1, len = strlen (text);
2017-05-30 02:43:42 +02:00
for (struct ell_v *v = ell.globals; v; v = v->next)
if (n < 127 && !strncmp (v->head->string, text, len))
buf[n++] = ell_format ("%s", v->head->string);
for (struct ell_native_fn *iter = ell.native; iter; iter = iter->next)
if (n < 127 && !strncmp (iter->name, text, len))
2017-05-30 02:43:42 +02:00
buf[n++] = ell_format ("%s", iter->name);
if (n < 2)
return NULL;
// This never actually completes anything, just shows the options,
// we'd have to figure out the longest common prefix
2017-05-30 02:43:42 +02:00
buf[0] = ell_format ("%s", text);
buf[n++] = NULL;
char **copy = malloc (sizeof *buf * n);
memcpy (copy, buf, sizeof *buf * n);
return copy;
}
2017-05-21 13:01:23 +02:00
int
main (int argc, char *argv[]) {
(void) argc;
2017-05-30 02:43:42 +02:00
ell_init (&ell);
if (!ell_std_initialize (&ell))
2017-05-21 13:01:23 +02:00
printf ("%s\n", "runtime library initialization failed");
using_history ();
const char *slash = strrchr (argv[0], '/');
rl_readline_name = slash ? ++slash : argv[0];
2017-05-23 18:08:00 +02:00
rl_startup_hook = init_readline;
rl_attempted_completion_function = complete;
2017-05-21 13:01:23 +02:00
char *line;
while ((line = readline ("> "))) {
2017-05-30 02:43:42 +02:00
struct ell_parser p;
ell_parser_init (&p, line, strlen (line));
2017-05-21 13:01:23 +02:00
add_history (line);
const char *e = NULL;
2017-05-30 02:43:42 +02:00
struct ell_v *program = ell_parser_run (&p, &e);
2017-05-21 13:01:23 +02:00
free (line);
2017-05-21 13:22:24 +02:00
if (e)
2017-05-21 13:01:23 +02:00
printf ("\x1b[31m%s: %s\x1b[0m\n", "parse error", e);
2017-05-21 13:22:24 +02:00
else
2017-05-30 02:43:42 +02:00
run (&ell, program);
ell_parser_free (&p);
2017-05-21 13:01:23 +02:00
}
2017-05-21 13:22:24 +02:00
putchar ('\n');
2017-05-30 02:43:42 +02:00
ell_free (&ell);
2017-05-21 13:01:23 +02:00
return 0;
}