2006-09-22 01:54:47 +02:00
|
|
|
// Evaluate the 'for' function
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
eval_for(void)
|
|
|
|
{
|
2006-10-13 17:26:03 +02:00
|
|
|
push(cadr(p1)); /* index expr is quoted */
|
2006-09-22 01:54:47 +02:00
|
|
|
push(caddr(p1));
|
2006-10-06 20:28:26 +02:00
|
|
|
eval();
|
2006-09-22 01:54:47 +02:00
|
|
|
push(cadddr(p1));
|
2006-10-06 20:28:26 +02:00
|
|
|
eval();
|
2006-09-22 01:54:47 +02:00
|
|
|
push(cddddr(p1));
|
|
|
|
for_function();
|
|
|
|
}
|
|
|
|
|
2006-10-06 20:28:26 +02:00
|
|
|
/* Input: tos-4 Index expression
|
2004-03-03 21:24:06 +01:00
|
|
|
|
2006-10-06 20:28:26 +02:00
|
|
|
tos-3 Initial value
|
2006-09-22 01:54:47 +02:00
|
|
|
|
2006-10-06 20:28:26 +02:00
|
|
|
tos-2 Final value
|
|
|
|
|
|
|
|
tos-1 Statement list
|
|
|
|
|
|
|
|
Output: Result on stack
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define A p1
|
|
|
|
#define B p2
|
|
|
|
#define T p3
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
for_function(void)
|
|
|
|
{
|
|
|
|
int i, j, k;
|
|
|
|
|
|
|
|
save();
|
|
|
|
|
|
|
|
B = pop();
|
2006-10-06 20:28:26 +02:00
|
|
|
k = pop_integer();
|
|
|
|
j = pop_integer();
|
2004-03-03 21:24:06 +01:00
|
|
|
A = pop();
|
|
|
|
|
|
|
|
if (j == (int) 0x80000000)
|
2006-10-06 20:28:26 +02:00
|
|
|
stop("2nd arg of \"for\" function: integer expected");
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
if (k == (int) 0x80000000)
|
2006-10-06 20:28:26 +02:00
|
|
|
stop("3rd arg of \"for\" function: integer expected");
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
for (i = j; i <= k; i++) {
|
2006-09-22 01:54:47 +02:00
|
|
|
T = B;
|
|
|
|
while (iscons(T)) {
|
|
|
|
push(car(T));
|
2006-10-06 20:28:26 +02:00
|
|
|
push(A);
|
|
|
|
push_integer(i);
|
|
|
|
subst();
|
2006-09-22 01:54:47 +02:00
|
|
|
eval();
|
|
|
|
pop();
|
|
|
|
T = cdr(T);
|
|
|
|
}
|
2004-03-03 21:24:06 +01:00
|
|
|
}
|
|
|
|
|
2006-01-16 20:37:31 +01:00
|
|
|
push(symbol(NIL));
|
2004-03-03 21:24:06 +01:00
|
|
|
|
|
|
|
restore();
|
|
|
|
}
|
|
|
|
|
2007-05-08 16:57:30 +02:00
|
|
|
#if SELFTEST
|
|
|
|
|
2004-03-03 21:24:06 +01:00
|
|
|
static char *s[] = {
|
|
|
|
|
2006-09-22 01:54:47 +02:00
|
|
|
"x=0",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"y=2",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"for(k,1,9,x=sqrt(2+x),y=2y/x)",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"float(y)",
|
|
|
|
"3.14159",
|
2006-10-13 17:26:03 +02:00
|
|
|
|
|
|
|
/* ensure index expr is quoted */
|
|
|
|
|
|
|
|
"for(i,1,3,j=i)",
|
|
|
|
"",
|
|
|
|
|
|
|
|
"j",
|
|
|
|
"3",
|
2004-03-03 21:24:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
test_for(void)
|
|
|
|
{
|
|
|
|
test(__FILE__, s, sizeof s / sizeof (char *));
|
|
|
|
}
|
2007-05-08 16:57:30 +02:00
|
|
|
|
|
|
|
#endif
|