/contrib/groff/src/preproc/pic/pic.y

https://bitbucket.org/freebsd/freebsd-head/ · Happy · 1898 lines · 1807 code · 91 blank · 0 comment · 0 complexity · 44dd625f16f386f5b1aefd3762395d63 MD5 · raw file

  1. /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003, 2004, 2005
  2. Free Software Foundation, Inc.
  3. Written by James Clark (jjc@jclark.com)
  4. This file is part of groff.
  5. groff is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 2, or (at your option) any later
  8. version.
  9. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with groff; see the file COPYING. If not, write to the Free Software
  15. Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
  16. %{
  17. #include "pic.h"
  18. #include "ptable.h"
  19. #include "object.h"
  20. extern int delim_flag;
  21. extern void copy_rest_thru(const char *, const char *);
  22. extern void copy_file_thru(const char *, const char *, const char *);
  23. extern void push_body(const char *);
  24. extern void do_for(char *var, double from, double to,
  25. int by_is_multiplicative, double by, char *body);
  26. extern void do_lookahead();
  27. /* Maximum number of characters produced by printf("%g") */
  28. #define GDIGITS 14
  29. int yylex();
  30. void yyerror(const char *);
  31. void reset(const char *nm);
  32. void reset_all();
  33. place *lookup_label(const char *);
  34. void define_label(const char *label, const place *pl);
  35. direction current_direction;
  36. position current_position;
  37. implement_ptable(place)
  38. PTABLE(place) top_table;
  39. PTABLE(place) *current_table = &top_table;
  40. saved_state *current_saved_state = 0;
  41. object_list olist;
  42. const char *ordinal_postfix(int n);
  43. const char *object_type_name(object_type type);
  44. char *format_number(const char *form, double n);
  45. char *do_sprintf(const char *form, const double *v, int nv);
  46. %}
  47. %union {
  48. char *str;
  49. int n;
  50. double x;
  51. struct { double x, y; } pair;
  52. struct { double x; char *body; } if_data;
  53. struct { char *str; const char *filename; int lineno; } lstr;
  54. struct { double *v; int nv; int maxv; } dv;
  55. struct { double val; int is_multiplicative; } by;
  56. place pl;
  57. object *obj;
  58. corner crn;
  59. path *pth;
  60. object_spec *spec;
  61. saved_state *pstate;
  62. graphics_state state;
  63. object_type obtype;
  64. }
  65. %token <str> LABEL
  66. %token <str> VARIABLE
  67. %token <x> NUMBER
  68. %token <lstr> TEXT
  69. %token <lstr> COMMAND_LINE
  70. %token <str> DELIMITED
  71. %token <n> ORDINAL
  72. %token TH
  73. %token LEFT_ARROW_HEAD
  74. %token RIGHT_ARROW_HEAD
  75. %token DOUBLE_ARROW_HEAD
  76. %token LAST
  77. %token UP
  78. %token DOWN
  79. %token LEFT
  80. %token RIGHT
  81. %token BOX
  82. %token CIRCLE
  83. %token ELLIPSE
  84. %token ARC
  85. %token LINE
  86. %token ARROW
  87. %token MOVE
  88. %token SPLINE
  89. %token HEIGHT
  90. %token RADIUS
  91. %token FIGNAME
  92. %token WIDTH
  93. %token DIAMETER
  94. %token UP
  95. %token DOWN
  96. %token RIGHT
  97. %token LEFT
  98. %token FROM
  99. %token TO
  100. %token AT
  101. %token WITH
  102. %token BY
  103. %token THEN
  104. %token SOLID
  105. %token DOTTED
  106. %token DASHED
  107. %token CHOP
  108. %token SAME
  109. %token INVISIBLE
  110. %token LJUST
  111. %token RJUST
  112. %token ABOVE
  113. %token BELOW
  114. %token OF
  115. %token THE
  116. %token WAY
  117. %token BETWEEN
  118. %token AND
  119. %token HERE
  120. %token DOT_N
  121. %token DOT_E
  122. %token DOT_W
  123. %token DOT_S
  124. %token DOT_NE
  125. %token DOT_SE
  126. %token DOT_NW
  127. %token DOT_SW
  128. %token DOT_C
  129. %token DOT_START
  130. %token DOT_END
  131. %token DOT_X
  132. %token DOT_Y
  133. %token DOT_HT
  134. %token DOT_WID
  135. %token DOT_RAD
  136. %token SIN
  137. %token COS
  138. %token ATAN2
  139. %token LOG
  140. %token EXP
  141. %token SQRT
  142. %token K_MAX
  143. %token K_MIN
  144. %token INT
  145. %token RAND
  146. %token SRAND
  147. %token COPY
  148. %token THRU
  149. %token TOP
  150. %token BOTTOM
  151. %token UPPER
  152. %token LOWER
  153. %token SH
  154. %token PRINT
  155. %token CW
  156. %token CCW
  157. %token FOR
  158. %token DO
  159. %token IF
  160. %token ELSE
  161. %token ANDAND
  162. %token OROR
  163. %token NOTEQUAL
  164. %token EQUALEQUAL
  165. %token LESSEQUAL
  166. %token GREATEREQUAL
  167. %token LEFT_CORNER
  168. %token RIGHT_CORNER
  169. %token NORTH
  170. %token SOUTH
  171. %token EAST
  172. %token WEST
  173. %token CENTER
  174. %token END
  175. %token START
  176. %token RESET
  177. %token UNTIL
  178. %token PLOT
  179. %token THICKNESS
  180. %token FILL
  181. %token COLORED
  182. %token OUTLINED
  183. %token SHADED
  184. %token ALIGNED
  185. %token SPRINTF
  186. %token COMMAND
  187. %token DEFINE
  188. %token UNDEF
  189. %left '.'
  190. /* this ensures that plot 17 "%g" parses as (plot 17 "%g") */
  191. %left PLOT
  192. %left TEXT SPRINTF
  193. /* give text adjustments higher precedence than TEXT, so that
  194. box "foo" above ljust == box ("foo" above ljust)
  195. */
  196. %left LJUST RJUST ABOVE BELOW
  197. %left LEFT RIGHT
  198. /* Give attributes that take an optional expression a higher
  199. precedence than left and right, so that eg `line chop left'
  200. parses properly. */
  201. %left CHOP SOLID DASHED DOTTED UP DOWN FILL COLORED OUTLINED
  202. %left LABEL
  203. %left VARIABLE NUMBER '(' SIN COS ATAN2 LOG EXP SQRT K_MAX K_MIN INT RAND SRAND LAST
  204. %left ORDINAL HERE '`'
  205. %left BOX CIRCLE ELLIPSE ARC LINE ARROW SPLINE '['
  206. /* these need to be lower than '-' */
  207. %left HEIGHT RADIUS WIDTH DIAMETER FROM TO AT THICKNESS
  208. /* these must have higher precedence than CHOP so that `label %prec CHOP'
  209. works */
  210. %left DOT_N DOT_E DOT_W DOT_S DOT_NE DOT_SE DOT_NW DOT_SW DOT_C
  211. %left DOT_START DOT_END TOP BOTTOM LEFT_CORNER RIGHT_CORNER
  212. %left UPPER LOWER NORTH SOUTH EAST WEST CENTER START END
  213. %left ','
  214. %left OROR
  215. %left ANDAND
  216. %left EQUALEQUAL NOTEQUAL
  217. %left '<' '>' LESSEQUAL GREATEREQUAL
  218. %left BETWEEN OF
  219. %left AND
  220. %left '+' '-'
  221. %left '*' '/' '%'
  222. %right '!'
  223. %right '^'
  224. %type <x> expr any_expr text_expr
  225. %type <by> optional_by
  226. %type <pair> expr_pair position_not_place
  227. %type <if_data> simple_if
  228. %type <obj> nth_primitive
  229. %type <crn> corner
  230. %type <pth> path label_path relative_path
  231. %type <pl> place label element element_list middle_element_list
  232. %type <spec> object_spec
  233. %type <pair> position
  234. %type <obtype> object_type
  235. %type <n> optional_ordinal_last ordinal
  236. %type <str> macro_name until
  237. %type <dv> sprintf_args
  238. %type <lstr> text print_args print_arg
  239. %%
  240. top:
  241. optional_separator
  242. | element_list
  243. {
  244. if (olist.head)
  245. print_picture(olist.head);
  246. }
  247. ;
  248. element_list:
  249. optional_separator middle_element_list optional_separator
  250. { $$ = $2; }
  251. ;
  252. middle_element_list:
  253. element
  254. { $$ = $1; }
  255. | middle_element_list separator element
  256. { $$ = $1; }
  257. ;
  258. optional_separator:
  259. /* empty */
  260. | separator
  261. ;
  262. separator:
  263. ';'
  264. | separator ';'
  265. ;
  266. placeless_element:
  267. FIGNAME '=' macro_name
  268. {
  269. a_delete graphname;
  270. graphname = new char[strlen($3) + 1];
  271. strcpy(graphname, $3);
  272. a_delete $3;
  273. }
  274. |
  275. VARIABLE '=' any_expr
  276. {
  277. define_variable($1, $3);
  278. a_delete $1;
  279. }
  280. | VARIABLE ':' '=' any_expr
  281. {
  282. place *p = lookup_label($1);
  283. if (!p) {
  284. lex_error("variable `%1' not defined", $1);
  285. YYABORT;
  286. }
  287. p->obj = 0;
  288. p->x = $4;
  289. p->y = 0.0;
  290. a_delete $1;
  291. }
  292. | UP
  293. { current_direction = UP_DIRECTION; }
  294. | DOWN
  295. { current_direction = DOWN_DIRECTION; }
  296. | LEFT
  297. { current_direction = LEFT_DIRECTION; }
  298. | RIGHT
  299. { current_direction = RIGHT_DIRECTION; }
  300. | COMMAND_LINE
  301. {
  302. olist.append(make_command_object($1.str, $1.filename,
  303. $1.lineno));
  304. }
  305. | COMMAND print_args
  306. {
  307. olist.append(make_command_object($2.str, $2.filename,
  308. $2.lineno));
  309. }
  310. | PRINT print_args
  311. {
  312. fprintf(stderr, "%s\n", $2.str);
  313. a_delete $2.str;
  314. fflush(stderr);
  315. }
  316. | SH
  317. { delim_flag = 1; }
  318. DELIMITED
  319. {
  320. delim_flag = 0;
  321. if (safer_flag)
  322. lex_error("unsafe to run command `%1'", $3);
  323. else
  324. system($3);
  325. a_delete $3;
  326. }
  327. | COPY TEXT
  328. {
  329. if (yychar < 0)
  330. do_lookahead();
  331. do_copy($2.str);
  332. // do not delete the filename
  333. }
  334. | COPY TEXT THRU
  335. { delim_flag = 2; }
  336. DELIMITED
  337. { delim_flag = 0; }
  338. until
  339. {
  340. if (yychar < 0)
  341. do_lookahead();
  342. copy_file_thru($2.str, $5, $7);
  343. // do not delete the filename
  344. a_delete $5;
  345. a_delete $7;
  346. }
  347. | COPY THRU
  348. { delim_flag = 2; }
  349. DELIMITED
  350. { delim_flag = 0; }
  351. until
  352. {
  353. if (yychar < 0)
  354. do_lookahead();
  355. copy_rest_thru($4, $6);
  356. a_delete $4;
  357. a_delete $6;
  358. }
  359. | FOR VARIABLE '=' expr TO expr optional_by DO
  360. { delim_flag = 1; }
  361. DELIMITED
  362. {
  363. delim_flag = 0;
  364. if (yychar < 0)
  365. do_lookahead();
  366. do_for($2, $4, $6, $7.is_multiplicative, $7.val, $10);
  367. }
  368. | simple_if
  369. {
  370. if (yychar < 0)
  371. do_lookahead();
  372. if ($1.x != 0.0)
  373. push_body($1.body);
  374. a_delete $1.body;
  375. }
  376. | simple_if ELSE
  377. { delim_flag = 1; }
  378. DELIMITED
  379. {
  380. delim_flag = 0;
  381. if (yychar < 0)
  382. do_lookahead();
  383. if ($1.x != 0.0)
  384. push_body($1.body);
  385. else
  386. push_body($4);
  387. a_delete $1.body;
  388. a_delete $4;
  389. }
  390. | reset_variables
  391. | RESET
  392. { define_variable("scale", 1.0); }
  393. ;
  394. macro_name:
  395. VARIABLE
  396. | LABEL
  397. ;
  398. reset_variables:
  399. RESET VARIABLE
  400. {
  401. reset($2);
  402. a_delete $2;
  403. }
  404. | reset_variables VARIABLE
  405. {
  406. reset($2);
  407. a_delete $2;
  408. }
  409. | reset_variables ',' VARIABLE
  410. {
  411. reset($3);
  412. a_delete $3;
  413. }
  414. ;
  415. print_args:
  416. print_arg
  417. { $$ = $1; }
  418. | print_args print_arg
  419. {
  420. $$.str = new char[strlen($1.str) + strlen($2.str) + 1];
  421. strcpy($$.str, $1.str);
  422. strcat($$.str, $2.str);
  423. a_delete $1.str;
  424. a_delete $2.str;
  425. if ($1.filename) {
  426. $$.filename = $1.filename;
  427. $$.lineno = $1.lineno;
  428. }
  429. else if ($2.filename) {
  430. $$.filename = $2.filename;
  431. $$.lineno = $2.lineno;
  432. }
  433. }
  434. ;
  435. print_arg:
  436. expr %prec ','
  437. {
  438. $$.str = new char[GDIGITS + 1];
  439. sprintf($$.str, "%g", $1);
  440. $$.filename = 0;
  441. $$.lineno = 0;
  442. }
  443. | text
  444. { $$ = $1; }
  445. | position %prec ','
  446. {
  447. $$.str = new char[GDIGITS + 2 + GDIGITS + 1];
  448. sprintf($$.str, "%g, %g", $1.x, $1.y);
  449. $$.filename = 0;
  450. $$.lineno = 0;
  451. }
  452. ;
  453. simple_if:
  454. IF any_expr THEN
  455. { delim_flag = 1; }
  456. DELIMITED
  457. {
  458. delim_flag = 0;
  459. $$.x = $2;
  460. $$.body = $5;
  461. }
  462. ;
  463. until:
  464. /* empty */
  465. { $$ = 0; }
  466. | UNTIL TEXT
  467. { $$ = $2.str; }
  468. ;
  469. any_expr:
  470. expr
  471. { $$ = $1; }
  472. | text_expr
  473. { $$ = $1; }
  474. ;
  475. text_expr:
  476. text EQUALEQUAL text
  477. {
  478. $$ = strcmp($1.str, $3.str) == 0;
  479. a_delete $1.str;
  480. a_delete $3.str;
  481. }
  482. | text NOTEQUAL text
  483. {
  484. $$ = strcmp($1.str, $3.str) != 0;
  485. a_delete $1.str;
  486. a_delete $3.str;
  487. }
  488. | text_expr ANDAND text_expr
  489. { $$ = ($1 != 0.0 && $3 != 0.0); }
  490. | text_expr ANDAND expr
  491. { $$ = ($1 != 0.0 && $3 != 0.0); }
  492. | expr ANDAND text_expr
  493. { $$ = ($1 != 0.0 && $3 != 0.0); }
  494. | text_expr OROR text_expr
  495. { $$ = ($1 != 0.0 || $3 != 0.0); }
  496. | text_expr OROR expr
  497. { $$ = ($1 != 0.0 || $3 != 0.0); }
  498. | expr OROR text_expr
  499. { $$ = ($1 != 0.0 || $3 != 0.0); }
  500. | '!' text_expr
  501. { $$ = ($2 == 0.0); }
  502. ;
  503. optional_by:
  504. /* empty */
  505. {
  506. $$.val = 1.0;
  507. $$.is_multiplicative = 0;
  508. }
  509. | BY expr
  510. {
  511. $$.val = $2;
  512. $$.is_multiplicative = 0;
  513. }
  514. | BY '*' expr
  515. {
  516. $$.val = $3;
  517. $$.is_multiplicative = 1;
  518. }
  519. ;
  520. element:
  521. object_spec
  522. {
  523. $$.obj = $1->make_object(&current_position,
  524. &current_direction);
  525. if ($$.obj == 0)
  526. YYABORT;
  527. delete $1;
  528. if ($$.obj)
  529. olist.append($$.obj);
  530. else {
  531. $$.x = current_position.x;
  532. $$.y = current_position.y;
  533. }
  534. }
  535. | LABEL ':' optional_separator element
  536. {
  537. $$ = $4;
  538. define_label($1, & $$);
  539. a_delete $1;
  540. }
  541. | LABEL ':' optional_separator position_not_place
  542. {
  543. $$.obj = 0;
  544. $$.x = $4.x;
  545. $$.y = $4.y;
  546. define_label($1, & $$);
  547. a_delete $1;
  548. }
  549. | LABEL ':' optional_separator place
  550. {
  551. $$ = $4;
  552. define_label($1, & $$);
  553. a_delete $1;
  554. }
  555. | '{'
  556. {
  557. $<state>$.x = current_position.x;
  558. $<state>$.y = current_position.y;
  559. $<state>$.dir = current_direction;
  560. }
  561. element_list '}'
  562. {
  563. current_position.x = $<state>2.x;
  564. current_position.y = $<state>2.y;
  565. current_direction = $<state>2.dir;
  566. }
  567. optional_element
  568. {
  569. $$ = $3;
  570. }
  571. | placeless_element
  572. {
  573. $$.obj = 0;
  574. $$.x = current_position.x;
  575. $$.y = current_position.y;
  576. }
  577. ;
  578. optional_element:
  579. /* empty */
  580. {}
  581. | element
  582. {}
  583. ;
  584. object_spec:
  585. BOX
  586. { $$ = new object_spec(BOX_OBJECT); }
  587. | CIRCLE
  588. { $$ = new object_spec(CIRCLE_OBJECT); }
  589. | ELLIPSE
  590. { $$ = new object_spec(ELLIPSE_OBJECT); }
  591. | ARC
  592. {
  593. $$ = new object_spec(ARC_OBJECT);
  594. $$->dir = current_direction;
  595. }
  596. | LINE
  597. {
  598. $$ = new object_spec(LINE_OBJECT);
  599. lookup_variable("lineht", & $$->segment_height);
  600. lookup_variable("linewid", & $$->segment_width);
  601. $$->dir = current_direction;
  602. }
  603. | ARROW
  604. {
  605. $$ = new object_spec(ARROW_OBJECT);
  606. lookup_variable("lineht", & $$->segment_height);
  607. lookup_variable("linewid", & $$->segment_width);
  608. $$->dir = current_direction;
  609. }
  610. | MOVE
  611. {
  612. $$ = new object_spec(MOVE_OBJECT);
  613. lookup_variable("moveht", & $$->segment_height);
  614. lookup_variable("movewid", & $$->segment_width);
  615. $$->dir = current_direction;
  616. }
  617. | SPLINE
  618. {
  619. $$ = new object_spec(SPLINE_OBJECT);
  620. lookup_variable("lineht", & $$->segment_height);
  621. lookup_variable("linewid", & $$->segment_width);
  622. $$->dir = current_direction;
  623. }
  624. | text %prec TEXT
  625. {
  626. $$ = new object_spec(TEXT_OBJECT);
  627. $$->text = new text_item($1.str, $1.filename, $1.lineno);
  628. }
  629. | PLOT expr
  630. {
  631. $$ = new object_spec(TEXT_OBJECT);
  632. $$->text = new text_item(format_number(0, $2), 0, -1);
  633. }
  634. | PLOT expr text
  635. {
  636. $$ = new object_spec(TEXT_OBJECT);
  637. $$->text = new text_item(format_number($3.str, $2),
  638. $3.filename, $3.lineno);
  639. a_delete $3.str;
  640. }
  641. | '['
  642. {
  643. saved_state *p = new saved_state;
  644. $<pstate>$ = p;
  645. p->x = current_position.x;
  646. p->y = current_position.y;
  647. p->dir = current_direction;
  648. p->tbl = current_table;
  649. p->prev = current_saved_state;
  650. current_position.x = 0.0;
  651. current_position.y = 0.0;
  652. current_table = new PTABLE(place);
  653. current_saved_state = p;
  654. olist.append(make_mark_object());
  655. }
  656. element_list ']'
  657. {
  658. current_position.x = $<pstate>2->x;
  659. current_position.y = $<pstate>2->y;
  660. current_direction = $<pstate>2->dir;
  661. $$ = new object_spec(BLOCK_OBJECT);
  662. olist.wrap_up_block(& $$->oblist);
  663. $$->tbl = current_table;
  664. current_table = $<pstate>2->tbl;
  665. current_saved_state = $<pstate>2->prev;
  666. delete $<pstate>2;
  667. }
  668. | object_spec HEIGHT expr
  669. {
  670. $$ = $1;
  671. $$->height = $3;
  672. $$->flags |= HAS_HEIGHT;
  673. }
  674. | object_spec RADIUS expr
  675. {
  676. $$ = $1;
  677. $$->radius = $3;
  678. $$->flags |= HAS_RADIUS;
  679. }
  680. | object_spec WIDTH expr
  681. {
  682. $$ = $1;
  683. $$->width = $3;
  684. $$->flags |= HAS_WIDTH;
  685. }
  686. | object_spec DIAMETER expr
  687. {
  688. $$ = $1;
  689. $$->radius = $3/2.0;
  690. $$->flags |= HAS_RADIUS;
  691. }
  692. | object_spec expr %prec HEIGHT
  693. {
  694. $$ = $1;
  695. $$->flags |= HAS_SEGMENT;
  696. switch ($$->dir) {
  697. case UP_DIRECTION:
  698. $$->segment_pos.y += $2;
  699. break;
  700. case DOWN_DIRECTION:
  701. $$->segment_pos.y -= $2;
  702. break;
  703. case RIGHT_DIRECTION:
  704. $$->segment_pos.x += $2;
  705. break;
  706. case LEFT_DIRECTION:
  707. $$->segment_pos.x -= $2;
  708. break;
  709. }
  710. }
  711. | object_spec UP
  712. {
  713. $$ = $1;
  714. $$->dir = UP_DIRECTION;
  715. $$->flags |= HAS_SEGMENT;
  716. $$->segment_pos.y += $$->segment_height;
  717. }
  718. | object_spec UP expr
  719. {
  720. $$ = $1;
  721. $$->dir = UP_DIRECTION;
  722. $$->flags |= HAS_SEGMENT;
  723. $$->segment_pos.y += $3;
  724. }
  725. | object_spec DOWN
  726. {
  727. $$ = $1;
  728. $$->dir = DOWN_DIRECTION;
  729. $$->flags |= HAS_SEGMENT;
  730. $$->segment_pos.y -= $$->segment_height;
  731. }
  732. | object_spec DOWN expr
  733. {
  734. $$ = $1;
  735. $$->dir = DOWN_DIRECTION;
  736. $$->flags |= HAS_SEGMENT;
  737. $$->segment_pos.y -= $3;
  738. }
  739. | object_spec RIGHT
  740. {
  741. $$ = $1;
  742. $$->dir = RIGHT_DIRECTION;
  743. $$->flags |= HAS_SEGMENT;
  744. $$->segment_pos.x += $$->segment_width;
  745. }
  746. | object_spec RIGHT expr
  747. {
  748. $$ = $1;
  749. $$->dir = RIGHT_DIRECTION;
  750. $$->flags |= HAS_SEGMENT;
  751. $$->segment_pos.x += $3;
  752. }
  753. | object_spec LEFT
  754. {
  755. $$ = $1;
  756. $$->dir = LEFT_DIRECTION;
  757. $$->flags |= HAS_SEGMENT;
  758. $$->segment_pos.x -= $$->segment_width;
  759. }
  760. | object_spec LEFT expr
  761. {
  762. $$ = $1;
  763. $$->dir = LEFT_DIRECTION;
  764. $$->flags |= HAS_SEGMENT;
  765. $$->segment_pos.x -= $3;
  766. }
  767. | object_spec FROM position
  768. {
  769. $$ = $1;
  770. $$->flags |= HAS_FROM;
  771. $$->from.x = $3.x;
  772. $$->from.y = $3.y;
  773. }
  774. | object_spec TO position
  775. {
  776. $$ = $1;
  777. if ($$->flags & HAS_SEGMENT)
  778. $$->segment_list = new segment($$->segment_pos,
  779. $$->segment_is_absolute,
  780. $$->segment_list);
  781. $$->flags |= HAS_SEGMENT;
  782. $$->segment_pos.x = $3.x;
  783. $$->segment_pos.y = $3.y;
  784. $$->segment_is_absolute = 1;
  785. $$->flags |= HAS_TO;
  786. $$->to.x = $3.x;
  787. $$->to.y = $3.y;
  788. }
  789. | object_spec AT position
  790. {
  791. $$ = $1;
  792. $$->flags |= HAS_AT;
  793. $$->at.x = $3.x;
  794. $$->at.y = $3.y;
  795. if ($$->type != ARC_OBJECT) {
  796. $$->flags |= HAS_FROM;
  797. $$->from.x = $3.x;
  798. $$->from.y = $3.y;
  799. }
  800. }
  801. | object_spec WITH path
  802. {
  803. $$ = $1;
  804. $$->flags |= HAS_WITH;
  805. $$->with = $3;
  806. }
  807. | object_spec WITH position %prec ','
  808. {
  809. $$ = $1;
  810. $$->flags |= HAS_WITH;
  811. position pos;
  812. pos.x = $3.x;
  813. pos.y = $3.y;
  814. $$->with = new path(pos);
  815. }
  816. | object_spec BY expr_pair
  817. {
  818. $$ = $1;
  819. $$->flags |= HAS_SEGMENT;
  820. $$->segment_pos.x += $3.x;
  821. $$->segment_pos.y += $3.y;
  822. }
  823. | object_spec THEN
  824. {
  825. $$ = $1;
  826. if ($$->flags & HAS_SEGMENT) {
  827. $$->segment_list = new segment($$->segment_pos,
  828. $$->segment_is_absolute,
  829. $$->segment_list);
  830. $$->flags &= ~HAS_SEGMENT;
  831. $$->segment_pos.x = $$->segment_pos.y = 0.0;
  832. $$->segment_is_absolute = 0;
  833. }
  834. }
  835. | object_spec SOLID
  836. {
  837. $$ = $1; // nothing
  838. }
  839. | object_spec DOTTED
  840. {
  841. $$ = $1;
  842. $$->flags |= IS_DOTTED;
  843. lookup_variable("dashwid", & $$->dash_width);
  844. }
  845. | object_spec DOTTED expr
  846. {
  847. $$ = $1;
  848. $$->flags |= IS_DOTTED;
  849. $$->dash_width = $3;
  850. }
  851. | object_spec DASHED
  852. {
  853. $$ = $1;
  854. $$->flags |= IS_DASHED;
  855. lookup_variable("dashwid", & $$->dash_width);
  856. }
  857. | object_spec DASHED expr
  858. {
  859. $$ = $1;
  860. $$->flags |= IS_DASHED;
  861. $$->dash_width = $3;
  862. }
  863. | object_spec FILL
  864. {
  865. $$ = $1;
  866. $$->flags |= IS_DEFAULT_FILLED;
  867. }
  868. | object_spec FILL expr
  869. {
  870. $$ = $1;
  871. $$->flags |= IS_FILLED;
  872. $$->fill = $3;
  873. }
  874. | object_spec SHADED text
  875. {
  876. $$ = $1;
  877. $$->flags |= (IS_SHADED | IS_FILLED);
  878. $$->shaded = new char[strlen($3.str)+1];
  879. strcpy($$->shaded, $3.str);
  880. }
  881. | object_spec COLORED text
  882. {
  883. $$ = $1;
  884. $$->flags |= (IS_SHADED | IS_OUTLINED | IS_FILLED);
  885. $$->shaded = new char[strlen($3.str)+1];
  886. strcpy($$->shaded, $3.str);
  887. $$->outlined = new char[strlen($3.str)+1];
  888. strcpy($$->outlined, $3.str);
  889. }
  890. | object_spec OUTLINED text
  891. {
  892. $$ = $1;
  893. $$->flags |= IS_OUTLINED;
  894. $$->outlined = new char[strlen($3.str)+1];
  895. strcpy($$->outlined, $3.str);
  896. }
  897. | object_spec CHOP
  898. {
  899. $$ = $1;
  900. // line chop chop means line chop 0 chop 0
  901. if ($$->flags & IS_DEFAULT_CHOPPED) {
  902. $$->flags |= IS_CHOPPED;
  903. $$->flags &= ~IS_DEFAULT_CHOPPED;
  904. $$->start_chop = $$->end_chop = 0.0;
  905. }
  906. else if ($$->flags & IS_CHOPPED) {
  907. $$->end_chop = 0.0;
  908. }
  909. else {
  910. $$->flags |= IS_DEFAULT_CHOPPED;
  911. }
  912. }
  913. | object_spec CHOP expr
  914. {
  915. $$ = $1;
  916. if ($$->flags & IS_DEFAULT_CHOPPED) {
  917. $$->flags |= IS_CHOPPED;
  918. $$->flags &= ~IS_DEFAULT_CHOPPED;
  919. $$->start_chop = 0.0;
  920. $$->end_chop = $3;
  921. }
  922. else if ($$->flags & IS_CHOPPED) {
  923. $$->end_chop = $3;
  924. }
  925. else {
  926. $$->start_chop = $$->end_chop = $3;
  927. $$->flags |= IS_CHOPPED;
  928. }
  929. }
  930. | object_spec SAME
  931. {
  932. $$ = $1;
  933. $$->flags |= IS_SAME;
  934. }
  935. | object_spec INVISIBLE
  936. {
  937. $$ = $1;
  938. $$->flags |= IS_INVISIBLE;
  939. }
  940. | object_spec LEFT_ARROW_HEAD
  941. {
  942. $$ = $1;
  943. $$->flags |= HAS_LEFT_ARROW_HEAD;
  944. }
  945. | object_spec RIGHT_ARROW_HEAD
  946. {
  947. $$ = $1;
  948. $$->flags |= HAS_RIGHT_ARROW_HEAD;
  949. }
  950. | object_spec DOUBLE_ARROW_HEAD
  951. {
  952. $$ = $1;
  953. $$->flags |= (HAS_LEFT_ARROW_HEAD|HAS_RIGHT_ARROW_HEAD);
  954. }
  955. | object_spec CW
  956. {
  957. $$ = $1;
  958. $$->flags |= IS_CLOCKWISE;
  959. }
  960. | object_spec CCW
  961. {
  962. $$ = $1;
  963. $$->flags &= ~IS_CLOCKWISE;
  964. }
  965. | object_spec text %prec TEXT
  966. {
  967. $$ = $1;
  968. text_item **p;
  969. for (p = & $$->text; *p; p = &(*p)->next)
  970. ;
  971. *p = new text_item($2.str, $2.filename, $2.lineno);
  972. }
  973. | object_spec LJUST
  974. {
  975. $$ = $1;
  976. if ($$->text) {
  977. text_item *p;
  978. for (p = $$->text; p->next; p = p->next)
  979. ;
  980. p->adj.h = LEFT_ADJUST;
  981. }
  982. }
  983. | object_spec RJUST
  984. {
  985. $$ = $1;
  986. if ($$->text) {
  987. text_item *p;
  988. for (p = $$->text; p->next; p = p->next)
  989. ;
  990. p->adj.h = RIGHT_ADJUST;
  991. }
  992. }
  993. | object_spec ABOVE
  994. {
  995. $$ = $1;
  996. if ($$->text) {
  997. text_item *p;
  998. for (p = $$->text; p->next; p = p->next)
  999. ;
  1000. p->adj.v = ABOVE_ADJUST;
  1001. }
  1002. }
  1003. | object_spec BELOW
  1004. {
  1005. $$ = $1;
  1006. if ($$->text) {
  1007. text_item *p;
  1008. for (p = $$->text; p->next; p = p->next)
  1009. ;
  1010. p->adj.v = BELOW_ADJUST;
  1011. }
  1012. }
  1013. | object_spec THICKNESS expr
  1014. {
  1015. $$ = $1;
  1016. $$->flags |= HAS_THICKNESS;
  1017. $$->thickness = $3;
  1018. }
  1019. | object_spec ALIGNED
  1020. {
  1021. $$ = $1;
  1022. $$->flags |= IS_ALIGNED;
  1023. }
  1024. ;
  1025. text:
  1026. TEXT
  1027. { $$ = $1; }
  1028. | SPRINTF '(' TEXT sprintf_args ')'
  1029. {
  1030. $$.filename = $3.filename;
  1031. $$.lineno = $3.lineno;
  1032. $$.str = do_sprintf($3.str, $4.v, $4.nv);
  1033. a_delete $4.v;
  1034. a_delete $3.str;
  1035. }
  1036. ;
  1037. sprintf_args:
  1038. /* empty */
  1039. {
  1040. $$.v = 0;
  1041. $$.nv = 0;
  1042. $$.maxv = 0;
  1043. }
  1044. | sprintf_args ',' expr
  1045. {
  1046. $$ = $1;
  1047. if ($$.nv >= $$.maxv) {
  1048. if ($$.nv == 0) {
  1049. $$.v = new double[4];
  1050. $$.maxv = 4;
  1051. }
  1052. else {
  1053. double *oldv = $$.v;
  1054. $$.maxv *= 2;
  1055. #if 0
  1056. $$.v = new double[$$.maxv];
  1057. memcpy($$.v, oldv, $$.nv*sizeof(double));
  1058. #else
  1059. // workaround for bug in Compaq C++ V6.5-033
  1060. // for Compaq Tru64 UNIX V5.1A (Rev. 1885)
  1061. double *foo = new double[$$.maxv];
  1062. memcpy(foo, oldv, $$.nv*sizeof(double));
  1063. $$.v = foo;
  1064. #endif
  1065. a_delete oldv;
  1066. }
  1067. }
  1068. $$.v[$$.nv] = $3;
  1069. $$.nv += 1;
  1070. }
  1071. ;
  1072. position:
  1073. position_not_place
  1074. { $$ = $1; }
  1075. | place
  1076. {
  1077. position pos = $1;
  1078. $$.x = pos.x;
  1079. $$.y = pos.y;
  1080. }
  1081. | '(' place ')'
  1082. {
  1083. position pos = $2;
  1084. $$.x = pos.x;
  1085. $$.y = pos.y;
  1086. }
  1087. ;
  1088. position_not_place:
  1089. expr_pair
  1090. { $$ = $1; }
  1091. | position '+' expr_pair
  1092. {
  1093. $$.x = $1.x + $3.x;
  1094. $$.y = $1.y + $3.y;
  1095. }
  1096. | '(' position '+' expr_pair ')'
  1097. {
  1098. $$.x = $2.x + $4.x;
  1099. $$.y = $2.y + $4.y;
  1100. }
  1101. | position '-' expr_pair
  1102. {
  1103. $$.x = $1.x - $3.x;
  1104. $$.y = $1.y - $3.y;
  1105. }
  1106. | '(' position '-' expr_pair ')'
  1107. {
  1108. $$.x = $2.x - $4.x;
  1109. $$.y = $2.y - $4.y;
  1110. }
  1111. | '(' position ',' position ')'
  1112. {
  1113. $$.x = $2.x;
  1114. $$.y = $4.y;
  1115. }
  1116. | expr between position AND position
  1117. {
  1118. $$.x = (1.0 - $1)*$3.x + $1*$5.x;
  1119. $$.y = (1.0 - $1)*$3.y + $1*$5.y;
  1120. }
  1121. | '(' expr between position AND position ')'
  1122. {
  1123. $$.x = (1.0 - $2)*$4.x + $2*$6.x;
  1124. $$.y = (1.0 - $2)*$4.y + $2*$6.y;
  1125. }
  1126. | expr '<' position ',' position '>'
  1127. {
  1128. $$.x = (1.0 - $1)*$3.x + $1*$5.x;
  1129. $$.y = (1.0 - $1)*$3.y + $1*$5.y;
  1130. }
  1131. | '(' expr '<' position ',' position '>' ')'
  1132. {
  1133. $$.x = (1.0 - $2)*$4.x + $2*$6.x;
  1134. $$.y = (1.0 - $2)*$4.y + $2*$6.y;
  1135. }
  1136. ;
  1137. between:
  1138. BETWEEN
  1139. | OF THE WAY BETWEEN
  1140. ;
  1141. expr_pair:
  1142. expr ',' expr
  1143. {
  1144. $$.x = $1;
  1145. $$.y = $3;
  1146. }
  1147. | '(' expr_pair ')'
  1148. { $$ = $2; }
  1149. ;
  1150. place:
  1151. /* line at A left == line (at A) left */
  1152. label %prec CHOP
  1153. { $$ = $1; }
  1154. | label corner
  1155. {
  1156. path pth($2);
  1157. if (!pth.follow($1, & $$))
  1158. YYABORT;
  1159. }
  1160. | corner label
  1161. {
  1162. path pth($1);
  1163. if (!pth.follow($2, & $$))
  1164. YYABORT;
  1165. }
  1166. | corner OF label
  1167. {
  1168. path pth($1);
  1169. if (!pth.follow($3, & $$))
  1170. YYABORT;
  1171. }
  1172. | HERE
  1173. {
  1174. $$.x = current_position.x;
  1175. $$.y = current_position.y;
  1176. $$.obj = 0;
  1177. }
  1178. ;
  1179. label:
  1180. LABEL
  1181. {
  1182. place *p = lookup_label($1);
  1183. if (!p) {
  1184. lex_error("there is no place `%1'", $1);
  1185. YYABORT;
  1186. }
  1187. $$ = *p;
  1188. a_delete $1;
  1189. }
  1190. | nth_primitive
  1191. { $$.obj = $1; }
  1192. | label '.' LABEL
  1193. {
  1194. path pth($3);
  1195. if (!pth.follow($1, & $$))
  1196. YYABORT;
  1197. }
  1198. ;
  1199. ordinal:
  1200. ORDINAL
  1201. { $$ = $1; }
  1202. | '`' any_expr TH
  1203. {
  1204. // XXX Check for overflow (and non-integers?).
  1205. $$ = (int)$2;
  1206. }
  1207. ;
  1208. optional_ordinal_last:
  1209. LAST
  1210. { $$ = 1; }
  1211. | ordinal LAST
  1212. { $$ = $1; }
  1213. ;
  1214. nth_primitive:
  1215. ordinal object_type
  1216. {
  1217. int count = 0;
  1218. object *p;
  1219. for (p = olist.head; p != 0; p = p->next)
  1220. if (p->type() == $2 && ++count == $1) {
  1221. $$ = p;
  1222. break;
  1223. }
  1224. if (p == 0) {
  1225. lex_error("there is no %1%2 %3", $1, ordinal_postfix($1),
  1226. object_type_name($2));
  1227. YYABORT;
  1228. }
  1229. }
  1230. | optional_ordinal_last object_type
  1231. {
  1232. int count = 0;
  1233. object *p;
  1234. for (p = olist.tail; p != 0; p = p->prev)
  1235. if (p->type() == $2 && ++count == $1) {
  1236. $$ = p;
  1237. break;
  1238. }
  1239. if (p == 0) {
  1240. lex_error("there is no %1%2 last %3", $1,
  1241. ordinal_postfix($1), object_type_name($2));
  1242. YYABORT;
  1243. }
  1244. }
  1245. ;
  1246. object_type:
  1247. BOX
  1248. { $$ = BOX_OBJECT; }
  1249. | CIRCLE
  1250. { $$ = CIRCLE_OBJECT; }
  1251. | ELLIPSE
  1252. { $$ = ELLIPSE_OBJECT; }
  1253. | ARC
  1254. { $$ = ARC_OBJECT; }
  1255. | LINE
  1256. { $$ = LINE_OBJECT; }
  1257. | ARROW
  1258. { $$ = ARROW_OBJECT; }
  1259. | SPLINE
  1260. { $$ = SPLINE_OBJECT; }
  1261. | '[' ']'
  1262. { $$ = BLOCK_OBJECT; }
  1263. | TEXT
  1264. { $$ = TEXT_OBJECT; }
  1265. ;
  1266. label_path:
  1267. '.' LABEL
  1268. { $$ = new path($2); }
  1269. | label_path '.' LABEL
  1270. {
  1271. $$ = $1;
  1272. $$->append($3);
  1273. }
  1274. ;
  1275. relative_path:
  1276. corner %prec CHOP
  1277. { $$ = new path($1); }
  1278. /* give this a lower precedence than LEFT and RIGHT so that
  1279. [A: box] with .A left == [A: box] with (.A left) */
  1280. | label_path %prec TEXT
  1281. { $$ = $1; }
  1282. | label_path corner
  1283. {
  1284. $$ = $1;
  1285. $$->append($2);
  1286. }
  1287. ;
  1288. path:
  1289. relative_path
  1290. { $$ = $1; }
  1291. | '(' relative_path ',' relative_path ')'
  1292. {
  1293. $$ = $2;
  1294. $$->set_ypath($4);
  1295. }
  1296. /* The rest of these rules are a compatibility sop. */
  1297. | ORDINAL LAST object_type relative_path
  1298. {
  1299. lex_warning("`%1%2 last %3' in `with' argument ignored",
  1300. $1, ordinal_postfix($1), object_type_name($3));
  1301. $$ = $4;
  1302. }
  1303. | LAST object_type relative_path
  1304. {
  1305. lex_warning("`last %1' in `with' argument ignored",
  1306. object_type_name($2));
  1307. $$ = $3;
  1308. }
  1309. | ORDINAL object_type relative_path
  1310. {
  1311. lex_warning("`%1%2 %3' in `with' argument ignored",
  1312. $1, ordinal_postfix($1), object_type_name($2));
  1313. $$ = $3;
  1314. }
  1315. | LABEL relative_path
  1316. {
  1317. lex_warning("initial `%1' in `with' argument ignored", $1);
  1318. a_delete $1;
  1319. $$ = $2;
  1320. }
  1321. ;
  1322. corner:
  1323. DOT_N
  1324. { $$ = &object::north; }
  1325. | DOT_E
  1326. { $$ = &object::east; }
  1327. | DOT_W
  1328. { $$ = &object::west; }
  1329. | DOT_S
  1330. { $$ = &object::south; }
  1331. | DOT_NE
  1332. { $$ = &object::north_east; }
  1333. | DOT_SE
  1334. { $$ = &object:: south_east; }
  1335. | DOT_NW
  1336. { $$ = &object::north_west; }
  1337. | DOT_SW
  1338. { $$ = &object::south_west; }
  1339. | DOT_C
  1340. { $$ = &object::center; }
  1341. | DOT_START
  1342. { $$ = &object::start; }
  1343. | DOT_END
  1344. { $$ = &object::end; }
  1345. | TOP
  1346. { $$ = &object::north; }
  1347. | BOTTOM
  1348. { $$ = &object::south; }
  1349. | LEFT
  1350. { $$ = &object::west; }
  1351. | RIGHT
  1352. { $$ = &object::east; }
  1353. | UPPER LEFT
  1354. { $$ = &object::north_west; }
  1355. | LOWER LEFT
  1356. { $$ = &object::south_west; }
  1357. | UPPER RIGHT
  1358. { $$ = &object::north_east; }
  1359. | LOWER RIGHT
  1360. { $$ = &object::south_east; }
  1361. | LEFT_CORNER
  1362. { $$ = &object::west; }
  1363. | RIGHT_CORNER
  1364. { $$ = &object::east; }
  1365. | UPPER LEFT_CORNER
  1366. { $$ = &object::north_west; }
  1367. | LOWER LEFT_CORNER
  1368. { $$ = &object::south_west; }
  1369. | UPPER RIGHT_CORNER
  1370. { $$ = &object::north_east; }
  1371. | LOWER RIGHT_CORNER
  1372. { $$ = &object::south_east; }
  1373. | NORTH
  1374. { $$ = &object::north; }
  1375. | SOUTH
  1376. { $$ = &object::south; }
  1377. | EAST
  1378. { $$ = &object::east; }
  1379. | WEST
  1380. { $$ = &object::west; }
  1381. | CENTER
  1382. { $$ = &object::center; }
  1383. | START
  1384. { $$ = &object::start; }
  1385. | END
  1386. { $$ = &object::end; }
  1387. ;
  1388. expr:
  1389. VARIABLE
  1390. {
  1391. if (!lookup_variable($1, & $$)) {
  1392. lex_error("there is no variable `%1'", $1);
  1393. YYABORT;
  1394. }
  1395. a_delete $1;
  1396. }
  1397. | NUMBER
  1398. { $$ = $1; }
  1399. | place DOT_X
  1400. {
  1401. if ($1.obj != 0)
  1402. $$ = $1.obj->origin().x;
  1403. else
  1404. $$ = $1.x;
  1405. }
  1406. | place DOT_Y
  1407. {
  1408. if ($1.obj != 0)
  1409. $$ = $1.obj->origin().y;
  1410. else
  1411. $$ = $1.y;
  1412. }
  1413. | place DOT_HT
  1414. {
  1415. if ($1.obj != 0)
  1416. $$ = $1.obj->height();
  1417. else
  1418. $$ = 0.0;
  1419. }
  1420. | place DOT_WID
  1421. {
  1422. if ($1.obj != 0)
  1423. $$ = $1.obj->width();
  1424. else
  1425. $$ = 0.0;
  1426. }
  1427. | place DOT_RAD
  1428. {
  1429. if ($1.obj != 0)
  1430. $$ = $1.obj->radius();
  1431. else
  1432. $$ = 0.0;
  1433. }
  1434. | expr '+' expr
  1435. { $$ = $1 + $3; }
  1436. | expr '-' expr
  1437. { $$ = $1 - $3; }
  1438. | expr '*' expr
  1439. { $$ = $1 * $3; }
  1440. | expr '/' expr
  1441. {
  1442. if ($3 == 0.0) {
  1443. lex_error("division by zero");
  1444. YYABORT;
  1445. }
  1446. $$ = $1/$3;
  1447. }
  1448. | expr '%' expr
  1449. {
  1450. if ($3 == 0.0) {
  1451. lex_error("modulus by zero");
  1452. YYABORT;
  1453. }
  1454. $$ = fmod($1, $3);
  1455. }
  1456. | expr '^' expr
  1457. {
  1458. errno = 0;
  1459. $$ = pow($1, $3);
  1460. if (errno == EDOM) {
  1461. lex_error("arguments to `^' operator out of domain");
  1462. YYABORT;
  1463. }
  1464. if (errno == ERANGE) {
  1465. lex_error("result of `^' operator out of range");
  1466. YYABORT;
  1467. }
  1468. }
  1469. | '-' expr %prec '!'
  1470. { $$ = -$2; }
  1471. | '(' any_expr ')'
  1472. { $$ = $2; }
  1473. | SIN '(' any_expr ')'
  1474. {
  1475. errno = 0;
  1476. $$ = sin($3);
  1477. if (errno == ERANGE) {
  1478. lex_error("sin result out of range");
  1479. YYABORT;
  1480. }
  1481. }
  1482. | COS '(' any_expr ')'
  1483. {
  1484. errno = 0;
  1485. $$ = cos($3);
  1486. if (errno == ERANGE) {
  1487. lex_error("cos result out of range");
  1488. YYABORT;
  1489. }
  1490. }
  1491. | ATAN2 '(' any_expr ',' any_expr ')'
  1492. {
  1493. errno = 0;
  1494. $$ = atan2($3, $5);
  1495. if (errno == EDOM) {
  1496. lex_error("atan2 argument out of domain");
  1497. YYABORT;
  1498. }
  1499. if (errno == ERANGE) {
  1500. lex_error("atan2 result out of range");
  1501. YYABORT;
  1502. }
  1503. }
  1504. | LOG '(' any_expr ')'
  1505. {
  1506. errno = 0;
  1507. $$ = log10($3);
  1508. if (errno == ERANGE) {
  1509. lex_error("log result out of range");
  1510. YYABORT;
  1511. }
  1512. }
  1513. | EXP '(' any_expr ')'
  1514. {
  1515. errno = 0;
  1516. $$ = pow(10.0, $3);
  1517. if (errno == ERANGE) {
  1518. lex_error("exp result out of range");
  1519. YYABORT;
  1520. }
  1521. }
  1522. | SQRT '(' any_expr ')'
  1523. {
  1524. errno = 0;
  1525. $$ = sqrt($3);
  1526. if (errno == EDOM) {
  1527. lex_error("sqrt argument out of domain");
  1528. YYABORT;
  1529. }
  1530. }
  1531. | K_MAX '(' any_expr ',' any_expr ')'
  1532. { $$ = $3 > $5 ? $3 : $5; }
  1533. | K_MIN '(' any_expr ',' any_expr ')'
  1534. { $$ = $3 < $5 ? $3 : $5; }
  1535. | INT '(' any_expr ')'
  1536. { $$ = floor($3); }
  1537. | RAND '(' any_expr ')'
  1538. { $$ = 1.0 + floor(((rand()&0x7fff)/double(0x7fff))*$3); }
  1539. | RAND '(' ')'
  1540. {
  1541. /* return a random number in the range [0,1) */
  1542. /* portable, but not very random */
  1543. $$ = (rand() & 0x7fff) / double(0x8000);
  1544. }
  1545. | SRAND '(' any_expr ')'
  1546. {
  1547. $$ = 0;
  1548. srand((unsigned int)$3);
  1549. }
  1550. | expr '<' expr
  1551. { $$ = ($1 < $3); }
  1552. | expr LESSEQUAL expr
  1553. { $$ = ($1 <= $3); }
  1554. | expr '>' expr
  1555. { $$ = ($1 > $3); }
  1556. | expr GREATEREQUAL expr
  1557. { $$ = ($1 >= $3); }
  1558. | expr EQUALEQUAL expr
  1559. { $$ = ($1 == $3); }
  1560. | expr NOTEQUAL expr
  1561. { $$ = ($1 != $3); }
  1562. | expr ANDAND expr
  1563. { $$ = ($1 != 0.0 && $3 != 0.0); }
  1564. | expr OROR expr
  1565. { $$ = ($1 != 0.0 || $3 != 0.0); }
  1566. | '!' expr
  1567. { $$ = ($2 == 0.0); }
  1568. ;
  1569. %%
  1570. /* bison defines const to be empty unless __STDC__ is defined, which it
  1571. isn't under cfront */
  1572. #ifdef const
  1573. #undef const
  1574. #endif
  1575. static struct {
  1576. const char *name;
  1577. double val;
  1578. int scaled; // non-zero if val should be multiplied by scale
  1579. } defaults_table[] = {
  1580. { "arcrad", .25, 1 },
  1581. { "arrowht", .1, 1 },
  1582. { "arrowwid", .05, 1 },
  1583. { "circlerad", .25, 1 },
  1584. { "boxht", .5, 1 },
  1585. { "boxwid", .75, 1 },
  1586. { "boxrad", 0.0, 1 },
  1587. { "dashwid", .05, 1 },
  1588. { "ellipseht", .5, 1 },
  1589. { "ellipsewid", .75, 1 },
  1590. { "moveht", .5, 1 },
  1591. { "movewid", .5, 1 },
  1592. { "lineht", .5, 1 },
  1593. { "linewid", .5, 1 },
  1594. { "textht", 0.0, 1 },
  1595. { "textwid", 0.0, 1 },
  1596. { "scale", 1.0, 0 },
  1597. { "linethick", -1.0, 0 }, // in points
  1598. { "fillval", .5, 0 },
  1599. { "arrowhead", 1.0, 0 },
  1600. { "maxpswid", 8.5, 0 },
  1601. { "maxpsht", 11.0, 0 },
  1602. };
  1603. place *lookup_label(const char *label)
  1604. {
  1605. saved_state *state = current_saved_state;
  1606. PTABLE(place) *tbl = current_table;
  1607. for (;;) {
  1608. place *pl = tbl->lookup(label);
  1609. if (pl)
  1610. return pl;
  1611. if (!state)
  1612. return 0;
  1613. tbl = state->tbl;
  1614. state = state->prev;
  1615. }
  1616. }
  1617. void define_label(const char *label, const place *pl)
  1618. {
  1619. place *p = new place[1];
  1620. *p = *pl;
  1621. current_table->define(label, p);
  1622. }
  1623. int lookup_variable(const char *name, double *val)
  1624. {
  1625. place *pl = lookup_label(name);
  1626. if (pl) {
  1627. *val = pl->x;
  1628. return 1;
  1629. }
  1630. return 0;
  1631. }
  1632. void define_variable(const char *name, double val)
  1633. {
  1634. place *p = new place[1];
  1635. p->obj = 0;
  1636. p->x = val;
  1637. p->y = 0.0;
  1638. current_table->define(name, p);
  1639. if (strcmp(name, "scale") == 0) {
  1640. // When the scale changes, reset all scaled pre-defined variables to
  1641. // their default values.
  1642. for (unsigned int i = 0;
  1643. i < sizeof(defaults_table)/sizeof(defaults_table[0]); i++)
  1644. if (defaults_table[i].scaled)
  1645. define_variable(defaults_table[i].name, val*defaults_table[i].val);
  1646. }
  1647. }
  1648. // called once only (not once per parse)
  1649. void parse_init()
  1650. {
  1651. current_direction = RIGHT_DIRECTION;
  1652. current_position.x = 0.0;
  1653. current_position.y = 0.0;
  1654. // This resets everything to its default value.
  1655. reset_all();
  1656. }
  1657. void reset(const char *nm)
  1658. {
  1659. for (unsigned int i = 0;
  1660. i < sizeof(defaults_table)/sizeof(defaults_table[0]); i++)
  1661. if (strcmp(nm, defaults_table[i].name) == 0) {
  1662. double val = defaults_table[i].val;
  1663. if (defaults_table[i].scaled) {
  1664. double scale;
  1665. lookup_variable("scale", &scale);
  1666. val *= scale;
  1667. }
  1668. define_variable(defaults_table[i].name, val);
  1669. return;
  1670. }
  1671. lex_error("`%1' is not a predefined variable", nm);
  1672. }
  1673. void reset_all()
  1674. {
  1675. // We only have to explicitly reset the pre-defined variables that
  1676. // aren't scaled because `scale' is not scaled, and changing the
  1677. // value of `scale' will reset all the pre-defined variables that
  1678. // are scaled.
  1679. for (unsigned int i = 0;
  1680. i < sizeof(defaults_table)/sizeof(defaults_table[0]); i++)
  1681. if (!defaults_table[i].scaled)
  1682. define_variable(defaults_table[i].name, defaults_table[i].val);
  1683. }
  1684. // called after each parse
  1685. void parse_cleanup()
  1686. {
  1687. while (current_saved_state != 0) {
  1688. delete current_table;
  1689. current_table = current_saved_state->tbl;
  1690. saved_state *tem = current_saved_state;
  1691. current_saved_state = current_saved_state->prev;
  1692. delete tem;
  1693. }
  1694. assert(current_table == &top_table);
  1695. PTABLE_ITERATOR(place) iter(current_table);
  1696. const char *key;
  1697. place *pl;
  1698. while (iter.next(&key, &pl))
  1699. if (pl->obj != 0) {
  1700. position pos = pl->obj->origin();
  1701. pl->obj = 0;
  1702. pl->x = pos.x;
  1703. pl->y = pos.y;
  1704. }
  1705. while (olist.head != 0) {
  1706. object *tem = olist.head;
  1707. olist.head = olist.head->next;
  1708. delete tem;
  1709. }
  1710. olist.tail = 0;
  1711. current_direction = RIGHT_DIRECTION;
  1712. current_position.x = 0.0;
  1713. current_position.y = 0.0;
  1714. }
  1715. const char *ordinal_postfix(int n)
  1716. {
  1717. if (n < 10 || n > 20)
  1718. switch (n % 10) {
  1719. case 1:
  1720. return "st";
  1721. case 2:
  1722. return "nd";
  1723. case 3:
  1724. return "rd";
  1725. }
  1726. return "th";
  1727. }
  1728. const char *object_type_name(object_type type)
  1729. {
  1730. switch (type) {
  1731. case BOX_OBJECT:
  1732. return "box";
  1733. case CIRCLE_OBJECT:
  1734. return "circle";
  1735. case ELLIPSE_OBJECT:
  1736. return "ellipse";
  1737. case ARC_OBJECT:
  1738. return "arc";
  1739. case SPLINE_OBJECT:
  1740. return "spline";
  1741. case LINE_OBJECT:
  1742. return "line";
  1743. case ARROW_OBJECT:
  1744. return "arrow";
  1745. case MOVE_OBJECT:
  1746. return "move";
  1747. case TEXT_OBJECT:
  1748. return "\"\"";
  1749. case BLOCK_OBJECT:
  1750. return "[]";
  1751. case OTHER_OBJECT:
  1752. case MARK_OBJECT:
  1753. default:
  1754. break;
  1755. }
  1756. return "object";
  1757. }
  1758. static char sprintf_buf[1024];
  1759. char *format_number(const char *form, double n)
  1760. {
  1761. if (form == 0)
  1762. form = "%g";
  1763. return do_sprintf(form, &n, 1);
  1764. }
  1765. char *do_sprintf(const char *form, const double *v, int nv)
  1766. {
  1767. string result;
  1768. int i = 0;
  1769. string one_format;
  1770. while (*form) {
  1771. if (*form == '%') {
  1772. one_format += *form++;
  1773. for (; *form != '\0' && strchr("#-+ 0123456789.", *form) != 0; form++)
  1774. one_format += *form;
  1775. if (*form == '\0' || strchr("eEfgG%", *form) == 0) {
  1776. lex_error("bad sprintf format");
  1777. result += one_format;
  1778. result += form;
  1779. break;
  1780. }
  1781. if (*form == '%') {
  1782. one_format += *form++;
  1783. one_format += '\0';
  1784. snprintf(sprintf_buf, sizeof(sprintf_buf),
  1785. "%s", one_format.contents());
  1786. }
  1787. else {
  1788. if (i >= nv) {
  1789. lex_error("too few arguments to snprintf");
  1790. result += one_format;
  1791. result += form;
  1792. break;
  1793. }
  1794. one_format += *form++;
  1795. one_format += '\0';
  1796. snprintf(sprintf_buf, sizeof(sprintf_buf),
  1797. one_format.contents(), v[i++]);
  1798. }
  1799. one_format.clear();
  1800. result += sprintf_buf;
  1801. }
  1802. else
  1803. result += *form++;
  1804. }
  1805. result += '\0';
  1806. return strsave(result.contents());
  1807. }