1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
#include "yacas/yacasprivate.h"
#include "yacas/mathuserfunc.h"
#include "yacas/lispobject.h"
#include "yacas/lispeval.h"
#include "yacas/standard.h"
#include "yacas/patterns.h"
#include "yacas/patternclass.h"
#include "yacas/substitute.h"
#define InternalEval aEnvironment.iEvaluator->Eval
bool BranchingUserFunction::BranchRule::Matches(LispEnvironment& aEnvironment, LispPtr* aArguments)
{
LispPtr pred;
InternalEval(aEnvironment, pred, iPredicate);
return IsTrue(aEnvironment,pred);
}
LispInt BranchingUserFunction::BranchRule::Precedence() const
{
return iPrecedence;
}
LispPtr& BranchingUserFunction::BranchRule::Body()
{
return iBody;
}
bool BranchingUserFunction::BranchRuleTruePredicate::Matches(LispEnvironment& aEnvironment, LispPtr* aArguments)
{
return true;
}
bool BranchingUserFunction::BranchPattern::Matches(LispEnvironment& aEnvironment, LispPtr* aArguments)
{
return iPatternClass->Matches(aEnvironment,aArguments);
}
LispInt BranchingUserFunction::BranchPattern::Precedence() const
{
return iPrecedence;
}
LispPtr& BranchingUserFunction::BranchPattern::Body()
{
return iBody;
}
BranchingUserFunction::BranchingUserFunction(LispPtr& aParameters)
: iParameters(),iRules(),iParamList(aParameters)
{
for (LispIterator iter(aParameters); iter.getObj(); ++iter)
{
if (!iter.getObj()->String())
throw LispErrCreatingUserFunction();
BranchParameter param(iter.getObj()->String());
iParameters.push_back(param);
}
}
BranchingUserFunction::~BranchingUserFunction()
{
for (BranchRuleBase* p: iRules)
delete p;
}
void BranchingUserFunction::Evaluate(LispPtr& aResult,LispEnvironment& aEnvironment,
LispPtr& aArguments)
{
LispInt arity = Arity();
LispInt i;
//hier
if (Traced())
{
LispPtr tr(LispSubList::New(aArguments));
TraceShowEnter(aEnvironment,tr);
tr = (nullptr);
}
LispIterator iter(aArguments);
++iter;
// unrollable arguments
LispPtr* arguments;
if (arity==0)
arguments = nullptr;
else
{
assert(arity>0);
arguments = NEW LispPtr[arity];
}
LocalArgs args(arguments);
// Walk over all arguments, evaluating them as necessary
for (i=0;i<arity;i++,++iter)
{
if (!iter.getObj())
throw LispErrWrongNumberOfArgs();
if (iParameters[i].iHold)
{
arguments[i] = (iter.getObj()->Copy());
}
else
{
//Check(iter.getObj(), KLispErrWrongNumberOfArgs); // checked above
InternalEval(aEnvironment, arguments[i], *iter);
}
}
if (Traced())
{
LispIterator iter(aArguments);
for (i=0;i<arity;i++)
{
TraceShowArg(aEnvironment,*++iter,arguments[i]);
}
}
// declare a new local stack.
LispLocalFrame frame(aEnvironment,Fenced());
// define the local variables.
for (i=0;i<arity;i++)
{
const LispString* variable = iParameters[i].iParameter;
// set the variable to the new value
aEnvironment.NewLocal(variable,arguments[i]);
}
// walk the rules database, returning the evaluated result if the
// predicate is true.
const std::size_t nrRules = iRules.size();
UserStackInformation &st = aEnvironment.iEvaluator->StackInformation();
for (std::size_t i=0;i<nrRules;i++)
{
BranchRuleBase* thisRule = iRules[i];
assert(thisRule);
st.iRulePrecedence = thisRule->Precedence();
bool matches = thisRule->Matches(aEnvironment, arguments);
if (matches)
{
st.iSide = 1;
InternalEval(aEnvironment, aResult, thisRule->Body());
goto FINISH;
}
// If rules got inserted, walk back
while (thisRule != iRules[i] && i>0) i--;
}
// No predicate was true: return a new expression with the evaluated
// arguments.
{
LispPtr full(aArguments->Copy());
if (arity == 0)
{
full->Nixed() = (nullptr);
}
else
{
full->Nixed() = (arguments[0]);
for (i=0;i<arity-1;i++)
{
arguments[i]->Nixed() = (arguments[i+1]);
}
}
aResult = (LispSubList::New(full));
}
FINISH:
if (Traced())
{
LispPtr tr(LispSubList::New(aArguments));
TraceShowLeave(aEnvironment, aResult,tr);
tr = (nullptr);
}
}
void BranchingUserFunction::HoldArgument(const LispString * aVariable)
{
const std::size_t nrc = iParameters.size();
for (std::size_t i=0; i < nrc; ++i)
{
if (iParameters[i].iParameter == aVariable)
iParameters[i].iHold = true;
}
}
LispInt BranchingUserFunction::Arity() const
{
return iParameters.size();
}
LispInt BranchingUserFunction::IsArity(LispInt aArity) const
{
return (Arity() == aArity);
}
void BranchingUserFunction::DeclareRule(LispInt aPrecedence, LispPtr& aPredicate,
LispPtr& aBody)
{
// New branching rule.
BranchRule* newRule = NEW BranchRule(aPrecedence,aPredicate,aBody);
if (!newRule)
throw LispErrCreatingRule();
InsertRule(aPrecedence,newRule);
}
void BranchingUserFunction::DeclareRule(LispInt aPrecedence, LispPtr& aBody)
{
// New branching rule.
BranchRule* newRule = NEW BranchRuleTruePredicate(aPrecedence,aBody);
if (!newRule)
throw LispErrCreatingRule();
InsertRule(aPrecedence,newRule);
}
void BranchingUserFunction::DeclarePattern(LispInt aPrecedence, LispPtr& aPredicate,
LispPtr& aBody)
{
// New branching rule.
BranchPattern* newRule = NEW BranchPattern(aPrecedence,aPredicate,aBody);
if (!newRule)
throw LispErrCreatingRule();
InsertRule(aPrecedence,newRule);
}
void BranchingUserFunction::InsertRule(LispInt aPrecedence,BranchRuleBase* newRule)
{
// Find place to insert
LispInt low,high,mid;
low=0;
high=iRules.size();
// Constant time: find out if the precedence is before any of the
// currently defined rules or past them.
if (high>0)
{
if (iRules[0]->Precedence() > aPrecedence)
{
mid=0;
goto CONTINUE;
}
if (iRules[high-1]->Precedence() < aPrecedence)
{
mid=high;
goto CONTINUE;
}
}
// Otherwise, O(log n) search algorithm for place to insert
for(;;)
{
if (low>=high)
{
mid=low;
goto CONTINUE;
}
mid = (low+high)>>1;
if (iRules[mid]->Precedence() > aPrecedence)
{
high = mid;
}
else if (iRules[mid]->Precedence() < aPrecedence)
{
low = (++mid);
}
else
{
goto CONTINUE;
}
}
CONTINUE:
// Insert it
iRules.insert(iRules.begin() + mid, newRule);
}
const LispPtr& BranchingUserFunction::ArgList() const
{
return iParamList;
}
ListedBranchingUserFunction::ListedBranchingUserFunction(LispPtr& aParameters)
: BranchingUserFunction(aParameters)
{
}
LispInt ListedBranchingUserFunction::IsArity(LispInt aArity) const
{
// nr arguments handled is bound by a minimum: the number of arguments
// to this function.
return Arity() <= aArity;
}
void ListedBranchingUserFunction::Evaluate(LispPtr& aResult,LispEnvironment& aEnvironment,
LispPtr& aArguments)
{
LispPtr newArgs;
LispIterator iter(aArguments);
LispPtr* ptr = &newArgs;
const LispInt arity = Arity();
// Make a copy of the arguments first
// TODO: if we were to change the internal representation to a cons cell, this copying would not be needed
for (LispInt i = 0; i < arity && iter.getObj(); ++i,++iter)
{
*ptr = iter.getObj()->Copy();
ptr = &((*ptr)->Nixed());
}
if (!iter.getObj()->Nixed())
{
(*ptr) = (iter.getObj()->Copy());
++iter;
assert(!iter.getObj());
}
else
{
LispPtr head(aEnvironment.iList->Copy());
head->Nixed() = iter.getObj();
*ptr = (LispSubList::New(head));
}
BranchingUserFunction::Evaluate(aResult, aEnvironment, newArgs);
}
MacroUserFunction::MacroUserFunction(LispPtr& aParameters)
: BranchingUserFunction(aParameters)
{
LispIterator iter(aParameters);
for (LispInt i=0; iter.getObj(); i++,++iter)
{
if (!iter.getObj()->String())
throw LispErrCreatingUserFunction();
iParameters[i].iHold = true;
}
UnFence();
}
void MacroUserFunction::Evaluate(LispPtr& aResult,LispEnvironment& aEnvironment,
LispPtr& aArguments)
{
LispInt arity = Arity();
LispInt i;
if (Traced())
{
LispPtr tr(LispSubList::New(aArguments));
TraceShowEnter(aEnvironment,tr);
tr = (nullptr);
}
LispIterator iter(aArguments);
++iter;
// unrollable arguments
LispPtr* arguments;
if (arity==0)
arguments = nullptr;
else
{
assert(arity>0);
arguments = NEW LispPtr[arity];
}
LocalArgs args(arguments);
// Walk over all arguments, evaluating them as necessary
for (i=0;i<arity;i++,++iter)
{
if (!iter.getObj())
throw LispErrWrongNumberOfArgs();
if (iParameters[i].iHold)
{
arguments[i] = (iter.getObj()->Copy());
}
else
{
InternalEval(aEnvironment, arguments[i], *iter);
}
}
if (Traced())
{
LispIterator iter(aArguments);
// TODO: ideally we would only need an iterator here
++iter;
for (i=0;i<arity;i++)
{
TraceShowArg(aEnvironment,*iter,arguments[i]);
++iter;
}
}
LispPtr substedBody;
{
// declare a new local stack.
LispLocalFrame frame(aEnvironment,false);
// define the local variables.
for (i=0;i<arity;i++)
{
const LispString* variable = iParameters[i].iParameter;
// set the variable to the new value
aEnvironment.NewLocal(variable,arguments[i]);
}
// walk the rules database, returning the evaluated result if the
// predicate is true.
const std::size_t nrRules = iRules.size();
UserStackInformation &st = aEnvironment.iEvaluator->StackInformation();
for (std::size_t i=0;i<nrRules;i++)
{
BranchRuleBase* thisRule = iRules[i];
assert(thisRule);
st.iRulePrecedence = thisRule->Precedence();
bool matches = thisRule->Matches(aEnvironment, arguments);
if (matches)
{
st.iSide = 1;
BackQuoteBehaviour behaviour(aEnvironment);
InternalSubstitute(substedBody, thisRule->Body(), behaviour);
break;
}
// If rules got inserted, walk back
while (thisRule != iRules[i] && i>0) i--;
}
}
if (!!substedBody)
{
InternalEval(aEnvironment, aResult, substedBody);
}
else
// No predicate was true: return a new expression with the evaluated
// arguments.
{
LispPtr full(aArguments->Copy());
if (arity == 0)
{
full->Nixed() = (nullptr);
}
else
{
full->Nixed() = (arguments[0]);
for (i=0;i<arity-1;i++)
{
arguments[i]->Nixed() = (arguments[i+1]);
}
}
aResult = (LispSubList::New(full));
}
if (Traced())
{
LispPtr tr(LispSubList::New(aArguments));
TraceShowLeave(aEnvironment, aResult,tr);
tr = (nullptr);
}
}
ListedMacroUserFunction::ListedMacroUserFunction(LispPtr& aParameters)
: MacroUserFunction(aParameters)
{
}
LispInt ListedMacroUserFunction::IsArity(LispInt aArity) const
{
return Arity() <= aArity;
}
void ListedMacroUserFunction::Evaluate(LispPtr& aResult,LispEnvironment& aEnvironment,
LispPtr& aArguments)
{
LispPtr newArgs;
LispIterator iter(aArguments);
LispPtr* ptr = &newArgs;
LispInt arity = Arity();
LispInt i=0;
// TODO: the code would look a lot easier if we could do with only an iterator
while (i < arity && iter.getObj())
{
(*ptr) = (iter.getObj()->Copy());
ptr = &((*ptr)->Nixed());
i++;
++iter;
}
if (!iter.getObj()->Nixed()) {
*ptr = iter.getObj()->Copy();
(*ptr)->Nixed();
++iter;
assert(!iter.getObj());
} else {
LispPtr head(aEnvironment.iList->Copy());
head->Nixed() = iter.getObj();
*ptr = LispSubList::New(head);
}
MacroUserFunction::Evaluate(aResult, aEnvironment, newArgs);
}
|