2014-09-08 05:47:20 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
|
|
|
#include "picrin/pair.h"
|
2014-09-08 05:50:15 -04:00
|
|
|
#include "picrin/port.h"
|
|
|
|
#include "picrin/error.h"
|
2014-09-08 05:47:20 -04:00
|
|
|
|
2014-09-08 05:50:15 -04:00
|
|
|
static void
|
|
|
|
pic_load_port(pic_state *pic, struct pic_port *port)
|
2014-09-08 05:47:20 -04:00
|
|
|
{
|
2014-09-08 05:50:15 -04:00
|
|
|
pic_value form;
|
2014-09-08 05:47:20 -04:00
|
|
|
|
2014-09-08 05:50:15 -04:00
|
|
|
pic_try {
|
|
|
|
size_t ai = pic_gc_arena_preserve(pic);
|
2014-09-08 05:47:20 -04:00
|
|
|
|
2014-09-08 05:50:15 -04:00
|
|
|
while (! pic_eof_p(form = pic_read(pic, port))) {
|
|
|
|
pic_eval(pic, form, pic->lib);
|
2014-09-08 05:47:20 -04:00
|
|
|
|
2014-09-08 05:50:15 -04:00
|
|
|
pic_gc_arena_restore(pic, ai);
|
2014-09-08 05:47:20 -04:00
|
|
|
}
|
2014-09-08 05:50:15 -04:00
|
|
|
}
|
|
|
|
pic_catch {
|
|
|
|
pic_errorf(pic, "load error: %s", pic_errmsg(pic));
|
|
|
|
}
|
|
|
|
}
|
2014-09-08 05:47:20 -04:00
|
|
|
|
2014-09-08 05:50:15 -04:00
|
|
|
void
|
|
|
|
pic_load_cstr(pic_state *pic, const char *src)
|
|
|
|
{
|
|
|
|
struct pic_port *port = pic_open_input_string(pic, src);
|
2014-09-08 05:47:20 -04:00
|
|
|
|
2014-09-08 05:50:15 -04:00
|
|
|
pic_load_port(pic, port);
|
2014-09-08 05:47:20 -04:00
|
|
|
|
2014-09-08 05:50:15 -04:00
|
|
|
pic_close_port(pic, port);
|
2014-09-08 05:47:20 -04:00
|
|
|
}
|
|
|
|
|
2014-09-08 05:50:15 -04:00
|
|
|
void
|
|
|
|
pic_load(pic_state *pic, const char *filename)
|
2014-09-08 05:47:20 -04:00
|
|
|
{
|
2014-09-08 05:50:15 -04:00
|
|
|
struct pic_port *port;
|
|
|
|
xFILE *file;
|
2014-09-08 05:47:20 -04:00
|
|
|
|
2014-09-08 05:50:15 -04:00
|
|
|
file = xfopen(filename, "r");
|
2014-09-08 05:47:20 -04:00
|
|
|
if (file == NULL) {
|
2014-09-08 05:50:15 -04:00
|
|
|
pic_errorf(pic, "could not open file: %s", filename);
|
2014-09-08 05:47:20 -04:00
|
|
|
}
|
|
|
|
|
2014-09-08 05:50:15 -04:00
|
|
|
port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT);
|
|
|
|
port->file = file;
|
|
|
|
port->flags = PIC_PORT_IN | PIC_PORT_TEXT;
|
|
|
|
port->status = PIC_PORT_OPEN;
|
2014-09-08 05:47:20 -04:00
|
|
|
|
2014-09-08 05:50:15 -04:00
|
|
|
pic_load_port(pic, port);
|
2014-09-08 05:47:20 -04:00
|
|
|
|
2014-09-08 05:50:15 -04:00
|
|
|
pic_close_port(pic, port);
|
2014-09-08 05:47:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_load_load(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value envid;
|
|
|
|
char *fn;
|
|
|
|
|
|
|
|
pic_get_args(pic, "z|o", &fn, &envid);
|
|
|
|
|
2014-09-08 05:50:15 -04:00
|
|
|
pic_load(pic, fn);
|
|
|
|
|
|
|
|
return pic_none_value();
|
2014-09-08 05:47:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_init_load(pic_state *pic)
|
|
|
|
{
|
2014-09-08 06:38:19 -04:00
|
|
|
pic_defun(pic, "load", pic_load_load);
|
2014-09-08 05:47:20 -04:00
|
|
|
}
|