Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
pic_catch binds current error object to a variable
Browse files Browse the repository at this point in the history
  • Loading branch information
nyuichi committed Feb 25, 2016
1 parent 73e3865 commit 8a9a120
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 20 deletions.
6 changes: 4 additions & 2 deletions etc/mkloader.pl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
void
pic_load_piclib(pic_state *pic)
{
pic_value e;
EOL

foreach my $file (@ARGV) {
Expand All @@ -51,10 +53,10 @@
print " pic_load_cstr(pic, &${var}[0][0]);\n";
print<<EOL
}
pic_catch {
pic_catch(e) {
/* error! */
xfputs(pic, "fatal error: failure in loading $dirname/$basename\\n", xstderr);
pic_raise(pic, pic_err(pic));
pic_raise(pic, e);
}
EOL
}
Expand Down
6 changes: 3 additions & 3 deletions extlib/benz/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1083,17 +1083,17 @@ pic_value
pic_eval(pic_state *pic, pic_value program, const char *lib)
{
const char *prev_lib = pic_current_library(pic);
pic_value env, r;
pic_value env, r, e;

env = pic_library_environment(pic, lib);

pic_in_library(pic, lib);
pic_try {
r = pic_call(pic, pic_compile(pic, pic_expand(pic, program, env)), 0);
}
pic_catch {
pic_catch(e) {
pic_in_library(pic, prev_lib);
pic_raise(pic, pic_err(pic));
pic_raise(pic, e);
}
pic_in_library(pic, prev_lib);

Expand Down
8 changes: 4 additions & 4 deletions extlib/benz/include/picrin/extra.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,21 @@ xFILE *xfopen_null(pic_state *, const char *mode);
do { \
extern pic_value pic_start_try(pic_state *, PIC_JMPBUF *); \
extern void pic_end_try(pic_state *, pic_value); \
extern pic_value pic_err(pic_state *); \
PIC_JMPBUF jmp; \
if (PIC_SETJMP(pic, jmp) == 0) { \
pic_value pic_try_cookie_ = pic_start_try(pic, &jmp);
#define pic_catch pic_catch_(PIC_GENSYM(label))
#define pic_catch_(label) \
#define pic_catch(e) pic_catch_(e, PIC_GENSYM(label))
#define pic_catch_(e, label) \
pic_end_try(pic, pic_try_cookie_); \
} else { \
e = pic_err(pic); \
goto label; \
} \
} while (0); \
if (0) \
label:

pic_value pic_err(pic_state *);

/* for debug */

void pic_warnf(pic_state *, const char *, ...);
Expand Down
6 changes: 3 additions & 3 deletions extlib/benz/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ pic_load(pic_state *pic, pic_value port)
void
pic_load_cstr(pic_state *pic, const char *str)
{
pic_value port = pic_open_port(pic, xfopen_buf(pic, str, strlen(str), "r"));
pic_value e, port = pic_open_port(pic, xfopen_buf(pic, str, strlen(str), "r"));

pic_try {
pic_load(pic, port);
}
pic_catch {
pic_catch(e) {
pic_close_port(pic, port);
pic_raise(pic, pic_err(pic));
pic_raise(pic, e);
}

pic_close_port(pic, port);
Expand Down
4 changes: 2 additions & 2 deletions extlib/benz/number.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ pic_number_string_to_number(pic_state *pic)
int radix = 10;
long num;
char *eptr;
pic_value flo;
pic_value flo, e;

pic_get_args(pic, "z|i", &str, &radix);

Expand All @@ -269,7 +269,7 @@ pic_number_string_to_number(pic_state *pic)
pic_try {
flo = pic_read_cstr(pic, str);
}
pic_catch {
pic_catch(e) {
/* swallow error */
flo = pic_false_value(pic);
}
Expand Down
11 changes: 6 additions & 5 deletions extlib/benz/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ pic_read(pic_state *pic, pic_value port)
pic_value val;
xFILE *file = pic_fileno(pic, port);
int c;
pic_value e;

reader_init(pic, &p);

Expand All @@ -852,9 +853,9 @@ pic_read(pic_state *pic, pic_value port)
val = pic_eof_object(pic);
}
}
pic_catch {
pic_catch(e) {
reader_destroy(pic, &p);
pic_raise(pic, pic_err(pic));
pic_raise(pic, e);
}

pic_leave(pic, ai);
Expand All @@ -865,14 +866,14 @@ pic_value
pic_read_cstr(pic_state *pic, const char *str)
{
pic_value port = pic_open_port(pic, xfopen_buf(pic, str, strlen(str), "r"));
pic_value form;
pic_value form, e;

pic_try {
form = pic_read(pic, port);
}
pic_catch {
pic_catch(e) {
pic_close_port(pic, port);
pic_raise(pic, pic_err(pic));
pic_raise(pic, e);
}

pic_close_port(pic, port);
Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ main(int argc, char *argv[], char **envp)
{
char t;
pic_state *pic;
pic_value e;
int status;

pic = pic_open(pic_default_allocf, NULL);
Expand All @@ -43,7 +44,7 @@ main(int argc, char *argv[], char **envp)

status = 0;
}
pic_catch {
pic_catch(e) {
pic_print_error(pic, xstderr);
status = 1;
}
Expand Down

0 comments on commit 8a9a120

Please sign in to comment.