From e841764b6875ab409a4b1b7008bc55efc03c6f49 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Wed, 26 Aug 2015 22:21:34 +0900 Subject: [PATCH 1/3] introduce PTR_CASE macro --- extlib/benz/vm.c | 161 +++++++++-------------------------------------- 1 file changed, 31 insertions(+), 130 deletions(-) diff --git a/extlib/benz/vm.c b/extlib/benz/vm.c index 23b3f26e..5faa602b 100644 --- a/extlib/benz/vm.c +++ b/extlib/benz/vm.c @@ -26,12 +26,12 @@ pic_get_proc(pic_state *pic) * I int *, bool * int with exactness * f double * float * F double *, bool * float with exactness - * s pic_str ** string object + * c char * char * z char ** c string + * s pic_str ** string object * m pic_sym ** symbol * v pic_vec ** vector object * b pic_blob ** bytevector object - * c char * char * l struct pic_proc ** lambda object * p struct pic_port ** port object * d struct pic_dict ** dictionary object @@ -186,17 +186,17 @@ pic_get_args(pic_state *pic, const char *format, ...) } break; } - case 's': { - pic_str **str; + case 'c': { + char *k; pic_value v; - str = va_arg(ap, pic_str **); + k = va_arg(ap, char *); v = GET_OPERAND(pic,i); - if (pic_str_p(v)) { - *str = pic_str_ptr(v); + if (pic_char_p(v)) { + *k = pic_char(v); } else { - pic_errorf(pic, "pic_get_args: expected string, but got ~s", v); + pic_errorf(pic, "pic_get_args: expected char, but got ~s", v); } break; } @@ -214,132 +214,33 @@ pic_get_args(pic_state *pic, const char *format, ...) } break; } - case 'm': { - pic_sym **m; - pic_value v; - m = va_arg(ap, pic_sym **); - v = GET_OPERAND(pic,i); - if (pic_sym_p(v)) { - *m = pic_sym_ptr(v); +#define PTR_CASE(c, type, ctype) \ + case c: { \ + ctype *ptr; \ + pic_value v; \ + \ + ptr = va_arg(ap, ctype *); \ + v = GET_OPERAND(pic, i); \ + if (pic_## type ##_p(v)) { \ + *ptr = pic_## type ##_ptr(v); \ + } \ + else { \ + pic_errorf(pic, "pic_get_args: expected " #type ", but got ~s", v); \ + } \ + break; \ } - else { - pic_errorf(pic, "pic_get_args: expected symbol, but got ~s", v); - } - break; - } - case 'v': { - struct pic_vector **vec; - pic_value v; - vec = va_arg(ap, struct pic_vector **); - v = GET_OPERAND(pic,i); - if (pic_vec_p(v)) { - *vec = pic_vec_ptr(v); - } - else { - pic_errorf(pic, "pic_get_args: expected vector, but got ~s", v); - } - break; - } - case 'b': { - struct pic_blob **b; - pic_value v; + PTR_CASE('s', str, pic_str *) + PTR_CASE('m', sym, pic_sym *) + PTR_CASE('v', vec, pic_vec *) + PTR_CASE('b', blob, pic_blob *) + PTR_CASE('l', proc, struct pic_proc *) + PTR_CASE('p', port, struct pic_port *) + PTR_CASE('d', dict, struct pic_dict *) + PTR_CASE('r', record, struct pic_record *) + PTR_CASE('e', error, struct pic_error *) - b = va_arg(ap, struct pic_blob **); - v = GET_OPERAND(pic,i); - if (pic_blob_p(v)) { - *b = pic_blob_ptr(v); - } - else { - pic_errorf(pic, "pic_get_args: expected bytevector, but got ~s", v); - } - break; - } - case 'c': { - char *k; - pic_value v; - - k = va_arg(ap, char *); - v = GET_OPERAND(pic,i); - if (pic_char_p(v)) { - *k = pic_char(v); - } - else { - pic_errorf(pic, "pic_get_args: expected char, but got ~s", v); - } - break; - } - case 'l': { - struct pic_proc **l; - pic_value v; - - l = va_arg(ap, struct pic_proc **); - v = GET_OPERAND(pic,i); - if (pic_proc_p(v)) { - *l = pic_proc_ptr(v); - } - else { - pic_errorf(pic, "pic_get_args, expected procedure, but got ~s", v); - } - break; - } - case 'p': { - struct pic_port **p; - pic_value v; - - p = va_arg(ap, struct pic_port **); - v = GET_OPERAND(pic,i); - if (pic_port_p(v)) { - *p = pic_port_ptr(v); - } - else { - pic_errorf(pic, "pic_get_args, expected port, but got ~s", v); - } - break; - } - case 'd': { - struct pic_dict **d; - pic_value v; - - d = va_arg(ap, struct pic_dict **); - v = GET_OPERAND(pic,i); - if (pic_dict_p(v)) { - *d = pic_dict_ptr(v); - } - else { - pic_errorf(pic, "pic_get_args, expected dictionary, but got ~s", v); - } - break; - } - case 'r': { - struct pic_record **r; - pic_value v; - - r = va_arg(ap, struct pic_record **); - v = GET_OPERAND(pic,i); - if (pic_record_p(v)) { - *r = pic_record_ptr(v); - } - else { - pic_errorf(pic, "pic_get_args: expected record, but got ~s", v); - } - break; - } - case 'e': { - struct pic_error **e; - pic_value v; - - e = va_arg(ap, struct pic_error **); - v = GET_OPERAND(pic,i); - if (pic_error_p(v)) { - *e = pic_error_ptr(v); - } - else { - pic_errorf(pic, "pic_get_args, expected error"); - } - break; - } default: pic_errorf(pic, "pic_get_args: invalid argument specifier '%c' given", c); } From 210fecab3f58ab667ac12d759e2ed7e8bfb94f48 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Wed, 26 Aug 2015 22:25:56 +0900 Subject: [PATCH 2/3] unify 'i' and 'I', and 'f' and 'F' --- extlib/benz/vm.c | 52 +++++++----------------------------------------- 1 file changed, 7 insertions(+), 45 deletions(-) diff --git a/extlib/benz/vm.c b/extlib/benz/vm.c index 5faa602b..f43a4c19 100644 --- a/extlib/benz/vm.c +++ b/extlib/benz/vm.c @@ -102,32 +102,13 @@ pic_get_args(pic_state *pic, const char *format, ...) *p = GET_OPERAND(pic,i); break; } - case 'f': { + case 'f': case 'F': { double *f; + bool *e, dummy; pic_value v; f = va_arg(ap, double *); - - v = GET_OPERAND(pic, i); - switch (pic_type(v)) { - case PIC_TT_FLOAT: - *f = pic_float(v); - break; - case PIC_TT_INT: - *f = pic_int(v); - break; - default: - pic_errorf(pic, "pic_get_args: expected float or int, but got ~s", v); - } - break; - } - case 'F': { - double *f; - bool *e; - pic_value v; - - f = va_arg(ap, double *); - e = va_arg(ap, bool *); + e = (c == 'F' ? va_arg(ap, bool *) : &dummy); v = GET_OPERAND(pic, i); switch (pic_type(v)) { @@ -144,18 +125,18 @@ pic_get_args(pic_state *pic, const char *format, ...) } break; } - case 'I': { + case 'i': case 'I': { int *k; - bool *e; + bool *e, dummy; pic_value v; k = va_arg(ap, int *); - e = va_arg(ap, bool *); + e = (c == 'I' ? va_arg(ap, bool *) : &dummy); v = GET_OPERAND(pic, i); switch (pic_type(v)) { case PIC_TT_FLOAT: - *k = (int)pic_float(v); + *k = pic_float(v); *e = false; break; case PIC_TT_INT: @@ -167,25 +148,6 @@ pic_get_args(pic_state *pic, const char *format, ...) } break; } - case 'i': { - int *k; - pic_value v; - - k = va_arg(ap, int *); - - v = GET_OPERAND(pic, i); - switch (pic_type(v)) { - case PIC_TT_FLOAT: - *k = (int)pic_float(v); - break; - case PIC_TT_INT: - *k = pic_int(v); - break; - default: - pic_errorf(pic, "pic_get_args: expected int, but got ~s", v); - } - break; - } case 'c': { char *k; pic_value v; From 333813d0ba31b08c42e4e144185fc92555b55d3b Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Wed, 26 Aug 2015 22:37:40 +0900 Subject: [PATCH 3/3] unify more cases --- extlib/benz/vm.c | 106 +++++++++++++++-------------------------------- 1 file changed, 33 insertions(+), 73 deletions(-) diff --git a/extlib/benz/vm.c b/extlib/benz/vm.c index f43a4c19..c730faad 100644 --- a/extlib/benz/vm.c +++ b/extlib/benz/vm.c @@ -102,82 +102,36 @@ pic_get_args(pic_state *pic, const char *format, ...) *p = GET_OPERAND(pic,i); break; } - case 'f': case 'F': { - double *f; - bool *e, dummy; - pic_value v; - f = va_arg(ap, double *); - e = (c == 'F' ? va_arg(ap, bool *) : &dummy); - - v = GET_OPERAND(pic, i); - switch (pic_type(v)) { - case PIC_TT_FLOAT: - *f = pic_float(v); - *e = false; - break; - case PIC_TT_INT: - *f = pic_int(v); - *e = true; - break; - default: - pic_errorf(pic, "pic_get_args: expected float or int, but got ~s", v); +#define NUM_CASE(c1, c2, ctype) \ + case c1: case c2: { \ + ctype *n; \ + bool *e, dummy; \ + pic_value v; \ + \ + n = va_arg(ap, ctype *); \ + e = (c == c2 ? va_arg(ap, bool *) : &dummy); \ + \ + v = GET_OPERAND(pic, i); \ + switch (pic_type(v)) { \ + case PIC_TT_FLOAT: \ + *n = pic_float(v); \ + *e = false; \ + break; \ + case PIC_TT_INT: \ + *n = pic_int(v); \ + *e = true; \ + break; \ + default: \ + pic_errorf(pic, "pic_get_args: expected float or int, but got ~s", v); \ + } \ + break; \ } - break; - } - case 'i': case 'I': { - int *k; - bool *e, dummy; - pic_value v; - k = va_arg(ap, int *); - e = (c == 'I' ? va_arg(ap, bool *) : &dummy); + NUM_CASE('i', 'I', int) + NUM_CASE('f', 'F', double) - v = GET_OPERAND(pic, i); - switch (pic_type(v)) { - case PIC_TT_FLOAT: - *k = pic_float(v); - *e = false; - break; - case PIC_TT_INT: - *k = pic_int(v); - *e = true; - break; - default: - pic_errorf(pic, "pic_get_args: expected float or int, but got ~s", v); - } - break; - } - case 'c': { - char *k; - pic_value v; - - k = va_arg(ap, char *); - v = GET_OPERAND(pic,i); - if (pic_char_p(v)) { - *k = pic_char(v); - } - else { - pic_errorf(pic, "pic_get_args: expected char, but got ~s", v); - } - break; - } - case 'z': { - const char **cstr; - pic_value v; - - cstr = va_arg(ap, const char **); - v = GET_OPERAND(pic,i); - if (pic_str_p(v)) { - *cstr = pic_str_cstr(pic, pic_str_ptr(v)); - } - else { - pic_errorf(pic, "pic_get_args: expected string, but got ~s", v); - } - break; - } - -#define PTR_CASE(c, type, ctype) \ +#define VAL_CASE(c, type, ctype, conv) \ case c: { \ ctype *ptr; \ pic_value v; \ @@ -185,7 +139,7 @@ pic_get_args(pic_state *pic, const char *format, ...) ptr = va_arg(ap, ctype *); \ v = GET_OPERAND(pic, i); \ if (pic_## type ##_p(v)) { \ - *ptr = pic_## type ##_ptr(v); \ + *ptr = conv; \ } \ else { \ pic_errorf(pic, "pic_get_args: expected " #type ", but got ~s", v); \ @@ -193,6 +147,12 @@ pic_get_args(pic_state *pic, const char *format, ...) break; \ } + VAL_CASE('c', char, char, pic_char(v)) + VAL_CASE('z', str, const char *, pic_str_cstr(pic, pic_str_ptr(v))) + +#define PTR_CASE(c, type, ctype) \ + VAL_CASE(c, type, ctype, pic_## type ##_ptr(v)) + PTR_CASE('s', str, pic_str *) PTR_CASE('m', sym, pic_sym *) PTR_CASE('v', vec, pic_vec *)