picrin/lib/ext/main.c

62 lines
1.5 KiB
C
Raw Normal View History

2017-04-14 10:58:02 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
#include "picrin/extra.h"
int
main(int argc, char *argv[])
{
pic_state *pic;
pic_value e, port;
pic = pic_open(pic_default_allocf, NULL);
pic_try {
if (argc == 1) { /* repl */
while (1) {
pic_printf(pic, "> ");
e = pic_funcall(pic, "read", 0);
2017-04-14 10:58:02 -04:00
if (pic_eof_p(pic, e))
break;
pic_printf(pic, "~s\n", pic_funcall(pic, "eval", 1, e));
}
} else if (argc == 2) { /* load file */
FILE *file = fopen(argv[1], "r");
if (! file) {
fprintf(stderr, "could not open file %s\n", argv[1]);
exit(1);
}
port = pic_fopen(pic, file, "r");
while (1) {
e = pic_funcall(pic, "read", 1, port);
2017-04-14 10:58:02 -04:00
if (pic_eof_p(pic, e))
break;
pic_void(pic, pic_funcall(pic, "eval", 1, e));
}
2017-04-22 20:16:40 -04:00
} else if (argc >= 3 && strcmp(argv[1], "-c") == 0) { /* compile */
const char *name = argv[2];
if (argc == 3) {
2017-04-14 10:58:02 -04:00
port = pic_stdin(pic);
} else {
2017-04-22 20:16:40 -04:00
FILE *file = fopen(argv[3], "r");
2017-04-14 10:58:02 -04:00
if (! file) {
2017-04-22 20:16:40 -04:00
fprintf(stderr, "could not open file %s\n", argv[3]);
2017-04-14 10:58:02 -04:00
exit(1);
}
port = pic_fopen(pic, file, "r");
}
2017-04-22 20:16:40 -04:00
pic_serialize(pic, name, pic_assemble(pic, pic_funcall(pic, "compile", 1, pic_funcall(pic, "read", 1, port))));
2017-04-14 10:58:02 -04:00
} else {
fprintf(stderr, "usage: mini-picrin [-c] [file]\n");
exit(1);
}
}
pic_catch(e) {
pic_print_error(pic, pic_stderr(pic), e);
}
pic_close(pic);
}