Merge pull request #249 from picrin-scheme/better-error-message2
Better error message2
This commit is contained in:
commit
9ee2227d06
|
@ -555,9 +555,13 @@ analyze_quote(analyze_state *state, pic_value obj)
|
|||
return pic_list2(pic, pic_obj_value(pic->sQUOTE), pic_list_ref(pic, obj, 1));
|
||||
}
|
||||
|
||||
#define ARGC_ASSERT_GE(n) do { \
|
||||
|
||||
#define ARGC_ASSERT_GE(n, name) do { \
|
||||
if (pic_length(pic, obj) < (n) + 1) { \
|
||||
pic_errorf(pic, "wrong number of arguments"); \
|
||||
pic_errorf(pic, \
|
||||
#name ": wrong number of arguments (%d for at least %d)", \
|
||||
pic_length(pic, obj) - 1, \
|
||||
n); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
@ -569,13 +573,14 @@ analyze_quote(analyze_state *state, pic_value obj)
|
|||
} \
|
||||
} while (0)
|
||||
|
||||
|
||||
static pic_value
|
||||
analyze_add(analyze_state *state, pic_value obj, bool tailpos)
|
||||
{
|
||||
pic_state *pic = state->pic;
|
||||
pic_value args, arg, it;
|
||||
|
||||
ARGC_ASSERT_GE(0);
|
||||
ARGC_ASSERT_GE(0, "+");
|
||||
switch (pic_length(pic, obj)) {
|
||||
case 1:
|
||||
return pic_list2(pic, pic_obj_value(pic->sQUOTE), pic_int_value(0));
|
||||
|
@ -594,7 +599,7 @@ analyze_sub(analyze_state *state, pic_value obj)
|
|||
pic_state *pic = state->pic;
|
||||
pic_value args, arg, it;
|
||||
|
||||
ARGC_ASSERT_GE(1);
|
||||
ARGC_ASSERT_GE(1, "-");
|
||||
switch (pic_length(pic, obj)) {
|
||||
case 2:
|
||||
return pic_list2(pic, pic_obj_value(pic->sMINUS),
|
||||
|
@ -612,7 +617,7 @@ analyze_mul(analyze_state *state, pic_value obj, bool tailpos)
|
|||
pic_state *pic = state->pic;
|
||||
pic_value args, arg, it;
|
||||
|
||||
ARGC_ASSERT_GE(0);
|
||||
ARGC_ASSERT_GE(0, "*");
|
||||
switch (pic_length(pic, obj)) {
|
||||
case 1:
|
||||
return pic_list2(pic, pic_obj_value(pic->sQUOTE), pic_int_value(1));
|
||||
|
@ -631,7 +636,7 @@ analyze_div(analyze_state *state, pic_value obj)
|
|||
pic_state *pic = state->pic;
|
||||
pic_value args, arg, it;
|
||||
|
||||
ARGC_ASSERT_GE(1);
|
||||
ARGC_ASSERT_GE(1, "/");
|
||||
switch (pic_length(pic, obj)) {
|
||||
case 2:
|
||||
args = pic_cdr(pic, obj);
|
||||
|
@ -688,7 +693,7 @@ analyze_call_with_values(analyze_state *state, pic_value obj, bool tailpos)
|
|||
pic_sym *call;
|
||||
|
||||
if (pic_length(pic, obj) != 3) {
|
||||
pic_errorf(pic, "wrong number of arguments");
|
||||
pic_errorf(pic, "call-with-values: wrong number of arguments (%d for 2)", pic_length(pic, obj) - 1);
|
||||
}
|
||||
|
||||
if (! tailpos) {
|
||||
|
@ -701,9 +706,10 @@ analyze_call_with_values(analyze_state *state, pic_value obj, bool tailpos)
|
|||
return pic_list3(pic, pic_obj_value(call), prod, cnsm);
|
||||
}
|
||||
|
||||
#define ARGC_ASSERT(n) do { \
|
||||
#define ARGC_ASSERT(n, name) do { \
|
||||
if (pic_length(pic, obj) != (n) + 1) { \
|
||||
pic_errorf(pic, "wrong number of arguments"); \
|
||||
pic_errorf(pic, #name ": wrong number of arguments (%d for %d)", \
|
||||
pic_length(pic, obj) - 1, n); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
@ -724,6 +730,7 @@ analyze_call_with_values(analyze_state *state, pic_value obj, bool tailpos)
|
|||
analyze(state, pic_list_ref(pic, obj, 1), false), \
|
||||
analyze(state, pic_list_ref(pic, obj, 2), false))
|
||||
|
||||
|
||||
static pic_value
|
||||
analyze_node(analyze_state *state, pic_value obj, bool tailpos)
|
||||
{
|
||||
|
@ -763,27 +770,27 @@ analyze_node(analyze_state *state, pic_value obj, bool tailpos)
|
|||
return analyze_quote(state, obj);
|
||||
}
|
||||
else if (sym == state->rCONS) {
|
||||
ARGC_ASSERT(2);
|
||||
ARGC_ASSERT(2, "cons");
|
||||
return CONSTRUCT_OP2(pic->sCONS);
|
||||
}
|
||||
else if (sym == state->rCAR) {
|
||||
ARGC_ASSERT(1);
|
||||
ARGC_ASSERT(1, "car");
|
||||
return CONSTRUCT_OP1(pic->sCAR);
|
||||
}
|
||||
else if (sym == state->rCDR) {
|
||||
ARGC_ASSERT(1);
|
||||
ARGC_ASSERT(1, "cdr");
|
||||
return CONSTRUCT_OP1(pic->sCDR);
|
||||
}
|
||||
else if (sym == state->rNILP) {
|
||||
ARGC_ASSERT(1);
|
||||
ARGC_ASSERT(1, "nil?");
|
||||
return CONSTRUCT_OP1(pic->sNILP);
|
||||
}
|
||||
else if (sym == state->rSYMBOLP) {
|
||||
ARGC_ASSERT(1);
|
||||
ARGC_ASSERT(1, "symbol?");
|
||||
return CONSTRUCT_OP1(pic->sSYMBOLP);
|
||||
}
|
||||
else if (sym == state->rPAIRP) {
|
||||
ARGC_ASSERT(1);
|
||||
ARGC_ASSERT(1, "pair?");
|
||||
return CONSTRUCT_OP1(pic->sPAIRP);
|
||||
}
|
||||
else if (sym == state->rADD) {
|
||||
|
@ -819,7 +826,7 @@ analyze_node(analyze_state *state, pic_value obj, bool tailpos)
|
|||
return CONSTRUCT_OP2(pic->sGE);
|
||||
}
|
||||
else if (sym == state->rNOT) {
|
||||
ARGC_ASSERT(1);
|
||||
ARGC_ASSERT(1, "not");
|
||||
return CONSTRUCT_OP1(pic->sNOT);
|
||||
}
|
||||
else if (sym == state->rVALUES) {
|
||||
|
|
138
extlib/benz/vm.c
138
extlib/benz/vm.c
|
@ -58,50 +58,71 @@ int
|
|||
pic_get_args(pic_state *pic, const char *format, ...)
|
||||
{
|
||||
char c;
|
||||
int i = 1, argc = pic->ci->argc;
|
||||
size_t paramc, optc, min;
|
||||
size_t i , argc = pic->ci->argc - 1;
|
||||
va_list ap;
|
||||
bool opt = false;
|
||||
bool rest = false, opt = false;
|
||||
|
||||
va_start(ap, format);
|
||||
while ((c = *format++)) {
|
||||
switch (c) {
|
||||
default:
|
||||
if (argc <= i && ! opt) {
|
||||
pic_errorf(pic, "wrong number of arguments");
|
||||
}
|
||||
break;
|
||||
case '|':
|
||||
break;
|
||||
case '*':
|
||||
break;
|
||||
}
|
||||
/* paramc: required args count as scheme proc
|
||||
optc: optional args count as scheme proc
|
||||
argc: passed args count as scheme proc
|
||||
vargc: args count passed to this function
|
||||
*/
|
||||
|
||||
/* in order to run out of all arguments passed to this function
|
||||
(i.e. do va_arg for each argument), optional argument existence
|
||||
check is done in every case closure */
|
||||
|
||||
if (c == '*')
|
||||
break;
|
||||
|
||||
switch (c) {
|
||||
case '|':
|
||||
/* check nparams first */
|
||||
for (paramc = 0, c = *format; c; c = format[++paramc]) {
|
||||
if (c == '|') {
|
||||
opt = true;
|
||||
break;
|
||||
}
|
||||
else if (c == '*') {
|
||||
rest = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (optc = 0; opt && c; c = format[paramc + opt + ++optc]) {
|
||||
if (c == '*') {
|
||||
rest = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* '|' should be followed by at least 1 char */
|
||||
assert(opt <= optc);
|
||||
/* '*' should not be followed by any char */
|
||||
assert(format[paramc + opt + optc + rest] == '\0');
|
||||
|
||||
/* check argc. */
|
||||
if (argc < paramc || (paramc + optc < argc && ! rest)) {
|
||||
pic_errorf(pic, "%s: wrong number of arguments (%d for %s%d)",
|
||||
pic_symbol_name(pic, pic_proc_name(pic_proc_ptr(GET_OPERAND(pic, 0)))) ,
|
||||
argc,
|
||||
rest? "at least " : "",
|
||||
paramc);
|
||||
}
|
||||
|
||||
/* start dispatching */
|
||||
va_start(ap, format);
|
||||
min = paramc + optc < argc ? paramc + optc : argc;
|
||||
for(i = 1; i < min + 1; i++) {
|
||||
|
||||
c = *format++;
|
||||
/* skip '|' if exists. This is always safe because of assert and argc check */
|
||||
c = c == '|' ? *format++ : c;
|
||||
|
||||
switch (c) {
|
||||
case 'o': {
|
||||
pic_value *p;
|
||||
|
||||
p = va_arg(ap, pic_value*);
|
||||
if (i < argc) {
|
||||
*p = GET_OPERAND(pic,i);
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'f': {
|
||||
double *f;
|
||||
|
||||
f = va_arg(ap, double *);
|
||||
if (i < argc) {
|
||||
pic_value v;
|
||||
|
||||
v = GET_OPERAND(pic, i);
|
||||
|
@ -115,8 +136,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
default:
|
||||
pic_errorf(pic, "pic_get_args: expected float or int, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'F': {
|
||||
|
@ -125,7 +144,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
|
||||
f = va_arg(ap, double *);
|
||||
e = va_arg(ap, bool *);
|
||||
if (i < argc) {
|
||||
pic_value v;
|
||||
|
||||
v = GET_OPERAND(pic, i);
|
||||
|
@ -141,8 +159,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
default:
|
||||
pic_errorf(pic, "pic_get_args: expected float or int, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'I': {
|
||||
|
@ -151,7 +167,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
|
||||
k = va_arg(ap, int *);
|
||||
e = va_arg(ap, bool *);
|
||||
if (i < argc) {
|
||||
pic_value v;
|
||||
|
||||
v = GET_OPERAND(pic, i);
|
||||
|
@ -167,15 +182,12 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
default:
|
||||
pic_errorf(pic, "pic_get_args: expected float or int, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'i': {
|
||||
int *k;
|
||||
|
||||
k = va_arg(ap, int *);
|
||||
if (i < argc) {
|
||||
pic_value v;
|
||||
|
||||
v = GET_OPERAND(pic, i);
|
||||
|
@ -189,15 +201,12 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
default:
|
||||
pic_errorf(pic, "pic_get_args: expected int, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'k': {
|
||||
size_t *k;
|
||||
|
||||
k = va_arg(ap, size_t *);
|
||||
if (i < argc) {
|
||||
pic_value v;
|
||||
int x;
|
||||
size_t s;
|
||||
|
@ -220,8 +229,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
default:
|
||||
pic_errorf(pic, "pic_get_args: expected int, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 's': {
|
||||
|
@ -229,7 +236,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
pic_value v;
|
||||
|
||||
str = va_arg(ap, pic_str **);
|
||||
if (i < argc) {
|
||||
v = GET_OPERAND(pic,i);
|
||||
if (pic_str_p(v)) {
|
||||
*str = pic_str_ptr(v);
|
||||
|
@ -237,8 +243,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
else {
|
||||
pic_errorf(pic, "pic_get_args: expected string, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'z': {
|
||||
|
@ -246,14 +250,11 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
pic_value v;
|
||||
|
||||
cstr = va_arg(ap, const char **);
|
||||
if (i < argc) {
|
||||
v = GET_OPERAND(pic,i);
|
||||
if (! pic_str_p(v)) {
|
||||
pic_errorf(pic, "pic_get_args: expected string, but got ~s", v);
|
||||
}
|
||||
*cstr = pic_str_cstr(pic_str_ptr(v));
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'm': {
|
||||
|
@ -261,7 +262,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
pic_value v;
|
||||
|
||||
m = va_arg(ap, pic_sym **);
|
||||
if (i < argc) {
|
||||
v = GET_OPERAND(pic,i);
|
||||
if (pic_sym_p(v)) {
|
||||
*m = pic_sym_ptr(v);
|
||||
|
@ -269,8 +269,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
else {
|
||||
pic_errorf(pic, "pic_get_args: expected symbol, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'v': {
|
||||
|
@ -278,7 +276,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
pic_value v;
|
||||
|
||||
vec = va_arg(ap, struct pic_vector **);
|
||||
if (i < argc) {
|
||||
v = GET_OPERAND(pic,i);
|
||||
if (pic_vec_p(v)) {
|
||||
*vec = pic_vec_ptr(v);
|
||||
|
@ -286,8 +283,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
else {
|
||||
pic_errorf(pic, "pic_get_args: expected vector, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'b': {
|
||||
|
@ -295,7 +290,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
pic_value v;
|
||||
|
||||
b = va_arg(ap, struct pic_blob **);
|
||||
if (i < argc) {
|
||||
v = GET_OPERAND(pic,i);
|
||||
if (pic_blob_p(v)) {
|
||||
*b = pic_blob_ptr(v);
|
||||
|
@ -303,8 +297,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
else {
|
||||
pic_errorf(pic, "pic_get_args: expected bytevector, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'c': {
|
||||
|
@ -312,7 +304,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
pic_value v;
|
||||
|
||||
k = va_arg(ap, char *);
|
||||
if (i < argc) {
|
||||
v = GET_OPERAND(pic,i);
|
||||
if (pic_char_p(v)) {
|
||||
*k = pic_char(v);
|
||||
|
@ -320,8 +311,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
else {
|
||||
pic_errorf(pic, "pic_get_args: expected char, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'l': {
|
||||
|
@ -329,7 +318,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
pic_value v;
|
||||
|
||||
l = va_arg(ap, struct pic_proc **);
|
||||
if (i < argc) {
|
||||
v = GET_OPERAND(pic,i);
|
||||
if (pic_proc_p(v)) {
|
||||
*l = pic_proc_ptr(v);
|
||||
|
@ -337,8 +325,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
else {
|
||||
pic_errorf(pic, "pic_get_args, expected procedure, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'p': {
|
||||
|
@ -346,7 +332,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
pic_value v;
|
||||
|
||||
p = va_arg(ap, struct pic_port **);
|
||||
if (i < argc) {
|
||||
v = GET_OPERAND(pic,i);
|
||||
if (pic_port_p(v)) {
|
||||
*p = pic_port_ptr(v);
|
||||
|
@ -354,8 +339,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
else {
|
||||
pic_errorf(pic, "pic_get_args, expected port, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'd': {
|
||||
|
@ -363,7 +346,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
pic_value v;
|
||||
|
||||
d = va_arg(ap, struct pic_dict **);
|
||||
if (i < argc) {
|
||||
v = GET_OPERAND(pic,i);
|
||||
if (pic_dict_p(v)) {
|
||||
*d = pic_dict_ptr(v);
|
||||
|
@ -371,8 +353,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
else {
|
||||
pic_errorf(pic, "pic_get_args, expected dictionary, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'r': {
|
||||
|
@ -380,7 +360,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
pic_value v;
|
||||
|
||||
r = va_arg(ap, struct pic_record **);
|
||||
if (i < argc) {
|
||||
v = GET_OPERAND(pic,i);
|
||||
if (pic_record_p(v)) {
|
||||
*r = pic_record_ptr(v);
|
||||
|
@ -388,8 +367,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
else {
|
||||
pic_errorf(pic, "pic_get_args: expected record, but got ~s", v);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'e': {
|
||||
|
@ -397,7 +374,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
pic_value v;
|
||||
|
||||
e = va_arg(ap, struct pic_error **);
|
||||
if (i < argc) {
|
||||
v = GET_OPERAND(pic,i);
|
||||
if (pic_error_p(v)) {
|
||||
*e = pic_error_ptr(v);
|
||||
|
@ -405,31 +381,23 @@ pic_get_args(pic_state *pic, const char *format, ...)
|
|||
else {
|
||||
pic_errorf(pic, "pic_get_args, expected error");
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
pic_errorf(pic, "pic_get_args: invalid argument specifier '%c' given", c);
|
||||
}
|
||||
}
|
||||
if ('*' == c) {
|
||||
if (rest) {
|
||||
size_t *n;
|
||||
pic_value **argv;
|
||||
|
||||
n = va_arg(ap, size_t *);
|
||||
argv = va_arg(ap, pic_value **);
|
||||
if (i <= argc) {
|
||||
*n = (size_t)(argc - i);
|
||||
*n = (size_t)(argc - (i - 1));
|
||||
*argv = &GET_OPERAND(pic, i);
|
||||
i = argc;
|
||||
}
|
||||
}
|
||||
else if (argc > i) {
|
||||
pic_errorf(pic, "wrong number of arguments");
|
||||
}
|
||||
va_end(ap);
|
||||
return i - 1;
|
||||
return argc;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -915,7 +883,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value args)
|
|||
|
||||
if (ci->argc != irep->argc) {
|
||||
if (! (irep->varg && ci->argc >= irep->argc)) {
|
||||
pic_errorf(pic, "wrong number of arguments (%d for %d%s)", ci->argc - 1, irep->argc - 1, (irep->varg ? "+" : ""));
|
||||
pic_errorf(pic, "wrong number of arguments (%d for %s%d)", ci->argc - 1, (irep->varg ? "at least " : ""), irep->argc - 1);
|
||||
}
|
||||
}
|
||||
/* prepare rest args */
|
||||
|
|
Loading…
Reference in New Issue