Extend sum() to cover the case of just adding up all of its arguments.
Also a small amount of preparation of storing parse trees instead of linear sequences of tokens got mixed in here, namely a new type of token called a "FUNCALL".
This commit is contained in:
parent
07bf78c7bb
commit
26ea0e05b3
@ -45,6 +45,7 @@ extern char *strdup(const char* s);
|
|||||||
#else
|
#else
|
||||||
#define CONST_PI ((double)3.14159265358979323846)
|
#define CONST_PI ((double)3.14159265358979323846)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
@ -304,6 +305,11 @@ static double deg2rad(double x)
|
|||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
|
static void duperror(Token* tok, const char* erro) {
|
||||||
|
tok->type = EEK;
|
||||||
|
tok->u.err = strdup(erro);
|
||||||
|
}
|
||||||
|
|
||||||
typedef enum {ABSOLUTE, RELATIVE, EXCEL} LocConvention;
|
typedef enum {ABSOLUTE, RELATIVE, EXCEL} LocConvention;
|
||||||
|
|
||||||
static Token excel_adr_func(int argc, const Token argv[]);
|
static Token excel_adr_func(int argc, const Token argv[]);
|
||||||
@ -340,12 +346,8 @@ static Token adr_func(int argc, const Token argv[], LocConvention lcon)
|
|||||||
else if (argv[i].type != EMPTY) break;
|
else if (argv[i].type != EMPTY) break;
|
||||||
}
|
}
|
||||||
if (i < argc)
|
if (i < argc)
|
||||||
{
|
duperror(&result, _("Usage: &([integer x][,[integer y][,[integer z]]])"));
|
||||||
result.type=EEK;
|
else result.type = LOCATION;
|
||||||
result.u.err=strcpy(malloc(strlen(_("Usage: &([integer x][,[integer y][,[integer z]]])"))+1),_("Usage: &([integer x][,[integer y][,[integer z]]])"));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
result.type = LOCATION;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
@ -408,16 +410,14 @@ static Token excel_adr_func(int argc, const Token argv[])
|
|||||||
Token result;
|
Token result;
|
||||||
char *usage = _("Usage: X&(THERE_LABEL, HERE_LABEL, [fix_x], [fix_y], [fix_z])");
|
char *usage = _("Usage: X&(THERE_LABEL, HERE_LABEL, [fix_x], [fix_y], [fix_z])");
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
result.type = EEK;
|
duperror(&result, usage);
|
||||||
result.u.err = strdup(usage);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
if (argv[0].type == EEK) return argv[0];
|
if (argv[0].type == EEK) return argv[0];
|
||||||
if (argv[1].type == EEK) return argv[1];
|
if (argv[1].type == EEK) return argv[1];
|
||||||
if (argv[0].type != LOCATION || argv[1].type != LOCATION)
|
if (argv[0].type != LOCATION || argv[1].type != LOCATION)
|
||||||
{
|
{
|
||||||
result.type = EEK;
|
duperror(&result, usage);
|
||||||
result.u.err = strdup(usage);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -432,8 +432,7 @@ static Token excel_adr_func(int argc, const Token argv[])
|
|||||||
if (argv[i].type == EEK) return argv[i];
|
if (argv[i].type == EEK) return argv[i];
|
||||||
if (!INTPATIBLE(argv[i]))
|
if (!INTPATIBLE(argv[i]))
|
||||||
{
|
{
|
||||||
result.type = EEK;
|
duperror(&result, usage);
|
||||||
result.u.err = strdup(usage);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
if (argv[i].type == INT && argv[i].u.integer > 0) fixed = true;
|
if (argv[i].type == INT && argv[i].u.integer > 0) fixed = true;
|
||||||
@ -503,9 +502,7 @@ static Token bitwise_func(int argc, const Token argv[], LogicalFunction lop)
|
|||||||
for (size_t i = 0; i < argc; ++i) {
|
for (size_t i = 0; i < argc; ++i) {
|
||||||
if (!INTPATIBLE(argv[i]))
|
if (!INTPATIBLE(argv[i]))
|
||||||
{
|
{
|
||||||
result.type = EEK;
|
duperror(&result, _("Bitwise functions operate only on integers."));
|
||||||
result.u.err =
|
|
||||||
strdup(_("Bitwise functions operate only on integers."));
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
int val = 0;
|
int val = 0;
|
||||||
@ -541,15 +538,11 @@ static Token e_func(int argc, const Token argv[])
|
|||||||
result.u.flt=CONST_E;
|
result.u.flt=CONST_E;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
else /* result is e type error */ /*{{{*/
|
else duperror(&result, _("Usage: e()"));
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("Usage: e()"));
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* eval */ /*{{{*/
|
/* eval */ /*{{{*/
|
||||||
static Token eval_func(int argc, const Token argv[])
|
static Token eval_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
@ -558,13 +551,7 @@ static Token eval_func(int argc, const Token argv[])
|
|||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
--max_eval;
|
--max_eval;
|
||||||
if (max_eval<0)
|
if (max_eval<0) duperror(&result, _("nested eval()"));
|
||||||
/* nesting error */ /*{{{*/
|
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("nested eval()"));
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
else if (argc==1 && argv[0].type==LOCATION)
|
else if (argc==1 && argv[0].type==LOCATION)
|
||||||
/* evaluate expression in cell at given position */ /*{{{*/
|
/* evaluate expression in cell at given position */ /*{{{*/
|
||||||
{
|
{
|
||||||
@ -577,13 +564,7 @@ static Token eval_func(int argc, const Token argv[])
|
|||||||
else result=eval(contents);
|
else result=eval(contents);
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
else
|
else duperror(&result, _("Usage: eval(location)"));
|
||||||
/* eval type error */ /*{{{*/
|
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strcpy(malloc(strlen(_("Usage: eval(location)"))+1),_("Usage: eval(location)"));
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
++max_eval;
|
++max_eval;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -599,15 +580,9 @@ static Token error_func(int argc, const Token argv[])
|
|||||||
/* asserts */ /*{{{*/
|
/* asserts */ /*{{{*/
|
||||||
assert(argv!=(Token*)0);
|
assert(argv!=(Token*)0);
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
result.type=EEK;
|
if (argc != 1 || argv[0].type != STRING)
|
||||||
if (argc!=1 || argv[0].type!=STRING)
|
duperror(&result, _("Usage: error(string message)"));
|
||||||
/* result is type error */ /*{{{*/
|
else duperror(&result, argv[0].u.string);
|
||||||
result.u.err=strcpy(malloc(strlen(_("Usage: error(string message)"))+1),_("Usage: error(string message)"));
|
|
||||||
/*}}}*/
|
|
||||||
else
|
|
||||||
/* result is user defined error */ /*{{{*/
|
|
||||||
result.u.err=strcpy(malloc(strlen(argv[0].u.string)+1),argv[0].u.string);
|
|
||||||
/*}}}*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
@ -673,22 +648,21 @@ static Token string_func(int argc, const Token argv[])
|
|||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
else /* return string type error */ /*{{{*/
|
else /* return string type error */ /*{{{*/
|
||||||
{
|
duperror(&result, _("Usage: string(location)|string(int)|string(float[,[int][,integer]])"));
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strcpy(malloc(strlen(_("Usage: string(location) or string(float[,[integer][,integer]])"))+1),_("Usage: string(location) or string(float[,[integer][,integer]])"));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* sum */ /*{{{*/
|
/* sum */ /*{{{*/
|
||||||
static Token sum_func(int argc, const Token argv[])
|
static Token sum_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
/* variables */ /*{{{*/
|
|
||||||
Token result;
|
Token result;
|
||||||
/*}}}*/
|
const char *usage = _("Usage: sum(loc_start, loc_end)|sum(val1, val2,...)");
|
||||||
|
|
||||||
|
if (argc == 0) {
|
||||||
|
duperror(&result, usage);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
if (argc==2 && argv[0].type==LOCATION && argv[1].type==LOCATION) /* result is sum */ /*{{{*/
|
if (argc==2 && argv[0].type==LOCATION && argv[1].type==LOCATION) /* result is sum */ /*{{{*/
|
||||||
{
|
{
|
||||||
/* variables */ /*{{{*/
|
/* variables */ /*{{{*/
|
||||||
@ -714,17 +688,22 @@ static Token sum_func(int argc, const Token argv[])
|
|||||||
result=tmp;
|
result=tmp;
|
||||||
if (result.type==EEK) return result;
|
if (result.type==EEK) return result;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Token tmp;
|
||||||
|
/* Try to add up the args */
|
||||||
|
result = tcopy(argv[0]);
|
||||||
|
for (size_t i = 1; i < argc; ++i)
|
||||||
|
{
|
||||||
|
tmp = tadd(result, argv[i]);
|
||||||
|
tfree(&result);
|
||||||
|
result = tmp;
|
||||||
|
if (result.type == EEK) return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/*}}}*/
|
|
||||||
else /* result is sum type error */ /*{{{*/
|
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strcpy(malloc(strlen(_("Usage: sum(location,location)"))+1),_("Usage: sum(location,location)"));
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* n */ /*{{{*/
|
/* n */ /*{{{*/
|
||||||
static Token n_func(int argc, const Token argv[])
|
static Token n_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
@ -759,13 +738,7 @@ static Token n_func(int argc, const Token argv[])
|
|||||||
result.u.integer = n;
|
result.u.integer = n;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
else
|
else duperror(&result, _("Usage: n(location,location)"));
|
||||||
/* result is n type error */ /*{{{*/
|
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strcpy(malloc(strlen(_("Usage: n(location,location)"))+1),_("Usage: n(location,location)"));
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
@ -813,29 +786,18 @@ static Token int_func(int argc, const Token argv[])
|
|||||||
|
|
||||||
errno=0;
|
errno=0;
|
||||||
result.u.integer=strtol(argv[0].u.string,&s,10);
|
result.u.integer=strtol(argv[0].u.string,&s,10);
|
||||||
if (s==(char*)0 || *s)
|
if (s==(char*)0 || *s) duperror(&result, _("int(string): invalid string"));
|
||||||
{
|
else if (errno==ERANGE &&
|
||||||
result.type=EEK;
|
(result.u.integer==LONG_MAX || result.u.integer==LONG_MIN))
|
||||||
result.u.err=strdup(_("int(string): invalid string"));
|
duperror(&result, _("int(string): domain error"));
|
||||||
}
|
|
||||||
else if (errno==ERANGE && (result.u.integer==LONG_MAX || result.u.integer==LONG_MIN))
|
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("int(string): domain error"));
|
|
||||||
}
|
|
||||||
else result.type=INT;
|
else result.type=INT;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
else
|
else duperror(&result, _("Usage: int(float[,integer,integer])"));
|
||||||
/* result is int type error */ /*{{{*/
|
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strcpy(malloc(strlen(_("Usage: int(float[,integer,integer])"))+1),_("Usage: int(float[,integer,integer])"));
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* frac */ /*{{{*/
|
/* frac */ /*{{{*/
|
||||||
static Token frac_func(int argc, const Token argv[])
|
static Token frac_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
@ -851,16 +813,11 @@ static Token frac_func(int argc, const Token argv[])
|
|||||||
result.u.flt=modf(argv[0].u.flt,&foo);
|
result.u.flt=modf(argv[0].u.flt,&foo);
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
else
|
else duperror(&result, _("Usage: frac(float)"));
|
||||||
/* result is frac type error */ /*{{{*/
|
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strcpy(malloc(strlen(_("Usage: frac(float)"))+1),_("Usage: frac(float)"));
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* len */ /*{{{*/
|
/* len */ /*{{{*/
|
||||||
static Token len_func(int argc, const Token argv[])
|
static Token len_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
@ -875,13 +832,7 @@ static Token len_func(int argc, const Token argv[])
|
|||||||
result.u.integer=strlen(argv[0].u.string);
|
result.u.integer=strlen(argv[0].u.string);
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
else
|
else duperror(&result, _("Usage: len(string)"));
|
||||||
/* result is frac type error */ /*{{{*/
|
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("Usage: len(string)"));
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
@ -919,15 +870,11 @@ static Token log_func(int argc, const Token argv[])
|
|||||||
else result.u.flt=log(x)/log(y);
|
else result.u.flt=log(x)/log(y);
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
else /* result is log type error */ /*{{{*/
|
else duperror(&result, _("Usage: log(float[,float])"));
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("Usage: log(float[,float])"));
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* minmax */ /*{{{*/
|
/* minmax */ /*{{{*/
|
||||||
static Token minmax_func(int argc, const Token argv[], int min)
|
static Token minmax_func(int argc, const Token argv[], int min)
|
||||||
{
|
{
|
||||||
@ -1005,15 +952,13 @@ static Token minmax_func(int argc, const Token argv[], int min)
|
|||||||
return argv[mini];
|
return argv[mini];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
/* result is min/max type error */ /*{{{*/
|
duperror(&result,
|
||||||
{
|
min ? _("Usage: min(location,location) or min(val1, val2,...)")
|
||||||
result.type=EEK;
|
: _("Usage: max(location,location) or max(val1,val2,...)"));
|
||||||
result.u.err=strdup(min ? _("Usage: min(location,location) or min(val1, val2,...)") : _("Usage: max(location,location) or max(val1,val2,...)"));
|
return result;
|
||||||
return result;
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* min */ /*{{{*/
|
/* min */ /*{{{*/
|
||||||
static Token min_func(int argc, const Token argv[])
|
static Token min_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
@ -1047,13 +992,7 @@ static Token abs_func(int argc, const Token argv[])
|
|||||||
result.u.integer=(argv[0].u.integer<0 ? -argv[0].u.integer : argv[0].u.integer);
|
result.u.integer=(argv[0].u.integer<0 ? -argv[0].u.integer : argv[0].u.integer);
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
else
|
else duperror(&result, _("Usage: abs(float|integer)"));
|
||||||
/* result is abs type error */ /*{{{*/
|
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("Usage: abs(float|integer)"));
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
@ -1072,14 +1011,11 @@ static Token env_func(int argc, const Token argv[])
|
|||||||
result.type=STRING;
|
result.type=STRING;
|
||||||
result.u.string=strdup(e);
|
result.u.string=strdup(e);
|
||||||
}
|
}
|
||||||
else
|
else duperror(&result, _("Usage: $(string)"));
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("Usage: $(string)"));
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* float */ /*{{{*/
|
/* float */ /*{{{*/
|
||||||
static Token float_func(int argc, const Token argv[])
|
static Token float_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
@ -1090,28 +1026,18 @@ static Token float_func(int argc, const Token argv[])
|
|||||||
{
|
{
|
||||||
char *p;
|
char *p;
|
||||||
|
|
||||||
result.u.flt=strtod(argv[0].u.string,&p);
|
result.u.flt = strtod(argv[0].u.string, &p);
|
||||||
if (p!=argv[0].u.string && *p=='\0' && dblfinite(result.u.flt)==(const char*)0)
|
if (p != argv[0].u.string && *p=='\0'
|
||||||
{
|
&& dblfinite(result.u.flt) == (const char*)0)
|
||||||
result.type=FLOAT;
|
result.type = FLOAT;
|
||||||
}
|
else duperror(&result, _("Not a (finite) floating point number"));
|
||||||
else
|
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("Not a (finite) floating point number"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
else
|
|
||||||
/* float type error */ /*{{{*/
|
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("Usage: float(string)"));
|
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
else duperror(&result, _("Usage: float(string)"));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* strftime */ /*{{{*/
|
/* strftime */ /*{{{*/
|
||||||
static Token strftime_func(int argc, const Token argv[])
|
static Token strftime_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
@ -1147,15 +1073,11 @@ static Token strftime_func(int argc, const Token argv[])
|
|||||||
result.type=STRING;
|
result.type=STRING;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
else /* strftime type error */ /*{{{*/
|
else duperror(&result, _("Usage: strftime(string[,integer])"));
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("Usage: strftime(string[,integer])"));
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* clock */ /*{{{*/
|
/* clock */ /*{{{*/
|
||||||
static Token clock_func(int argc, const Token argv[])
|
static Token clock_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
@ -1191,132 +1113,137 @@ static Token clock_func(int argc, const Token argv[])
|
|||||||
result.type=EMPTY;
|
result.type=EMPTY;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
else /* wrong usage */ /*{{{*/
|
else duperror(&result, _("Usage: clock(condition,location[,location])"));
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("Usage: clock(condition,location[,location])"));
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* poly */ /*{{{*/
|
/* poly */ /*{{{*/
|
||||||
static Token poly_func(int argc, const Token argv[])
|
static Token poly_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
/* variables */ /*{{{*/
|
Token result, tmp;
|
||||||
Token result;
|
const char *usage = _("Usage: poly(float|integer,float|integer,...)");
|
||||||
int i;
|
|
||||||
/*}}}*/
|
|
||||||
|
|
||||||
for (i=0; i<argc; ++i) if (argc<2 || (argv[i].type!=INT && argv[i].type!=FLOAT)) /* type error */ /*{{{*/
|
result.type = EMPTY;
|
||||||
{
|
if (argc < 2) duperror(&result, usage);
|
||||||
result.type=EEK;
|
else for (size_t i = 0; i < argc; ++i)
|
||||||
result.u.err=strcpy(malloc(strlen(_("Usage: poly(float|integer,float|integer,...)"))+1),_("Usage: poly(float|integer,float|integer,...)"));
|
if (argv[i].type != INT && argv[i].type != FLOAT)
|
||||||
}
|
duperror(&result, usage);
|
||||||
/*}}}*/
|
if (result.type == EEK) return result;
|
||||||
else
|
|
||||||
{
|
|
||||||
Token tmp;
|
|
||||||
|
|
||||||
result=tcopy(argv[1]);
|
result = tcopy(argv[1]);
|
||||||
for (i=2; i<argc; ++i)
|
for (size_t i = 2; i < argc; ++i)
|
||||||
{
|
{
|
||||||
tmp=tmul(result,argv[0]);
|
tmp = tmul(result,argv[0]);
|
||||||
tfree(&result);
|
tfree(&result);
|
||||||
result=tmp;
|
result = tmp;
|
||||||
tmp=tadd(result,argv[i]);
|
tmp = tadd(result,argv[i]);
|
||||||
tfree(&result);
|
tfree(&result);
|
||||||
result=tmp;
|
result = tmp;
|
||||||
if (result.type==EEK) return result;
|
if (result.type == EEK) return result;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* sin */ /*{{{*/
|
/* sin */ /*{{{*/
|
||||||
static Token sin_func(int argc, const Token argv[])
|
static Token sin_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,sin,"sin");
|
return sci_func(argc,argv,sin,"sin");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* cos */ /*{{{*/
|
/* cos */ /*{{{*/
|
||||||
static Token cos_func(int argc, const Token argv[])
|
static Token cos_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,cos,"cos");
|
return sci_func(argc,argv,cos,"cos");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* tan */ /*{{{*/
|
/* tan */ /*{{{*/
|
||||||
static Token tan_func(int argc, const Token argv[])
|
static Token tan_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,tan,"tan");
|
return sci_func(argc,argv,tan,"tan");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* sinh */ /*{{{*/
|
/* sinh */ /*{{{*/
|
||||||
static Token sinh_func(int argc, const Token argv[])
|
static Token sinh_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,sinh,"sinh");
|
return sci_func(argc,argv,sinh,"sinh");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* cosh */ /*{{{*/
|
/* cosh */ /*{{{*/
|
||||||
static Token cosh_func(int argc, const Token argv[])
|
static Token cosh_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,cosh,"cosh");
|
return sci_func(argc,argv,cosh,"cosh");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* tanh */ /*{{{*/
|
/* tanh */ /*{{{*/
|
||||||
static Token tanh_func(int argc, const Token argv[])
|
static Token tanh_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,tanh,"tanh");
|
return sci_func(argc,argv,tanh,"tanh");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* asin */ /*{{{*/
|
/* asin */ /*{{{*/
|
||||||
static Token asin_func(int argc, const Token argv[])
|
static Token asin_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,asin,"asin");
|
return sci_func(argc,argv,asin,"asin");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* acos */ /*{{{*/
|
/* acos */ /*{{{*/
|
||||||
static Token acos_func(int argc, const Token argv[])
|
static Token acos_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,acos,"acos");
|
return sci_func(argc,argv,acos,"acos");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* atan */ /*{{{*/
|
/* atan */ /*{{{*/
|
||||||
static Token atan_func(int argc, const Token argv[])
|
static Token atan_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,atan,"atan");
|
return sci_func(argc,argv,atan,"atan");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* arsinh */ /*{{{*/
|
/* arsinh */ /*{{{*/
|
||||||
static Token arsinh_func(int argc, const Token argv[])
|
static Token arsinh_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,arsinh,"arsinh");
|
return sci_func(argc,argv,arsinh,"arsinh");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* arcosh */ /*{{{*/
|
/* arcosh */ /*{{{*/
|
||||||
static Token arcosh_func(int argc, const Token argv[])
|
static Token arcosh_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,arcosh,"arcosh");
|
return sci_func(argc,argv,arcosh,"arcosh");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* artanh */ /*{{{*/
|
/* artanh */ /*{{{*/
|
||||||
static Token artanh_func(int argc, const Token argv[])
|
static Token artanh_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,artanh,"artanh");
|
return sci_func(argc,argv,artanh,"artanh");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* rad2deg */ /*{{{*/
|
/* rad2deg */ /*{{{*/
|
||||||
static Token rad2deg_func(int argc, const Token argv[])
|
static Token rad2deg_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,rad2deg,"rad2deg");
|
return sci_func(argc,argv,rad2deg,"rad2deg");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* deg2rad */ /*{{{*/
|
/* deg2rad */ /*{{{*/
|
||||||
static Token deg2rad_func(int argc, const Token argv[])
|
static Token deg2rad_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
return sci_func(argc,argv,deg2rad,"deg2rad");
|
return sci_func(argc,argv,deg2rad,"deg2rad");
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* rnd */ /*{{{*/
|
/* rnd */ /*{{{*/
|
||||||
static Token rnd_func(int argc, const Token argv[])
|
static Token rnd_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
@ -1329,14 +1256,11 @@ static Token rnd_func(int argc, const Token argv[])
|
|||||||
result.type=FLOAT;
|
result.type=FLOAT;
|
||||||
result.u.flt=rand()/((double)RAND_MAX);
|
result.u.flt=rand()/((double)RAND_MAX);
|
||||||
}
|
}
|
||||||
else
|
else duperror(&result, _("Usage: rnd()"));
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("Usage: rnd()"));
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* substr */ /*{{{*/
|
/* substr */ /*{{{*/
|
||||||
static Token substr_func(int argc, const Token argv[])
|
static Token substr_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
@ -1367,14 +1291,11 @@ static Token substr_func(int argc, const Token argv[])
|
|||||||
result.type=EMPTY;
|
result.type=EMPTY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else duperror(&result, _("Usage: substr(string,integer,integer)"));
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("Usage: substr(string,integer,integer)"));
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* strptime */ /*{{{*/
|
/* strptime */ /*{{{*/
|
||||||
static Token strptime_func(int argc, const Token argv[])
|
static Token strptime_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
@ -1394,15 +1315,11 @@ static Token strptime_func(int argc, const Token argv[])
|
|||||||
result.type=INT;
|
result.type=INT;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
else /* strftime type error */ /*{{{*/
|
else duperror(&result, _("Usage: strptime(string,string)"));
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("Usage: strptime(string,string)"));
|
|
||||||
}
|
|
||||||
/*}}}*/
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
|
||||||
/* time */ /*{{{*/
|
/* time */ /*{{{*/
|
||||||
static Token time_func(int argc, const Token argv[])
|
static Token time_func(int argc, const Token argv[])
|
||||||
{
|
{
|
||||||
@ -1413,11 +1330,7 @@ static Token time_func(int argc, const Token argv[])
|
|||||||
result.type=INT;
|
result.type=INT;
|
||||||
result.u.integer=time((time_t*)0);
|
result.u.integer=time((time_t*)0);
|
||||||
}
|
}
|
||||||
else
|
else duperror(&result, _("Usage: time()"));
|
||||||
{
|
|
||||||
result.type=EEK;
|
|
||||||
result.u.err=strdup(_("Usage: time()"));
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/*}}}*/
|
/*}}}*/
|
||||||
|
@ -31,7 +31,7 @@ extern double strtod(const char *nptr, char **endptr); /* SunOS 4 hack */
|
|||||||
const char *Type_Name[] =
|
const char *Type_Name[] =
|
||||||
{ [EMPTY] = "EMPTY", [STRING] = "STRING", [FLOAT] = "FLOAT", [INT] = "INT",
|
{ [EMPTY] = "EMPTY", [STRING] = "STRING", [FLOAT] = "FLOAT", [INT] = "INT",
|
||||||
[OPERATOR] = "OPERATOR", [LIDENT] = "LABEL", [FIDENT] = "FUNCTION",
|
[OPERATOR] = "OPERATOR", [LIDENT] = "LABEL", [FIDENT] = "FUNCTION",
|
||||||
[LOCATION] = "LOCATION", [EEK] = "ERROR"
|
[LOCATION] = "LOCATION", [FUNCALL] = "FUNCTION-CALL", [EEK] = "ERROR"
|
||||||
};
|
};
|
||||||
|
|
||||||
/* identcode -- return number of identifier */ /*{{{*/
|
/* identcode -- return number of identifier */ /*{{{*/
|
||||||
|
@ -11,7 +11,7 @@ extern "C" {
|
|||||||
typedef enum {
|
typedef enum {
|
||||||
EMPTY
|
EMPTY
|
||||||
#ifndef __cplusplus
|
#ifndef __cplusplus
|
||||||
, STRING, FLOAT, INT, OPERATOR, LIDENT, FIDENT, LOCATION, EEK
|
, STRING, FLOAT, INT, OPERATOR, LIDENT, FIDENT, LOCATION, FUNCALL, EEK
|
||||||
#endif
|
#endif
|
||||||
} Type;
|
} Type;
|
||||||
|
|
||||||
@ -37,7 +37,16 @@ typedef enum { X=0, Y=1, Z=2, HYPER} Dimensions;
|
|||||||
bool loc_in_box(const Location test,
|
bool loc_in_box(const Location test,
|
||||||
const Location b, const Location c);
|
const Location b, const Location c);
|
||||||
|
|
||||||
|
typedef struct Token_struc Token;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
|
{
|
||||||
|
int fident;
|
||||||
|
int argc;
|
||||||
|
Token *argv;
|
||||||
|
} FunctionCall;
|
||||||
|
|
||||||
|
typedef struct Token_struc
|
||||||
{
|
{
|
||||||
Type type;
|
Type type;
|
||||||
union
|
union
|
||||||
@ -49,6 +58,7 @@ typedef struct
|
|||||||
char *lident;
|
char *lident;
|
||||||
int fident;
|
int fident;
|
||||||
Location location;
|
Location location;
|
||||||
|
FunctionCall funcall;
|
||||||
char *err;
|
char *err;
|
||||||
} u;
|
} u;
|
||||||
} Token;
|
} Token;
|
||||||
|
Loading…
Reference in New Issue
Block a user