PageRenderTime 105ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/jade_0_6/jade/style/primitive.cxx

#
C++ | 2517 lines | 2365 code | 121 blank | 31 comment | 529 complexity | 1cbb77e064a2bbd980aa0cd9b94386cb MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0
  1. // Copyright (c) 1996 James Clark
  2. // See the file copying.txt for copying permission.
  3. #include "stylelib.h"
  4. #include "Interpreter.h"
  5. #include "InterpreterMessages.h"
  6. #include "EvalContext.h"
  7. #include "SosofoObj.h"
  8. #include "Style.h"
  9. #include "macros.h"
  10. #include "ELObjMessageArg.h"
  11. #include "LocNode.h"
  12. #include <math.h>
  13. #include <limits.h>
  14. #include <stdio.h>
  15. #include <time.h>
  16. #ifdef DSSSL_NAMESPACE
  17. namespace DSSSL_NAMESPACE {
  18. #endif
  19. class DescendantsNodeListObj : public NodeListObj {
  20. public:
  21. void *operator new(size_t, Collector &c) {
  22. return c.allocateObject(1);
  23. }
  24. DescendantsNodeListObj(const NodePtr &, unsigned = 0);
  25. NodePtr nodeListFirst(EvalContext &, Interpreter &);
  26. NodeListObj *nodeListRest(EvalContext &, Interpreter &);
  27. NodeListObj *nodeListChunkRest(EvalContext &, Interpreter &, bool &);
  28. private:
  29. static void advance(NodePtr &, unsigned &);
  30. static void chunkAdvance(NodePtr &, unsigned &);
  31. // nodes in node list are strictly after this node
  32. NodePtr start_;
  33. unsigned depth_;
  34. };
  35. class SiblingNodeListObj : public NodeListObj {
  36. public:
  37. void *operator new(size_t, Collector &c) {
  38. return c.allocateObject(1);
  39. }
  40. SiblingNodeListObj(const NodePtr &first, const NodePtr &end);
  41. NodePtr nodeListFirst(EvalContext &, Interpreter &);
  42. NodeListObj *nodeListRest(EvalContext &, Interpreter &);
  43. NodeListObj *nodeListChunkRest(EvalContext &, Interpreter &, bool &);
  44. private:
  45. NodePtr first_;
  46. NodePtr end_;
  47. };
  48. class SelectByClassNodeListObj : public NodeListObj {
  49. public:
  50. SelectByClassNodeListObj(NodeListObj *nl, ComponentName::Id);
  51. NodePtr nodeListFirst(EvalContext &, Interpreter &);
  52. NodeListObj *nodeListRest(EvalContext &, Interpreter &);
  53. NodeListObj *nodeListChunkRest(EvalContext &, Interpreter &, bool &);
  54. void traceSubObjects(Collector &) const;
  55. private:
  56. NodeListObj *nodeList_;
  57. ComponentName::Id cls_;
  58. };
  59. class MapNodeListObj : public NodeListObj {
  60. public:
  61. class Context : public Resource {
  62. public:
  63. Context(const EvalContext &, const Location &);
  64. void set(EvalContext &) const;
  65. void traceSubObjects(Collector &) const;
  66. Location loc;
  67. private:
  68. NodePtr currentNode_;
  69. const ProcessingMode *processingMode_;
  70. StyleObj *overridingStyle_;
  71. bool haveStyleStack_;
  72. };
  73. void *operator new(size_t, Collector &c) {
  74. return c.allocateObject(1);
  75. }
  76. MapNodeListObj(FunctionObj *func, NodeListObj *nl, const ConstPtr<Context> &, NodeListObj *mapped = 0);
  77. NodePtr nodeListFirst(EvalContext &, Interpreter &);
  78. NodeListObj *nodeListRest(EvalContext &, Interpreter &);
  79. void traceSubObjects(Collector &) const;
  80. bool suppressError();
  81. private:
  82. void mapNext(EvalContext &, Interpreter &);
  83. FunctionObj *func_;
  84. NodeListObj *nl_;
  85. NodeListObj *mapped_;
  86. ConstPtr<Context> context_;
  87. };
  88. class ElementPattern : public Resource {
  89. public:
  90. virtual ~ElementPattern();
  91. virtual bool matches(const NodePtr &, SdataMapper &) const = 0;
  92. };
  93. class SelectElementsNodeListObj : public NodeListObj {
  94. public:
  95. void *operator new(size_t, Collector &c) {
  96. return c.allocateObject(1);
  97. }
  98. SelectElementsNodeListObj(NodeListObj *, const ConstPtr<ElementPattern> &);
  99. void traceSubObjects(Collector &) const;
  100. NodePtr nodeListFirst(EvalContext &, Interpreter &);
  101. NodeListObj *nodeListRest(EvalContext &, Interpreter &);
  102. private:
  103. NodeListObj *nodeList_;
  104. ConstPtr<ElementPattern> pattern_;
  105. };
  106. class UnionElementPattern : public ElementPattern {
  107. public:
  108. UnionElementPattern(const ConstPtr<ElementPattern> &,
  109. const ConstPtr<ElementPattern> &);
  110. bool matches(const NodePtr &, SdataMapper &) const;
  111. private:
  112. ConstPtr<ElementPattern> pat1_;
  113. ConstPtr<ElementPattern> pat2_;
  114. };
  115. class SimpleElementPattern : public ElementPattern {
  116. public:
  117. SimpleElementPattern(Vector<StringC> &);
  118. bool matches(const NodePtr &, SdataMapper &) const;
  119. private:
  120. // gi followed by att name/value pairs.
  121. // empty gi matches any gi
  122. Vector<StringC> giAtts_;
  123. };
  124. class ParentElementPattern : public ElementPattern {
  125. public:
  126. ParentElementPattern(const ConstPtr<ElementPattern> &pat,
  127. const ConstPtr<ElementPattern> &parentPat);
  128. bool matches(const NodePtr &, SdataMapper &) const;
  129. private:
  130. ConstPtr<ElementPattern> pat_;
  131. ConstPtr<ElementPattern> parentPat_;
  132. };
  133. class NoElementPattern : public ElementPattern {
  134. public:
  135. NoElementPattern() { }
  136. bool matches(const NodePtr &, SdataMapper &) const;
  137. };
  138. #define PRIMITIVE(name, string, nRequired, nOptional, rest) \
  139. class name ## PrimitiveObj : public PrimitiveObj { \
  140. public: \
  141. static const Signature signature_; \
  142. name ## PrimitiveObj() : PrimitiveObj(&signature_) { } \
  143. ELObj *primitiveCall(int, ELObj **, EvalContext &, Interpreter &, const Location &); \
  144. }; \
  145. const Signature name ## PrimitiveObj::signature_ \
  146. = { nRequired, nOptional, rest };
  147. #define XPRIMITIVE PRIMITIVE
  148. #include "primitive.h"
  149. #undef PRIMITIVE
  150. #undef XPRIMITIVE
  151. #define DEFPRIMITIVE(name, argc, argv, context, interp, loc) \
  152. ELObj *name ## PrimitiveObj \
  153. ::primitiveCall(int argc, ELObj **argv, EvalContext &context, Interpreter &interp, \
  154. const Location &loc)
  155. DEFPRIMITIVE(Cons, argc, argv, context, interp, loc)
  156. {
  157. return new (interp) PairObj(argv[0], argv[1]);
  158. }
  159. DEFPRIMITIVE(List, argc, argv, context, interp, loc)
  160. {
  161. if (argc == 0)
  162. return interp.makeNil();
  163. PairObj *head = new (interp) PairObj(argv[0], 0);
  164. ELObjDynamicRoot protect(interp, head);
  165. PairObj *tail = head;
  166. for (int i = 1; i < argc; i++) {
  167. PairObj *tem = new (interp) PairObj(argv[i], 0);
  168. tail->setCdr(tem);
  169. tail = tem;
  170. }
  171. tail->setCdr(interp.makeNil());
  172. return head;
  173. }
  174. DEFPRIMITIVE(IsNull, argc, argv, context, interp, loc)
  175. {
  176. if (argv[0]->isNil())
  177. return interp.makeTrue();
  178. else
  179. return interp.makeFalse();
  180. }
  181. DEFPRIMITIVE(IsList, argc, argv, context, interp, loc)
  182. {
  183. ELObj *obj = argv[0];
  184. for (;;) {
  185. PairObj *pair = obj->asPair();
  186. if (pair)
  187. obj = pair->cdr();
  188. else if (obj->isNil())
  189. return interp.makeTrue();
  190. else
  191. break;
  192. }
  193. return interp.makeFalse();
  194. }
  195. DEFPRIMITIVE(IsEqual, argc, argv, context, interp, loc)
  196. {
  197. if (*argv[0] == *argv[1])
  198. return interp.makeTrue();
  199. else
  200. return interp.makeFalse();
  201. }
  202. DEFPRIMITIVE(Car, argc, argv, context, interp, loc)
  203. {
  204. PairObj *pair = argv[0]->asPair();
  205. if (!pair)
  206. return argError(interp, loc,
  207. InterpreterMessages::notAPair, 0, argv[0]);
  208. else
  209. return pair->car();
  210. }
  211. DEFPRIMITIVE(Cdr, argc, argv, context, interp, loc)
  212. {
  213. PairObj *pair = argv[0]->asPair();
  214. if (!pair)
  215. return argError(interp, loc,
  216. InterpreterMessages::notAPair, 0, argv[0]);
  217. else
  218. return pair->cdr();
  219. }
  220. DEFPRIMITIVE(Append, argc, argv, context, interp, loc)
  221. {
  222. if (argc == 0)
  223. return interp.makeNil();
  224. PairObj *tail = interp.makePair(0, 0);
  225. PairObj *head = tail;
  226. ELObjDynamicRoot protect(interp, head);
  227. for (int i = 0; i < argc - 1; i++) {
  228. for (ELObj *p = argv[i]; !p->isNil();) {
  229. PairObj *tem = p->asPair();
  230. if (!tem)
  231. return argError(interp, loc,
  232. InterpreterMessages::notAList, i, p);
  233. PairObj *newTail = new (interp) PairObj(tem->car(), 0);
  234. tail->setCdr(newTail);
  235. tail = newTail;
  236. p = tem->cdr();
  237. }
  238. }
  239. tail->setCdr(argv[argc - 1]);
  240. return head->cdr();
  241. }
  242. DEFPRIMITIVE(Reverse, argc, argv, context, interp, loc)
  243. {
  244. ELObjDynamicRoot protect(interp, interp.makeNil());
  245. ELObj *p = argv[0];
  246. while (!p->isNil()) {
  247. PairObj *tem = p->asPair();
  248. if (!tem)
  249. return argError(interp, loc,
  250. InterpreterMessages::notAList, 0, argv[0]);
  251. protect = new (interp) PairObj(tem->car(), protect);
  252. p = tem->cdr();
  253. }
  254. return protect;
  255. }
  256. DEFPRIMITIVE(ListTail, argc, argv, context, interp, loc)
  257. {
  258. long k;
  259. if (!argv[1]->exactIntegerValue(k))
  260. return argError(interp, loc,
  261. InterpreterMessages::notAnExactInteger, 1, argv[1]);
  262. if (k < 0) {
  263. interp.setNextLocation(loc);
  264. interp.message(InterpreterMessages::outOfRange);
  265. return interp.makeError();
  266. }
  267. ELObj *p = argv[0];
  268. for (; k > 0; k--) {
  269. PairObj *tem = p->asPair();
  270. if (!tem) {
  271. if (p->isNil()) {
  272. interp.setNextLocation(loc);
  273. interp.message(InterpreterMessages::outOfRange);
  274. return interp.makeError();
  275. }
  276. else
  277. return argError(interp, loc,
  278. InterpreterMessages::notAList, 0, argv[0]);
  279. }
  280. p = tem->cdr();
  281. }
  282. return p;
  283. }
  284. DEFPRIMITIVE(ListRef, argc, argv, context, interp, loc)
  285. {
  286. long k;
  287. if (!argv[1]->exactIntegerValue(k))
  288. return argError(interp, loc,
  289. InterpreterMessages::notAnExactInteger, 1, argv[1]);
  290. if (k < 0) {
  291. interp.setNextLocation(loc);
  292. interp.message(InterpreterMessages::outOfRange);
  293. return interp.makeError();
  294. }
  295. ELObj *p = argv[0];
  296. for (;;) {
  297. PairObj *tem = p->asPair();
  298. if (!tem)
  299. break;
  300. if (k == 0)
  301. return tem->car();
  302. --k;
  303. p = tem->cdr();
  304. }
  305. if (p->isNil()) {
  306. interp.setNextLocation(loc);
  307. interp.message(InterpreterMessages::outOfRange);
  308. return interp.makeError();
  309. }
  310. else
  311. return argError(interp, loc,
  312. InterpreterMessages::notAList, 0, argv[0]);
  313. }
  314. DEFPRIMITIVE(Member, argc, argv, context, interp, loc)
  315. {
  316. ELObj *p = argv[1];
  317. while (!p->isNil()) {
  318. PairObj *tem = p->asPair();
  319. if (!tem)
  320. return argError(interp, loc,
  321. InterpreterMessages::notAList, 1, argv[1]);
  322. if (*argv[0] == *tem->car())
  323. return p;
  324. p = tem->cdr();
  325. }
  326. return interp.makeFalse();
  327. }
  328. DEFPRIMITIVE(Length, argc, argv, context, interp, loc)
  329. {
  330. ELObj *obj = argv[0];
  331. long n = 0;
  332. for (;;) {
  333. PairObj *pair = obj->asPair();
  334. if (pair) {
  335. n++;
  336. obj = pair->cdr();
  337. }
  338. else if (obj->isNil())
  339. break;
  340. else if (interp.isError(obj))
  341. return obj;
  342. else
  343. return argError(interp, loc,
  344. InterpreterMessages::notAList, 0, obj);
  345. }
  346. return interp.makeInteger(n);
  347. }
  348. DEFPRIMITIVE(Not, argc, argv, context, interp, loc)
  349. {
  350. if (argv[0]->isTrue())
  351. return interp.makeFalse();
  352. else
  353. return interp.makeTrue();
  354. }
  355. DEFPRIMITIVE(IsSymbol, argc, argv, context, interp, loc)
  356. {
  357. if (argv[0]->asSymbol())
  358. return interp.makeTrue();
  359. else
  360. return interp.makeFalse();
  361. }
  362. DEFPRIMITIVE(IsKeyword, argc, argv, context, interp, loc)
  363. {
  364. if (argv[0]->asKeyword())
  365. return interp.makeTrue();
  366. else
  367. return interp.makeFalse();
  368. }
  369. DEFPRIMITIVE(IsInteger, argc, argv, context, interp, loc)
  370. {
  371. long n;
  372. if (argv[0]->exactIntegerValue(n))
  373. return interp.makeTrue();
  374. double x;
  375. if (argv[0]->realValue(x) && modf(x, &x) == 0.0)
  376. return interp.makeTrue();
  377. else
  378. return interp.makeFalse();
  379. }
  380. DEFPRIMITIVE(IsReal, argc, argv, context, interp, loc)
  381. {
  382. double x;
  383. if (argv[0]->realValue(x))
  384. return interp.makeTrue();
  385. else
  386. return interp.makeFalse();
  387. }
  388. DEFPRIMITIVE(IsNumber, argc, argv, context, interp, loc)
  389. {
  390. double x;
  391. if (argv[0]->realValue(x))
  392. return interp.makeTrue();
  393. else
  394. return interp.makeFalse();
  395. }
  396. DEFPRIMITIVE(IsQuantity, argc, argv, context, interp, loc)
  397. {
  398. long n;
  399. double d;
  400. int dim;
  401. if (argv[0]->quantityValue(n, d, dim) != ELObj::noQuantity)
  402. return interp.makeTrue();
  403. else
  404. return interp.makeFalse();
  405. }
  406. DEFPRIMITIVE(IsPair, argc, argv, context, interp, loc)
  407. {
  408. if (argv[0]->asPair())
  409. return interp.makeTrue();
  410. else
  411. return interp.makeFalse();
  412. }
  413. DEFPRIMITIVE(IsProcedure, argc, argv, context, interp, loc)
  414. {
  415. if (argv[0]->asFunction())
  416. return interp.makeTrue();
  417. else
  418. return interp.makeFalse();
  419. }
  420. DEFPRIMITIVE(IsBoolean, argc, argv, context, interp, loc)
  421. {
  422. if (argv[0] == interp.makeTrue())
  423. return argv[0];
  424. else if (argv[0] == interp.makeFalse())
  425. return interp.makeTrue();
  426. else
  427. return interp.makeFalse();
  428. }
  429. DEFPRIMITIVE(IsChar, argc, argv, context, interp, loc)
  430. {
  431. Char c;
  432. if (argv[0]->charValue(c))
  433. return interp.makeTrue();
  434. else
  435. return interp.makeFalse();
  436. }
  437. DEFPRIMITIVE(IsCharEqual, argc, argv, context, interp, loc)
  438. {
  439. Char c1, c2;
  440. if (!argv[0]->charValue(c1))
  441. return argError(interp, loc,
  442. InterpreterMessages::notAChar, 0, argv[0]);
  443. if (!argv[1]->charValue(c2))
  444. return argError(interp, loc,
  445. InterpreterMessages::notAChar, 1, argv[1]);
  446. if (c1 == c2)
  447. return interp.makeTrue();
  448. else
  449. return interp.makeFalse();
  450. }
  451. DEFPRIMITIVE(String, argc, argv, context, interp, loc)
  452. {
  453. StringObj *obj = new (interp) StringObj;
  454. for (int i = 0; i < argc; i++) {
  455. Char c;
  456. if (!argv[i]->charValue(c))
  457. return argError(interp, loc,
  458. InterpreterMessages::notAChar, i, argv[i]);
  459. *obj += c;
  460. }
  461. return obj;
  462. }
  463. DEFPRIMITIVE(SymbolToString, argc, argv, context, interp, loc)
  464. {
  465. SymbolObj *obj = argv[0]->asSymbol();
  466. if (!obj)
  467. return argError(interp, loc,
  468. InterpreterMessages::notASymbol, 0, argv[0]);
  469. return obj->name();
  470. }
  471. DEFPRIMITIVE(StringToSymbol, argc, argv, context, interp, loc)
  472. {
  473. const Char *s;
  474. size_t n;
  475. if (!argv[0]->stringData(s, n))
  476. return argError(interp, loc,
  477. InterpreterMessages::notAString, 0, argv[0]);
  478. return interp.makeSymbol(StringC(s, n));
  479. }
  480. DEFPRIMITIVE(IsString, argc, argv, context, interp, loc)
  481. {
  482. const Char *s;
  483. size_t n;
  484. if (argv[0]->stringData(s, n))
  485. return interp.makeTrue();
  486. else
  487. return interp.makeFalse();
  488. }
  489. DEFPRIMITIVE(StringLength, argc, argv, context, interp, loc)
  490. {
  491. const Char *s;
  492. size_t n;
  493. if (!argv[0]->stringData(s, n))
  494. return argError(interp, loc,
  495. InterpreterMessages::notAString, 0, argv[0]);
  496. return interp.makeInteger(n);
  497. }
  498. DEFPRIMITIVE(IsStringEqual, argc, argv, context, interp, loc)
  499. {
  500. const Char *s1, *s2;
  501. size_t n1, n2;
  502. if (!argv[0]->stringData(s1, n1))
  503. return argError(interp, loc,
  504. InterpreterMessages::notAString, 0, argv[0]);
  505. if (!argv[1]->stringData(s2, n2))
  506. return argError(interp, loc,
  507. InterpreterMessages::notAString, 1, argv[1]);
  508. if (n1 == n2
  509. && (n1 == 0 || memcmp(s1, s2, n1*sizeof(Char)) == 0))
  510. return interp.makeTrue();
  511. else
  512. return interp.makeFalse();
  513. }
  514. DEFPRIMITIVE(StringAppend, argc, argv, context, interp, loc)
  515. {
  516. StringObj *result = new (interp) StringObj;
  517. for (int i = 0; i < argc; i++) {
  518. const Char *s;
  519. size_t n;
  520. if (!argv[i]->stringData(s, n))
  521. return argError(interp, loc,
  522. InterpreterMessages::notAString, i,
  523. argv[i]);
  524. result->append(s, n);
  525. }
  526. return result;
  527. }
  528. DEFPRIMITIVE(StringRef, argc, argv, context, interp, loc)
  529. {
  530. const Char *s;
  531. size_t n;
  532. if (!argv[0]->stringData(s, n))
  533. return argError(interp, loc,
  534. InterpreterMessages::notAString, 0, argv[0]);
  535. long k;
  536. if (!argv[1]->exactIntegerValue(k))
  537. return argError(interp, loc,
  538. InterpreterMessages::notAnExactInteger, 1, argv[1]);
  539. if (k < 0 || (unsigned long)k >= n) {
  540. interp.setNextLocation(loc);
  541. interp.message(InterpreterMessages::outOfRange);
  542. return interp.makeError();
  543. }
  544. return interp.makeChar(s[size_t(k)]);
  545. }
  546. DEFPRIMITIVE(Substring, argc, argv, context, interp, loc)
  547. {
  548. const Char *s;
  549. size_t n;
  550. if (!argv[0]->stringData(s, n))
  551. return argError(interp, loc,
  552. InterpreterMessages::notAString, 0, argv[0]);
  553. long start;
  554. if (!argv[1]->exactIntegerValue(start))
  555. return argError(interp, loc,
  556. InterpreterMessages::notAnExactInteger, 1, argv[1]);
  557. long end;
  558. if (!argv[2]->exactIntegerValue(end))
  559. return argError(interp, loc,
  560. InterpreterMessages::notAnExactInteger, 2, argv[2]);
  561. if (start < 0 || (unsigned long)end > n || start > end) {
  562. interp.setNextLocation(loc);
  563. interp.message(InterpreterMessages::outOfRange);
  564. return interp.makeError();
  565. }
  566. return new (interp) StringObj(s + size_t(start), size_t(end - start));
  567. }
  568. DEFPRIMITIVE(Equal, argc, argv, context, interp, loc)
  569. {
  570. if (argc == 0)
  571. return interp.makeTrue();
  572. long lResult;
  573. double dResult;
  574. int dim;
  575. int i = 1;
  576. switch (argv[0]->quantityValue(lResult, dResult, dim)) {
  577. case ELObj::noQuantity:
  578. return argError(interp, loc,
  579. InterpreterMessages::notAQuantity, 0, argv[0]);
  580. case ELObj::longQuantity:
  581. break;
  582. case ELObj::doubleQuantity:
  583. goto useDouble;
  584. break;
  585. default:
  586. CANNOT_HAPPEN();
  587. }
  588. long lResult2;
  589. double dResult2;
  590. int dim2;
  591. for (; i < argc; i++) {
  592. switch (argv[i]->quantityValue(lResult2, dResult2, dim2)) {
  593. case ELObj::noQuantity:
  594. return argError(interp, loc,
  595. InterpreterMessages::notAQuantity, i, argv[i]);
  596. case ELObj::longQuantity:
  597. if (lResult2 != lResult || dim2 != dim)
  598. return interp.makeFalse();
  599. break;
  600. case ELObj::doubleQuantity:
  601. dResult = lResult;
  602. if (dResult2 != dResult || dim2 != dim)
  603. return interp.makeFalse();
  604. i++;
  605. goto useDouble;
  606. default:
  607. CANNOT_HAPPEN();
  608. }
  609. }
  610. return interp.makeTrue();
  611. useDouble:
  612. for (; i < argc; i++) {
  613. switch (argv[i]->quantityValue(lResult2, dResult2, dim2)) {
  614. case ELObj::noQuantity:
  615. return argError(interp, loc,
  616. InterpreterMessages::notAQuantity, i, argv[i]);
  617. case ELObj::longQuantity:
  618. if (lResult2 != dResult || dim2 != dim)
  619. return interp.makeFalse();
  620. break;
  621. case ELObj::doubleQuantity:
  622. if (dResult2 != dResult || dim2 != dim)
  623. return interp.makeFalse();
  624. break;
  625. }
  626. }
  627. return interp.makeTrue();
  628. }
  629. DEFPRIMITIVE(Plus, argc, argv, context, interp, loc)
  630. {
  631. if (argc == 0)
  632. return interp.makeInteger(0);
  633. long lResult;
  634. double dResult;
  635. bool usingD;
  636. int dim;
  637. switch (argv[0]->quantityValue(lResult, dResult, dim)) {
  638. case ELObj::noQuantity:
  639. {
  640. const LengthSpec *lsp = argv[0]->lengthSpec();
  641. if (!lsp)
  642. return argError(interp, loc,
  643. InterpreterMessages::notAQuantityOrLengthSpec, 0, argv[0]);
  644. LengthSpec ls(*lsp);
  645. for (int i = 1; i < argc; i++) {
  646. lsp = argv[i]->lengthSpec();
  647. if (lsp)
  648. ls += *lsp;
  649. else {
  650. switch (argv[i]->quantityValue(lResult, dResult, dim)) {
  651. case ELObj::noQuantity:
  652. return argError(interp, loc, InterpreterMessages::notAQuantityOrLengthSpec,
  653. i, argv[i]);
  654. case ELObj::longQuantity:
  655. dResult = lResult;
  656. // fall through
  657. case ELObj::doubleQuantity:
  658. if (dim != 1) {
  659. interp.setNextLocation(loc);
  660. interp.message(InterpreterMessages::incompatibleDimensions);
  661. return interp.makeError();
  662. }
  663. ls += dResult;
  664. break;
  665. }
  666. }
  667. }
  668. return new (interp) LengthSpecObj(ls);
  669. }
  670. case ELObj::longQuantity:
  671. usingD = 0;
  672. break;
  673. case ELObj::doubleQuantity:
  674. usingD = 1;
  675. break;
  676. default:
  677. CANNOT_HAPPEN();
  678. }
  679. for (int i = 1; i < argc; i++) {
  680. long lResult2;
  681. double dResult2;
  682. int dim2;
  683. switch (argv[i]->quantityValue(lResult2, dResult2, dim2)) {
  684. case ELObj::noQuantity:
  685. return argError(interp, loc, InterpreterMessages::notAQuantity,
  686. i, argv[i]);
  687. case ELObj::longQuantity:
  688. if (!usingD) {
  689. if (lResult2 < 0) {
  690. if (lResult >= LONG_MIN - lResult2) {
  691. lResult += lResult2;
  692. break;
  693. }
  694. }
  695. else {
  696. if (lResult <= LONG_MAX - lResult2) {
  697. lResult += lResult2;
  698. break;
  699. }
  700. }
  701. usingD = 1;
  702. dResult = double(lResult);
  703. }
  704. dResult += double(lResult2);
  705. break;
  706. case ELObj::doubleQuantity:
  707. if (!usingD) {
  708. dResult = lResult;
  709. usingD = 1;
  710. }
  711. dResult += dResult2;
  712. break;
  713. default:
  714. CANNOT_HAPPEN();
  715. }
  716. if (dim2 != dim) {
  717. interp.setNextLocation(loc);
  718. interp.message(InterpreterMessages::incompatibleDimensions);
  719. return interp.makeError();
  720. }
  721. }
  722. if (!usingD) {
  723. if (dim == 0)
  724. return interp.makeInteger(lResult);
  725. else if (dim == 1)
  726. return new (interp) LengthObj(lResult);
  727. else
  728. dResult = lResult;
  729. }
  730. if (dim == 0)
  731. return new (interp) RealObj(dResult);
  732. else
  733. return new (interp) QuantityObj(dResult, dim);
  734. }
  735. DEFPRIMITIVE(Minus, argc, argv, context, interp, loc)
  736. {
  737. long lResult;
  738. double dResult;
  739. bool usingD;
  740. int dim;
  741. switch (argv[0]->quantityValue(lResult, dResult, dim)) {
  742. case ELObj::noQuantity:
  743. {
  744. const LengthSpec *lsp = argv[0]->lengthSpec();
  745. if (!lsp)
  746. return argError(interp, loc,
  747. InterpreterMessages::notAQuantityOrLengthSpec, 0, argv[0]);
  748. LengthSpec ls(*lsp);
  749. for (int i = 1; i < argc; i++) {
  750. lsp = argv[i]->lengthSpec();
  751. if (lsp)
  752. ls -= *lsp;
  753. else {
  754. switch (argv[i]->quantityValue(lResult, dResult, dim)) {
  755. case ELObj::noQuantity:
  756. return argError(interp, loc, InterpreterMessages::notAQuantityOrLengthSpec,
  757. i, argv[i]);
  758. case ELObj::longQuantity:
  759. dResult = lResult;
  760. // fall through
  761. case ELObj::doubleQuantity:
  762. if (dim != 1) {
  763. interp.setNextLocation(loc);
  764. interp.message(InterpreterMessages::incompatibleDimensions);
  765. return interp.makeError();
  766. }
  767. ls -= dResult;
  768. break;
  769. }
  770. }
  771. }
  772. return new (interp) LengthSpecObj(ls);
  773. }
  774. case ELObj::longQuantity:
  775. usingD = 0;
  776. break;
  777. case ELObj::doubleQuantity:
  778. usingD = 1;
  779. break;
  780. default:
  781. CANNOT_HAPPEN();
  782. }
  783. if (argc == 1) {
  784. if (usingD)
  785. dResult = -dResult;
  786. else
  787. lResult = -lResult;
  788. }
  789. else {
  790. for (int i = 1; i < argc; i++) {
  791. long lResult2;
  792. double dResult2;
  793. int dim2;
  794. switch (argv[i]->quantityValue(lResult2, dResult2, dim2)) {
  795. case ELObj::noQuantity:
  796. return argError(interp, loc,
  797. InterpreterMessages::notAQuantity, i,
  798. argv[i]);
  799. case ELObj::longQuantity:
  800. if (!usingD) {
  801. if (lResult2 > 0) {
  802. if (lResult >= LONG_MIN + lResult2) {
  803. lResult -= lResult2;
  804. break;
  805. }
  806. }
  807. else {
  808. if (lResult <= LONG_MAX + lResult2) {
  809. lResult -= lResult2;
  810. break;
  811. }
  812. }
  813. usingD = 1;
  814. dResult = double(lResult);
  815. }
  816. dResult -= double(lResult2);
  817. break;
  818. case ELObj::doubleQuantity:
  819. if (!usingD) {
  820. dResult = lResult;
  821. usingD = 1;
  822. }
  823. dResult -= dResult2;
  824. break;
  825. default:
  826. CANNOT_HAPPEN();
  827. }
  828. if (dim2 != dim) {
  829. interp.setNextLocation(loc);
  830. interp.message(InterpreterMessages::incompatibleDimensions);
  831. return interp.makeError();
  832. }
  833. }
  834. }
  835. if (!usingD) {
  836. if (dim == 0)
  837. return interp.makeInteger(lResult);
  838. else if (dim == 1)
  839. return new (interp) LengthObj(lResult);
  840. else
  841. dResult = lResult;
  842. }
  843. if (dim == 0)
  844. return new (interp) RealObj(dResult);
  845. else
  846. return new (interp) QuantityObj(dResult, dim);
  847. }
  848. DEFPRIMITIVE(Multiply, argc, argv, context, interp, loc)
  849. {
  850. if (argc == 0)
  851. return interp.makeInteger(1);
  852. long lResult;
  853. double dResult;
  854. int dim;
  855. int i = 1;
  856. switch (argv[0]->quantityValue(lResult, dResult, dim)) {
  857. case ELObj::noQuantity:
  858. {
  859. const LengthSpec *ls = argv[0]->lengthSpec();
  860. if (ls) {
  861. LengthSpec result(*ls);
  862. double d;
  863. for (; i < argc; i++) {
  864. if (!argv[i]->realValue(d))
  865. return argError(interp, loc,
  866. InterpreterMessages::notANumber, 1, argv[1]);
  867. result *= d;
  868. }
  869. return new (interp) LengthSpecObj(result);
  870. }
  871. }
  872. return argError(interp, loc,
  873. InterpreterMessages::notAQuantity, 0, argv[0]);
  874. case ELObj::longQuantity:
  875. break;
  876. case ELObj::doubleQuantity:
  877. goto useDouble;
  878. default:
  879. CANNOT_HAPPEN();
  880. }
  881. long lResult2;
  882. double dResult2;
  883. int dim2;
  884. for (; i < argc; i++) {
  885. switch (argv[i]->quantityValue(lResult2, dResult2, dim2)) {
  886. case ELObj::noQuantity:
  887. return argError(interp, loc,
  888. InterpreterMessages::notAQuantity, i, argv[i]);
  889. case ELObj::longQuantity:
  890. dim += dim2;
  891. if (dim > 1
  892. || (lResult2 != 0
  893. && (lResult2 < 0
  894. ? (lResult > 0
  895. ? lResult > -(unsigned)LONG_MIN / -(unsigned)lResult2
  896. : -(unsigned)lResult > LONG_MAX / -(unsigned)lResult2)
  897. : (lResult > 0
  898. ? lResult > LONG_MAX / lResult2
  899. : -(unsigned)lResult > -(unsigned)LONG_MIN / lResult2)))) {
  900. dResult = double(lResult) * lResult2;
  901. i++;
  902. goto useDouble;
  903. }
  904. lResult *= lResult2;
  905. break;
  906. case ELObj::doubleQuantity:
  907. dim += dim2;
  908. dResult = lResult * dResult2;
  909. i++;
  910. goto useDouble;
  911. default:
  912. CANNOT_HAPPEN();
  913. }
  914. }
  915. if (dim == 0)
  916. return interp.makeInteger(lResult);
  917. else
  918. return new (interp) LengthObj(lResult);
  919. useDouble:
  920. for (; i < argc; i++) {
  921. switch (argv[i]->quantityValue(lResult2, dResult2, dim2)) {
  922. case ELObj::noQuantity:
  923. return argError(interp, loc,
  924. InterpreterMessages::notAQuantity, i, argv[i]);
  925. case ELObj::longQuantity:
  926. dResult *= lResult2;
  927. break;
  928. case ELObj::doubleQuantity:
  929. dResult *= dResult2;
  930. break;
  931. }
  932. dim += dim2;
  933. }
  934. if (dim == 0)
  935. return new (interp) RealObj(dResult);
  936. else
  937. return new (interp) QuantityObj(dResult, dim);
  938. }
  939. DEFPRIMITIVE(Divide, argc, argv, context, interp, loc)
  940. {
  941. long lResult;
  942. double dResult;
  943. int dim;
  944. if (argc == 1) {
  945. switch (argv[0]->quantityValue(lResult, dResult, dim)) {
  946. case ELObj::noQuantity:
  947. return argError(interp, loc,
  948. InterpreterMessages::notAQuantity, 0, argv[0]);
  949. case ELObj::longQuantity:
  950. if (lResult == 0)
  951. goto divide0;
  952. dResult = 1.0/lResult;
  953. break;
  954. case ELObj::doubleQuantity:
  955. if (dResult == 0.0)
  956. goto divide0;
  957. dResult = 1.0/dResult;
  958. break;
  959. default:
  960. CANNOT_HAPPEN();
  961. }
  962. dim = -dim;
  963. }
  964. else {
  965. int i = 1;
  966. switch (argv[0]->quantityValue(lResult, dResult, dim)) {
  967. case ELObj::noQuantity:
  968. {
  969. const LengthSpec *ls = argv[0]->lengthSpec();
  970. if (ls) {
  971. LengthSpec result(*ls);
  972. double d;
  973. for (; i < argc; i++) {
  974. if (!argv[i]->realValue(d))
  975. return argError(interp, loc,
  976. InterpreterMessages::notANumber, 1, argv[1]);
  977. if (d == 0.0)
  978. goto divide0;
  979. result /= d;
  980. }
  981. return new (interp) LengthSpecObj(result);
  982. }
  983. }
  984. return argError(interp, loc,
  985. InterpreterMessages::notAQuantity, 0, argv[0]);
  986. case ELObj::longQuantity:
  987. break;
  988. case ELObj::doubleQuantity:
  989. goto useDouble;
  990. default:
  991. CANNOT_HAPPEN();
  992. }
  993. long lResult2;
  994. double dResult2;
  995. int dim2;
  996. for (; i < argc; i++) {
  997. switch (argv[i]->quantityValue(lResult2, dResult2, dim2)) {
  998. case ELObj::noQuantity:
  999. return argError(interp, loc,
  1000. InterpreterMessages::notAQuantity, 0, argv[0]);
  1001. case ELObj::longQuantity:
  1002. if (lResult2 == 0)
  1003. goto divide0;
  1004. dim -= dim2;
  1005. // If dim and dim2 are both 1, must goto useDouble:
  1006. // since lengths are inexact, result must be inexact.
  1007. if (dim2 == 0 && lResult % lResult2 == 0) {
  1008. lResult /= lResult2;
  1009. break;
  1010. }
  1011. dResult = double(lResult)/lResult2;
  1012. i++;
  1013. goto useDouble;
  1014. case ELObj::doubleQuantity:
  1015. dim -= dim2;
  1016. dResult = lResult;
  1017. if (dResult2 == 0.0)
  1018. goto divide0;
  1019. dResult /= dResult2;
  1020. i++;
  1021. goto useDouble;
  1022. default:
  1023. CANNOT_HAPPEN();
  1024. }
  1025. }
  1026. if (dim == 0)
  1027. return interp.makeInteger(lResult);
  1028. else
  1029. return new (interp) LengthObj(lResult);
  1030. useDouble:
  1031. for (; i < argc; i++) {
  1032. switch (argv[i]->quantityValue(lResult2, dResult2, dim2)) {
  1033. case ELObj::noQuantity:
  1034. return argError(interp, loc,
  1035. InterpreterMessages::notAQuantity, i, argv[i]);
  1036. case ELObj::longQuantity:
  1037. if (lResult2 == 0)
  1038. goto divide0;
  1039. dResult /= lResult2;
  1040. break;
  1041. case ELObj::doubleQuantity:
  1042. dResult /= dResult2;
  1043. if (dResult2 == 0.0)
  1044. goto divide0;
  1045. break;
  1046. }
  1047. dim -= dim2;
  1048. }
  1049. }
  1050. if (dim == 0)
  1051. return new (interp) RealObj(dResult);
  1052. else
  1053. return new (interp) QuantityObj(dResult, dim);
  1054. divide0:
  1055. interp.setNextLocation(loc);
  1056. interp.message(InterpreterMessages::divideBy0);
  1057. return interp.makeError();
  1058. }
  1059. DEFPRIMITIVE(Quotient, argc, argv, context, interp, loc)
  1060. {
  1061. long n1;
  1062. long n2;
  1063. if (argv[0]->exactIntegerValue(n1) && argv[1]->exactIntegerValue(n2)) {
  1064. if (n2 == 0) {
  1065. interp.setNextLocation(loc);
  1066. interp.message(InterpreterMessages::divideBy0);
  1067. return interp.makeError();
  1068. }
  1069. // This isn't strictly portable.
  1070. return interp.makeInteger(n1 / n2);
  1071. }
  1072. double d1;
  1073. if (!argv[0]->realValue(d1) || modf(d1, &d1) != 0.0)
  1074. return argError(interp, loc,
  1075. InterpreterMessages::notAnExactInteger, 0, argv[0]);
  1076. double d2;
  1077. if (!argv[1]->realValue(d2) || modf(d2, &d2) != 0.0)
  1078. return argError(interp, loc,
  1079. InterpreterMessages::notAnExactInteger, 1, argv[1]);
  1080. if (d2 == 0.0) {
  1081. interp.setNextLocation(loc);
  1082. interp.message(InterpreterMessages::divideBy0);
  1083. return interp.makeError();
  1084. }
  1085. return new (interp) RealObj((d1 - fmod(d1, d2))/d2);
  1086. }
  1087. DEFPRIMITIVE(Remainder, argc, argv, context, interp, loc)
  1088. {
  1089. long n1;
  1090. long n2;
  1091. if (argv[0]->exactIntegerValue(n1) && argv[1]->exactIntegerValue(n2)) {
  1092. if (n2 == 0) {
  1093. interp.setNextLocation(loc);
  1094. interp.message(InterpreterMessages::divideBy0);
  1095. return interp.makeError();
  1096. }
  1097. // This isn't strictly portable.
  1098. return interp.makeInteger(n1 % n2);
  1099. }
  1100. double d1;
  1101. if (!argv[0]->realValue(d1) || modf(d1, &d1) != 0.0)
  1102. return argError(interp, loc,
  1103. InterpreterMessages::notAnExactInteger, 0, argv[0]);
  1104. double d2;
  1105. if (!argv[1]->realValue(d2) || modf(d2, &d2) != 0.0)
  1106. return argError(interp, loc,
  1107. InterpreterMessages::notAnExactInteger, 1, argv[1]);
  1108. if (d2 == 0.0) {
  1109. interp.setNextLocation(loc);
  1110. interp.message(InterpreterMessages::divideBy0);
  1111. return interp.makeError();
  1112. }
  1113. return new (interp) RealObj(fmod(d1, d2));
  1114. }
  1115. DEFPRIMITIVE(Modulo, argc, argv, context, interp, loc)
  1116. {
  1117. long n1;
  1118. long n2;
  1119. if (argv[0]->exactIntegerValue(n1) && argv[1]->exactIntegerValue(n2)) {
  1120. if (n2 == 0) {
  1121. interp.setNextLocation(loc);
  1122. interp.message(InterpreterMessages::divideBy0);
  1123. return interp.makeError();
  1124. }
  1125. long r = n1 % n2;
  1126. if (n2 > 0 ? r < 0 : r > 0)
  1127. r += n2;
  1128. return interp.makeInteger(r);
  1129. }
  1130. double d1;
  1131. if (!argv[0]->realValue(d1) || modf(d1, &d1) != 0.0)
  1132. return argError(interp, loc,
  1133. InterpreterMessages::notAnExactInteger, 0, argv[0]);
  1134. double d2;
  1135. if (!argv[1]->realValue(d2) || modf(d2, &d2) != 0.0)
  1136. return argError(interp, loc,
  1137. InterpreterMessages::notAnExactInteger, 1, argv[1]);
  1138. if (d2 == 0.0) {
  1139. interp.setNextLocation(loc);
  1140. interp.message(InterpreterMessages::divideBy0);
  1141. return interp.makeError();
  1142. }
  1143. double r = fmod(d1, d2);
  1144. if (d2 > 0 ? r < 0 : r > 0)
  1145. r += d2;
  1146. return new (interp) RealObj(r);
  1147. }
  1148. #define DEFCOMPARE(NAME, OP) \
  1149. DEFPRIMITIVE(NAME, argc, argv, context, interp, loc) \
  1150. { \
  1151. if (argc == 0) \
  1152. return interp.makeTrue(); \
  1153. long lResult; \
  1154. double dResult; \
  1155. int dim; \
  1156. bool lastWasDouble; \
  1157. switch (argv[0]->quantityValue(lResult, dResult, dim)) { \
  1158. case ELObj::noQuantity: \
  1159. return argError(interp, loc, \
  1160. InterpreterMessages::notAQuantity, 0, argv[0]); \
  1161. case ELObj::longQuantity: \
  1162. lastWasDouble = 0; \
  1163. break; \
  1164. case ELObj::doubleQuantity: \
  1165. lastWasDouble = 1; \
  1166. break; \
  1167. default: \
  1168. CANNOT_HAPPEN(); \
  1169. } \
  1170. for (int i = 1; i < argc; i++) { \
  1171. long lResult2; \
  1172. double dResult2; \
  1173. int dim2; \
  1174. switch (argv[i]->quantityValue(lResult2, dResult2, dim2)) { \
  1175. case ELObj::noQuantity: \
  1176. return argError(interp, loc, \
  1177. InterpreterMessages::notAQuantity, i, argv[i]); \
  1178. case ELObj::longQuantity: \
  1179. if (dim2 != dim) \
  1180. goto badDim; \
  1181. if (!(lastWasDouble \
  1182. ? (dResult OP lResult2) \
  1183. : (lResult OP lResult2))) \
  1184. return interp.makeFalse(); \
  1185. lResult = lResult2; \
  1186. lastWasDouble = 0; \
  1187. break; \
  1188. case ELObj::doubleQuantity: \
  1189. if (dim != dim2) \
  1190. goto badDim; \
  1191. if (!(lastWasDouble \
  1192. ? (dResult OP dResult2) \
  1193. : (lResult OP dResult2))) \
  1194. return interp.makeFalse(); \
  1195. dResult = dResult2; \
  1196. lastWasDouble = 1; \
  1197. break; \
  1198. } \
  1199. } \
  1200. return interp.makeTrue(); \
  1201. badDim: \
  1202. interp.setNextLocation(loc); \
  1203. interp.message(InterpreterMessages::incompatibleDimensions); \
  1204. return interp.makeError(); \
  1205. }
  1206. DEFCOMPARE(Less, <)
  1207. DEFCOMPARE(Greater, >)
  1208. DEFCOMPARE(LessEqual, <=)
  1209. DEFCOMPARE(GreaterEqual, >=)
  1210. DEFPRIMITIVE(Min, argc, argv, context, interp, loc)
  1211. {
  1212. long lResult;
  1213. double dResult;
  1214. int dim;
  1215. int i = 1;
  1216. switch (argv[0]->quantityValue(lResult, dResult, dim)) {
  1217. case ELObj::noQuantity:
  1218. return argError(interp, loc,
  1219. InterpreterMessages::notAQuantity, 0, argv[0]);
  1220. case ELObj::longQuantity:
  1221. break;
  1222. case ELObj::doubleQuantity:
  1223. goto useDouble;
  1224. default:
  1225. CANNOT_HAPPEN();
  1226. }
  1227. // Note that result is inexact if any of the arguments are
  1228. for (; i < argc; i++) {
  1229. long lResult2;
  1230. double dResult2;
  1231. int dim2;
  1232. switch (argv[i]->quantityValue(lResult2, dResult2, dim2)) {
  1233. case ELObj::noQuantity:
  1234. return argError(interp, loc,
  1235. InterpreterMessages::notAQuantity, i, argv[i]);
  1236. case ELObj::longQuantity:
  1237. if (dim2 != dim)
  1238. goto badDim;
  1239. if (lResult2 < lResult)
  1240. lResult = lResult2;
  1241. break;
  1242. case ELObj::doubleQuantity:
  1243. if (dim != dim2)
  1244. goto badDim;
  1245. if (dResult2 < lResult)
  1246. dResult = dResult2;
  1247. else if (dim)
  1248. break;
  1249. else
  1250. dResult = lResult;
  1251. i++;
  1252. goto useDouble;
  1253. }
  1254. }
  1255. if (dim == 0)
  1256. return interp.makeInteger(lResult);
  1257. else
  1258. return new (interp) LengthObj(lResult);
  1259. useDouble:
  1260. for (; i < argc; i++) {
  1261. long lResult2;
  1262. double dResult2;
  1263. int dim2;
  1264. switch (argv[i]->quantityValue(lResult2, dResult2, dim2)) {
  1265. case ELObj::noQuantity:
  1266. return argError(interp, loc,
  1267. InterpreterMessages::notAQuantity, i, argv[i]);
  1268. case ELObj::longQuantity:
  1269. if (dim2 != dim)
  1270. goto badDim;
  1271. if (lResult2 < dResult)
  1272. dResult = lResult2;
  1273. break;
  1274. case ELObj::doubleQuantity:
  1275. if (dim != dim2)
  1276. goto badDim;
  1277. if (dResult2 < dResult)
  1278. dResult = dResult2;
  1279. break;
  1280. }
  1281. }
  1282. if (dim == 0)
  1283. return new (interp) RealObj(dResult);
  1284. else
  1285. return new (interp) QuantityObj(dResult, dim);
  1286. badDim:
  1287. interp.setNextLocation(loc);
  1288. interp.message(InterpreterMessages::incompatibleDimensions);
  1289. return interp.makeError();
  1290. }
  1291. DEFPRIMITIVE(Max, argc, argv, context, interp, loc)
  1292. {
  1293. long lResult;
  1294. double dResult;
  1295. int dim;
  1296. int i = 1;
  1297. switch (argv[0]->quantityValue(lResult, dResult, dim)) {
  1298. case ELObj::noQuantity:
  1299. return argError(interp, loc,
  1300. InterpreterMessages::notAQuantity, 0, argv[0]);
  1301. case ELObj::longQuantity:
  1302. break;
  1303. case ELObj::doubleQuantity:
  1304. goto useDouble;
  1305. default:
  1306. CANNOT_HAPPEN();
  1307. }
  1308. // Note that result is inexact if any of the arguments are
  1309. for (; i < argc; i++) {
  1310. long lResult2;
  1311. double dResult2;
  1312. int dim2;
  1313. switch (argv[i]->quantityValue(lResult2, dResult2, dim2)) {
  1314. case ELObj::noQuantity:
  1315. return argError(interp, loc,
  1316. InterpreterMessages::notAQuantity, i, argv[i]);
  1317. case ELObj::longQuantity:
  1318. if (dim2 != dim)
  1319. goto badDim;
  1320. if (lResult2 > lResult)
  1321. lResult = lResult2;
  1322. break;
  1323. case ELObj::doubleQuantity:
  1324. if (dim != dim2)
  1325. goto badDim;
  1326. if (dResult2 > lResult)
  1327. dResult = dResult2;
  1328. else if (dim)
  1329. break;
  1330. else
  1331. dResult = lResult;
  1332. i++;
  1333. goto useDouble;
  1334. }
  1335. }
  1336. if (dim == 0)
  1337. return interp.makeInteger(lResult);
  1338. else
  1339. return new (interp) LengthObj(lResult);
  1340. useDouble:
  1341. for (; i < argc; i++) {
  1342. long lResult2;
  1343. double dResult2;
  1344. int dim2;
  1345. switch (argv[i]->quantityValue(lResult2, dResult2, dim2)) {
  1346. case ELObj::noQuantity:
  1347. return argError(interp, loc,
  1348. InterpreterMessages::notAQuantity, i, argv[i]);
  1349. case ELObj::longQuantity:
  1350. if (dim2 != dim)
  1351. goto badDim;
  1352. if (lResult2 > dResult)
  1353. dResult = lResult2;
  1354. break;
  1355. case ELObj::doubleQuantity:
  1356. if (dim != dim2)
  1357. goto badDim;
  1358. if (dResult2 > dResult)
  1359. dResult = dResult2;
  1360. break;
  1361. }
  1362. }
  1363. if (dim == 0)
  1364. return new (interp) RealObj(dResult);
  1365. else
  1366. return new (interp) QuantityObj(dResult, dim);
  1367. badDim:
  1368. interp.setNextLocation(loc);
  1369. interp.message(InterpreterMessages::incompatibleDimensions);
  1370. return interp.makeError();
  1371. }
  1372. DEFPRIMITIVE(Floor, argc, argv, context, interp, loc)
  1373. {
  1374. double d;
  1375. if (argv[0]->inexactRealValue(d))
  1376. return new (interp) RealObj(floor(d));
  1377. long n;
  1378. if (argv[0]->exactIntegerValue(n))
  1379. return argv[0];
  1380. return argError(interp, loc,
  1381. InterpreterMessages::notANumber, 0, argv[0]);
  1382. }
  1383. DEFPRIMITIVE(Ceiling, argc, argv, context, interp, loc)
  1384. {
  1385. double d;
  1386. if (argv[0]->inexactRealValue(d))
  1387. return new (interp) RealObj(ceil(d));
  1388. long n;
  1389. if (argv[0]->exactIntegerValue(n))
  1390. return argv[0];
  1391. return argError(interp, loc,
  1392. InterpreterMessages::notANumber, 0, argv[0]);
  1393. }
  1394. DEFPRIMITIVE(Round, argc, argv, context, interp, loc)
  1395. {
  1396. double d;
  1397. if (argv[0]->inexactRealValue(d)) {
  1398. double result = floor(d + .5);
  1399. // That rounded it upwards.
  1400. // Now figure out if that was different from round to
  1401. // even.
  1402. if (result - d == 0.5 && fmod(result, 2.0) != 0)
  1403. result -= 1.0;
  1404. return new (interp) RealObj(result);
  1405. }
  1406. long n;
  1407. if (argv[0]->exactIntegerValue(n))
  1408. return argv[0];
  1409. return argError(interp, loc,
  1410. InterpreterMessages::notANumber, 0, argv[0]);
  1411. }
  1412. DEFPRIMITIVE(Truncate, argc, argv, context, interp, loc)
  1413. {
  1414. double d;
  1415. if (argv[0]->inexactRealValue(d)) {
  1416. double iPart;
  1417. modf(d, &iPart);
  1418. return new (interp) RealObj(iPart);
  1419. }
  1420. long n;
  1421. if (argv[0]->exactIntegerValue(n))
  1422. return argv[0];
  1423. return argError(interp, loc,
  1424. InterpreterMessages::notANumber, 0, argv[0]);
  1425. }
  1426. DEFPRIMITIVE(Abs, argc, argv, context, interp, loc)
  1427. {
  1428. long lResult;
  1429. double dResult;
  1430. int dim;
  1431. switch (argv[0]->quantityValue(lResult, dResult, dim)) {
  1432. case ELObj::noQuantity:
  1433. return argError(interp, loc,
  1434. InterpreterMessages::notAQuantity, 0, argv[0]);
  1435. case ELObj::longQuantity:
  1436. if (lResult != LONG_MIN) {
  1437. if (lResult >= 0)
  1438. return argv[0];
  1439. if (dim == 0)
  1440. return interp.makeInteger(-lResult);
  1441. else
  1442. return new (interp) LengthObj(-lResult);
  1443. }
  1444. dResult = lResult;
  1445. break;
  1446. case ELObj::doubleQuantity:
  1447. break;
  1448. default:
  1449. CANNOT_HAPPEN();
  1450. }
  1451. if (dResult >= 0)
  1452. return argv[0];
  1453. if (dim == 0)
  1454. return new (interp) RealObj(-dResult);
  1455. else
  1456. return new (interp) QuantityObj(-dResult, dim);
  1457. }
  1458. DEFPRIMITIVE(Sqrt, argc, argv, context, interp, loc)
  1459. {
  1460. long lResult;
  1461. double dResult;
  1462. int dim;
  1463. ELObj::QuantityType type
  1464. = argv[0]->quantityValue(lResult, dResult, dim);
  1465. switch (type) {
  1466. case ELObj::noQuantity:
  1467. return argError(interp, loc,
  1468. InterpreterMessages::notAQuantity, 0, argv[0]);
  1469. case ELObj::longQuantity:
  1470. dResult = lResult;
  1471. break;
  1472. case ELObj::doubleQuantity:
  1473. break;
  1474. default:
  1475. CANNOT_HAPPEN();
  1476. }
  1477. if ((dim & 1) || dResult < 0.0) {
  1478. interp.setNextLocation(loc);
  1479. interp.message(InterpreterMessages::outOfRange);
  1480. return interp.makeError();
  1481. }
  1482. dim /= 2;
  1483. dResult = sqrt(dResult);
  1484. if (type == ELObj::longQuantity && dim == 0) {
  1485. long n = long(dResult);
  1486. if (n*n == lResult)
  1487. return interp.makeInteger(n);
  1488. }
  1489. return new (interp) QuantityObj(dResult, dim);
  1490. }
  1491. DEFPRIMITIVE(Time, argc, argv, context, interp, loc)
  1492. {
  1493. // This assumes a Posix compatible time().
  1494. time_t t = time(0);
  1495. return interp.makeInteger(long(t));
  1496. }
  1497. DEFPRIMITIVE(TimeToString, argc, argv, context, interp, loc)
  1498. {
  1499. long k;
  1500. if (!argv[0]->exactIntegerValue(k))
  1501. return argError(interp, loc,
  1502. InterpreterMessages::notAnExactInteger, 0, argv[0]);
  1503. time_t t = time_t(k);
  1504. const struct tm *p;
  1505. if (argc > 1 && argv[1] != interp.makeFalse())
  1506. p = gmtime(&t);
  1507. else
  1508. p = localtime(&t);
  1509. char buf[64];
  1510. sprintf(buf, "%04d-%02d-%02dT%02d:%02d:%02d",
  1511. p->tm_year + 1900, p->tm_mon + 1, p->tm_mday,
  1512. p->tm_hour, p->tm_min, p->tm_sec);
  1513. return new (interp) StringObj(interp.makeStringC(buf));
  1514. }
  1515. DEFPRIMITIVE(CharProperty, argc, argv, context, interp, loc)
  1516. {
  1517. SymbolObj *sym = argv[0]->asSymbol();
  1518. if (!sym)
  1519. return argError(interp, loc,
  1520. InterpreterMessages::notASymbol, 0, argv[0]);
  1521. Char c;
  1522. if (!argv[1]->charValue(c))
  1523. return argError(interp, loc,
  1524. InterpreterMessages::notAChar, 1, argv[1]);
  1525. // FIXME
  1526. if (argc > 2)
  1527. return argv[2];
  1528. else
  1529. return interp.makeFalse();
  1530. }
  1531. DEFPRIMITIVE(Literal, argc, argv, context, interp, loc)
  1532. {
  1533. if (argc == 0)
  1534. return new (interp) EmptySosofoObj;
  1535. const Char *s;
  1536. size_t n;
  1537. if (!argv[0]->stringData(s, n))
  1538. return argError(interp, loc, InterpreterMessages::notAString,
  1539. 0, argv[0]);
  1540. if (argc == 1)
  1541. return new (interp) LiteralSosofoObj(argv[0]);
  1542. StringObj *strObj = new (interp) StringObj(s, n);
  1543. for (int i = 1; i < argc; i++) {
  1544. if (!argv[i]->stringData(s, n))
  1545. return argError(interp, loc, InterpreterMessages::notAString,
  1546. i, argv[i]);
  1547. strObj->append(s, n);
  1548. }
  1549. ELObjDynamicRoot protect(interp, strObj);
  1550. return new (interp) LiteralSosofoObj(strObj);
  1551. }
  1552. DEFPRIMITIVE(ProcessChildren, argc, argv, context, interp, loc)
  1553. {
  1554. if (!context.processingMode) {
  1555. interp.setNextLocation(loc);
  1556. interp.message(InterpreterMessages::noCurrentProcessingMode);
  1557. return interp.makeError();
  1558. }
  1559. return new (interp) ProcessChildrenSosofoObj(context.processingMode);
  1560. }
  1561. DEFPRIMITIVE(ProcessChildrenTrim, argc, argv, context, interp, loc)
  1562. {
  1563. if (!context.processingMode) {
  1564. interp.setNextLocation(loc);
  1565. interp.message(InterpreterMessages::noCurrentProcessingMode);
  1566. return interp.makeError();
  1567. }
  1568. return new (interp) ProcessChildrenTrimSosofoObj(context.processingMode);
  1569. }
  1570. DEFPRIMITIVE(SosofoAppend, argc, argv, context, interp, loc)
  1571. {
  1572. AppendSosofoObj *obj = new (interp) AppendSosofoObj;
  1573. for (int i = 0; i < argc; i++) {
  1574. SosofoObj *sosofo = argv[i]->asSosofo();
  1575. if (!sosofo)
  1576. return argError(interp, loc, InterpreterMessages::notASosofo,
  1577. i, argv[i]);
  1578. obj->append(sosofo);
  1579. }
  1580. return obj;
  1581. }
  1582. DEFPRIMITIVE(NextMatch, argc, argv, context, interp, loc)
  1583. {
  1584. if (!context.processingMode) {
  1585. interp.setNextLocation(loc);
  1586. interp.message(InterpreterMessages::noCurrentProcessingMode);
  1587. return interp.makeError();
  1588. }
  1589. StyleObj *style;
  1590. if (argc == 0)
  1591. style = 0;
  1592. else {
  1593. style = argv[0]->asStyle();
  1594. if (!style)
  1595. return argError(interp, loc, InterpreterMessages::notAStyle, 0, argv[0]);
  1596. }
  1597. return new (interp) NextMatchSosofoObj(style);
  1598. }
  1599. DEFPRIMITIVE(EmptySosofo, argc, argv, context, interp, loc)
  1600. {
  1601. return new (interp) EmptySosofoObj;
  1602. }
  1603. DEFPRIMITIVE(SosofoLabel, argc, argv, context, interp, loc)
  1604. {
  1605. SosofoObj *sosofo = argv[0]->asSosofo();
  1606. if (!sosofo)
  1607. return argError(interp, loc, InterpreterMessages::notASosofo,
  1608. 0, argv[0]);
  1609. SymbolObj *sym = argv[1]->asSymbol();
  1610. if (!sym)
  1611. return argError(interp, loc,
  1612. InterpreterMessages::notASymbol, 1, argv[1]);
  1613. return new (interp) LabelSosofoObj(sym, loc, sosofo);
  1614. }
  1615. DEFPRIMITIVE(SosofoDiscardLabeled, argc, argv, context, interp, loc)
  1616. {
  1617. SosofoObj *sosofo = argv[0]->asSosofo();
  1618. if (!sosofo)
  1619. return argError(interp, loc, InterpreterMessages::notASosofo,
  1620. 0, argv[0]);
  1621. SymbolObj *sym = argv[1]->asSymbol();
  1622. if (!sym)
  1623. return argError(interp, loc,
  1624. InterpreterMessages::notASymbol, 1, argv[1]);
  1625. return new (interp) DiscardLabeledSosofoObj(sym, sosofo);
  1626. }
  1627. DEFPRIMITIVE(IsSosofo, argc, argv, context, interp, loc)
  1628. {
  1629. if (argv[0]->asSosofo())
  1630. return interp.makeTrue();
  1631. else
  1632. return interp.makeFalse();
  1633. }
  1634. DEFPRIMITIVE(MergeStyle, argc, argv, context, interp, loc)
  1635. {
  1636. MergeStyleObj *merged = new (interp) MergeStyleObj;
  1637. for (int i = 0; i < argc; i++) {
  1638. StyleObj *style = argv[i]->asStyle();
  1639. if (!style)
  1640. return argError(interp, loc,
  1641. InterpreterMessages::notAStyle, i, argv[i]);
  1642. merged->append(style);
  1643. }
  1644. return merged;
  1645. }
  1646. DEFPRIMITIVE(IsStyle, argc, argv, context, interp, loc)
  1647. {
  1648. if (argv[0]->asStyle())
  1649. return interp.makeTrue();
  1650. else
  1651. return interp.makeFalse();
  1652. }
  1653. DEFPRIMITIVE(CurrentNodePageNumberSosofo, argc, argv, context, interp, loc)
  1654. {
  1655. if (!context.currentNode)
  1656. return noCurrentNodeError(interp, loc);
  1657. return new (interp) CurrentNodePageNumberSosofoObj(context.currentNode);
  1658. }
  1659. DEFPRIMITIVE(PageNumberSosofo, argc, argv, context, interp, loc)
  1660. {
  1661. return new (interp) PageNumberSosofoObj;
  1662. }
  1663. DEFPRIMITIVE(ProcessElementWithId, argc, argv, context, interp, loc)
  1664. {
  1665. const Char *s;
  1666. size_t n;
  1667. if (!argv[0]->stringData(s, n))
  1668. return argError(interp, loc, InterpreterMessages::notAString, 0, argv[0]);
  1669. if (!context.currentNode)
  1670. return noCurrentNodeError(interp, loc);
  1671. if (!context.processingMode) {
  1672. interp.setNextLocation(loc);
  1673. interp.message(InterpreterMessages::noCurrentProcessingMode);
  1674. return interp.makeError();
  1675. }
  1676. NodePtr root;
  1677. NamedNodeListPtr elements;
  1678. if (context.currentNode->getGroveRoot(root) == accessOK
  1679. && root->getElements(elements) == accessOK) {
  1680. NodePtr node;
  1681. if (elements->namedNode(GroveString(s, n), node) == accessOK)
  1682. return new (interp) ProcessNodeSosofoObj(node, context.processingMode);
  1683. }
  1684. return new (interp) EmptySosofoObj;
  1685. }
  1686. static
  1687. ConstPtr<ElementPattern> convertToPattern(ELObj *obj, Interpreter &interp)
  1688. {
  1689. Vector<StringC> giAtts(1);
  1690. StringObj *str = obj->convertToString();
  1691. if (str) {
  1692. const Char *s;
  1693. size_t n;
  1694. str->stringData(s, n);
  1695. if (!n)
  1696. return new NoElementPattern;
  1697. giAtts[0].assign(s, n);
  1698. interp.normalizeGeneralName(giAtts[0]);
  1699. return new SimpleElementPattern(giAtts);
  1700. }
  1701. else if (obj == interp.makeTrue() || obj->isNil())
  1702. return new SimpleElementPattern(giAtts);
  1703. PairObj *pair = obj->asPair();
  1704. if (!pair)
  1705. return 0;
  1706. str = pair->car()->convertToString();
  1707. if (str) {
  1708. const Char *s;
  1709. size_t n;
  1710. str->stringData(s, n);
  1711. if (!n)
  1712. return new NoElementPattern;
  1713. giAtts[0].assign(s, n);
  1714. interp.normalizeGeneralName(giAtts[0]);
  1715. }
  1716. else if (pair->car() != interp.makeTrue())
  1717. return 0;
  1718. obj = pair->cdr();
  1719. if (!obj->isNil()) {
  1720. pair = obj->asPair();
  1721. if (!pair)
  1722. return 0;
  1723. if (pair->car()->isNil())
  1724. obj = pair->cdr();
  1725. else {
  1726. PairObj *atts = pair->car()->asPair();
  1727. if (atts) {
  1728. obj = pair->cdr();
  1729. for (;;) {
  1730. str = atts->car()->convertToString();
  1731. if (!str)
  1732. return 0;
  1733. const Char *s;
  1734. size_t n;
  1735. str->stringData(s, n);
  1736. giAtts.push_back(StringC(s, n));
  1737. if (atts->cdr()->isNil())
  1738. break;
  1739. atts = atts->cdr()->asPair();
  1740. if (!atts)
  1741. return 0;
  1742. }
  1743. if ((giAtts.size() & 1) == 0)
  1744. return 0;
  1745. }
  1746. }
  1747. if (!obj->isNil()) {
  1748. ConstPtr<ElementPattern> parentPat = convertToPattern(obj, interp);
  1749. if (parentPat.isNull())
  1750. return parentPat;
  1751. return new ParentElementPattern(new SimpleElementPattern(giAtts), parentPat);
  1752. }
  1753. }
  1754. return new SimpleElementPattern(giAtts);
  1755. }
  1756. DEFPRIMITIVE(ProcessFirstDescendant, argc, argv, context, interp, loc)
  1757. {
  1758. if (!context.processingMode) {
  1759. interp.setNextLocation(loc);
  1760. interp.message(InterpreterMessages::noCurrentProcessingMode);
  1761. return interp.makeError();
  1762. }
  1763. if (!context.currentNode)
  1764. return noCurrentNodeError(interp, loc);
  1765. ConstPtr<ElementPattern> pattern;
  1766. for (size_t i = 0; i < argc; i++) {
  1767. ConstPtr<ElementPattern> tem = convertToPattern(argv[i], interp);
  1768. if (tem.isNull())
  1769. return argError(interp, loc,
  1770. InterpreterMessages::notAPattern, i, argv[i]);
  1771. if (pattern.isNull())
  1772. pattern = tem;
  1773. else
  1774. pattern = new UnionElementPattern(tem, pattern);
  1775. }
  1776. if (pattern.isNull())
  1777. return new (interp) EmptySosofoObj;
  1778. NodeListObj *nl = new (interp) DescendantsNodeListObj(context.currentNode);
  1779. ELObjDynamicRoot protect(interp, nl);
  1780. nl = new (interp) SelectElementsNodeListObj(nl, pattern);
  1781. protect = nl;
  1782. NodePtr nd(nl->nodeListFirst(context, interp));
  1783. if (!nd)
  1784. return new (interp) EmptySosofoObj;
  1785. return new (interp) ProcessNodeSosofoObj(nd, context.processingMode);
  1786. }
  1787. DEFPRIMITIVE(ProcessMatchingChildren, argc, argv, context, interp, loc)
  1788. {
  1789. if (!context.processingMode) {
  1790. interp.setNextLocation(loc);
  1791. interp.message(InterpreterMessages::noCurrentProcessingMode);
  1792. return interp.makeError();
  1793. }
  1794. if (!context.currentNode)
  1795. return noCurrentNodeError(interp, loc);
  1796. ConstPtr<ElementPattern> pattern;
  1797. for (size_t i = 0; i < argc; i++) {
  1798. ConstPtr<ElementPattern> tem = convertToPattern(argv[i], interp);
  1799. if (tem.isNull())
  1800. return argError(interp, loc,
  1801. InterpreterMessages::notAPattern, i, argv[i]);
  1802. if (pattern.isNull())
  1803. pattern = tem;
  1804. else
  1805. pattern = new UnionElementPattern(tem, pattern);
  1806. }
  1807. NodeListPtr nlPtr;
  1808. // FIXME handle root
  1809. if (pattern.isNull() || context.currentNode->children(nlPtr) != accessOK)
  1810. return new (interp) EmptySosofoObj;
  1811. NodeListObj *nl = new (interp) NodeListPtrNodeListObj(nlPtr);
  1812. ELObjDynamicRoot protect(interp, nl);
  1813. nl = new (interp) SelectElementsNodeListObj(nl, pattern);
  1814. protect = nl;
  1815. return new (interp) ProcessNodeListSosofoObj(nl, context.processingMode);
  1816. }
  1817. DEFPRIMITIVE(SelectElements, argc, argv, context, interp, loc)
  1818. {
  1819. NodeListObj *nl = argv[0]->asNodeList();
  1820. if (!nl)
  1821. return argError(interp, loc,
  1822. InterpreterMessages::notANodeList, 0, argv[0]);
  1823. ConstPtr<ElementPattern> pattern(convertToPattern(argv[1], interp));
  1824. if (pattern.isNull())
  1825. return argError(interp, loc,
  1826. InterpreterMessages::notAPattern, 1, argv[1]);
  1827. return new (interp) SelectElementsNodeListObj(nl, pattern);
  1828. }
  1829. DEFPRIMITIVE(ProcessNodeList, argc, argv, context, interp, loc)
  1830. {
  1831. if (!context.processingMode) {
  1832. interp.setNextLocation(loc);
  1833. interp.message(InterpreterMessages::noCurrentProcessingMode);
  1834. return interp.makeError();
  1835. }
  1836. NodeListObj *nl = argv[0]->asNodeList();
  1837. if (!nl)
  1838. return argError(interp, loc,
  1839. InterpreterMessages::notANodeList, 0, argv[0]);
  1840. return new (interp) ProcessNodeListSosofoObj(nl, context.processingMode);
  1841. }
  1842. static
  1843. void reverse(StringC &s)
  1844. {
  1845. size_t i = 0;
  1846. size_t j = s.size() - 1;
  1847. while (i < j) {
  1848. Char tem = s[i];
  1849. s[i] = s[j];
  1850. s[j] = tem;
  1851. i++;
  1852. j--;
  1853. }
  1854. }
  1855. static
  1856. StringC formatNumberLetter(long n, const char *letters)
  1857. {
  1858. StringC result;
  1859. if (n == 0)
  1860. result += '0';
  1861. else {
  1862. bool neg;
  1863. // FIXME possibility of overflow
  1864. if (n < 0) {
  1865. n = -n;
  1866. neg = 1;
  1867. }
  1868. else
  1869. neg = 0;
  1870. do {
  1871. n--;
  1872. int r = n % 26;
  1873. n -= r;
  1874. n /= 26;
  1875. result += letters[r];
  1876. } while (n > 0);
  1877. if (neg)
  1878. result += '-';
  1879. reverse(result);
  1880. }
  1881. return result;
  1882. }
  1883. static
  1884. StringC formatNumberDecimal(long n, size_t minWidth)
  1885. {
  1886. StringC result;
  1887. char buf[32];
  1888. sprintf(buf, "%ld", n);
  1889. const char *p = buf;
  1890. if (*p == '-') {
  1891. p++;
  1892. result += '-';
  1893. }
  1894. size_t len = strlen(p);
  1895. while (len < minWidth) {
  1896. result += '0';
  1897. len++;
  1898. }
  1899. while (*p)
  1900. result += *p++;
  1901. return result;
  1902. }
  1903. static
  1904. StringC formatNumberRoman(long n, const char *letters)
  1905. {
  1906. StringC result;
  1907. if (n > 5000 || n < -5000 || n == 0)
  1908. return formatNumberDecimal(n, 1);
  1909. if (n < 0) {
  1910. n = -n;
  1911. result += '-';
  1912. }
  1913. while (n >= 1000) {
  1914. result += letters[0];
  1915. n -= 1000;
  1916. }
  1917. for (int i = 100; i > 0; i /= 10, letters += 2) {
  1918. long q = n / i;
  1919. n -= q * i;
  1920. switch (q) {
  1921. case 1:
  1922. result += letters[2];
  1923. break;
  1924. case 2:
  1925. result += letters[2];
  1926. result += letters[2];
  1927. break;
  1928. case 3:
  1929. result += letters[2];
  1930. result += letters[2];
  1931. result += letters[2];
  1932. break;
  1933. case 4:
  1934. result += letters[2];
  1935. result += letters[1];
  1936. break;
  1937. case 5:
  1938. result += letters[1];
  1939. break;
  1940. case 6:
  1941. result += letters[1];
  1942. result += letters[2];
  1943. break;
  1944. case 7:
  1945. result += letters[1];
  1946. result += letters[2];
  1947. result += letters[2];
  1948. break;
  1949. case 8:
  1950. result += letters[1];
  1951. result += letters[2];
  1952. result += letters[2];
  1953. result += letters[2];
  1954. break;
  1955. case 9:
  1956. result += letters[2];
  1957. result += letters[0];
  1958. break;
  1959. }
  1960. }
  1961. return result;
  1962. }
  1963. static
  1964. bool formatNumber(long n, const Char *s, size_t len, StringC &result)
  1965. {
  1966. if (len > 0) {
  1967. switch (s[len - 1]) {
  1968. case 'a':
  1969. result += formatNumberLetter(n, "abcdefghijklmnopqrstuvwxyz");
  1970. return 1;
  1971. case 'A':
  1972. result += formatNumberLetter(n, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  1973. return 1;
  1974. case 'i':
  1975. result += formatNumberRoman(n, "mdclxvi");
  1976. return 1;
  1977. case 'I':
  1978. result += formatNumberRoman(n, "MDCLXVI");
  1979. return 1;
  1980. case '1':
  1981. result += formatNumberDecimal(n, len);
  1982. return 1;
  1983. default:
  1984. break;
  1985. }
  1986. }
  1987. result += formatNumberDecimal(n, 1);
  1988. return 0;
  1989. }
  1990. DEFPRIMITIVE(FormatNumber, argc, argv, context, interp, loc)
  1991. {
  1992. long n;
  1993. if (!argv[0]->exactIntegerValue(n))
  1994. return argError(interp, loc,
  1995. InterpreterMessages::notAnExactInteger, 0, argv[0]);
  1996. const Char *s;
  1997. size_t len;
  1998. if (!argv[1]->stringData(s, len))
  1999. return argError(interp, loc, InterpreterMessages::notAString, 1, argv[1]);
  2000. StringObj *result = new (interp) StringObj;
  2001. if (!formatNumber(n, s, len, *result)) {
  2002. interp.setNextLocation(loc);
  2003. interp.message(InterpreterMessages::invalidNumberFormat,
  2004. StringMessageArg(StringC(s, len)));
  2005. }
  2006. return result;
  2007. }
  2008. DEFPRIMITIVE(FormatNumberList, argc, argv, context, interp, loc)
  2009. {
  2010. ELObj *numbers = argv[0];
  2011. ELObj *formats = argv[1];
  2012. ELObj *seps = argv[2];
  2013. StringObj *result = new (interp) StringObj;
  2014. while (!numbers->isNil()) {
  2015. PairObj *tem;
  2016. const Char *s;
  2017. size_t len;
  2018. if (numbers != argv[0]) {
  2019. if (!seps->stringData(s, len)) {
  2020. tem = seps->asPair();
  2021. if (!tem)
  2022. return argError(interp, loc,
  2023. InterpreterMessages::notAList, 2, argv[2]);
  2024. if (!tem->car()->stringData(s, len))
  2025. return argError(interp, loc,
  2026. InterpreterMessages::notAString, 2, tem->car());
  2027. seps = tem->cdr();
  2028. }
  2029. result->append(s, len);
  2030. }
  2031. tem = numbers->asPair();
  2032. if (!tem)
  2033. return argError(interp, loc,
  2034. InterpreterMessages::notAList, 0, argv[0]);
  2035. long k;
  2036. if (!tem->car()->exactIntegerValue(k))
  2037. // FIXME message not quite right
  2038. return argError(interp, loc,
  2039. InterpreterMessages::notAnExactInteger, 0, tem->car());
  2040. numbers = tem->cdr();
  2041. if (!formats->stringData(s, len)) {
  2042. tem = formats->asPair();
  2043. if (!tem)
  2044. return argError(interp, loc,
  2045. InterpreterMessages::notAList, 1, argv[1]);
  2046. if (!tem->car()->stringData(s, len))
  2047. return argError(interp, loc,
  2048. InterpreterMessages::notAString, 0, tem->car());
  2049. formats = tem->cdr();
  2050. }
  2051. if (!formatNumber(k, s, len, *result)) {
  2052. interp.setNextLocation(loc);
  2053. interp.message(InterpreterMessages::invalidNumberFormat,
  2054. StringMessageArg(StringC(s, len)));
  2055. }
  2056. }
  2057. return result;
  2058. }
  2059. DEFPRIMITIVE(ExternalProcedure, argc, argv, context, interp, loc)
  2060. {
  2061. const Char *s;
  2062. size_t n;
  2063. if (!argv[0]->stringData(s, n))
  2064. return argError(interp, loc,
  2065. InterpreterMessages::notAString, 0, argv[0]);
  2066. StringC tem(s, n);
  2067. FunctionObj *func = interp.lookupExternalProc(tem);
  2068. if (func)
  2069. return func;
  2070. return interp.makeFalse();
  2071. }
  2072. DEFPRIMITIVE(Error, argc, argv, context, interp, loc)
  2073. {
  2074. const Char *s;
  2075. size_t n;
  2076. if (!argv[0]->stringData(s, n))
  2077. return argError(interp, loc,
  2078. InterpreterMessages::notAString, 0, argv[0]);
  2079. interp.setNextLocation(loc);
  2080. interp.message(InterpreterMessages::errorProc,
  2081. StringMessageArg(StringC(s, n)));
  2082. return interp.makeError();
  2083. }
  2084. DEFPRIMITIVE(StringToNumber, argc, argv, context, interp, loc)
  2085. {
  2086. const Char *s;
  2087. size_t n;
  2088. if (!argv[0]->stringData(s, n))
  2089. return argError(interp, loc,
  2090. InterpreterMessages::notAString, 0, argv[0]);
  2091. long radix;
  2092. if (argc > 1) {
  2093. if (!argv[1]->exactIntegerValue(radix))
  2094. return argError(interp, loc,
  2095. InterpreterMessages::notAnExactInteger, 1, argv[1]);
  2096. switch (radix) {
  2097. case 2:
  2098. case 8:
  2099. case 10:
  2100. case 16:
  2101. break;
  2102. default:
  2103. interp.setNextLocation(loc);
  2104. interp.message(InterpreterMessages::invalidRadix);
  2105. radix = 10;
  2106. break;
  2107. }
  2108. }
  2109. else
  2110. radix = 10;
  2111. StringC tem(s, n);
  2112. return interp.convertNumber(tem, 0, int(radix));
  2113. }
  2114. DEFPRIMITIVE(NumberToString, argc, argv, context, interp, loc)
  2115. {
  2116. // FIXME use optional radix
  2117. double x;
  2118. if (!argv[0]->realValue(x))
  2119. return argError(interp, loc,
  2120. InterpreterMessages::notANumber, 0, argv[0]);
  2121. StrOutputCharStream os;
  2122. argv[0]->print(interp, os);
  2123. StringC tem;
  2124. os.extractString(tem);
  2125. return new (interp) StringObj(tem);
  2126. }
  2127. DEFPRIMITIVE(DisplaySize, argc, argv, context, interp, loc)
  2128. {
  2129. return new (interp) LengthSpecObj(LengthSpec(LengthSpec::displaySize, 1.0));
  2130. }
  2131. DEFPRIMITIVE(TableUnit, argc, argv, context, interp, loc)
  2132. {
  2133. long k;
  2134. if (!argv[0]->exactIntegerValue(k))
  2135. return argError(interp, loc,
  2136. InterpreterMessages::notAnExactInteger, 0, argv[0]);
  2137. return new (interp) LengthSpecObj(LengthSpec(LengthSpec::tableUnit, double(k)));
  2138. }
  2139. DEFPRIMITIVE(IsDisplaySpace, argc, argv, context, interp, loc)
  2140. {
  2141. if (argv[0]->asDisplaySpace())
  2142. return interp.makeTrue();
  2143. else
  2144. return interp.makeFalse();
  2145. }
  2146. DEFPRIMITIVE(DisplaySpace, argc, argv, context, interp, loc)
  2147. {
  2148. FOTBuilder::DisplaySpace displaySpace;
  2149. if (!interp.convertLengthSpec(argv[0], displaySpace.nominal))
  2150. return argError(interp, loc,
  2151. InterpreterMessages::notALengthSpec, 0, argv[0]);
  2152. displaySpace.min = displaySpace.nominal;
  2153. displaySpace.max = displaySpace.nominal;
  2154. // first specified keyword argument takes priority,
  2155. // so scan them backwards...
  2156. for (int i = argc - 1; i > 0; i -= 2) {
  2157. if ((argc & 1) == 0) {
  2158. interp.setNextLocation(loc);
  2159. interp.message(InterpreterMessages::oddKeyArgs);
  2160. return interp.makeError();
  2161. }
  2162. KeywordObj *keyObj = argv[i - 1]->asKeyword();
  2163. if (!keyObj) {
  2164. interp.setNextLocation(loc);
  2165. interp.message(InterpreterMessages::keyArgsNotKey);
  2166. return interp.makeError();
  2167. }
  2168. Identifier::SyntacticKey key;
  2169. if (!keyObj->identifier()->syntacticKey(key)) {
  2170. interp.setNextLocation(loc);
  2171. interp.message(InterpreterMessages::invalidKeyArg,
  2172. StringMessageArg(keyObj->identifier()->name()));
  2173. return interp.makeError();
  2174. }
  2175. else {
  2176. switch (key) {
  2177. case Identifier::keyMin:
  2178. if (!interp.convertLengthSpec(argv[i], displaySpace.min))
  2179. return argError(interp, loc,
  2180. InterpreterMessages::notALengthSpec, i, argv[i]);
  2181. break;
  2182. case Identifier::keyMax:
  2183. if (!interp.convertLengthSpec(argv[i], displaySpace.max))
  2184. return argError(interp, loc,
  2185. InterpreterMessages::notALengthSpec, i, argv[i]);
  2186. break;
  2187. case Identifier::keyIsConditional:
  2188. if (argv[i] == interp.makeTrue())
  2189. displaySpace.conditional = 1;
  2190. else if (argv[i] == interp.makeFalse())
  2191. displaySpace.conditional = 0;
  2192. else
  2193. return argError(interp, loc,
  2194. InterpreterMessages::notABoolean, i, argv[i]);
  2195. break;
  2196. case Identifier::keyPriority:
  2197. if (argv[i]->exactIntegerValue(displaySpace.priority))
  2198. displaySpace.force = 0;
  2199. else {
  2200. SymbolObj *sym = argv[i]->asSymbol();
  2201. if (sym && sym->cValue() == FOTBuilder::symbolForce)
  2202. displaySpace.force = 1;
  2203. else
  2204. return argError(interp, loc,
  2205. InterpreterMessages::notAPriority, i, argv[i]);
  2206. }
  2207. break;
  2208. default:
  2209. interp.setNextLocation(loc);
  2210. interp.message(InterpreterMessages::invalidKeyArg,
  2211. StringMessageArg(keyObj->identifier()->name()));
  2212. return interp.makeError();
  2213. }
  2214. }
  2215. }
  2216. return new (interp) DisplaySpaceObj(displaySpace);
  2217. }
  2218. DEFPRIMITIVE(IsInlineSpace, argc, argv, context, interp, loc)
  2219. {
  2220. if (argv[0]->asInlineSpace())
  2221. return interp.makeTrue();
  2222. else
  2223. return interp.makeFalse();
  2224. }
  2225. DEFPRIMITIVE(InlineSpace, argc, argv, context, interp, loc)
  2226. {
  2227. FOTBuilder::InlineSpace inlineSpace;
  2228. if (!interp.convertLengthSpec(argv[0], inlineSpace.nominal))
  2229. return argError(interp, loc,
  2230. InterpreterMessages::notALengthSpec, 0, argv[0]);
  2231. inlineSpace.min = inlineSpace.nominal;
  2232. inlineSpace.max = inlineSpace.nominal;
  2233. // first specified keyword argument takes priority,
  2234. // so scan them backwards...
  2235. for (int i = argc - 1; i > 0; i -= 2) {
  2236. if ((argc & 1) == 0) {
  2237. interp.setNextLocation(loc);
  2238. interp.message(InterpreterMessages::oddKeyArgs);
  2239. return interp.makeError();
  2240. }
  2241. KeywordObj *keyObj = argv[i - 1]->asKeyword();
  2242. if (!keyObj) {
  2243. interp.setNextLocation(loc);
  2244. interp.message(InterpreterMessages::keyArgsNotKey);
  2245. return interp.makeError();
  2246. }
  2247. Identifier::SyntacticKey key;
  2248. if (!keyObj->identifier()->syntacticKey(key)) {
  2249. interp.setNextLocation(loc);
  2250. interp.message(InterpreterMessages::invalidKeyArg,
  2251. StringMessageArg(keyObj->identifier()->name()));
  2252. return interp.makeError();
  2253. }
  2254. else {
  2255. switch (key) {
  2256. case Identifier::keyMin:
  2257. if (!interp.convertLengthSpec(argv[i], inlineSpace.min))
  2258. return argError(interp, loc,
  2259. InterpreterMessages::notALengthSpec, i, argv[i]);
  2260. break;
  2261. case Identifier::keyMax:
  2262. if (!interp.convertLengthSpec(argv[i], inlineSpace.max))
  2263. return argError(interp, loc,
  2264. InterpreterMessages::notALengthSpec, i, argv[i]);
  2265. break;
  2266. default:
  2267. interp.setNextLocation(loc);
  2268. interp.message(InterpreterMessages::invalidKeyArg,
  2269. StringMessageArg(keyObj->identifier()->name()));
  2270. return interp.makeError();
  2271. }
  2272. }
  2273. }
  2274. return new (interp) InlineSpaceObj(inlineSpace);
  2275. return argv[0];
  2276. }
  2277. DEFPRIMITIVE(IsColor, argc, argv, context, interp, loc)
  2278. {
  2279. if (argv[0]->asColor())
  2280. return interp.makeTrue();
  2281. else
  2282. return interp.makeFalse();
  2283. }
  2284. DEFPRIMITIVE(IsColorSpace, argc, argv, context, interp, loc)
  2285. {
  2286. if (argv[0]->asColorSpace())
  2287. return interp.makeTrue();
  2288. else
  2289. return interp.makeFalse();
  2290. }
  2291. DEFPRIMITIVE(ColorSpace, argc, argv, context, interp, loc)
  2292. {
  2293. const Char *s;
  2294. size_t n;
  2295. if (!argv[0]->stringData(s, n))
  2296. return argError(interp, loc,
  2297. InterpreterMessages::notAString, 0, argv[0]);
  2298. if (StringC(s, n)
  2299. != interp.makeStringC("ISO/IEC 10179:1996//Color-Space Family::Device RGB")) {
  2300. interp.setNextLocation(loc);
  2301. interp.message(InterpreterMessages::unknownColorSpaceFamily,
  2302. StringMessageArg(StringC(s, n)));
  2303. return interp.makeError();
  2304. }
  2305. if (argc > 1) {
  2306. interp.setNextLocation(loc);
  2307. interp.message(InterpreterMessages::deviceRGBColorSpaceNoArgs);
  2308. }
  2309. return new (interp) DeviceRGBColorSpaceObj;
  2310. }
  2311. DEFPRIMITIVE(Color, argc, argv, context, interp, loc)
  2312. {
  2313. ColorSpaceObj *colorSpace = argv[0]->asColorSpace();
  2314. if (!colorSpace)
  2315. return argError(interp, loc,
  2316. InterpreterMessages::notAColorSpace, 0, argv[0]);
  2317. return colorSpace->makeColor(argc - 1, argv + 1, interp, loc);
  2318. }
  2319. DEFPRIMITIVE(IsAddress, argc, argv, context, interp, loc)
  2320. {
  2321. if (argv[0]->asAddress())
  2322. return interp.makeTrue();
  2323. else
  2324. return interp.makeFalse();
  2325. }
  2326. DEFPRIMITIVE(IsAddressLocal, argc, argv, context, interp, loc)
  2327. {
  2328. AddressObj *address = argv[0]->asAddress();
  2329. if (!address)
  2330. return argError(interp, loc,
  2331. InterpreterMessages::notAnAddress, 0, argv[0]);
  2332. if (!context.currentNode)
  2333. return noCurrentNodeError(interp, loc);
  2334. switch (address->address().type) {
  2335. case FOTBuilder::Address::resolvedNode:
  2336. if (address->address().node->sameGrove(*context.currentNode))
  2337. return interp.makeTrue();
  2338. else
  2339. return interp.makeFalse();
  2340. case FOTBuilder::Address::idref:
  2341. return interp.makeTrue();
  2342. case FOTBuilder::Address::entity:
  2343. return interp.makeFalse();
  2344. default:
  2345. break;
  2346. }
  2347. return interp.makeFalse();
  2348. }
  2349. DEFPRIMITIVE(IsAddressVisited, argc, argv, context, interp, loc)
  2350. {
  2351. AddressObj *address = argv[0]->asAddress();
  2352. if (!address)
  2353. return argError(interp, loc,
  2354. InterpreterMessages::notAnAddress, 0, argv[0]);
  2355. // FIXME
  2356. return interp.makeFalse();
  2357. }
  2358. DEFPRIMITIVE(CurrentNodeAddress, argc, argv, context, interp, loc)
  2359. {
  2360. if (!context.currentNode)
  2361. return noCurrentNodeError(interp, loc);
  2362. return new (interp) AddressObj(FOTBuilder::Address::resolvedNode, context.currentNode);
  2363. }
  2364. DEFPRIMITIVE(HytimeLinkend, argc, argv, context, interp, loc)
  2365. {
  2366. if (!context.currentNode)
  2367. return noCurrentNodeError(interp, loc);
  2368. return new (interp) AddressObj(FOTBuilder::Address::hytimeLinkend, context.currentNode);
  2369. }
  2370. DEFPRIMITIVE(SgmlDocumentAddress, argc, argv, context, interp, loc)
  2371. {
  2372. const Char *s;
  2373. size_t n;
  2374. if (!argv[0]->stringData(s, n))
  2375. return argError(interp, loc,
  2376. InterpreterMessages::notAString, 0, argv[0]);
  2377. StringC sysid(s, n);
  2378. if (!argv[1]->stringData(s, n))
  2379. return argError(interp, loc,
  2380. InterpreterMessages::notAString, 1, argv[1]);
  2381. return new (interp) AddressObj(FOTBuilder::Address::sgmlDocument, NodePtr(), sysid, StringC(s, n));
  2382. }
  2383. DEFPRIMITIVE(IdrefAddress, argc, argv, context, interp, loc)
  2384. {
  2385. // The advantage of doing this rather than using an NodeAddressObj,
  2386. // is that when it's a forward reference we don't have to
  2387. // wait for the node. It might be cleaner to use a ProxyNode class
  2388. // for this.
  2389. const Char *s;
  2390. size_t n;
  2391. if (!argv[0]->stringData(s, n))
  2392. return argError(interp, loc,
  2393. InterpreterMessages::notAString, 0, argv[0]);
  2394. if (!context.currentNode)
  2395. return noCurrentNodeError(interp, loc);
  2396. return new