Commit ddece2e1 authored by Michael Meeks's avatar Michael Meeks

Added optional token to function arguments.

parent 7a88fba5
1999-03-25 Michael Meeks <michael@imaginator.com>
* src/expr.c (eval_funcall): Updated to add new optional argument
token.
* src/expr.h: Comments to explain above.
* src/fn-eng.c (gnumeric_erf, gnumeric_delta): Drastic clean to use
new optional token.
1999-03-23 Jukka-Pekka Iivonen <iivonen@iki.fi> 1999-03-23 Jukka-Pekka Iivonen <iivonen@iki.fi>
* src/fn-math.c: ceiling added. * src/fn-math.c: ceiling added.
......
1999-03-25 Michael Meeks <michael@imaginator.com>
* src/expr.c (eval_funcall): Updated to add new optional argument
token.
* src/expr.h: Comments to explain above.
* src/fn-eng.c (gnumeric_erf, gnumeric_delta): Drastic clean to use
new optional token.
1999-03-23 Jukka-Pekka Iivonen <iivonen@iki.fi> 1999-03-23 Jukka-Pekka Iivonen <iivonen@iki.fi>
* src/fn-math.c: ceiling added. * src/fn-math.c: ceiling added.
......
1999-03-25 Michael Meeks <michael@imaginator.com>
* src/expr.c (eval_funcall): Updated to add new optional argument
token.
* src/expr.h: Comments to explain above.
* src/fn-eng.c (gnumeric_erf, gnumeric_delta): Drastic clean to use
new optional token.
1999-03-23 Jukka-Pekka Iivonen <iivonen@iki.fi> 1999-03-23 Jukka-Pekka Iivonen <iivonen@iki.fi>
* src/fn-math.c: ceiling added. * src/fn-math.c: ceiling added.
......
1999-03-25 Michael Meeks <michael@imaginator.com>
* src/expr.c (eval_funcall): Updated to add new optional argument
token.
* src/expr.h: Comments to explain above.
* src/fn-eng.c (gnumeric_erf, gnumeric_delta): Drastic clean to use
new optional token.
1999-03-23 Jukka-Pekka Iivonen <iivonen@iki.fi> 1999-03-23 Jukka-Pekka Iivonen <iivonen@iki.fi>
* src/fn-math.c: ceiling added. * src/fn-math.c: ceiling added.
......
...@@ -484,37 +484,13 @@ static char *help_erf = { ...@@ -484,37 +484,13 @@ static char *help_erf = {
static Value * static Value *
gnumeric_erf (void *sheet, GList *l, int eval_col, int eval_row, char **error_string) gnumeric_erf (struct FunctionDefinition *i, Value *argv [], char **error_string)
{ {
float_t ans, lower, upper=0.0 ; float_t ans, lower, upper=0.0 ;
int argc = g_list_length (l) ;
Value *vlower, *vupper=0 ;
if (argc < 1 || argc > 2 || !l->data) {
*error_string = _("Invalid number of arguments") ;
return NULL ;
}
vlower = eval_expr (sheet, l->data, eval_col, eval_row, error_string) ; lower = value_get_as_double(argv[0]) ;
if (vlower->type != VALUE_INTEGER && if (argv[1])
vlower->type != VALUE_FLOAT) { upper = value_get_as_double(argv[1]) ;
*error_string = _("#VALUE!") ;
return NULL ;
}
lower = value_get_as_double(vlower) ;
value_release (vlower) ;
l = g_list_next(l) ;
if (l && l->data) {
vupper = eval_expr (sheet, l->data, eval_col, eval_row, error_string) ;
if (vupper->type != VALUE_INTEGER &&
vupper->type != VALUE_FLOAT) {
*error_string = _("#VALUE!") ;
return NULL ;
}
upper = value_get_as_double(vupper) ;
value_release (vupper) ;
}
if (lower < 0.0 || upper < 0.0) { if (lower < 0.0 || upper < 0.0) {
*error_string = _("#NUM!") ; *error_string = _("#NUM!") ;
...@@ -522,7 +498,7 @@ gnumeric_erf (void *sheet, GList *l, int eval_col, int eval_row, char **error_st ...@@ -522,7 +498,7 @@ gnumeric_erf (void *sheet, GList *l, int eval_col, int eval_row, char **error_st
} }
ans = erf(lower) ; ans = erf(lower) ;
if (vupper) if (argv[1])
ans = erf(upper) - ans ; ans = erf(upper) - ans ;
return value_float (ans) ; return value_float (ans) ;
...@@ -569,35 +545,17 @@ static char *help_delta = { ...@@ -569,35 +545,17 @@ static char *help_delta = {
static Value * static Value *
gnumeric_delta (void *sheet, GList *l, int eval_col, int eval_row, char **error_string) gnumeric_delta (struct FunctionDefinition *i, Value *argv [], char **error_string)
{ {
int ans = 0 ; int ans = 0 ;
int argc = g_list_length (l) ;
Value *vx, *vy ; Value *vx, *vy ;
if (argc < 1 || argc > 2 || !l->data) { vx = argv[0] ;
*error_string = _("Invalid number of arguments") ; if (argv[1])
return NULL ; vy = argv[1] ;
}
vx = eval_expr (sheet, l->data, eval_col, eval_row, error_string) ;
if (vx->type != VALUE_INTEGER &&
vx->type != VALUE_FLOAT) {
*error_string = _("#VALUE!") ;
return NULL ;
}
l = g_list_next(l) ;
if (l && l->data) {
vy = eval_expr (sheet, l->data, eval_col, eval_row, error_string) ;
if (vy->type != VALUE_INTEGER &&
vy->type != VALUE_FLOAT) {
*error_string = _("#VALUE!") ;
return NULL ;
}
}
else else
vy = value_int (0) ; vy = value_int(0) ;
switch (vx->type) switch (vx->type)
{ {
case VALUE_INTEGER: case VALUE_INTEGER:
...@@ -637,8 +595,6 @@ gnumeric_delta (void *sheet, GList *l, int eval_col, int eval_row, char **error_ ...@@ -637,8 +595,6 @@ gnumeric_delta (void *sheet, GList *l, int eval_col, int eval_row, char **error_
return NULL ; return NULL ;
} }
value_release (vx) ;
value_release (vy) ;
return value_int (ans) ; return value_int (ans) ;
} }
...@@ -764,8 +720,8 @@ FunctionDefinition eng_functions [] = { ...@@ -764,8 +720,8 @@ FunctionDefinition eng_functions [] = {
{ "dec2bin", 0, "xnum,ynum", &help_dec2bin, gnumeric_dec2bin, NULL }, { "dec2bin", 0, "xnum,ynum", &help_dec2bin, gnumeric_dec2bin, NULL },
{ "dec2oct", 0, "xnum,ynum", &help_dec2oct, gnumeric_dec2oct, NULL }, { "dec2oct", 0, "xnum,ynum", &help_dec2oct, gnumeric_dec2oct, NULL },
{ "dec2hex", 0, "xnum,ynum", &help_dec2hex, gnumeric_dec2hex, NULL }, { "dec2hex", 0, "xnum,ynum", &help_dec2hex, gnumeric_dec2hex, NULL },
{ "delta", 0, "xnum,ynum", &help_delta, gnumeric_delta, NULL }, { "delta", "f|f", "xnum,ynum", &help_delta, NULL, gnumeric_delta },
{ "erf", 0, "lower,upper", &help_erf, gnumeric_erf, NULL }, { "erf", "f|f", "lower,upper", &help_erf, NULL, gnumeric_erf },
{ "erfc", "f", "number", &help_erfc, NULL, gnumeric_erfc }, { "erfc", "f", "number", &help_erfc, NULL, gnumeric_erfc },
{ "gestep", 0, "xnum,ynum", &help_gestep, gnumeric_gestep, NULL }, { "gestep", 0, "xnum,ynum", &help_gestep, gnumeric_gestep, NULL },
{ "hex2bin", 0, "xnum,ynum", &help_hex2bin, gnumeric_hex2bin, NULL }, { "hex2bin", 0, "xnum,ynum", &help_hex2bin, gnumeric_hex2bin, NULL },
......
...@@ -475,17 +475,28 @@ eval_funcall (Sheet *sheet, ExprTree *tree, int eval_col, int eval_row, char **e ...@@ -475,17 +475,28 @@ eval_funcall (Sheet *sheet, ExprTree *tree, int eval_col, int eval_row, char **e
{ {
/* Functions that take pre-computed Values */ /* Functions that take pre-computed Values */
Value **values; Value **values;
int fn_argc; int fn_argc_min=0, fn_argc_max=0, var_len=0 ;
char *arg_type = fd->args; char *arg_type = fd->args, *argptr = fd->args;
/* Get variable limits */
while (*argptr) {
if (*argptr++=='|') {
var_len = 1 ;
continue ;
}
if (!var_len)
fn_argc_min++ ;
fn_argc_max++ ;
}
fn_argc = strlen (fd->args); /* fn_argc = strlen (fd->args); */
if (fn_argc != argc){ if (argc > fn_argc_max || argc < fn_argc_min) {
*error_string = _("Invalid number of arguments"); *error_string = _("Invalid number of arguments");
return NULL; return NULL;
} }
values = g_new (Value *, argc); values = g_new (Value *, fn_argc_max);
for (arg = 0; l; l = l->next, arg++, arg_type++){ for (arg = 0; l; l = l->next, arg++, arg_type++){
ExprTree *t = (ExprTree *) l->data; ExprTree *t = (ExprTree *) l->data;
...@@ -493,8 +504,8 @@ eval_funcall (Sheet *sheet, ExprTree *tree, int eval_col, int eval_row, char **e ...@@ -493,8 +504,8 @@ eval_funcall (Sheet *sheet, ExprTree *tree, int eval_col, int eval_row, char **e
v = eval_expr (sheet, t, eval_col, eval_row, error_string); v = eval_expr (sheet, t, eval_col, eval_row, error_string);
if (v == NULL) if (v == NULL)
goto free_list; goto free_list;
if (*arg_type=='|') arg_type++ ;
switch (*arg_type){ switch (*arg_type) {
case 'f': case 'f':
if (v->type == VALUE_INTEGER || if (v->type == VALUE_INTEGER ||
v->type == VALUE_FLOAT) v->type == VALUE_FLOAT)
...@@ -513,11 +524,15 @@ eval_funcall (Sheet *sheet, ExprTree *tree, int eval_col, int eval_row, char **e ...@@ -513,11 +524,15 @@ eval_funcall (Sheet *sheet, ExprTree *tree, int eval_col, int eval_row, char **e
values [arg] = v; values [arg] = v;
} }
while (arg<fn_argc_max)
values[arg++] = NULL ;
v = fd->fn (fd, values, error_string); v = fd->fn (fd, values, error_string);
free_list: free_list:
for (i = 0; i < arg; i++) for (i = 0; i < arg; i++) {
value_release (values [i]); if (values[i] != NULL)
value_release (values [i]) ;
}
g_free (values); g_free (values);
return v; return v;
} }
......
...@@ -107,12 +107,15 @@ struct FunctionDefinition { ...@@ -107,12 +107,15 @@ struct FunctionDefinition {
/* The function name */ /* The function name */
char *name; char *name;
/* The types accepted: /**
* The types accepted:
* f for float * f for float
* s for string * s for string
* b for boolean * b for boolean
* ? for any kind * ? for any kind
*/ * For optional arguments do:
* "ff|ss" where the strings are optional
**/
char *args; char *args;
char *named_arguments; char *named_arguments;
char **help; char **help;
......
...@@ -484,37 +484,13 @@ static char *help_erf = { ...@@ -484,37 +484,13 @@ static char *help_erf = {
static Value * static Value *
gnumeric_erf (void *sheet, GList *l, int eval_col, int eval_row, char **error_string) gnumeric_erf (struct FunctionDefinition *i, Value *argv [], char **error_string)
{ {
float_t ans, lower, upper=0.0 ; float_t ans, lower, upper=0.0 ;
int argc = g_list_length (l) ;
Value *vlower, *vupper=0 ;
if (argc < 1 || argc > 2 || !l->data) {
*error_string = _("Invalid number of arguments") ;
return NULL ;
}
vlower = eval_expr (sheet, l->data, eval_col, eval_row, error_string) ; lower = value_get_as_double(argv[0]) ;
if (vlower->type != VALUE_INTEGER && if (argv[1])
vlower->type != VALUE_FLOAT) { upper = value_get_as_double(argv[1]) ;
*error_string = _("#VALUE!") ;
return NULL ;
}
lower = value_get_as_double(vlower) ;
value_release (vlower) ;
l = g_list_next(l) ;
if (l && l->data) {
vupper = eval_expr (sheet, l->data, eval_col, eval_row, error_string) ;
if (vupper->type != VALUE_INTEGER &&
vupper->type != VALUE_FLOAT) {
*error_string = _("#VALUE!") ;
return NULL ;
}
upper = value_get_as_double(vupper) ;
value_release (vupper) ;
}
if (lower < 0.0 || upper < 0.0) { if (lower < 0.0 || upper < 0.0) {
*error_string = _("#NUM!") ; *error_string = _("#NUM!") ;
...@@ -522,7 +498,7 @@ gnumeric_erf (void *sheet, GList *l, int eval_col, int eval_row, char **error_st ...@@ -522,7 +498,7 @@ gnumeric_erf (void *sheet, GList *l, int eval_col, int eval_row, char **error_st
} }
ans = erf(lower) ; ans = erf(lower) ;
if (vupper) if (argv[1])
ans = erf(upper) - ans ; ans = erf(upper) - ans ;
return value_float (ans) ; return value_float (ans) ;
...@@ -569,35 +545,17 @@ static char *help_delta = { ...@@ -569,35 +545,17 @@ static char *help_delta = {
static Value * static Value *
gnumeric_delta (void *sheet, GList *l, int eval_col, int eval_row, char **error_string) gnumeric_delta (struct FunctionDefinition *i, Value *argv [], char **error_string)
{ {
int ans = 0 ; int ans = 0 ;
int argc = g_list_length (l) ;
Value *vx, *vy ; Value *vx, *vy ;
if (argc < 1 || argc > 2 || !l->data) { vx = argv[0] ;
*error_string = _("Invalid number of arguments") ; if (argv[1])
return NULL ; vy = argv[1] ;
}
vx = eval_expr (sheet, l->data, eval_col, eval_row, error_string) ;
if (vx->type != VALUE_INTEGER &&
vx->type != VALUE_FLOAT) {
*error_string = _("#VALUE!") ;
return NULL ;
}
l = g_list_next(l) ;
if (l && l->data) {
vy = eval_expr (sheet, l->data, eval_col, eval_row, error_string) ;
if (vy->type != VALUE_INTEGER &&
vy->type != VALUE_FLOAT) {
*error_string = _("#VALUE!") ;
return NULL ;
}
}
else else
vy = value_int (0) ; vy = value_int(0) ;
switch (vx->type) switch (vx->type)
{ {
case VALUE_INTEGER: case VALUE_INTEGER:
...@@ -637,8 +595,6 @@ gnumeric_delta (void *sheet, GList *l, int eval_col, int eval_row, char **error_ ...@@ -637,8 +595,6 @@ gnumeric_delta (void *sheet, GList *l, int eval_col, int eval_row, char **error_
return NULL ; return NULL ;
} }
value_release (vx) ;
value_release (vy) ;
return value_int (ans) ; return value_int (ans) ;
} }
...@@ -764,8 +720,8 @@ FunctionDefinition eng_functions [] = { ...@@ -764,8 +720,8 @@ FunctionDefinition eng_functions [] = {
{ "dec2bin", 0, "xnum,ynum", &help_dec2bin, gnumeric_dec2bin, NULL }, { "dec2bin", 0, "xnum,ynum", &help_dec2bin, gnumeric_dec2bin, NULL },
{ "dec2oct", 0, "xnum,ynum", &help_dec2oct, gnumeric_dec2oct, NULL }, { "dec2oct", 0, "xnum,ynum", &help_dec2oct, gnumeric_dec2oct, NULL },
{ "dec2hex", 0, "xnum,ynum", &help_dec2hex, gnumeric_dec2hex, NULL }, { "dec2hex", 0, "xnum,ynum", &help_dec2hex, gnumeric_dec2hex, NULL },
{ "delta", 0, "xnum,ynum", &help_delta, gnumeric_delta, NULL }, { "delta", "f|f", "xnum,ynum", &help_delta, NULL, gnumeric_delta },
{ "erf", 0, "lower,upper", &help_erf, gnumeric_erf, NULL }, { "erf", "f|f", "lower,upper", &help_erf, NULL, gnumeric_erf },
{ "erfc", "f", "number", &help_erfc, NULL, gnumeric_erfc }, { "erfc", "f", "number", &help_erfc, NULL, gnumeric_erfc },
{ "gestep", 0, "xnum,ynum", &help_gestep, gnumeric_gestep, NULL }, { "gestep", 0, "xnum,ynum", &help_gestep, gnumeric_gestep, NULL },
{ "hex2bin", 0, "xnum,ynum", &help_hex2bin, gnumeric_hex2bin, NULL }, { "hex2bin", 0, "xnum,ynum", &help_hex2bin, gnumeric_hex2bin, NULL },
......
...@@ -484,37 +484,13 @@ static char *help_erf = { ...@@ -484,37 +484,13 @@ static char *help_erf = {
static Value * static Value *
gnumeric_erf (void *sheet, GList *l, int eval_col, int eval_row, char **error_string) gnumeric_erf (struct FunctionDefinition *i, Value *argv [], char **error_string)
{ {
float_t ans, lower, upper=0.0 ; float_t ans, lower, upper=0.0 ;
int argc = g_list_length (l) ;
Value *vlower, *vupper=0 ;
if (argc < 1 || argc > 2 || !l->data) {
*error_string = _("Invalid number of arguments") ;
return NULL ;
}
vlower = eval_expr (sheet, l->data, eval_col, eval_row, error_string) ; lower = value_get_as_double(argv[0]) ;
if (vlower->type != VALUE_INTEGER && if (argv[1])
vlower->type != VALUE_FLOAT) { upper = value_get_as_double(argv[1]) ;
*error_string = _("#VALUE!") ;
return NULL ;
}
lower = value_get_as_double(vlower) ;
value_release (vlower) ;
l = g_list_next(l) ;
if (l && l->data) {
vupper = eval_expr (sheet, l->data, eval_col, eval_row, error_string) ;
if (vupper->type != VALUE_INTEGER &&
vupper->type != VALUE_FLOAT) {
*error_string = _("#VALUE!") ;
return NULL ;
}
upper = value_get_as_double(vupper) ;
value_release (vupper) ;
}
if (lower < 0.0 || upper < 0.0) { if (lower < 0.0 || upper < 0.0) {
*error_string = _("#NUM!") ; *error_string = _("#NUM!") ;
...@@ -522,7 +498,7 @@ gnumeric_erf (void *sheet, GList *l, int eval_col, int eval_row, char **error_st ...@@ -522,7 +498,7 @@ gnumeric_erf (void *sheet, GList *l, int eval_col, int eval_row, char **error_st
} }
ans = erf(lower) ; ans = erf(lower) ;
if (vupper) if (argv[1])
ans = erf(upper) - ans ; ans = erf(upper) - ans ;
return value_float (ans) ; return value_float (ans) ;
...@@ -569,35 +545,17 @@ static char *help_delta = { ...@@ -569,35 +545,17 @@ static char *help_delta = {
static Value * static Value *
gnumeric_delta (void *sheet, GList *l, int eval_col, int eval_row, char **error_string) gnumeric_delta (struct FunctionDefinition *i, Value *argv [], char **error_string)
{ {
int ans = 0 ; int ans = 0 ;
int argc = g_list_length (l) ;
Value *vx, *vy ; Value *vx, *vy ;
if (argc < 1 || argc > 2 || !l->data) { vx = argv[0] ;
*error_string = _("Invalid number of arguments") ; if (argv[1])
return NULL ; vy = argv[1] ;
}
vx = eval_expr (sheet, l->data, eval_col, eval_row, error_string) ;
if (vx->type != VALUE_INTEGER &&
vx->type != VALUE_FLOAT) {
*error_string = _("#VALUE!") ;
return NULL ;
}
l = g_list_next(l) ;
if (l && l->data) {
vy = eval_expr (sheet, l->data, eval_col, eval_row, error_string) ;
if (vy->type != VALUE_INTEGER &&
vy->type != VALUE_FLOAT) {
*error_string = _("#VALUE!") ;
return NULL ;
}
}
else else
vy = value_int (0) ; vy = value_int(0) ;
switch (vx->type) switch (vx->type)
{ {
case VALUE_INTEGER: case VALUE_INTEGER:
...@@ -637,8 +595,6 @@ gnumeric_delta (void *sheet, GList *l, int eval_col, int eval_row, char **error_ ...@@ -637,8 +595,6 @@ gnumeric_delta (void *sheet, GList *l, int eval_col, int eval_row, char **error_
return NULL ; return NULL ;
} }
value_release (vx) ;
value_release (vy) ;
return value_int (ans) ; return value_int (ans) ;
} }
...@@ -764,8 +720,8 @@ FunctionDefinition eng_functions [] = { ...@@ -764,8 +720,8 @@ FunctionDefinition eng_functions [] = {
{ "dec2bin", 0, "xnum,ynum", &help_dec2bin, gnumeric_dec2bin, NULL }, { "dec2bin", 0, "xnum,ynum", &help_dec2bin, gnumeric_dec2bin, NULL },
{ "dec2oct", 0, "xnum,ynum", &help_dec2oct, gnumeric_dec2oct, NULL }, { "dec2oct", 0, "xnum,ynum", &help_dec2oct, gnumeric_dec2oct, NULL },
{ "dec2hex", 0, "xnum,ynum", &help_dec2hex, gnumeric_dec2hex, NULL }, { "dec2hex", 0, "xnum,ynum", &help_dec2hex, gnumeric_dec2hex, NULL },
{ "delta", 0, "xnum,ynum", &help_delta, gnumeric_delta, NULL }, { "delta", "f|f", "xnum,ynum", &help_delta, NULL, gnumeric_delta },
{ "erf", 0, "lower,upper", &help_erf, gnumeric_erf, NULL }, { "erf", "f|f", "lower,upper", &help_erf, NULL, gnumeric_erf },
{ "erfc", "f", "number", &help_erfc, NULL, gnumeric_erfc }, { "erfc", "f", "number", &help_erfc, NULL, gnumeric_erfc },
{ "gestep", 0, "xnum,ynum", &help_gestep, gnumeric_gestep, NULL }, { "gestep", 0, "xnum,ynum", &help_gestep, gnumeric_gestep, NULL },
{ "hex2bin", 0, "xnum,ynum", &help_hex2bin, gnumeric_hex2bin, NULL }, { "hex2bin", 0, "xnum,ynum", &help_hex2bin, gnumeric_hex2bin, NULL },
......
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