Commit 6415faab authored by dearblue's avatar dearblue

Integrate each element expansion process of the argument

The previously used `given` variable will be merged into the `pickarg` pointer variable, which points to the argument currently being processed for each loop.
parent ca295a2c
...@@ -915,7 +915,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -915,7 +915,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_bool argv_on_stack = argc >= 0; mrb_bool argv_on_stack = argc >= 0;
mrb_bool opt = FALSE; mrb_bool opt = FALSE;
mrb_bool opt_skip = TRUE; mrb_bool opt_skip = TRUE;
mrb_bool given = TRUE; const mrb_value *pickarg = NULL; /* arguments currently being processed */
mrb_value kdict; mrb_value kdict;
mrb_bool reqkarg = FALSE; mrb_bool reqkarg = FALSE;
int argc_min = 0, argc_max = 0; int argc_min = 0, argc_max = 0;
...@@ -973,9 +973,12 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -973,9 +973,12 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
case '|': case '*': case '&': case '?': case ':': case '|': case '*': case '&': case '?': case ':':
break; break;
default: default:
if (argc <= i) { if (i < argc) {
pickarg = &argv[i++];
}
else {
if (opt) { if (opt) {
given = FALSE; pickarg = NULL;
} }
else { else {
mrb_argnum_error(mrb, argc, argc_min, argc_max); mrb_argnum_error(mrb, argc, argc_min, argc_max);
...@@ -998,8 +1001,8 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -998,8 +1001,8 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_value *p; mrb_value *p;
p = va_arg(ap, mrb_value*); p = va_arg(ap, mrb_value*);
if (i < argc) { if (pickarg) {
*p = argv[i++]; *p = *pickarg;
} }
} }
break; break;
...@@ -1008,14 +1011,11 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -1008,14 +1011,11 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_value *p; mrb_value *p;
p = va_arg(ap, mrb_value*); p = va_arg(ap, mrb_value*);
if (i < argc) { if (pickarg) {
mrb_value ss; if (!class_ptr_p(*pickarg)) {
mrb_raisef(mrb, E_TYPE_ERROR, "%v is not class/module", *pickarg);
ss = argv[i++];
if (!class_ptr_p(ss)) {
mrb_raisef(mrb, E_TYPE_ERROR, "%v is not class/module", ss);
} }
*p = ss; *p = *pickarg;
} }
} }
break; break;
...@@ -1024,14 +1024,11 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -1024,14 +1024,11 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
struct RClass **p; struct RClass **p;
p = va_arg(ap, struct RClass**); p = va_arg(ap, struct RClass**);
if (i < argc) { if (pickarg) {
mrb_value ss; if (!class_ptr_p(*pickarg)) {
mrb_raisef(mrb, E_TYPE_ERROR, "%v is not class/module", *pickarg);
ss = argv[i++];
if (!class_ptr_p(ss)) {
mrb_raisef(mrb, E_TYPE_ERROR, "%v is not class/module", ss);
} }
*p = mrb_class_ptr(ss); *p = mrb_class_ptr(*pickarg);
} }
} }
break; break;
...@@ -1040,11 +1037,11 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -1040,11 +1037,11 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_value *p; mrb_value *p;
p = va_arg(ap, mrb_value*); p = va_arg(ap, mrb_value*);
if (i < argc) { if (pickarg) {
*p = argv[i++]; if (!(altmode && mrb_nil_p(*pickarg))) {
if (!(altmode && mrb_nil_p(*p))) { mrb_to_str(mrb, *pickarg);
mrb_to_str(mrb, *p);
} }
*p = *pickarg;
} }
} }
break; break;
...@@ -1053,11 +1050,11 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -1053,11 +1050,11 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_value *p; mrb_value *p;
p = va_arg(ap, mrb_value*); p = va_arg(ap, mrb_value*);
if (i < argc) { if (pickarg) {
*p = argv[i++]; if (!(altmode && mrb_nil_p(*pickarg))) {
if (!(altmode && mrb_nil_p(*p))) { to_ary(mrb, *pickarg);
*p = to_ary(mrb, *p);
} }
*p = *pickarg;
} }
} }
break; break;
...@@ -1066,72 +1063,66 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -1066,72 +1063,66 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_value *p; mrb_value *p;
p = va_arg(ap, mrb_value*); p = va_arg(ap, mrb_value*);
if (i < argc) { if (pickarg) {
*p = argv[i++]; if (!(altmode && mrb_nil_p(*pickarg))) {
if (!(altmode && mrb_nil_p(*p))) { to_hash(mrb, *pickarg);
*p = to_hash(mrb, *p);
} }
*p = *pickarg;
} }
} }
break; break;
case 's': case 's':
{ {
mrb_value ss;
const char **ps = 0; const char **ps = 0;
mrb_int *pl = 0; mrb_int *pl = 0;
ps = va_arg(ap, const char**); ps = va_arg(ap, const char**);
pl = va_arg(ap, mrb_int*); pl = va_arg(ap, mrb_int*);
if (i < argc) { if (pickarg) {
ss = argv[i++]; if (altmode && mrb_nil_p(*pickarg)) {
if (altmode && mrb_nil_p(ss)) {
*ps = NULL; *ps = NULL;
*pl = 0; *pl = 0;
} }
else { else {
mrb_to_str(mrb, ss); mrb_to_str(mrb, *pickarg);
*ps = RSTRING_PTR(ss); *ps = RSTRING_PTR(*pickarg);
*pl = RSTRING_LEN(ss); *pl = RSTRING_LEN(*pickarg);
} }
} }
} }
break; break;
case 'z': case 'z':
{ {
mrb_value ss;
const char **ps; const char **ps;
ps = va_arg(ap, const char**); ps = va_arg(ap, const char**);
if (i < argc) { if (pickarg) {
ss = argv[i++]; if (altmode && mrb_nil_p(*pickarg)) {
if (altmode && mrb_nil_p(ss)) {
*ps = NULL; *ps = NULL;
} }
else { else {
mrb_to_str(mrb, ss); mrb_to_str(mrb, *pickarg);
*ps = RSTRING_CSTR(mrb, ss); *ps = RSTRING_CSTR(mrb, *pickarg);
} }
} }
} }
break; break;
case 'a': case 'a':
{ {
mrb_value aa;
struct RArray *a; struct RArray *a;
const mrb_value **pb; const mrb_value **pb;
mrb_int *pl; mrb_int *pl;
pb = va_arg(ap, const mrb_value**); pb = va_arg(ap, const mrb_value**);
pl = va_arg(ap, mrb_int*); pl = va_arg(ap, mrb_int*);
if (i < argc) { if (pickarg) {
aa = argv[i++]; if (altmode && mrb_nil_p(*pickarg)) {
if (altmode && mrb_nil_p(aa)) {
*pb = 0; *pb = 0;
*pl = 0; *pl = 0;
} }
else { else {
aa = to_ary(mrb, aa); to_ary(mrb, *pickarg);
a = mrb_ary_ptr(aa); a = mrb_ary_ptr(*pickarg);
*pb = ARY_PTR(a); *pb = ARY_PTR(a);
*pl = ARY_LEN(a); *pl = ARY_LEN(a);
} }
...@@ -1142,20 +1133,17 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -1142,20 +1133,17 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
{ {
void* *p; void* *p;
struct RClass *klass; struct RClass *klass;
mrb_value ss;
p = va_arg(ap, void**); p = va_arg(ap, void**);
klass = va_arg(ap, struct RClass*); klass = va_arg(ap, struct RClass*);
if (i < argc) { if (pickarg) {
ss = argv[i++]; if (!mrb_obj_is_kind_of(mrb, *pickarg, klass)) {
if (!mrb_obj_is_kind_of(mrb, ss, klass)) { mrb_raisef(mrb, E_TYPE_ERROR, "%v is not a %C", *pickarg, klass);
mrb_raisef(mrb, E_TYPE_ERROR, "%v is not a %C", ss, klass);
} }
if (!mrb_istruct_p(ss)) if (!mrb_istruct_p(*pickarg)) {
{ mrb_raisef(mrb, E_TYPE_ERROR, "%v is not inline struct", *pickarg);
mrb_raisef(mrb, E_TYPE_ERROR, "%v is not inline struct", ss);
} }
*p = mrb_istruct_ptr(ss); *p = mrb_istruct_ptr(*pickarg);
} }
} }
break; break;
...@@ -1165,8 +1153,8 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -1165,8 +1153,8 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_float *p; mrb_float *p;
p = va_arg(ap, mrb_float*); p = va_arg(ap, mrb_float*);
if (i < argc) { if (pickarg) {
*p = mrb_as_float(mrb, argv[i]); i++; *p = mrb_as_float(mrb, *pickarg);
} }
} }
break; break;
...@@ -1176,8 +1164,8 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -1176,8 +1164,8 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_int *p; mrb_int *p;
p = va_arg(ap, mrb_int*); p = va_arg(ap, mrb_int*);
if (i < argc) { if (pickarg) {
*p = mrb_as_int(mrb, argv[i]); i++; *p = mrb_as_int(mrb, *pickarg);
} }
} }
break; break;
...@@ -1185,9 +1173,8 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -1185,9 +1173,8 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
{ {
mrb_bool *boolp = va_arg(ap, mrb_bool*); mrb_bool *boolp = va_arg(ap, mrb_bool*);
if (i < argc) { if (pickarg) {
mrb_value b = argv[i++]; *boolp = mrb_test(*pickarg);
*boolp = mrb_test(b);
} }
} }
break; break;
...@@ -1196,11 +1183,8 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -1196,11 +1183,8 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_sym *symp; mrb_sym *symp;
symp = va_arg(ap, mrb_sym*); symp = va_arg(ap, mrb_sym*);
if (i < argc) { if (pickarg) {
mrb_value ss; *symp = to_sym(mrb, *pickarg);
ss = argv[i++];
*symp = to_sym(mrb, ss);
} }
} }
break; break;
...@@ -1211,13 +1195,12 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -1211,13 +1195,12 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
datap = va_arg(ap, void**); datap = va_arg(ap, void**);
type = va_arg(ap, struct mrb_data_type const*); type = va_arg(ap, struct mrb_data_type const*);
if (i < argc) { if (pickarg) {
mrb_value dd = argv[i++]; if (altmode && mrb_nil_p(*pickarg)) {
if (altmode && mrb_nil_p(dd)) {
*datap = 0; *datap = 0;
} }
else { else {
*datap = mrb_data_get_ptr(mrb, dd, type); *datap = mrb_data_get_ptr(mrb, *pickarg, type);
} }
} }
} }
...@@ -1249,7 +1232,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -1249,7 +1232,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_bool *p; mrb_bool *p;
p = va_arg(ap, mrb_bool*); p = va_arg(ap, mrb_bool*);
*p = given; *p = pickarg ? TRUE : FALSE;
} }
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment