fpd.y 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. %{
  2. /*
  3. * fpd.y - FootPrint Definition language
  4. *
  5. * Written 2009-2012 by Werner Almesberger
  6. * Copyright 2009-2012 by Werner Almesberger
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "util.h"
  16. #include "error.h"
  17. #include "coord.h"
  18. #include "expr.h"
  19. #include "obj.h"
  20. #include "meas.h"
  21. #include "gui_status.h"
  22. #include "gui_inst.h" /* for %meas */
  23. #include "dump.h"
  24. #include "tsort.h"
  25. #include "fpd.h"
  26. #include "y.tab.h"
  27. struct expr *expr_result;
  28. const char *var_id;
  29. struct value *var_value_list;
  30. static struct frame *curr_frame;
  31. static struct table *curr_table;
  32. static struct row *curr_row;
  33. static struct vec *last_vec = NULL;
  34. static struct table **next_table;
  35. static struct loop **next_loop;
  36. static struct vec **next_vec;
  37. static struct obj **next_obj;
  38. static int n_vars, n_values;
  39. static const char *id_sin, *id_cos, *id_sqrt, *id_floor;
  40. static struct tsort *tsort;
  41. /* ----- lookup functions -------------------------------------------------- */
  42. static struct frame *find_frame(const char *name)
  43. {
  44. struct frame *f;
  45. for (f = frames->next; f; f = f->next)
  46. if (f->name == name)
  47. return f;
  48. return NULL;
  49. }
  50. static struct vec *find_vec(const struct frame *frame, const char *name)
  51. {
  52. struct vec *v;
  53. for (v = frame->vecs; v; v = v->next)
  54. if (v->name == name)
  55. return v;
  56. return NULL;
  57. }
  58. static struct obj *find_obj(const struct frame *frame, const char *name)
  59. {
  60. struct obj *obj;
  61. for (obj = frame->objs; obj; obj = obj->next)
  62. if (obj->name == name)
  63. return obj;
  64. return NULL;
  65. }
  66. static int find_label(const struct frame *frame, const char *name)
  67. {
  68. if (find_vec(frame, name))
  69. return 1;
  70. if (find_obj(frame, name))
  71. return 1;
  72. return 0;
  73. }
  74. static struct var *find_var(const struct frame *frame, const char *name)
  75. {
  76. const struct table *table;
  77. struct var *var;
  78. struct loop *loop;
  79. for (table = frame->tables; table; table = table->next)
  80. for (var = table->vars; var; var = var->next)
  81. if (!var->key && var->name == name)
  82. return var;
  83. for (loop = frame->loops; loop; loop = loop->next)
  84. if (loop->var.name == name)
  85. return &loop->var;
  86. return NULL;
  87. }
  88. /* ----- item creation ----------------------------------------------------- */
  89. static void set_frame(struct frame *frame)
  90. {
  91. curr_frame = frame;
  92. next_table = &frame->tables;
  93. next_loop = &frame->loops;
  94. next_vec = &frame->vecs;
  95. next_obj = &frame->objs;
  96. last_vec = NULL;
  97. }
  98. static void make_var(const char *id, int key, struct expr *expr)
  99. {
  100. struct table *table;
  101. table = zalloc_type(struct table);
  102. table->vars = zalloc_type(struct var);
  103. table->vars->name = id;
  104. table->vars->frame = curr_frame;
  105. table->vars->table = table;
  106. table->vars->key = key;
  107. table->rows = zalloc_type(struct row);
  108. table->rows->table = table;
  109. table->rows->values = zalloc_type(struct value);
  110. table->rows->values->expr = expr;
  111. table->rows->values->row = table->rows;
  112. table->active_row = table->rows;
  113. *next_table = table;
  114. next_table = &table->next;
  115. }
  116. static void make_loop(const char *id, struct expr *from, struct expr *to)
  117. {
  118. struct loop *loop;
  119. loop = alloc_type(struct loop);
  120. loop->var.name = id;
  121. loop->var.next = NULL;
  122. loop->var.frame = curr_frame;
  123. loop->var.table = NULL;
  124. loop->from.expr = from;
  125. loop->from.row = NULL;
  126. loop->from.next = NULL;
  127. loop->to.expr = to;
  128. loop->to.row = NULL;
  129. loop->to.next = NULL;
  130. loop->next = NULL;
  131. loop->active = 0;
  132. loop->initialized = 0;
  133. *next_loop = loop;
  134. next_loop = &loop->next;
  135. }
  136. static struct obj *new_obj(enum obj_type type)
  137. {
  138. struct obj *obj;
  139. obj = alloc_type(struct obj);
  140. obj->type = type;
  141. obj->name = NULL;
  142. obj->frame = curr_frame;
  143. obj->next = NULL;
  144. obj->lineno = lineno;
  145. return obj;
  146. }
  147. /* ---- frame qualifiers --------------------------------------------------- */
  148. static int duplicate_qualifier(const struct frame_qual *a,
  149. const struct frame_qual *b)
  150. {
  151. if (!b)
  152. return 0;
  153. if (a != b && a->frame == b->frame) {
  154. yyerrorf("duplicate qualifier \"%s\"", a->frame->name);
  155. return 1;
  156. }
  157. if (b && duplicate_qualifier(a, b->next))
  158. return 1;
  159. return duplicate_qualifier(a->next, a->next);
  160. }
  161. static int can_reach(const struct frame *curr, const struct frame_qual *qual,
  162. const struct frame *end)
  163. {
  164. const struct obj *obj;
  165. if (curr == end)
  166. return !qual;
  167. /*
  168. * Don't recurse for removing the qualifier alone. We require a frame
  169. * reference step as well, so that things like foo.foo.foo.bar.vec
  170. * aren't allowed.
  171. *
  172. * Since a duplicate qualifier can never work, we test for this error
  173. * explicitly in "duplicate_qualifier".
  174. */
  175. if (qual && curr == qual->frame)
  176. qual = qual->next;
  177. for (obj = curr->objs; obj; obj = obj->next)
  178. if (obj->type == ot_frame)
  179. if (can_reach(obj->u.frame.ref, qual, end))
  180. return 1;
  181. return 0;
  182. }
  183. static int check_qbase(struct qbase *qbase)
  184. {
  185. if (duplicate_qualifier(qbase->qualifiers, qbase->qualifiers))
  186. return 0;
  187. if (!can_reach(frames, qbase->qualifiers, qbase->vec->frame))
  188. yywarn("not all qualifiers can be reached");
  189. return 1;
  190. }
  191. /* ----- debugging directives ---------------------------------------------- */
  192. static int dbg_delete(const char *frame_name, const char *name)
  193. {
  194. struct vec *vec;
  195. struct obj *obj;
  196. struct frame *frame;
  197. if (!frame_name) {
  198. frame = curr_frame;
  199. } else {
  200. frame = find_frame(frame_name);
  201. if (!frame) {
  202. yyerrorf("unknown frame \"%s\"", frame_name);
  203. return 0;
  204. }
  205. }
  206. vec = find_vec(frame, name);
  207. if (vec) {
  208. delete_vec(vec);
  209. return 1;
  210. }
  211. obj = find_obj(frame, name);
  212. if (obj) {
  213. delete_obj(obj);
  214. return 1;
  215. }
  216. if (!frame_name) {
  217. frame = find_frame(name);
  218. if (frame) {
  219. if (curr_frame == frame) {
  220. yyerrorf("a frame can't delete itself");
  221. return 0;
  222. }
  223. delete_frame(frame);
  224. return 1;
  225. }
  226. }
  227. if (frame_name)
  228. yyerrorf("unknown item \"%s.%s\"", frame_name, name);
  229. else
  230. yyerrorf("unknown item \"%s\"", name);
  231. return 0;
  232. }
  233. static int dbg_move(const char *name, int anchor, const char *dest)
  234. {
  235. struct vec *to, *vec;
  236. struct obj *obj;
  237. struct vec **anchors[3];
  238. int n_anchors;
  239. to = find_vec(curr_frame, dest);
  240. if (!to) {
  241. yyerrorf("unknown vector \"%s\"", dest);
  242. return 0;
  243. }
  244. vec = find_vec(curr_frame, name);
  245. if (vec) {
  246. if (anchor) {
  247. yyerrorf("invalid anchor (%d > 0)", anchor);
  248. return 0;
  249. }
  250. vec->base = to;
  251. return 1;
  252. }
  253. obj = find_obj(curr_frame, name);
  254. if (!obj) {
  255. yyerrorf("unknown item \"%s\"", name);
  256. return 0;
  257. }
  258. n_anchors = obj_anchors(obj, anchors);
  259. if (anchor >= n_anchors) {
  260. yyerrorf("invalid anchor (%d > %d)", anchor, n_anchors-1);
  261. return 0;
  262. }
  263. *anchors[anchor] = to;
  264. return 1;
  265. }
  266. /*
  267. * @@@ This is very similar to what we do in rule "obj". Consider merging.
  268. */
  269. /*
  270. * We need to pass base_frame and base_vec, not just the vector (with the
  271. * frame implied) since we can also reference the frame's origin, whose
  272. * "vector" is NULL.
  273. */
  274. static int dbg_link_frame(const char *frame_name,
  275. struct frame *base_frame, struct vec *base_vec)
  276. {
  277. struct frame *frame;
  278. struct obj *obj;
  279. assert(!base_vec || base_vec->frame == base_frame);
  280. frame = find_frame(frame_name);
  281. if (!frame) {
  282. yyerrorf("unknown frame \"%s\"", frame_name);
  283. return 0;
  284. }
  285. /* this can only fail in %frame */
  286. if (is_parent_of(frame, base_frame)) {
  287. yyerrorf("frame \"%s\" is a parent of \"%s\"",
  288. frame->name, base_frame->name);
  289. return 0;
  290. }
  291. obj = new_obj(ot_frame);
  292. obj->base = base_vec;
  293. obj->frame = base_frame;
  294. obj->u.frame.ref = frame;
  295. connect_obj(base_frame, obj);
  296. if (!frame->active_ref)
  297. frame->active_ref = obj;
  298. return 1;
  299. }
  300. int dbg_print(const struct expr *expr, const struct frame *frame)
  301. {
  302. const char *s;
  303. struct num num;
  304. s = eval_str(expr, frame);
  305. if (s) {
  306. printf("%s\n", s);
  307. return 1;
  308. }
  309. num = eval_num(expr, frame);
  310. if (is_undef(num))
  311. return 0;
  312. printf("%lg%s\n", num.n, str_unit(num));
  313. return 1;
  314. }
  315. static int dbg_meas(const char *name)
  316. {
  317. const struct obj *obj;
  318. const struct inst *inst;
  319. struct coord a1, b1;
  320. char *s;
  321. obj = find_obj(frames, name);
  322. if (!obj) {
  323. yyerrorf("unknown object \"%s\"", name);
  324. return 0;
  325. }
  326. /* from fped.c:main */
  327. if (!pkg_name)
  328. pkg_name = stralloc("_");
  329. reporter = report_to_stderr;
  330. if (!instantiate())
  331. return 0;
  332. inst = find_meas_hint(obj);
  333. if (!inst) {
  334. yyerrorf("measurement \"%s\" was not instantiated", name);
  335. return 0;
  336. }
  337. project_meas(inst, &a1, &b1);
  338. s = format_len(obj->u.meas.label ? obj->u.meas.label : "",
  339. dist_point(a1, b1), curr_unit);
  340. printf("%s\n", s);
  341. free(s);
  342. return 1;
  343. }
  344. %}
  345. %union {
  346. struct num num;
  347. int flag;
  348. char *str;
  349. const char *id;
  350. struct expr *expr;
  351. struct frame *frame;
  352. struct table *table;
  353. struct var *var;
  354. struct row *row;
  355. struct value *value;
  356. struct vec *vec;
  357. struct obj *obj;
  358. enum pad_type pt;
  359. enum meas_type mt;
  360. struct {
  361. int inverted;
  362. int max;
  363. } mo;
  364. struct {
  365. struct frame *frame;
  366. struct vec *vec;
  367. } qvec;
  368. struct qbase {
  369. struct vec *vec;
  370. struct frame_qual *qualifiers;
  371. } qbase;
  372. };
  373. %token START_FPD START_EXPR START_VAR START_VALUES
  374. %token TOK_SET TOK_LOOP TOK_PACKAGE TOK_FRAME TOK_TABLE TOK_VEC
  375. %token TOK_PAD TOK_RPAD TOK_HOLE TOK_RECT TOK_LINE TOK_CIRC TOK_ARC
  376. %token TOK_MEAS TOK_MEASX TOK_MEASY TOK_UNIT
  377. %token TOK_NEXT TOK_NEXT_INVERTED TOK_MAX TOK_MAX_INVERTED
  378. %token TOK_DBG_DEL TOK_DBG_MOVE TOK_DBG_FRAME
  379. %token TOK_DBG_PRINT TOK_DBG_IPRINT
  380. %token TOK_DBG_DUMP TOK_DBG_EXIT TOK_DBG_TSORT TOK_DBG_MEAS
  381. %token TOK_ALLOW_HOLES TOK_ALLOW_OVERLAP TOK_ALLOW_TOUCH
  382. %token <num> NUMBER
  383. %token <str> STRING
  384. %token <id> ID LABEL
  385. %type <table> table
  386. %type <var> vars var
  387. %type <row> rows
  388. %type <value> row value opt_value_list
  389. %type <vec> vec base
  390. %type <obj> object obj meas unlabeled_meas
  391. %type <expr> expr opt_expr add_expr mult_expr unary_expr primary_expr
  392. %type <num> opt_num
  393. %type <flag> opt_key
  394. %type <frame> frame_qualifier
  395. %type <str> opt_string
  396. %type <pt> pad_type
  397. %type <mt> meas_type
  398. %type <mo> meas_op
  399. %type <qvec> qualified_base
  400. %type <qbase> qbase qbase_unchecked
  401. %%
  402. all:
  403. START_FPD
  404. {
  405. frames = zalloc_type(struct frame);
  406. set_frame(frames);
  407. id_sin = unique("sin");
  408. id_cos = unique("cos");
  409. id_sqrt = unique("sqrt");
  410. id_floor = unique("floor");
  411. }
  412. fpd
  413. | START_EXPR expr
  414. {
  415. expr_result = $2;
  416. }
  417. | START_VAR ID opt_value_list
  418. {
  419. var_id = $2;
  420. var_value_list = $3;
  421. }
  422. | START_VALUES row
  423. {
  424. var_value_list = $2;
  425. }
  426. ;
  427. fpd:
  428. frame_defs part_name opt_setup opt_frame_items opt_measurements
  429. | frame_defs setup opt_frame_items opt_measurements
  430. | frame_defs frame_items opt_measurements
  431. | frame_defs opt_measurements
  432. ;
  433. part_name:
  434. TOK_PACKAGE STRING
  435. {
  436. const char *p;
  437. if (!*$2) {
  438. yyerrorf("invalid package name");
  439. YYABORT;
  440. }
  441. for (p = $2; *p; *p++)
  442. if (*p < 32 || *p > 126) {
  443. yyerrorf("invalid package name");
  444. YYABORT;
  445. }
  446. pkg_name = $2;
  447. }
  448. ;
  449. opt_setup:
  450. | setup
  451. ;
  452. setup:
  453. unit
  454. | allow_pads
  455. | allow_holes
  456. | unit allow_pads
  457. | unit allow_pads allow_holes
  458. | unit allow_holes
  459. | unit allow_holes allow_pads
  460. | allow_pads unit
  461. | allow_pads unit allow_holes
  462. | allow_holes unit
  463. | allow_holes unit allow_pads
  464. ;
  465. unit:
  466. TOK_UNIT ID
  467. {
  468. if (!strcmp($2, "mm"))
  469. curr_unit = curr_unit_mm;
  470. else if (!strcmp($2, "mil"))
  471. curr_unit = curr_unit_mil;
  472. else if (!strcmp($2, "auto"))
  473. curr_unit = curr_unit_auto;
  474. else {
  475. yyerrorf("unrecognized unit \"%s\"", $2);
  476. YYABORT;
  477. }
  478. }
  479. ;
  480. allow_pads:
  481. TOK_ALLOW_TOUCH
  482. {
  483. allow_overlap = ao_touch;
  484. }
  485. | TOK_ALLOW_OVERLAP
  486. {
  487. allow_overlap = ao_any;
  488. }
  489. ;
  490. allow_holes:
  491. TOK_ALLOW_HOLES
  492. {
  493. holes_linked = 0;
  494. }
  495. ;
  496. frame_defs:
  497. | frame_defs frame_def
  498. ;
  499. frame_def:
  500. TOK_FRAME ID '{'
  501. {
  502. if (find_frame($2)) {
  503. yyerrorf("duplicate frame \"%s\"", $2);
  504. YYABORT;
  505. }
  506. curr_frame = zalloc_type(struct frame);
  507. curr_frame->name = $2;
  508. set_frame(curr_frame);
  509. curr_frame->next = frames->next;
  510. frames->next = curr_frame;
  511. }
  512. opt_frame_items '}'
  513. {
  514. set_frame(frames);
  515. }
  516. ;
  517. opt_frame_items:
  518. | frame_items
  519. ;
  520. frame_items:
  521. frame_item
  522. | frame_item frame_items
  523. ;
  524. frame_item:
  525. table
  526. | TOK_SET opt_key ID '=' expr
  527. {
  528. if (!$2 && find_var(curr_frame, $3)) {
  529. yyerrorf("duplicate variable \"%s\"", $3);
  530. YYABORT;
  531. }
  532. make_var($3, $2, $5);
  533. }
  534. | TOK_LOOP ID '=' expr ',' expr
  535. {
  536. if (find_var(curr_frame, $2)) {
  537. yyerrorf("duplicate variable \"%s\"", $2);
  538. YYABORT;
  539. }
  540. make_loop($2, $4, $6);
  541. }
  542. | vec
  543. | LABEL vec
  544. {
  545. if (find_label(curr_frame, $1)) {
  546. yyerrorf("duplicate label \"%s\"", $1);
  547. YYABORT;
  548. }
  549. $2->name = $1;
  550. }
  551. | object
  552. | LABEL object
  553. {
  554. if (find_label(curr_frame, $1)) {
  555. yyerrorf("duplicate label \"%s\"", $1);
  556. YYABORT;
  557. }
  558. $2->name = $1;
  559. }
  560. | debug_item
  561. ;
  562. debug_item:
  563. TOK_DBG_DEL ID
  564. {
  565. if (!dbg_delete(NULL, $2))
  566. YYABORT;
  567. }
  568. | TOK_DBG_DEL ID '.' ID
  569. {
  570. if (!dbg_delete($2, $4))
  571. YYABORT;
  572. }
  573. | TOK_DBG_MOVE ID opt_num ID
  574. {
  575. if (!dbg_move($2, $3.n, $4))
  576. YYABORT;
  577. }
  578. | TOK_DBG_FRAME ID qualified_base
  579. {
  580. if (!dbg_link_frame($2, $3.frame, $3.vec))
  581. YYABORT;
  582. }
  583. | TOK_DBG_PRINT expr
  584. {
  585. if (!dbg_print($2, curr_frame))
  586. YYABORT;
  587. }
  588. | TOK_DBG_MEAS ID
  589. {
  590. if (!dbg_meas($2))
  591. YYABORT;
  592. }
  593. | TOK_DBG_DUMP
  594. {
  595. if (!dump(stdout, NULL)) {
  596. perror("stdout");
  597. exit(1);
  598. }
  599. }
  600. | TOK_DBG_EXIT
  601. {
  602. exit(0);
  603. }
  604. | TOK_DBG_TSORT '{'
  605. {
  606. tsort = begin_tsort();
  607. }
  608. sort_items '}'
  609. {
  610. void **sort, **walk;
  611. sort = end_tsort(tsort);
  612. for (walk = sort; *walk; walk++)
  613. printf("%s\n", (char *) *walk);
  614. free(sort);
  615. }
  616. ;
  617. sort_items:
  618. | sort_items '+' ID
  619. {
  620. add_node(tsort, (void *) $3, 0);
  621. }
  622. | sort_items '-' ID
  623. {
  624. add_node(tsort, (void *) $3, 1);
  625. }
  626. | sort_items ID ID opt_num
  627. {
  628. struct node *a, *b;
  629. /* order is important here ! */
  630. a = add_node(tsort, (void *) $2, 0);
  631. b = add_node(tsort, (void *) $3, 0);
  632. add_edge(a, b, $4.n);
  633. }
  634. ;
  635. table:
  636. TOK_TABLE
  637. {
  638. $<table>$ = zalloc_type(struct table);
  639. *next_table = $<table>$;
  640. curr_table = $<table>$;
  641. n_vars = 0;
  642. }
  643. '{' vars '}' rows
  644. {
  645. $$ = $<table>2;
  646. $$->vars = $4;
  647. $$->rows = $6;
  648. $$->active_row = $6;
  649. next_table = &$$->next;
  650. }
  651. ;
  652. vars:
  653. var
  654. {
  655. $$ = $1;
  656. }
  657. | vars ',' var
  658. {
  659. struct var **walk;
  660. $$ = $1;
  661. for (walk = &$$; *walk; walk = &(*walk)->next);
  662. *walk = $3;
  663. }
  664. ;
  665. var:
  666. opt_key ID
  667. {
  668. if (!$1 && find_var(curr_frame, $2)) {
  669. yyerrorf("duplicate variable \"%s\"", $2);
  670. YYABORT;
  671. }
  672. $$ = zalloc_type(struct var);
  673. $$->name = $2;
  674. $$->frame = curr_frame;
  675. $$->table = curr_table;
  676. $$->key = $1;
  677. $$->next = NULL;
  678. n_vars++;
  679. }
  680. ;
  681. opt_key:
  682. {
  683. $$ = 0;
  684. }
  685. | '?'
  686. {
  687. $$ = 1;
  688. }
  689. ;
  690. rows:
  691. {
  692. $$ = NULL;
  693. }
  694. | '{'
  695. {
  696. $<row>$ = alloc_type(struct row);
  697. $<row>$->table = curr_table;
  698. curr_row = $<row>$;;
  699. n_values = 0;
  700. }
  701. row '}'
  702. {
  703. if (n_vars != n_values) {
  704. yyerrorf("table has %d variables but row has "
  705. "%d values", n_vars, n_values);
  706. YYABORT;
  707. }
  708. $<row>2->values = $3;
  709. }
  710. rows
  711. {
  712. $$ = $<row>2;
  713. $$->next = $6;
  714. }
  715. ;
  716. row:
  717. value
  718. {
  719. $$ = $1;
  720. }
  721. | row ',' value
  722. {
  723. struct value **walk;
  724. $$ = $1;
  725. for (walk = &$$; *walk; walk = &(*walk)->next);
  726. *walk = $3;
  727. }
  728. ;
  729. value:
  730. expr
  731. {
  732. $$ = alloc_type(struct value);
  733. $$->expr = $1;
  734. $$->row = curr_row;
  735. $$->next = NULL;
  736. n_values++;
  737. }
  738. ;
  739. vec:
  740. TOK_VEC base '(' expr ',' expr ')'
  741. {
  742. $$ = alloc_type(struct vec);
  743. $$->nul_tag = 0;
  744. $$->name = NULL;
  745. $$->base = $2;
  746. $$->x = $4;
  747. $$->y = $6;
  748. $$->frame = curr_frame;
  749. $$->next = NULL;
  750. last_vec = $$;
  751. *next_vec = $$;
  752. next_vec = &$$->next;
  753. }
  754. ;
  755. base:
  756. '@'
  757. {
  758. $$ = NULL;
  759. }
  760. | '.'
  761. {
  762. $$ = last_vec;
  763. if (!$$) {
  764. yyerrorf(". without predecessor");
  765. YYABORT;
  766. }
  767. }
  768. | ID
  769. {
  770. $$ = find_vec(curr_frame, $1);
  771. if (!$$)
  772. $$ = (struct vec *) $1;
  773. }
  774. ;
  775. qualified_base:
  776. base
  777. {
  778. $$.frame = curr_frame;
  779. $$.vec = $1;
  780. }
  781. | frame_qualifier '@'
  782. {
  783. $$.frame = $1;
  784. $$.vec = NULL;
  785. }
  786. | frame_qualifier ID
  787. {
  788. $$.frame = $1;
  789. $$.vec = find_vec($1, $2);
  790. if (!$$.vec) {
  791. yyerrorf("unknown vector \"%s.%s\"",
  792. $1->name, $2);
  793. YYABORT;
  794. }
  795. }
  796. ;
  797. frame_qualifier:
  798. ID '.'
  799. {
  800. $$ = find_frame($1);
  801. if (!$$) {
  802. yyerrorf("unknown frame \"%s\"", $1);
  803. YYABORT;
  804. }
  805. }
  806. ;
  807. object:
  808. obj
  809. {
  810. $$ = $1;
  811. *next_obj = $1;
  812. next_obj = &$1->next;
  813. }
  814. ;
  815. obj:
  816. TOK_PAD STRING base base pad_type
  817. {
  818. $$ = new_obj(ot_pad);
  819. $$->base = $3;
  820. $$->u.pad.name = $2;
  821. $$->u.pad.other = $4;
  822. $$->u.pad.rounded = 0;
  823. $$->u.pad.type = $5;
  824. }
  825. | TOK_RPAD STRING base base pad_type
  826. {
  827. $$ = new_obj(ot_pad);
  828. $$->base = $3;
  829. $$->u.pad.name = $2;
  830. $$->u.pad.other = $4;
  831. $$->u.pad.rounded = 1;
  832. $$->u.pad.type = $5;
  833. }
  834. | TOK_HOLE base base
  835. {
  836. $$ = new_obj(ot_hole);
  837. $$->base = $2;
  838. $$->u.hole.other = $3;
  839. }
  840. | TOK_RECT base base opt_expr
  841. {
  842. $$ = new_obj(ot_rect);
  843. $$->base = $2;
  844. $$->u.rect.other = $3;
  845. $$->u.rect.width = $4;
  846. }
  847. | TOK_LINE base base opt_expr
  848. {
  849. $$ = new_obj(ot_line);
  850. $$->base = $2;
  851. $$->u.line.other = $3;
  852. $$->u.line.width = $4;
  853. }
  854. | TOK_CIRC base base opt_expr
  855. {
  856. $$ = new_obj(ot_arc);
  857. $$->base = $2;
  858. $$->u.arc.start = $3;
  859. $$->u.arc.end = $3;
  860. $$->u.arc.width = $4;
  861. }
  862. | TOK_ARC base base base opt_expr
  863. {
  864. $$ = new_obj(ot_arc);
  865. $$->base = $2;
  866. $$->u.arc.start = $3;
  867. $$->u.arc.end = $4;
  868. $$->u.arc.width = $5;
  869. }
  870. | TOK_FRAME ID
  871. {
  872. $<num>$.n = lineno;
  873. }
  874. base
  875. {
  876. $$ = new_obj(ot_frame);
  877. $$->base = $4;
  878. $$->u.frame.ref = find_frame($2);
  879. if (!$$->u.frame.ref) {
  880. yyerrorf("unknown frame \"%s\"", $2);
  881. YYABORT;
  882. }
  883. if (!$$->u.frame.ref->active_ref)
  884. $$->u.frame.ref->active_ref = $$;
  885. $$->u.frame.lineno = $<num>3.n;
  886. }
  887. | TOK_DBG_IPRINT expr
  888. {
  889. $$ = new_obj(ot_iprint);
  890. $$->base = NULL;
  891. $$->u.iprint.expr = $2;
  892. }
  893. ;
  894. pad_type:
  895. {
  896. $$ = pt_normal;
  897. }
  898. | ID
  899. {
  900. if (!strcmp($1, "bare"))
  901. $$ = pt_bare;
  902. else if (!strcmp($1, "trace"))
  903. $$ = pt_trace;
  904. else if (!strcmp($1, "paste"))
  905. $$ = pt_paste;
  906. else if (!strcmp($1, "mask"))
  907. $$ = pt_mask;
  908. else {
  909. yyerrorf("unknown pad type \"%s\"", $1);
  910. YYABORT;
  911. }
  912. }
  913. ;
  914. opt_measurements:
  915. | measurements
  916. ;
  917. measurements:
  918. unlabeled_meas /* @@@ hack */
  919. {
  920. *next_obj = $1;
  921. next_obj = &$1->next;
  922. }
  923. | measurements meas
  924. {
  925. *next_obj = $2;
  926. next_obj = &$2->next;
  927. }
  928. | measurements debug_item
  929. ;
  930. meas:
  931. unlabeled_meas
  932. {
  933. $$ = $1;
  934. }
  935. | LABEL unlabeled_meas
  936. {
  937. $$ = $2;
  938. if (find_label(curr_frame, $1)) {
  939. yyerrorf("duplicate label \"%s\"", $1);
  940. YYABORT;
  941. }
  942. $$->name = $1;
  943. }
  944. ;
  945. unlabeled_meas:
  946. meas_type opt_string qbase meas_op qbase opt_expr
  947. {
  948. struct meas *meas;
  949. $$ = new_obj(ot_meas);
  950. meas = &$$->u.meas;
  951. meas->type = $4.max ? $1+3 : $1;
  952. meas->label = $2;
  953. $$->base = $3.vec;
  954. meas->low_qual = $3.qualifiers;
  955. meas->inverted = $4.inverted;
  956. meas->high = $5.vec;
  957. meas->high_qual = $5.qualifiers;
  958. meas->offset = $6;
  959. $$->next = NULL;
  960. }
  961. ;
  962. qbase:
  963. qbase_unchecked
  964. {
  965. $$ = $1;
  966. if (!check_qbase(&$$))
  967. YYABORT;
  968. }
  969. ;
  970. qbase_unchecked:
  971. ID
  972. {
  973. $$.vec = find_vec(frames, $1);
  974. if (!$$.vec) {
  975. yyerrorf("unknown vector \"%s\"", $1);
  976. YYABORT;
  977. }
  978. $$.qualifiers = NULL;
  979. }
  980. | ID '.' ID
  981. {
  982. const struct frame *frame;
  983. frame = find_frame($1);
  984. $$.vec = frame ? find_vec(frame, $3) : NULL;
  985. if (!$$.vec) {
  986. yyerrorf("unknown vector \"%s.%s\"", $1, $3);
  987. YYABORT;
  988. }
  989. $$.qualifiers = NULL;
  990. }
  991. | ID '/' qbase
  992. {
  993. const struct frame *frame;
  994. struct frame_qual *qual;
  995. $$ = $3;
  996. frame = find_frame($1);
  997. if (!frame) {
  998. yyerrorf("unknown frame \"%s\"", $1);
  999. YYABORT;
  1000. } else {
  1001. qual = alloc_type(struct frame_qual);
  1002. qual->frame = frame;
  1003. qual->next = $$.qualifiers;
  1004. $$.qualifiers = qual;
  1005. }
  1006. }
  1007. ;
  1008. meas_type:
  1009. TOK_MEAS
  1010. {
  1011. $$ = mt_xy_next;
  1012. }
  1013. | TOK_MEASX
  1014. {
  1015. $$ = mt_x_next;
  1016. }
  1017. | TOK_MEASY
  1018. {
  1019. $$ = mt_y_next;
  1020. }
  1021. ;
  1022. meas_op:
  1023. TOK_NEXT
  1024. {
  1025. $$.max = 0;
  1026. $$.inverted = 0;
  1027. }
  1028. | TOK_NEXT_INVERTED
  1029. {
  1030. $$.max = 0;
  1031. $$.inverted = 1;
  1032. }
  1033. | TOK_MAX
  1034. {
  1035. $$.max = 1;
  1036. $$.inverted = 0;
  1037. }
  1038. | TOK_MAX_INVERTED
  1039. {
  1040. $$.max = 1;
  1041. $$.inverted = 1;
  1042. }
  1043. ;
  1044. opt_num:
  1045. {
  1046. $$.n = 0;
  1047. }
  1048. | NUMBER
  1049. {
  1050. $$ = $1;
  1051. }
  1052. ;
  1053. opt_string:
  1054. {
  1055. $$ = NULL;
  1056. }
  1057. | STRING
  1058. {
  1059. $$ = $1;
  1060. }
  1061. ;
  1062. opt_expr:
  1063. {
  1064. $$ = NULL;
  1065. }
  1066. | expr
  1067. {
  1068. $$ = $1;
  1069. }
  1070. ;
  1071. expr:
  1072. add_expr
  1073. {
  1074. $$ = $1;
  1075. }
  1076. ;
  1077. add_expr:
  1078. mult_expr
  1079. {
  1080. $$ = $1;
  1081. }
  1082. | add_expr '+' mult_expr
  1083. {
  1084. $$ = binary_op(op_add, $1, $3);
  1085. }
  1086. | add_expr '-' mult_expr
  1087. {
  1088. $$ = binary_op(op_sub, $1, $3);
  1089. }
  1090. ;
  1091. mult_expr:
  1092. unary_expr
  1093. {
  1094. $$ = $1;
  1095. }
  1096. | mult_expr '*' unary_expr
  1097. {
  1098. $$ = binary_op(op_mult, $1, $3);
  1099. }
  1100. | mult_expr '/' unary_expr
  1101. {
  1102. $$ = binary_op(op_div, $1, $3);
  1103. }
  1104. ;
  1105. unary_expr:
  1106. primary_expr
  1107. {
  1108. $$ = $1;
  1109. }
  1110. | '-' primary_expr
  1111. {
  1112. $$ = binary_op(op_minus, $2, NULL);
  1113. }
  1114. ;
  1115. primary_expr:
  1116. NUMBER
  1117. {
  1118. $$ = new_op(op_num);
  1119. $$->u.num = $1;
  1120. }
  1121. | ID
  1122. {
  1123. $$ = new_op(op_var);
  1124. $$->u.var = $1;
  1125. }
  1126. | STRING
  1127. {
  1128. $$ = new_op(op_string);
  1129. $$->u.str = $1;
  1130. }
  1131. | '(' expr ')'
  1132. {
  1133. $$ = $2;
  1134. }
  1135. | ID '(' expr ')'
  1136. {
  1137. if ($1 == id_sin)
  1138. $$ = binary_op(op_sin, $3, NULL);
  1139. else if ($1 == id_cos)
  1140. $$ = binary_op(op_cos, $3, NULL);
  1141. else if ($1 == id_sqrt)
  1142. $$ = binary_op(op_sqrt, $3, NULL);
  1143. else if ($1 == id_floor)
  1144. $$ = binary_op(op_floor, $3, NULL);
  1145. else {
  1146. yyerrorf("unknown function \"%s\"", $1);
  1147. YYABORT;
  1148. }
  1149. }
  1150. ;
  1151. /* special sub-grammar */
  1152. opt_value_list:
  1153. {
  1154. $$ = NULL;
  1155. }
  1156. | '=' row
  1157. {
  1158. $$ = $2;
  1159. }
  1160. ;