2019-07-22 20:32:33 +00:00
|
|
|
/* #includes */ /*{{{C}}}*//*{{{*/
|
|
|
|
#ifndef NO_POSIX_SOURCE
|
|
|
|
#undef _POSIX_SOURCE
|
|
|
|
#define _POSIX_SOURCE 1
|
|
|
|
#undef _POSIX_C_SOURCE
|
|
|
|
#define _POSIX_C_SOURCE 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DMALLOC
|
|
|
|
#include "dmalloc.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2019-07-30 19:07:54 +00:00
|
|
|
#include <float.h>
|
2019-07-22 20:32:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-07-30 19:07:54 +00:00
|
|
|
extern char *strdup(const char* s);
|
2019-07-22 20:32:33 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "eval.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "scanner.h"
|
|
|
|
#include "sheet.h"
|
|
|
|
/*}}}*/
|
|
|
|
/* #defines */ /*{{{*/
|
|
|
|
#define MAXARGC 16
|
|
|
|
/*}}}*/
|
|
|
|
|
|
|
|
/* prototypes */ /*{{{*/
|
2019-08-05 20:45:04 +00:00
|
|
|
static Token term(Token *n[], int *i, EvalMethod meth);
|
2019-07-22 20:32:33 +00:00
|
|
|
/*}}}*/
|
|
|
|
|
|
|
|
/* primary -- parse and evaluate a primary term */ /*{{{*/
|
2019-08-05 20:45:04 +00:00
|
|
|
static Token primary(Token *n[], int *i, EvalMethod meth)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
|
|
|
/* variables */ /*{{{*/
|
2019-08-05 20:45:04 +00:00
|
|
|
int argc;
|
|
|
|
int fident = -2;
|
2019-07-22 20:32:33 +00:00
|
|
|
Token *ident,argv[MAXARGC],result;
|
|
|
|
/*}}}*/
|
|
|
|
|
2019-08-05 20:45:04 +00:00
|
|
|
if (n[*i] == NULLTOKEN)
|
2019-07-22 20:32:33 +00:00
|
|
|
/* error */ /*{{{*/
|
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
duperror(&result, _("missing operator"));
|
2019-07-22 20:32:33 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/*}}}*/
|
2019-08-05 20:45:04 +00:00
|
|
|
switch (n[*i]->type)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
|
|
|
/* STRING, FLOAT, INT */ /*{{{*/
|
|
|
|
case STRING:
|
|
|
|
case FLOAT:
|
|
|
|
case INT:
|
|
|
|
return tcopy(*n[(*i)++]);
|
2019-08-05 20:45:04 +00:00
|
|
|
/*}}}*/
|
|
|
|
|
|
|
|
/* LIDENT */ /*{{{*/
|
|
|
|
case LIDENT:
|
|
|
|
{
|
|
|
|
ident = n[*i];
|
|
|
|
++(*i);
|
|
|
|
if (meth == FULL) return findlabel(upd_sheet,ident->u.lident);
|
|
|
|
return tcopy(*ident);
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
|
|
|
/*}}}*/
|
2019-08-05 20:45:04 +00:00
|
|
|
|
2019-07-22 20:32:33 +00:00
|
|
|
/* OPERATOR */ /*{{{*/
|
|
|
|
case OPERATOR:
|
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
switch (n[*i]->u.op)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
case OP: /* return paren term */ /*{{{*/
|
2019-07-22 20:32:33 +00:00
|
|
|
++(*i);
|
2019-08-05 20:45:04 +00:00
|
|
|
result = term(n, i, meth);
|
|
|
|
if (result.type == EEK) return result;
|
|
|
|
if (n[*i] != NULLTOKEN && n[*i]->type == OPERATOR && n[*i]->u.op==CP)
|
|
|
|
{
|
2019-07-22 20:32:33 +00:00
|
|
|
++(*i);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
tfree(&result);
|
2019-08-05 20:45:04 +00:00
|
|
|
duperror(&result, _(") expected"));
|
2019-07-22 20:32:33 +00:00
|
|
|
return result;
|
2019-08-05 20:45:04 +00:00
|
|
|
/*}}}*/
|
|
|
|
case MINUS: /* return negated term */ /*{{{*/
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
|
|
|
++(*i);
|
2019-08-05 20:45:04 +00:00
|
|
|
if (meth == FULL) return tneg(primary(n, i, meth));
|
|
|
|
Token arg = primary(n, i, meth);
|
|
|
|
if (arg.type == EEK) return arg;
|
|
|
|
result.type = FUNCALL;
|
|
|
|
result.u.funcall.fident = identcode("negate", 6);
|
|
|
|
result.u.funcall.argc = 1;
|
|
|
|
result.u.funcall.argv = malloc(sizeof(Token));
|
|
|
|
result.u.funcall.argv[0] = arg;
|
|
|
|
return result;
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
2019-08-05 20:45:04 +00:00
|
|
|
case CP:
|
|
|
|
duperror(&result, _("Extra umatched ')'"));
|
|
|
|
return result;
|
|
|
|
case COMMA:
|
|
|
|
duperror(&result, _("Occurrence of ',' outside parameter list"));
|
|
|
|
return result;
|
|
|
|
default:
|
|
|
|
/* Can also use any infix symbol as a function, but only with parens, not
|
|
|
|
bare */
|
|
|
|
if (n[(*i)+1] == NULLTOKEN || n[(*i)+1]->type != OPERATOR
|
|
|
|
|| n[(*i)+1]->u.op != OP)
|
|
|
|
{
|
|
|
|
const char *templ = "To use %s as function symbol, must use %s(...)";
|
|
|
|
result.type = EEK;
|
|
|
|
result.u.err = malloc(strlen(templ) + 2 * MAX_OP_NAME_LENGTH + 1);
|
|
|
|
sprintf(result.u.err, templ,
|
|
|
|
Op_Name[n[*i]->u.op], Op_Name[n[*i]->u.op]);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
fident = identcode(Op_Name[n[*i]->u.op], strlen(Op_Name[n[*i]->u.op]));
|
|
|
|
/* FALL THROUGH TO PROCESS OPERATOR AS FUNCTION CALL */
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*}}}*/
|
2019-08-05 20:45:04 +00:00
|
|
|
|
2019-07-22 20:32:33 +00:00
|
|
|
/* FIDENT */ /*{{{*/
|
|
|
|
case FIDENT:
|
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
if (fident == -2) fident = n[*i]->u.fident;
|
2019-07-22 20:32:33 +00:00
|
|
|
++(*i);
|
2019-08-05 20:45:04 +00:00
|
|
|
if (n[*i] == NULLTOKEN || n[*i]->type != OPERATOR
|
|
|
|
|| n[*i]->u.op != OP)
|
|
|
|
argc = -1;
|
|
|
|
else /* parse arguments and closing paren of function call */ /*{{{*/
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
|
|
|
++(*i);
|
2019-08-05 20:45:04 +00:00
|
|
|
argc = 0;
|
|
|
|
if (!(n[*i] != NULLTOKEN && n[*i]->type==OPERATOR && n[*i]->u.op==CP))
|
2019-07-22 20:32:33 +00:00
|
|
|
/* parse at least one argument */ /*{{{*/
|
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
if (n[*i] != NULLTOKEN && n[*i]->type==OPERATOR && n[*i]->u.op==COMMA)
|
2019-07-22 20:32:33 +00:00
|
|
|
/* empty argument */ /*{{{*/
|
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
argv[argc].type = EMPTY;
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
|
|
|
/*}}}*/
|
2019-08-05 20:45:04 +00:00
|
|
|
else argv[argc] = term(n, i, meth);
|
|
|
|
if (argv[argc].type == EEK) return argv[argc];
|
2019-07-22 20:32:33 +00:00
|
|
|
++argc;
|
2019-08-05 20:45:04 +00:00
|
|
|
while (n[*i] != NULLTOKEN && n[*i]->type == OPERATOR
|
|
|
|
&& n[*i]->u.op == COMMA)
|
2019-07-22 20:32:33 +00:00
|
|
|
/* parse the following argument */ /*{{{*/
|
|
|
|
{
|
|
|
|
++(*i);
|
2019-08-05 20:45:04 +00:00
|
|
|
if (argc < MAXARGC)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
if (n[*i] != NULLTOKEN && n[*i]->type == OPERATOR &&
|
|
|
|
(n[*i]->u.op == COMMA || n[*i]->u.op == CP))
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
|
|
|
argv[argc].type=EMPTY;
|
2019-08-05 20:45:04 +00:00
|
|
|
} else {
|
|
|
|
argv[argc] = term(n, i, meth);
|
|
|
|
if (argv[argc].type == EEK) {
|
|
|
|
for (size_t pa = 0; pa < argc; +pa) tfree(argv + pa);
|
|
|
|
return argv[argc];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
duperror(&result, _("too many arguments"));
|
|
|
|
for (size_t j=0; j < argc; ++j) tfree(&argv[j]);
|
2019-07-22 20:32:33 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
++argc;
|
|
|
|
}
|
|
|
|
/*}}}*/
|
|
|
|
}
|
|
|
|
/*}}}*/
|
2019-08-05 20:45:04 +00:00
|
|
|
if (n[*i] == NULLTOKEN || n[*i]->type != OPERATOR || n[*i]->u.op != CP)
|
2019-07-22 20:32:33 +00:00
|
|
|
/* ) expected */ /*{{{*/
|
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
for (size_t j = 0; j < argc; ++j) tfree(&argv[j]);
|
|
|
|
duperror(&result, _(") expected"));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
++(*i);
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
2019-08-05 20:45:04 +00:00
|
|
|
if (meth == FULL)
|
|
|
|
{
|
|
|
|
result = tfuncall(fident, argc, argv);
|
|
|
|
/* To allow a function to return one of its arguments, we need
|
|
|
|
to be sure not to free that argument: */
|
|
|
|
for (size_t j = 0; j < argc; ++j) tfree_protected(&argv[j], result);
|
|
|
|
return result;
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
2019-08-05 20:45:04 +00:00
|
|
|
result.type = FUNCALL;
|
|
|
|
result.u.funcall.fident = fident;
|
|
|
|
result.u.funcall.argc = argc;
|
|
|
|
if (argc > 0)
|
|
|
|
{
|
|
|
|
result.u.funcall.argv = malloc(argc*sizeof(Token));
|
|
|
|
for (size_t ai; ai < argc; ++ai)
|
|
|
|
result.u.funcall.argv[ai] = argv[ai];
|
|
|
|
}
|
|
|
|
return result;
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
|
|
|
/*}}}*/
|
2019-08-05 20:45:04 +00:00
|
|
|
|
|
|
|
/* FUNCALL */ /*{{{*/
|
|
|
|
case FUNCALL:
|
|
|
|
if (meth == FULL)
|
|
|
|
result = tfuncall(n[*i]->u.funcall.fident, n[*i]->u.funcall.argc,
|
|
|
|
n[*i]->u.funcall.argv);
|
|
|
|
else result = tcopy(*n[*i]);
|
|
|
|
++(*i);
|
|
|
|
return result;
|
|
|
|
/*}}}*/
|
|
|
|
|
2019-07-22 20:32:33 +00:00
|
|
|
default: ; /* fall through */
|
|
|
|
}
|
2019-08-05 20:45:04 +00:00
|
|
|
duperror(&result, _("value expected"));
|
2019-07-22 20:32:33 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/*}}}*/
|
2019-08-05 20:45:04 +00:00
|
|
|
|
2019-07-22 20:32:33 +00:00
|
|
|
/* powterm -- parse and evaluate a x^y term */ /*{{{*/
|
2019-08-05 20:45:04 +00:00
|
|
|
static Token powterm(Token *n[], int *i, EvalMethod meth)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
|
|
|
Token l;
|
2019-08-05 20:45:04 +00:00
|
|
|
size_t npows = 0;
|
2019-07-22 20:32:33 +00:00
|
|
|
|
2019-08-05 20:45:04 +00:00
|
|
|
l = primary(n, i, meth);
|
|
|
|
if (l.type == EEK) return l;
|
|
|
|
while (n[*i] != (Token*)0 && n[*i]->type == OPERATOR && n[*i]->u.op == POW)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
Token r;
|
2019-07-22 20:32:33 +00:00
|
|
|
|
|
|
|
++(*i);
|
2019-08-05 20:45:04 +00:00
|
|
|
r = primary(n,i,meth);
|
|
|
|
if (meth == FULL)
|
|
|
|
{
|
|
|
|
Token result = tpow(l,r);
|
|
|
|
tfree(&l);
|
|
|
|
tfree(&r);
|
|
|
|
if (result.type == EEK) return result;
|
|
|
|
l = result;
|
|
|
|
} else {
|
|
|
|
if (r.type == EEK)
|
|
|
|
{
|
|
|
|
tfree(&l);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (npows == 0)
|
|
|
|
{
|
|
|
|
Token tmp = l;
|
|
|
|
l.type = FUNCALL;
|
|
|
|
l.u.funcall.fident = identcode("^", 1);
|
|
|
|
l.u.funcall.argc = 1;
|
|
|
|
l.u.funcall.argv = malloc(MAXARGC * sizeof(Token));
|
|
|
|
l.u.funcall.argv[0] = tmp;
|
|
|
|
}
|
|
|
|
if (npows + 1 >= MAXARGC)
|
|
|
|
{
|
|
|
|
tfree(&l);
|
|
|
|
tfree(&r);
|
|
|
|
duperror(&l, _("Exceeded maximum sequence length of ^"));
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
++npows; ++(l.u.funcall.argc);
|
|
|
|
l.u.funcall.argv[npows] = r;
|
|
|
|
}
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
/*}}}*/
|
2019-08-05 20:45:04 +00:00
|
|
|
|
2019-07-22 20:32:33 +00:00
|
|
|
/* piterm -- parse and evaluate a product/division/modulo term */ /*{{{*/
|
2019-08-05 20:45:04 +00:00
|
|
|
static Token piterm(Token *n[], int *i, EvalMethod meth)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
int mulident = identcode("*", 1);
|
2019-07-22 20:32:33 +00:00
|
|
|
Token l;
|
2019-08-05 20:45:04 +00:00
|
|
|
Operator op = CP;
|
|
|
|
bool first_funcall = true;
|
2019-07-22 20:32:33 +00:00
|
|
|
|
2019-08-05 20:45:04 +00:00
|
|
|
l = powterm(n, i, meth);
|
|
|
|
if (l.type == EEK) return l;
|
|
|
|
if (n[*i] != NULLTOKEN && n[*i]->type == OPERATOR) op = n[*i]->u.op;
|
|
|
|
|
|
|
|
while (op == DIV || op == MUL || op == MOD)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
Token r;
|
2019-07-22 20:32:33 +00:00
|
|
|
|
|
|
|
++(*i);
|
2019-08-05 20:45:04 +00:00
|
|
|
r = powterm(n, i, meth);
|
|
|
|
if (meth == FULL)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
Token result;
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case MUL: result = tmul(l,r); break;
|
|
|
|
case DIV: result = tdiv(l,r); break;
|
|
|
|
case MOD: result = tmod(l,r); break;
|
2019-07-22 20:32:33 +00:00
|
|
|
default: assert(0);
|
2019-08-05 20:45:04 +00:00
|
|
|
}
|
|
|
|
tfree(&l);
|
|
|
|
tfree(&r);
|
|
|
|
if (result.type == EEK) return result;
|
|
|
|
l = result;
|
|
|
|
} else {
|
|
|
|
if (r.type == EEK)
|
|
|
|
{
|
|
|
|
tfree(&l);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (first_funcall || l.u.funcall.fident != mulident || op != MUL)
|
|
|
|
{
|
|
|
|
first_funcall = false;
|
|
|
|
Token tmp = l;
|
|
|
|
l.u.funcall.fident = identcode(Op_Name[op], strlen(Op_Name[op]));
|
|
|
|
l.u.funcall.argc = 2;
|
|
|
|
if (op == MUL) l.u.funcall.argv = malloc(MAXARGC * sizeof(Token));
|
|
|
|
else l.u.funcall.argv = malloc(2*sizeof(Token));
|
|
|
|
l.u.funcall.argv[0] = tmp;
|
|
|
|
l.u.funcall.argv[1] = r;
|
|
|
|
} else {
|
|
|
|
if (l.u.funcall.argc >= MAXARGC)
|
|
|
|
{
|
|
|
|
tfree(&r);
|
|
|
|
tfree(&l);
|
|
|
|
duperror(&l, _("Exceeded maximum sequence length of *"));
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
l.u.funcall.argv[(l.u.funcall.argc)++] = r;
|
|
|
|
}
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
2019-08-05 20:45:04 +00:00
|
|
|
if (n[*i] != NULLTOKEN && n[*i]->type == OPERATOR) op = n[*i]->u.op;
|
|
|
|
else op = CP;
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
/*}}}*/
|
2019-08-05 20:45:04 +00:00
|
|
|
|
2019-07-22 20:32:33 +00:00
|
|
|
/* factor -- parse and evaluate a factor of sums/differences */ /*{{{*/
|
2019-08-05 20:45:04 +00:00
|
|
|
static Token factor(Token *n[], int *i, EvalMethod meth)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
int plusident = identcode("+", 1);
|
2019-07-22 20:32:33 +00:00
|
|
|
Token l;
|
2019-08-05 20:45:04 +00:00
|
|
|
Operator op = CP;
|
|
|
|
bool first_funcall = true;
|
2019-07-22 20:32:33 +00:00
|
|
|
|
2019-08-05 20:45:04 +00:00
|
|
|
l = piterm(n, i, meth);
|
|
|
|
if (l.type == EEK) return l;
|
|
|
|
if (n[*i] != NULLTOKEN && n[*i]->type == OPERATOR) op = n[*i]->u.op;
|
|
|
|
|
|
|
|
while (op == PLUS || op == MINUS)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
Token r;
|
2019-07-22 20:32:33 +00:00
|
|
|
|
|
|
|
++(*i);
|
2019-08-05 20:45:04 +00:00
|
|
|
r = piterm(n, i, meth);
|
|
|
|
if (meth == FULL)
|
|
|
|
{
|
|
|
|
Token result = (op==PLUS ? tadd(l,r) : tsub(l,r));
|
|
|
|
tfree(&l);
|
|
|
|
tfree(&r);
|
|
|
|
if (result.type == EEK) return result;
|
|
|
|
l = result;
|
|
|
|
} else {
|
|
|
|
if (r.type == EEK)
|
|
|
|
{
|
|
|
|
tfree(&l);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (first_funcall || l.u.funcall.fident != plusident || op != PLUS)
|
|
|
|
{
|
|
|
|
first_funcall = false;
|
|
|
|
Token tmp = l;
|
|
|
|
l.u.funcall.fident = identcode(Op_Name[op], strlen(Op_Name[op]));
|
|
|
|
l.u.funcall.argc = 2;
|
|
|
|
if (op == PLUS) l.u.funcall.argv = malloc(MAXARGC * sizeof(Token));
|
|
|
|
else l.u.funcall.argv = malloc(2*sizeof(Token));
|
|
|
|
l.u.funcall.argv[0] = tmp;
|
|
|
|
l.u.funcall.argv[1] = r;
|
|
|
|
} else {
|
|
|
|
if (l.u.funcall.argc >= MAXARGC)
|
|
|
|
{
|
|
|
|
tfree(&r);
|
|
|
|
tfree(&l);
|
|
|
|
duperror(&l, _("Exceeded maximum sequence length of +"));
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
l.u.funcall.argv[(l.u.funcall.argc)++] = r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n[*i] != NULLTOKEN && n[*i]->type == OPERATOR) op = n[*i]->u.op;
|
|
|
|
else op = CP;
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
/*}}}*/
|
2019-08-05 20:45:04 +00:00
|
|
|
|
2019-07-22 20:32:33 +00:00
|
|
|
/* term -- parse and evaluate a relational term */ /*{{{*/
|
2019-08-05 20:45:04 +00:00
|
|
|
static Token term(Token *n[], int *i, EvalMethod meth)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
Token l = factor(n, i, meth);
|
|
|
|
if (l.type == EEK) return l;
|
|
|
|
/* a < b < c used to mean (a < b) < c, but that does not make sense really
|
|
|
|
because there is not an ordering on bools (if we had a separate bool
|
|
|
|
type). So restrict to a single binary relation; one can still use parens
|
|
|
|
to get the old, odd behavior */
|
|
|
|
if (n[*i] != NULLTOKEN && n[*i]->type == OPERATOR
|
|
|
|
&& n[*i]->u.op >= LT && n[*i]->u.op <= NE)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
Operator op = n[*i]->u.op;
|
|
|
|
Token result, r;
|
2019-07-22 20:32:33 +00:00
|
|
|
|
|
|
|
++(*i);
|
2019-08-05 20:45:04 +00:00
|
|
|
r = factor(n, i, meth);
|
|
|
|
if (meth == FULL)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case LT: result=tlt(l,r); break;
|
|
|
|
case LE: result=tle(l,r); break;
|
|
|
|
case GE: result=tge(l,r); break;
|
|
|
|
case GT: result=tgt(l,r); break;
|
|
|
|
case ISEQUAL: result=teq(l,r); break;
|
|
|
|
case ABOUTEQ: result=tabouteq(l,r); break;
|
|
|
|
case NE: result=tne(l,r); break;
|
|
|
|
default: assert(0);
|
|
|
|
}
|
|
|
|
tfree(&l);
|
|
|
|
tfree(&r);
|
|
|
|
if (result.type == EEK) return result;
|
|
|
|
l = result;
|
|
|
|
} else {
|
|
|
|
if (r.type == EEK)
|
|
|
|
{
|
|
|
|
tfree(&l);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
Token tmp = l;
|
|
|
|
l.type = FUNCALL;
|
|
|
|
l.u.funcall.fident = identcode(Op_Name[op], strlen(Op_Name[op]));
|
|
|
|
l.u.funcall.argc = 2;
|
|
|
|
l.u.funcall.argv = malloc(2*sizeof(Token));
|
|
|
|
l.u.funcall.argv[0] = tmp;
|
|
|
|
l.u.funcall.argv[1] = r;
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
/*}}}*/
|
|
|
|
|
|
|
|
/* eval -- parse and evaluate token sequence */ /*{{{*/
|
2019-08-05 20:45:04 +00:00
|
|
|
Token eval(Token **n, EvalMethod meth)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
Token l;
|
|
|
|
int i = 0;
|
|
|
|
bool first_funcall = true;
|
|
|
|
|
|
|
|
assert(upd_sheet != (Sheet*)0);
|
|
|
|
l = term(n, &i, meth);
|
|
|
|
if (l.type == EEK) return l;
|
2019-07-22 20:32:33 +00:00
|
|
|
|
2019-08-05 20:45:04 +00:00
|
|
|
while (n[i] != NULLTOKEN)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-05 20:45:04 +00:00
|
|
|
Token r = term(n, &i, meth);
|
|
|
|
|
|
|
|
if (meth == FULL)
|
|
|
|
{
|
|
|
|
Token result = tconcat(l,r);
|
|
|
|
tfree(&l);
|
|
|
|
tfree(&r);
|
|
|
|
if (result.type == EEK) return result;
|
|
|
|
l = result;
|
|
|
|
} else {
|
|
|
|
if (r.type == EEK)
|
|
|
|
{
|
|
|
|
tfree(&l);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (first_funcall)
|
|
|
|
{
|
|
|
|
first_funcall = false;
|
|
|
|
Token tmp = l;
|
|
|
|
l.u.funcall.fident = identcode("concat", 6);
|
|
|
|
l.u.funcall.argc = 1;
|
|
|
|
l.u.funcall.argv = malloc(MAXARGC*sizeof(Token));
|
|
|
|
l.u.funcall.argv[0] = l;
|
|
|
|
}
|
|
|
|
if (l.u.funcall.argc >= MAXARGC)
|
|
|
|
{
|
|
|
|
tfree(&l);
|
|
|
|
tfree(&r);
|
|
|
|
duperror(&l, _("Exceeded max sequence lentgh of concatenated terms"));
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
l.u.funcall.argv[(l.u.funcall.argc)++] = r;
|
|
|
|
}
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
2019-08-05 20:45:04 +00:00
|
|
|
return l;
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
|
|
|
/*}}}*/
|
Prevent phantom values when clocking, resetting, and clocking again
In the end it turned out that the cause of the phantom values was
short-cutting in getvalue() when the contents of a cell were empty,
preventing the update of the internal cache of the value of the cell.
However, tracking this down (and getting the associated memory management
correct) necessitated implementing a debugging mode in which I could
dump the internal states of cells and print various other stuff to standard
output. It also involved understanding the meaning of various pointers in
the code, in the process of which I renamed some commonly used macros,
particularly the former SHEET(s,x,y,z) which was not returning a Sheet at
all but rather a pointer to a Cell. So this macro is now called CELL_AT. I
also replaced several very repeatedly used patterns of checking the validity
of locations and pointers with macros, now defined in sheet.h.
Therefore, unfortunately the (relatively small in the end) bugfix for this
major issue is entangled with numerous textual changes to the code made
in tracking it down.
Fixes #18.
Closes #19.
2019-07-24 17:47:39 +00:00
|
|
|
|
|
|
|
/* eval_safe -- like eval, but handles null pointer to token sequence */ /*{{{*/
|
2019-08-05 20:45:04 +00:00
|
|
|
Token eval_safe(Token **n, EvalMethod meth)
|
Prevent phantom values when clocking, resetting, and clocking again
In the end it turned out that the cause of the phantom values was
short-cutting in getvalue() when the contents of a cell were empty,
preventing the update of the internal cache of the value of the cell.
However, tracking this down (and getting the associated memory management
correct) necessitated implementing a debugging mode in which I could
dump the internal states of cells and print various other stuff to standard
output. It also involved understanding the meaning of various pointers in
the code, in the process of which I renamed some commonly used macros,
particularly the former SHEET(s,x,y,z) which was not returning a Sheet at
all but rather a pointer to a Cell. So this macro is now called CELL_AT. I
also replaced several very repeatedly used patterns of checking the validity
of locations and pointers with macros, now defined in sheet.h.
Therefore, unfortunately the (relatively small in the end) bugfix for this
major issue is entangled with numerous textual changes to the code made
in tracking it down.
Fixes #18.
Closes #19.
2019-07-24 17:47:39 +00:00
|
|
|
{
|
|
|
|
Token result;
|
|
|
|
if (n == EMPTY_TVEC)
|
|
|
|
{
|
|
|
|
result.type = EMPTY;
|
|
|
|
return result;
|
|
|
|
}
|
2019-08-05 20:45:04 +00:00
|
|
|
return eval(n, meth);
|
Prevent phantom values when clocking, resetting, and clocking again
In the end it turned out that the cause of the phantom values was
short-cutting in getvalue() when the contents of a cell were empty,
preventing the update of the internal cache of the value of the cell.
However, tracking this down (and getting the associated memory management
correct) necessitated implementing a debugging mode in which I could
dump the internal states of cells and print various other stuff to standard
output. It also involved understanding the meaning of various pointers in
the code, in the process of which I renamed some commonly used macros,
particularly the former SHEET(s,x,y,z) which was not returning a Sheet at
all but rather a pointer to a Cell. So this macro is now called CELL_AT. I
also replaced several very repeatedly used patterns of checking the validity
of locations and pointers with macros, now defined in sheet.h.
Therefore, unfortunately the (relatively small in the end) bugfix for this
major issue is entangled with numerous textual changes to the code made
in tracking it down.
Fixes #18.
Closes #19.
2019-07-24 17:47:39 +00:00
|
|
|
}
|