2014-01-17 06:58:31 -05:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
2013-10-09 03:58:35 -04:00
|
|
|
#include <stdio.h>
|
2013-12-17 23:21:26 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-10-24 06:06:31 -04:00
|
|
|
#include <unistd.h>
|
2013-10-09 03:58:35 -04:00
|
|
|
|
2013-10-10 03:15:41 -04:00
|
|
|
#include "picrin.h"
|
2013-10-30 04:06:01 -04:00
|
|
|
#include "picrin/pair.h"
|
2013-10-10 03:15:41 -04:00
|
|
|
|
2013-10-17 04:08:33 -04:00
|
|
|
#if PIC_ENABLE_READLINE
|
|
|
|
# include <readline/readline.h>
|
|
|
|
# include <readline/history.h>
|
|
|
|
#endif
|
|
|
|
|
2013-10-17 07:48:50 -04:00
|
|
|
#define CODE_MAX_LENGTH 1024
|
2013-10-09 04:14:48 -04:00
|
|
|
#define LINE_MAX_LENGTH 256
|
|
|
|
|
2013-10-24 06:06:31 -04:00
|
|
|
void
|
|
|
|
print_help(void)
|
|
|
|
{
|
|
|
|
const char *help =
|
|
|
|
"picrin scheme\n"
|
|
|
|
"\n"
|
2013-10-28 09:08:03 -04:00
|
|
|
"Usage: picrin [options] [file]\n"
|
2013-10-24 06:06:31 -04:00
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
2013-12-05 03:21:42 -05:00
|
|
|
" -e [program] run one liner ecript\n"
|
|
|
|
" -h show this help";
|
2013-10-24 06:06:31 -04:00
|
|
|
|
|
|
|
puts(help);
|
|
|
|
}
|
|
|
|
|
2014-01-08 01:38:47 -05:00
|
|
|
void
|
|
|
|
import_repllib(pic_state *pic)
|
|
|
|
{
|
|
|
|
int ai = pic_gc_arena_preserve(pic);
|
|
|
|
|
|
|
|
pic_import(pic, pic_parse(pic, "(scheme base)"));
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
puts("* imported repl libraries");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
}
|
|
|
|
|
2013-12-07 00:15:21 -05:00
|
|
|
int exit_status;
|
|
|
|
|
|
|
|
void
|
2013-10-28 09:08:03 -04:00
|
|
|
repl(pic_state *pic)
|
2013-10-09 03:58:35 -04:00
|
|
|
{
|
2013-10-17 07:48:50 -04:00
|
|
|
char code[CODE_MAX_LENGTH] = "", line[LINE_MAX_LENGTH];
|
2013-12-18 01:43:13 -05:00
|
|
|
char *prompt;
|
2013-10-30 04:06:01 -04:00
|
|
|
pic_value v, vs;
|
2013-10-11 23:55:05 -04:00
|
|
|
struct pic_proc *proc;
|
2013-10-30 04:06:01 -04:00
|
|
|
int ai, n, i;
|
2013-10-09 04:10:32 -04:00
|
|
|
|
2013-12-18 01:43:13 -05:00
|
|
|
#if PIC_ENABLE_READLINE
|
|
|
|
char *read_line;
|
|
|
|
#else
|
2013-10-19 14:05:42 -04:00
|
|
|
char last_char;
|
|
|
|
int char_index;
|
|
|
|
#endif
|
|
|
|
|
2013-10-14 05:29:30 -04:00
|
|
|
ai = pic_gc_arena_preserve(pic);
|
|
|
|
|
2013-10-09 04:10:32 -04:00
|
|
|
while (1) {
|
2013-10-17 07:48:50 -04:00
|
|
|
prompt = code[0] == '\0' ? "> " : "* ";
|
2013-10-17 04:08:33 -04:00
|
|
|
|
2013-12-09 01:36:41 -05:00
|
|
|
#if DEBUG
|
|
|
|
printf("[current ai = %d]\n", ai);
|
|
|
|
#endif
|
|
|
|
|
2013-10-17 04:08:33 -04:00
|
|
|
#if PIC_ENABLE_READLINE
|
2013-10-17 07:48:50 -04:00
|
|
|
read_line = readline(prompt);
|
2013-10-17 04:08:33 -04:00
|
|
|
if (read_line == NULL) {
|
2013-10-19 23:57:15 -04:00
|
|
|
goto eof;
|
2013-10-17 04:08:33 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
strncpy(line, read_line, LINE_MAX_LENGTH - 1);
|
|
|
|
add_history(read_line);
|
|
|
|
free(read_line);
|
|
|
|
}
|
|
|
|
#else
|
2013-12-18 01:43:13 -05:00
|
|
|
printf("%s", prompt);
|
2013-10-09 04:10:32 -04:00
|
|
|
|
|
|
|
char_index = 0;
|
|
|
|
while ((last_char = getchar()) != '\n') {
|
|
|
|
if (last_char == EOF)
|
|
|
|
goto eof;
|
2013-10-09 04:14:48 -04:00
|
|
|
if (char_index == LINE_MAX_LENGTH)
|
|
|
|
goto overflow;
|
2013-10-09 04:10:32 -04:00
|
|
|
line[char_index++] = last_char;
|
|
|
|
}
|
|
|
|
line[char_index] = '\0';
|
2013-10-17 04:08:33 -04:00
|
|
|
#endif
|
2013-10-09 04:10:32 -04:00
|
|
|
|
2013-10-17 07:48:50 -04:00
|
|
|
if (strlen(code) + strlen(line) >= CODE_MAX_LENGTH)
|
|
|
|
goto overflow;
|
|
|
|
strcat(code, line);
|
|
|
|
|
2013-10-12 00:06:02 -04:00
|
|
|
/* read */
|
2013-10-30 04:06:01 -04:00
|
|
|
n = pic_parse_cstr(pic, code, &vs);
|
2013-11-13 03:37:05 -05:00
|
|
|
if (n == PIC_PARSER_INCOMPLETE) { /* wait for more input */
|
2013-10-20 05:17:12 -04:00
|
|
|
goto next;
|
2013-10-17 07:48:50 -04:00
|
|
|
}
|
|
|
|
code[0] = '\0';
|
2013-11-13 03:37:05 -05:00
|
|
|
if (n == PIC_PARSER_ERROR) { /* parse error */
|
2013-10-20 05:17:12 -04:00
|
|
|
goto next;
|
2013-10-17 07:48:50 -04:00
|
|
|
}
|
2013-10-09 04:10:32 -04:00
|
|
|
|
2013-10-30 04:06:01 -04:00
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
v = pic_car(pic, vs);
|
|
|
|
|
2013-10-14 20:08:10 -04:00
|
|
|
#if DEBUG
|
2013-10-30 11:29:55 -04:00
|
|
|
printf("[read: ");
|
|
|
|
pic_debug(pic, v);
|
|
|
|
printf("]\n");
|
2013-10-14 20:08:10 -04:00
|
|
|
#endif
|
|
|
|
|
2013-10-30 04:06:01 -04:00
|
|
|
/* eval */
|
|
|
|
proc = pic_codegen(pic, v);
|
|
|
|
if (proc == NULL) {
|
|
|
|
printf("compilation error: %s\n", pic->errmsg);
|
|
|
|
pic->errmsg = NULL;
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
v = pic_apply(pic, proc, pic_nil_value());
|
|
|
|
if (pic_undef_p(v)) {
|
|
|
|
printf("runtime error: %s\n", pic->errmsg);
|
|
|
|
pic->errmsg = NULL;
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print */
|
|
|
|
printf("=> ");
|
|
|
|
pic_debug(pic, v);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
vs = pic_cdr(pic, vs);
|
2013-10-20 10:30:01 -04:00
|
|
|
}
|
2013-10-12 00:06:02 -04:00
|
|
|
|
2013-10-20 05:17:12 -04:00
|
|
|
next:
|
2013-10-14 05:29:30 -04:00
|
|
|
pic_gc_arena_restore(pic, ai);
|
2013-10-09 04:10:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
eof:
|
|
|
|
puts("");
|
2013-12-07 00:15:21 -05:00
|
|
|
exit_status = 0;
|
|
|
|
return;
|
2013-10-09 04:14:48 -04:00
|
|
|
|
|
|
|
overflow:
|
|
|
|
puts("** [fatal] line input overflow");
|
2013-12-07 00:15:21 -05:00
|
|
|
exit_status = 1;
|
|
|
|
return;
|
2013-10-28 09:08:03 -04:00
|
|
|
}
|
2013-10-09 04:10:32 -04:00
|
|
|
|
2013-12-07 00:15:21 -05:00
|
|
|
void
|
2013-10-28 09:08:03 -04:00
|
|
|
exec_file(pic_state *pic, const char *fname)
|
|
|
|
{
|
|
|
|
FILE *file;
|
2013-11-04 13:18:16 -05:00
|
|
|
int n, i;
|
|
|
|
pic_value vs;
|
2013-10-28 09:08:03 -04:00
|
|
|
struct pic_proc *proc;
|
|
|
|
|
|
|
|
file = fopen(fname, "r");
|
|
|
|
if (file == NULL) {
|
|
|
|
fprintf(stderr, "fatal error: could not read %s\n", fname);
|
2013-12-07 00:15:21 -05:00
|
|
|
goto abort;
|
2013-10-28 09:08:03 -04:00
|
|
|
}
|
|
|
|
|
2013-11-04 13:18:16 -05:00
|
|
|
n = pic_parse_file(pic, file, &vs);
|
|
|
|
if (n <= 0) {
|
2013-10-28 09:08:03 -04:00
|
|
|
fprintf(stderr, "fatal error: %s broken\n", fname);
|
2013-12-07 00:15:21 -05:00
|
|
|
goto abort;
|
2013-10-28 09:08:03 -04:00
|
|
|
}
|
|
|
|
|
2013-11-04 13:18:16 -05:00
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
pic_value v;
|
2013-10-28 09:08:03 -04:00
|
|
|
|
2013-11-04 13:18:16 -05:00
|
|
|
v = pic_car(pic, vs);
|
|
|
|
|
|
|
|
proc = pic_codegen(pic, v);
|
|
|
|
if (proc == NULL) {
|
|
|
|
fputs(pic->errmsg, stderr);
|
|
|
|
fprintf(stderr, "fatal error: %s compilation failure\n", fname);
|
2013-12-07 00:15:21 -05:00
|
|
|
goto abort;
|
2013-11-04 13:18:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
v = pic_apply(pic, proc, pic_nil_value());
|
|
|
|
if (pic_undef_p(v)) {
|
|
|
|
fputs(pic->errmsg, stderr);
|
|
|
|
fprintf(stderr, "fatal error: %s evaluation failure\n", fname);
|
2013-12-07 00:15:21 -05:00
|
|
|
goto abort;
|
2013-11-04 13:18:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
vs = pic_cdr(pic, vs);
|
2013-10-28 09:08:03 -04:00
|
|
|
}
|
2013-10-10 03:15:41 -04:00
|
|
|
|
2013-12-07 00:15:21 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
abort:
|
|
|
|
exit_status = 1;
|
|
|
|
return;
|
2013-10-09 03:58:35 -04:00
|
|
|
}
|
2013-10-28 09:08:03 -04:00
|
|
|
|
2013-12-07 00:15:21 -05:00
|
|
|
void
|
2013-12-05 03:10:28 -05:00
|
|
|
exec_string(pic_state *pic, const char *str)
|
|
|
|
{
|
|
|
|
int n, i;
|
|
|
|
pic_value vs, v;
|
|
|
|
struct pic_proc *proc;
|
|
|
|
int ai = pic_gc_arena_preserve(pic);
|
|
|
|
|
|
|
|
n = pic_parse_cstr(pic, str, &vs);
|
|
|
|
if (n < 0) {
|
2013-12-07 00:15:21 -05:00
|
|
|
goto abort;
|
2013-12-05 03:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
v = pic_car(pic, vs);
|
|
|
|
|
|
|
|
proc = pic_codegen(pic, v);
|
|
|
|
if (proc == NULL) {
|
2013-12-07 00:15:21 -05:00
|
|
|
goto abort;
|
2013-12-05 03:10:28 -05:00
|
|
|
}
|
|
|
|
v = pic_apply(pic, proc, pic_nil_value());
|
|
|
|
if (pic_undef_p(v)) {
|
2013-12-07 00:15:21 -05:00
|
|
|
goto abort;
|
2013-12-05 03:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
vs = pic_cdr(pic, vs);
|
|
|
|
|
|
|
|
pic_gc_arena_restore(pic, ai);
|
|
|
|
}
|
|
|
|
|
2013-12-07 00:15:21 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
abort:
|
|
|
|
exit_status = 1;
|
|
|
|
return;
|
2013-12-05 03:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *fname;
|
2013-12-07 00:11:15 -05:00
|
|
|
static char *script;
|
2013-12-05 03:10:28 -05:00
|
|
|
|
|
|
|
enum {
|
|
|
|
NO_MODE = 0,
|
|
|
|
INTERACTIVE_MODE,
|
|
|
|
FILE_EXEC_MODE,
|
|
|
|
ONE_LINER_MODE,
|
|
|
|
} mode;
|
|
|
|
|
|
|
|
void
|
|
|
|
parse_opt(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
while (~(r = getopt(argc, argv, "he:"))) {
|
|
|
|
switch (r) {
|
|
|
|
case 'h':
|
|
|
|
print_help();
|
|
|
|
exit(0);
|
|
|
|
case 'e':
|
2013-12-07 00:11:15 -05:00
|
|
|
script = optarg;
|
2013-12-05 03:10:28 -05:00
|
|
|
mode = ONE_LINER_MODE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
if (mode == NO_MODE)
|
|
|
|
mode = INTERACTIVE_MODE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fname = argv[0];
|
|
|
|
mode = FILE_EXEC_MODE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-28 09:08:03 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[], char **envp)
|
|
|
|
{
|
|
|
|
pic_state *pic;
|
|
|
|
|
|
|
|
pic = pic_open(argc, argv, envp);
|
|
|
|
|
2013-12-05 03:10:28 -05:00
|
|
|
parse_opt(argc, argv);
|
|
|
|
|
2014-01-08 01:38:47 -05:00
|
|
|
if (mode == INTERACTIVE_MODE || mode == ONE_LINER_MODE) {
|
|
|
|
import_repllib(pic);
|
|
|
|
}
|
|
|
|
|
2013-12-05 03:10:28 -05:00
|
|
|
switch (mode) {
|
|
|
|
case NO_MODE:
|
|
|
|
puts("logic flaw");
|
|
|
|
abort();
|
|
|
|
case INTERACTIVE_MODE:
|
2013-12-07 00:15:21 -05:00
|
|
|
repl(pic);
|
2013-12-05 03:10:28 -05:00
|
|
|
break;
|
|
|
|
case FILE_EXEC_MODE:
|
2013-12-07 00:15:21 -05:00
|
|
|
exec_file(pic, fname);
|
2013-12-05 03:10:28 -05:00
|
|
|
break;
|
|
|
|
case ONE_LINER_MODE:
|
2013-12-07 00:15:21 -05:00
|
|
|
exec_string(pic, script);
|
2013-12-05 03:10:28 -05:00
|
|
|
break;
|
2013-10-28 09:08:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pic_close(pic);
|
|
|
|
|
2014-01-16 08:03:00 -05:00
|
|
|
#if DEBUG
|
|
|
|
puts("* picrin successfully closed");
|
|
|
|
#endif
|
|
|
|
|
2013-12-07 00:15:21 -05:00
|
|
|
return exit_status;
|
2013-10-28 09:08:03 -04:00
|
|
|
}
|