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"
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
#include "func.h"
|
2019-07-22 20:32:33 +00:00
|
|
|
#include "main.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "scanner.h"
|
|
|
|
#include "sheet.h"
|
|
|
|
/*}}}*/
|
|
|
|
/* #defines */ /*{{{*/
|
|
|
|
#define MAXARGC 16
|
|
|
|
/*}}}*/
|
|
|
|
|
|
|
|
/* prototypes */ /*{{{*/
|
2019-08-27 06:56:19 +00:00
|
|
|
static Token boolterm(Operator bop, Token *n[], int *i, EvalMethod meth);
|
2019-07-22 20:32:33 +00:00
|
|
|
/*}}}*/
|
|
|
|
|
2019-08-27 06:56:19 +00:00
|
|
|
static Token term(Token *n[], int* i, EvalMethod meth)
|
|
|
|
{
|
|
|
|
return boolterm(LOR, n, i, meth);
|
|
|
|
}
|
|
|
|
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
/* full_eval_funcall -- evaluate the args of a funcall token and then
|
|
|
|
call the function on them
|
|
|
|
*/
|
|
|
|
static Token full_eval_funcall(Token *t)
|
|
|
|
{
|
|
|
|
assert(t->type == FUNCALL);
|
|
|
|
if (t->u.funcall.argc < 1)
|
|
|
|
return tfuncall(t->u.funcall.fident, t->u.funcall.argc, 0);
|
2019-08-27 06:56:19 +00:00
|
|
|
Token *eval_argv;
|
|
|
|
if (tfunc[t->u.funcall.fident].eval_as == MACRO)
|
|
|
|
eval_argv = t->u.funcall.argv;
|
|
|
|
else {
|
|
|
|
eval_argv = malloc(t->u.funcall.argc*sizeof(Token));
|
|
|
|
for (size_t ai = 0; ai < t->u.funcall.argc; ++ai) {
|
|
|
|
eval_argv[ai] = evaltoken(t->u.funcall.argv[ai], FULL);
|
|
|
|
}
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
}
|
|
|
|
Token result = tfuncall(t->u.funcall.fident, t->u.funcall.argc, eval_argv);
|
2019-08-27 06:56:19 +00:00
|
|
|
if (tfunc[t->u.funcall.fident].eval_as == FUNCT) {
|
|
|
|
/* To allow a function to return one of its arguments, we need
|
|
|
|
to be sure not to free that argument: */
|
|
|
|
for (size_t ai = 0; ai < t->u.funcall.argc; ++ai)
|
|
|
|
tfree_protected(&eval_argv[ai], result);
|
|
|
|
free(eval_argv);
|
|
|
|
}
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/*}}}*/
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
/* Unary minus will be handled in powterm */
|
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));
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
for (size_t ai = 0; ai < argc; ++ai)
|
2019-08-05 20:45:04 +00:00
|
|
|
result.u.funcall.argv[ai] = argv[ai];
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
} else result.u.funcall.argv = NULLTOKEN;
|
2019-08-05 20:45:04 +00:00
|
|
|
return result;
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
|
|
|
/*}}}*/
|
2019-08-05 20:45:04 +00:00
|
|
|
|
|
|
|
/* FUNCALL */ /*{{{*/
|
|
|
|
case FUNCALL:
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
if (meth == FULL) result = full_eval_funcall(n[*i]);
|
2019-08-05 20:45:04 +00:00
|
|
|
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
|
|
|
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
if (n[*i] != NULLTOKEN && n[*i]->type == OPERATOR && n[*i]->u.op == MINUS)
|
|
|
|
{
|
|
|
|
/* A - symbol here is a pain. If it is being used as a function symbol, it
|
|
|
|
is higher precedence than exponentiation. If it is unary negation,
|
|
|
|
then it's lower precedence and we have to grab a powterm to the right,
|
|
|
|
and negate it. As far as I can tell the only way to tell is to
|
|
|
|
look ahead a term to see if there's a comma...
|
|
|
|
*/
|
|
|
|
bool unaryneg = true;
|
|
|
|
int j = *i + 1;
|
|
|
|
if (n[j] == NULLTOKEN)
|
|
|
|
return duperror(&l, _("A bare - is not a valid expression"));
|
|
|
|
if (n[j]->type == OPERATOR && n[j]->u.op == OP)
|
|
|
|
{
|
|
|
|
++j;
|
|
|
|
Token dummy = term(n, &j, meth);
|
|
|
|
if (n[j] != NULLTOKEN && n[j]->type == OPERATOR && n[j]->u.op == COMMA)
|
|
|
|
unaryneg = false;
|
|
|
|
tfree(&dummy);
|
|
|
|
}
|
|
|
|
if (unaryneg)
|
|
|
|
{
|
|
|
|
++(*i);
|
|
|
|
l = powterm(n, i, meth);
|
|
|
|
if (meth == FULL) return tneg(l);
|
|
|
|
if (l.type == EEK) return l;
|
|
|
|
if (TOKISNUM(l)) return tneg(l);
|
|
|
|
Token result;
|
|
|
|
result.type = FUNCALL;
|
|
|
|
result.u.funcall.fident = FUNC_NEGATE;
|
|
|
|
result.u.funcall.argc = 1;
|
|
|
|
result.u.funcall.argv = malloc(sizeof(Token));
|
|
|
|
result.u.funcall.argv[0] = l;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2019-08-05 20:45:04 +00:00
|
|
|
l = primary(n, i, meth);
|
|
|
|
if (l.type == EEK) return l;
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
while (n[*i] != NULLTOKEN && 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);
|
2019-08-27 06:56:19 +00:00
|
|
|
tfree_protected(&l, result);
|
|
|
|
tfree_protected(&r, result);
|
2019-08-05 20:45:04 +00:00
|
|
|
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;
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
l.u.funcall.fident = FUNC_CARET;
|
2019-08-05 20:45:04 +00:00
|
|
|
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
|
|
|
{
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
FunctionIdentifier mulident = FUNC_ASTERISK;
|
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
|
|
|
}
|
2019-08-27 06:56:19 +00:00
|
|
|
tfree_protected(&l, result);
|
|
|
|
tfree_protected(&r, result);
|
2019-08-05 20:45:04 +00:00
|
|
|
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;
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
l.type = FUNCALL;
|
2019-08-05 20:45:04 +00:00
|
|
|
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-27 06:56:19 +00:00
|
|
|
Token l = piterm(n, i, meth);
|
|
|
|
if (l.type == EEK) return l;
|
|
|
|
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
FunctionIdentifier plusident = FUNC_PLUS_SYMBOL;
|
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
|
|
|
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
|
|
|
{
|
|
|
|
++(*i);
|
2019-08-27 06:56:19 +00:00
|
|
|
Token r = piterm(n, i, meth);
|
2019-08-05 20:45:04 +00:00
|
|
|
if (meth == FULL)
|
|
|
|
{
|
|
|
|
Token result = (op==PLUS ? tadd(l,r) : tsub(l,r));
|
2019-08-27 06:56:19 +00:00
|
|
|
tfree_protected(&l, result);
|
|
|
|
tfree_protected(&r, result);
|
2019-08-05 20:45:04 +00:00
|
|
|
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;
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
l.type = FUNCALL;
|
2019-08-05 20:45:04 +00:00
|
|
|
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);
|
2019-08-27 06:56:19 +00:00
|
|
|
return duperror(&l, _("Exceeded maximum sequence length of +"));
|
2019-08-05 20:45:04 +00:00
|
|
|
}
|
|
|
|
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-08-27 06:56:19 +00:00
|
|
|
/* relterm -- parse and evaluate a relational term */ /*{{{*/
|
|
|
|
static Token relterm(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;
|
2019-07-22 20:32:33 +00:00
|
|
|
|
2019-08-27 06:56:19 +00:00
|
|
|
bool firstcomp = true;
|
|
|
|
Token result = l;
|
|
|
|
Operator op = CP;
|
|
|
|
|
|
|
|
/* a < b < c now means a < b and b < c, so we have to save both the running
|
|
|
|
result and the last term
|
|
|
|
*/
|
|
|
|
if (n[*i] != NULLTOKEN && n[*i]->type == OPERATOR) op = n[*i]->u.op;
|
|
|
|
|
|
|
|
while (IS_RELATION_OP(op))
|
|
|
|
{
|
2019-07-22 20:32:33 +00:00
|
|
|
++(*i);
|
2019-08-27 06:56:19 +00:00
|
|
|
Token r = factor(n, i, LITERAL);
|
2019-08-05 20:45:04 +00:00
|
|
|
if (meth == FULL)
|
2019-07-22 20:32:33 +00:00
|
|
|
{
|
2019-08-27 06:56:19 +00:00
|
|
|
if (!firstcomp && result.type == BOOL && !result.u.bl) {
|
|
|
|
tfree(&r);
|
|
|
|
} else {
|
|
|
|
Token tmp = evaltoken(r, FULL);
|
|
|
|
tfree_protected(&r, tmp);
|
|
|
|
r = tmp;
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case LT: tmp = tlt(l,r); break;
|
|
|
|
case LE: tmp = tle(l,r); break;
|
|
|
|
case GE: tmp = tge(l,r); break;
|
|
|
|
case GT: tmp = tgt(l,r); break;
|
|
|
|
case ISEQUAL: tmp = teq(l,r); break;
|
|
|
|
case ABOUTEQ: tmp = tabouteq(l,r); break;
|
|
|
|
case NE: tmp = tne(l,r); break;
|
|
|
|
default: assert(0);
|
|
|
|
}
|
|
|
|
tfree_protected(&l, result);
|
|
|
|
l = r;
|
|
|
|
if (firstcomp) {
|
|
|
|
result = tmp;
|
|
|
|
} else {
|
|
|
|
Token newres = tand(result, tmp);
|
|
|
|
tfree_protected(&result, newres);
|
|
|
|
tfree_protected(&tmp, newres);
|
|
|
|
result = newres;
|
|
|
|
}
|
|
|
|
if (result.type == EEK) {
|
|
|
|
tfree_protected(&l, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { /* meth = LITERAL */
|
|
|
|
if (r.type == EEK)
|
2019-08-05 20:45:04 +00:00
|
|
|
{
|
2019-08-27 06:56:19 +00:00
|
|
|
tfree(&l);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
Token newcomp;
|
|
|
|
newcomp.type = FUNCALL;
|
|
|
|
newcomp.u.funcall.fident = identcode(Op_Name[op], strlen(Op_Name[op]));
|
|
|
|
newcomp.u.funcall.argc = 2;
|
|
|
|
newcomp.u.funcall.argv = malloc(2*sizeof(Token));
|
|
|
|
newcomp.u.funcall.argv[0] = l;
|
|
|
|
newcomp.u.funcall.argv[1] = r;
|
|
|
|
l = r;
|
|
|
|
if (firstcomp) {
|
|
|
|
result = newcomp;
|
|
|
|
firstcomp = false;
|
|
|
|
} else if (result.u.fident != FUNC_AND) {
|
|
|
|
Token holdres = result;
|
|
|
|
result.u.funcall.fident = FUNC_AND;
|
|
|
|
result.u.funcall.argc = 2;
|
|
|
|
result.u.funcall.argv = malloc(MAXARGC*sizeof(Token));
|
|
|
|
result.u.funcall.argv[0] = holdres;
|
|
|
|
result.u.funcall.argv[1] = newcomp;
|
|
|
|
} else if (result.u.funcall.argc >= MAXARGC) {
|
|
|
|
tfree(&result);
|
|
|
|
return
|
|
|
|
duperror(&result,
|
|
|
|
_("Exeeded maximum sequence length of comparisons"));
|
|
|
|
} else {
|
|
|
|
result.u.funcall.argv[(result.u.funcall.argc)++] = newcomp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n[*i] != NULLTOKEN && n[*i]->type == OPERATOR) op = n[*i] -> u.op;
|
|
|
|
else op = CP;
|
|
|
|
} /* while next op is a comparison */
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/*}}}*/
|
|
|
|
|
|
|
|
/* boolterm -- parse and evaluate a boolean term */ /*{{{*/
|
|
|
|
static Token boolterm(Operator bop, Token *n[], int *i, EvalMethod meth)
|
|
|
|
{
|
|
|
|
assert (bop == LAND || bop == LOR);
|
|
|
|
Token l;
|
|
|
|
Operator op = CP;
|
|
|
|
bool first_funcall = true;
|
|
|
|
|
|
|
|
l = (bop == LAND) ? relterm(n, i, meth) : boolterm(LAND, n, i, meth);
|
|
|
|
if (l.type == EEK) return l;
|
|
|
|
if (n[*i] != NULLTOKEN && n[*i]->type == OPERATOR) op = n[*i]->u.op;
|
|
|
|
|
|
|
|
while (op == bop)
|
|
|
|
{
|
|
|
|
++(*i);
|
|
|
|
/* Need to evaluate right operand literally for the sake of short
|
|
|
|
circuiting
|
|
|
|
*/
|
|
|
|
Token r =
|
|
|
|
(bop == LAND) ? relterm(n, i, LITERAL) : boolterm(LAND, n, i, LITERAL);
|
|
|
|
if (meth == FULL) {
|
|
|
|
if (l.type == BOOL
|
|
|
|
&& ((bop == LAND && !l.u.bl) || (bop == LOR && l.u.bl)))
|
|
|
|
tfree(&r);
|
|
|
|
else {
|
|
|
|
Token result = evaltoken(r, FULL);
|
|
|
|
tfree_protected(&r, result);
|
|
|
|
Token tmp = (bop == LAND) ? tand(l, result) : tor(r, result);
|
|
|
|
tfree_protected(&result, tmp);
|
|
|
|
tfree_protected(&l, tmp);
|
|
|
|
if (tmp.type == EEK) return tmp;
|
|
|
|
l = tmp;
|
2019-08-05 20:45:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (r.type == EEK)
|
|
|
|
{
|
|
|
|
tfree(&l);
|
|
|
|
return r;
|
|
|
|
}
|
2019-08-27 06:56:19 +00:00
|
|
|
if (first_funcall)
|
|
|
|
{
|
|
|
|
first_funcall = false;
|
|
|
|
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(MAXARGC * 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);
|
|
|
|
const char* templ = _("Exceeded max sequence length of %s");
|
|
|
|
l.type = EEK;
|
|
|
|
l.u.err = malloc(strlen(templ) + MAX_OP_NAME_LENGTH + 1);
|
|
|
|
sprintf(l.u.err, templ, Op_Name[op]);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
l.u.funcall.argv[(l.u.funcall.argc)++] = r;
|
|
|
|
}
|
2019-07-22 20:32:33 +00:00
|
|
|
}
|
2019-08-27 06:56:19 +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;
|
|
|
|
}
|
|
|
|
/*}}}*/
|
|
|
|
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
/* eval -- parse and evaluate nonempty token sequence
|
|
|
|
if the sequence might be empty, use eval_safe. */ /*{{{*/
|
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;
|
|
|
|
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
assert(meth == LITERAL || upd_sheet != (Sheet*)0);
|
2019-08-05 20:45:04 +00:00
|
|
|
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);
|
2019-08-27 06:56:19 +00:00
|
|
|
tfree_protected(&l, result);
|
|
|
|
tfree_protected(&r, result);
|
2019-08-05 20:45:04 +00:00
|
|
|
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;
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
l.type = FUNCALL;
|
|
|
|
l.u.funcall.fident = FUNC_CONCAT;
|
2019-08-05 20:45:04 +00:00
|
|
|
l.u.funcall.argc = 1;
|
|
|
|
l.u.funcall.argv = malloc(MAXARGC*sizeof(Token));
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
l.u.funcall.argv[0] = tmp;
|
2019-08-05 20:45:04 +00:00
|
|
|
}
|
|
|
|
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;
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
if (n == EMPTY_TVEC || *n == NULLTOKEN)
|
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
|
|
|
{
|
|
|
|
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
|
|
|
}
|
Streamline internals of teapot
The primary change is to add a “funcall” token, so that an entire expression
can be encapsulated as a single token. This change is used to allow a cell to
include simply a selection of appropriate semantic tokens. I.e., the content
and iterative content are now each a single token like the value and the
result value. Token vectors are used only as intermediate results in scanning
and parsing.
Not this means the cells are now in effect storing parse trees, so
computation should be slightly faster and future extensions (like #56) should
be facilitated.
This commit also takes the opportunity while internals are being altered to
add another token to a cell for future use for computed attributes, cf #22,
and to change the internal numerical values from double and ints to long
doubles and long longs. However, the change attempts to encapsulate that
choice so it would be easy to change back or change to another representation.
Note that these changes break savexdr(), as the internal binary format of
a cell is now different. Rather than reimplement it, it is deprecated as
the world does not need another binary spreadsheet format. Hence, the
ascii format for teapot spreadsheets becomes the primary file format.
Loading of old xdr files is still supported for backward compatibility.
Closes #59.
Also along the way, various other slight fixes and enhancements crept in,
a partial but probably not exhaustive list of which follows:
Fixes #31.
Further revisions and improvements to documentation.
Make the approximate comparison of floating point values scale more
accurately with the size of the doubles being compared.
Further extensions of absolute and relative cell addressing.
Addition of (circle constant) tau function/constant.
Modified string conversion to simply use internal printing routines, and
to take "scientific" and "decimal" keywords.
Allowed n() function to take a list of values, or just a single location
defaulting to the current location.
Added floor, ceil, trunc, and round functions, and allowed them to be
keywords controlling the int() integer conversion function as well.
Allowed substr() to drop its last argument to go to the end of the string.
Provided an enum of built-in functions to preserve legacy function
identifiers, allowing the large table inside func.c to be reorganized
in a clearer fashion.
Added additional annotation of properties of the built-in functions,
including precedence.
All operators are now also accessible as built-in functions.
Made precedence of unary - lower than ^ to match python.
Avoided inadvertently using FLTK @symbol abbreviations for formulas with
"@" in them.
2019-08-23 19:12:06 +00:00
|
|
|
|
|
|
|
/* evaltoken -- like eval, but evaluate a single token
|
|
|
|
NOTE: caller owns the result and must arrrange that it be
|
|
|
|
eventually tfree()ed */ /*{{{*/
|
|
|
|
Token evaltoken(Token n, EvalMethod meth)
|
|
|
|
{
|
|
|
|
if (meth == LITERAL) return tcopy(n);
|
|
|
|
switch (n.type) {
|
|
|
|
case EMPTY: return n;
|
|
|
|
case STRING: return tcopy(n);
|
|
|
|
case FLOAT:
|
|
|
|
case INT:
|
|
|
|
return n;
|
|
|
|
case OPERATOR: {
|
|
|
|
Token err;
|
|
|
|
err.type = EEK;
|
|
|
|
const char *templ = _("Attempt to eval bare operator token: ");
|
|
|
|
err.u.err = malloc(strlen(templ) + MAX_OP_NAME_LENGTH + 2);
|
|
|
|
strcpy(err.u.err, templ);
|
|
|
|
strcat(err.u.err, Op_Name[n.u.op]);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
case LIDENT: return findlabel(upd_sheet, n.u.lident);
|
|
|
|
case FIDENT: {
|
|
|
|
Token err;
|
|
|
|
err.type = EEK;
|
|
|
|
const char *templ = _("Attempt to eval bare function identifier token: ");
|
|
|
|
err.u.err = malloc(strlen(templ) + MAX_FUNC_NAME_LENGTH + 2);
|
|
|
|
strcpy(err.u.err, templ);
|
|
|
|
strcat(err.u.err, tfunc[n.u.fident].name);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
case LOCATION: return n;
|
|
|
|
case FUNCALL: return full_eval_funcall(&n);
|
|
|
|
case EEK: return tcopy(n);
|
|
|
|
default: assert(0);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|