gram.yy 35 KB

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