2004-03-03 21:24:06 +01:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
extern U *formal_arg[6];
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Example: f(x) = x^2
|
|
|
|
//
|
|
|
|
// p1 *-------*---------------*
|
|
|
|
// | | |
|
|
|
|
// setq *-------* *-------*-------*
|
|
|
|
// | | | | |
|
|
|
|
// f x power x 2
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define NAME p2
|
|
|
|
#define ARGS p3
|
|
|
|
#define BODY p4
|
|
|
|
#define BODY2 p5
|
|
|
|
#define TMP p6
|
|
|
|
|
|
|
|
void
|
|
|
|
define_user_function(void)
|
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
NAME = caadr(p1);
|
|
|
|
ARGS = cdadr(p1);
|
|
|
|
BODY = caddr(p1);
|
|
|
|
|
2005-08-06 22:57:37 +02:00
|
|
|
if (!issymbol(NAME))
|
|
|
|
stop("in function definition, user symbol expected for function name");
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
n = length(ARGS);
|
|
|
|
|
|
|
|
if (n > 6)
|
2005-08-06 22:57:37 +02:00
|
|
|
stop("more than 6 formal args in function definition");
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
// subst args in body
|
|
|
|
|
|
|
|
push(BODY);
|
|
|
|
TMP = ARGS;
|
|
|
|
for (i = 0; i < n; i++) {
|
2005-08-06 22:57:37 +02:00
|
|
|
if (!issymbol(car(TMP)))
|
|
|
|
stop("in function definition, formal arg is not a user symbol");
|
2004-03-03 21:24:06 +01:00
|
|
|
push(car(TMP));
|
|
|
|
push(formal_arg[i]);
|
|
|
|
subst();
|
|
|
|
TMP = cdr(TMP);
|
|
|
|
}
|
|
|
|
BODY2 = pop();
|
|
|
|
|
|
|
|
// binding
|
|
|
|
|
|
|
|
NAME->u.sym.binding = BODY;
|
|
|
|
NAME->u.sym.binding2 = BODY2;
|
|
|
|
|
|
|
|
// do eval, maybe
|
|
|
|
|
|
|
|
if (car(BODY) == symbol(EVAL)) {
|
|
|
|
|
|
|
|
// remove eval
|
|
|
|
|
|
|
|
NAME->u.sym.binding = cadr(BODY);
|
|
|
|
NAME->u.sym.binding2 = cadr(BODY2);
|
|
|
|
|
|
|
|
// build function call with quoted symbols
|
|
|
|
|
|
|
|
push(NAME);
|
|
|
|
TMP = ARGS;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
push_symbol(QUOTE);
|
|
|
|
push(car(TMP));
|
|
|
|
list(2);
|
|
|
|
TMP = cdr(TMP);
|
|
|
|
}
|
|
|
|
list(n + 1);
|
|
|
|
|
|
|
|
// eval
|
|
|
|
|
|
|
|
eval();
|
|
|
|
BODY = pop();
|
|
|
|
|
|
|
|
// subst args in body
|
|
|
|
|
|
|
|
push(BODY);
|
|
|
|
TMP = ARGS;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
push(car(TMP));
|
|
|
|
push(formal_arg[i]);
|
|
|
|
subst();
|
|
|
|
TMP = cdr(TMP);
|
|
|
|
}
|
|
|
|
BODY2 = pop();
|
|
|
|
|
|
|
|
// update bindings
|
|
|
|
|
|
|
|
NAME->u.sym.binding = BODY;
|
|
|
|
NAME->u.sym.binding2 = BODY2;
|
|
|
|
}
|
|
|
|
|
2006-01-16 20:37:31 +01:00
|
|
|
push(symbol(NIL)); // return value
|
2004-03-03 21:24:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: Tried doing func eval above using formal_arg and then back subst
|
|
|
|
// symbols from ARGLIST.
|
|
|
|
// The problem with this method is that the resulting BODY might be
|
|
|
|
// denormalized.
|
|
|
|
// In other words, the terms and factors are not sorted properly.
|
|
|
|
// This causes problems since add() and multiply() use a merge sort that
|
|
|
|
// requires sorted (normalized) terms and factors.
|
|
|
|
// The code above ensures a normalized BODY.
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Example: f(x,y)
|
|
|
|
//
|
|
|
|
// p1 -> (f x y)
|
|
|
|
//
|
|
|
|
// car(p1) -> f
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define FUNC_NAME p2
|
|
|
|
#define ACTUAL_ARGLIST p3
|
|
|
|
|
|
|
|
void
|
|
|
|
eval_user_function(void)
|
|
|
|
{
|
|
|
|
int h, i;
|
|
|
|
|
|
|
|
h = tos;
|
|
|
|
|
|
|
|
FUNC_NAME = car(p1);
|
|
|
|
ACTUAL_ARGLIST = cdr(p1);
|
|
|
|
|
|
|
|
// undefined function?
|
|
|
|
|
|
|
|
if (FUNC_NAME->u.sym.binding == FUNC_NAME) {
|
|
|
|
push(FUNC_NAME);
|
|
|
|
while (iscons(ACTUAL_ARGLIST)) {
|
|
|
|
push(car(ACTUAL_ARGLIST));
|
|
|
|
eval();
|
|
|
|
ACTUAL_ARGLIST = cdr(ACTUAL_ARGLIST);
|
|
|
|
}
|
|
|
|
list(tos - h);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is it a function?
|
|
|
|
|
2006-01-16 20:37:31 +01:00
|
|
|
if (FUNC_NAME->u.sym.binding2 == symbol(NIL))
|
2004-03-03 21:24:06 +01:00
|
|
|
stop("Attempt to call non-function");
|
|
|
|
|
|
|
|
// eval actual args in current formal arg context, don't modify formal args yet
|
|
|
|
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
push(car(ACTUAL_ARGLIST));
|
|
|
|
eval();
|
|
|
|
ACTUAL_ARGLIST = cdr(ACTUAL_ARGLIST);
|
2006-01-16 20:37:31 +01:00
|
|
|
push(symbol(NIL)); // make room for saving binding2
|
2004-03-03 21:24:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ok, now it's safe to modify formal args
|
|
|
|
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
TMP = formal_arg[i]->u.sym.binding;
|
|
|
|
formal_arg[i]->u.sym.binding = stack[h + 2 * i];
|
|
|
|
stack[h + 2 * i] = TMP;
|
|
|
|
TMP = formal_arg[i]->u.sym.binding2;
|
|
|
|
formal_arg[i]->u.sym.binding2 = stack[h + 2 * i + 1];
|
|
|
|
stack[h + 2 * i + 1] = TMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
// evaluate user-defined function
|
|
|
|
|
|
|
|
push(FUNC_NAME->u.sym.binding2);
|
|
|
|
eval();
|
|
|
|
|
|
|
|
// restore args
|
|
|
|
|
|
|
|
TMP = pop();
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
formal_arg[5 - i]->u.sym.binding2 = pop();
|
|
|
|
formal_arg[5 - i]->u.sym.binding = pop();
|
|
|
|
}
|
|
|
|
push(TMP);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
eval_binding2(void)
|
|
|
|
{
|
|
|
|
p1 = cadr(p1);
|
2005-08-06 22:57:37 +02:00
|
|
|
if (!issymbol(p1))
|
|
|
|
stop("symbol expected in binding2");
|
2004-03-03 21:24:06 +01:00
|
|
|
push(p1->u.sym.binding2);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Evaluate an expression with a symbol set to a value.
|
|
|
|
//
|
|
|
|
// Input: tos-3 expr
|
|
|
|
//
|
|
|
|
// tos-2 symbol
|
|
|
|
//
|
|
|
|
// tos-1 value
|
|
|
|
//
|
|
|
|
// Output: Result on stack
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define F p1
|
|
|
|
#define X p2
|
|
|
|
#define VAL p3
|
|
|
|
|
|
|
|
void
|
|
|
|
evalat(void)
|
|
|
|
{
|
|
|
|
int mark;
|
|
|
|
|
|
|
|
save();
|
|
|
|
|
|
|
|
VAL = pop();
|
|
|
|
X = pop();
|
|
|
|
|
2005-08-06 22:57:37 +02:00
|
|
|
if (!issymbol(X)) {
|
2004-03-03 21:24:06 +01:00
|
|
|
push(X);
|
|
|
|
push(VAL);
|
|
|
|
subst();
|
|
|
|
eval();
|
|
|
|
restore();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
push(X);
|
|
|
|
mark = save_symbols(1);
|
|
|
|
|
|
|
|
X->u.sym.binding = VAL;
|
2006-01-16 20:37:31 +01:00
|
|
|
X->u.sym.binding2 = symbol(NIL);
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
eval();
|
|
|
|
|
|
|
|
restore_symbols(mark);
|
|
|
|
|
|
|
|
restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *s[] = {
|
|
|
|
|
|
|
|
"f=quote(f)",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"x=quote(x)",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"y=quote(y)",
|
|
|
|
"",
|
|
|
|
|
|
|
|
// args of generic functions should be evaluated
|
|
|
|
|
|
|
|
"f(1+2,3*4)",
|
|
|
|
"f(3,12)",
|
|
|
|
|
|
|
|
// simple func def
|
|
|
|
|
|
|
|
"f(x)=x^2",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"f",
|
|
|
|
"x^2",
|
|
|
|
|
|
|
|
"binding2(f)",
|
2004-07-22 01:30:24 +02:00
|
|
|
"$1^2",
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
// symbols should be quoted in eval
|
|
|
|
|
|
|
|
"x=123",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"f(x)=eval(x^3)",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"f",
|
|
|
|
"x^3",
|
|
|
|
|
|
|
|
// bindings should be restored
|
|
|
|
|
|
|
|
"x=123",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"y=345",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"f(x,y)=x^2+y^3",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"f(2,3)",
|
|
|
|
"31",
|
|
|
|
|
|
|
|
"x",
|
|
|
|
"123",
|
|
|
|
|
|
|
|
"y",
|
|
|
|
"345",
|
|
|
|
|
|
|
|
// as above but this time with function bindings
|
|
|
|
|
|
|
|
"x(a)=sin(a)",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"y(b)=cos(b)",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"f(x,y)=x^2+y^3",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"f(2,3)",
|
|
|
|
"31",
|
|
|
|
|
|
|
|
"x",
|
|
|
|
"sin(a)",
|
|
|
|
|
|
|
|
"binding2(x)",
|
2004-07-22 01:30:24 +02:00
|
|
|
"sin($1)",
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
"y",
|
|
|
|
"cos(b)",
|
|
|
|
|
|
|
|
"binding2(y)",
|
2004-07-22 01:30:24 +02:00
|
|
|
"cos($1)",
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
|
|
|
|
"f=quote(f)",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"x=quote(x)",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"y=quote(y)",
|
|
|
|
"",
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
test_user_func(void)
|
|
|
|
{
|
|
|
|
test(__FILE__, s, sizeof s / sizeof (char *));
|
2004-06-09 04:45:50 +02:00
|
|
|
}
|