You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

892 lines
23 KiB

6 years ago
  1. /* description: Construct AST for jaz language. */
  2. /* lexical grammar */
  3. %lex
  4. %%
  5. \s+ { /* skip whitespace */ }
  6. \/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ { /* console.log("MULTILINE COMMENT: "+yytext); */ }
  7. \/\/.* { /* console.log("SINGLE LINE COMMENT: "+yytext); */ }
  8. var { return 'var'; }
  9. signal { return 'signal'; }
  10. input { return 'input'; }
  11. output { return 'output'; }
  12. linearCombination { return 'linearCombination'; }
  13. component { return 'component'; }
  14. template { return 'template'; }
  15. function { return 'function'; }
  16. if { return 'if'; }
  17. else { return 'else'; }
  18. for { return 'for'; }
  19. while { return 'while'; }
  20. do { return 'do'; }
  21. return { return 'return'; }
  22. include { return 'include'; }
  23. 0x[0-9A-Fa-f]* { return 'HEXNUMBER'; }
  24. [0-9]+ { return 'DECNUMBER'; }
  25. [a-zA-Z][a-zA-Z$_0-9]* { return 'IDENTIFIER'; }
  26. \"[^"]+\" { yytext = yytext.slice(1,-1); return 'STRING'; }
  27. \=\=\> { return '==>'; }
  28. \<\=\= { return '<=='; }
  29. \-\-\> { return '-->'; }
  30. \<\-\- { return '<--'; }
  31. \=\=\= { return '==='; }
  32. \>\>\= { return '>>='; }
  33. \<\<\= { return '<<='; }
  34. \&\& { return '&&'; }
  35. \|\| { return '||'; }
  36. \=\= { return '=='; }
  37. \<\= { return '<='; }
  38. \>\= { return '>='; }
  39. \!\= { return '!='; }
  40. \>\> { return '>>'; }
  41. \<\< { return '<<'; }
  42. \*\* { return '**'; }
  43. \+\+ { return '++'; }
  44. \-\- { return '--'; }
  45. \+\= { return '+='; }
  46. \-\= { return '-='; }
  47. \*\= { return '*='; }
  48. \/\= { return '/='; }
  49. \%\= { return '%='; }
  50. \|\= { return '|='; }
  51. \&\= { return '&='; }
  52. \^\= { return '^='; }
  53. \= { return '='; }
  54. \+ { return '+'; }
  55. \- { return '-'; }
  56. \* { return '*'; }
  57. \/ { return '/'; }
  58. \% { return '%'; }
  59. \^ { return '^'; }
  60. \& { return '&'; }
  61. \| { return '|'; }
  62. \! { return '!'; }
  63. \< { return '<'; }
  64. \> { return '>'; }
  65. \! { return '!'; }
  66. \? { return '?'; }
  67. \: { return ':'; }
  68. \( { return '('; }
  69. \) { return ')'; }
  70. \[ { return '['; }
  71. \] { return ']'; }
  72. \{ { return '{'; }
  73. \} { return '}'; }
  74. \; { return ';'; }
  75. \, { return ','; }
  76. \. { return '.'; }
  77. <<EOF>> { return 'EOF'; }
  78. . { console.log("INVALID: " + yytext); return 'INVALID'}
  79. /lex
  80. %left ';'
  81. %right 'if' 'else'
  82. %left EMPTY
  83. %left IDLIST
  84. %left ','
  85. %right '?' ':' TERCOND '=' '+=' '-=' '*=' '/=' '%=' '>>=' '<<=' '&=' '|=' '^=' '<==' '==>' '===' '<--' '-->'
  86. %left '||'
  87. %left '&&'
  88. %left '|'
  89. %left '^'
  90. %left '&'
  91. %left '==' '!='
  92. %left '<=' '>=' '<' '>'
  93. %left '<<' '>>'
  94. %left '+' '-'
  95. %left '*' '/' '%'
  96. %left '**'
  97. %right '++' '--' UMINUS UPLUS '!' '~'
  98. %left '.'
  99. %left DECL
  100. %left PLUSPLUSRIGHT MINUSMINUSRIGHT '[' ']' '(' ')'
  101. %left HIGH
  102. %{
  103. const bigInt = require('big-integer');
  104. const util = require('util');
  105. const __P__ = new bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  106. const __MASK__ = new bigInt(2).pow(253).minus(1);
  107. function setLines(dst, first, last) {
  108. last = last || first;
  109. dst.first_line = first.first_line;
  110. dst.first_column = first.first_column;
  111. dst.last_line = last.last_line;
  112. dst.last_column = last.last_column;
  113. }
  114. %}
  115. %start allStatments
  116. %% /* language grammar */
  117. allStatments
  118. : statmentList EOF
  119. {
  120. // console.log(JSON.stringify($1, null, 1));
  121. $$ = { type: "BLOCK", statements: $1.statments };
  122. setLines($$, @1);
  123. return $$
  124. }
  125. ;
  126. statmentList
  127. : statmentList statment
  128. {
  129. $1.statments.push($2);
  130. setLines($1, @1, @2);
  131. }
  132. | statment
  133. {
  134. $$ = { type: "STATMENTLIST", statments: [$1] };
  135. setLines($$, @1);
  136. }
  137. ;
  138. statment
  139. : functionDefinitionStatment
  140. {
  141. $$ = $1;
  142. }
  143. | templateDefinitionStatment
  144. {
  145. $$ = $1;
  146. }
  147. | ifStatment
  148. {
  149. $$ = $1;
  150. }
  151. | forStatment
  152. {
  153. $$ = $1;
  154. }
  155. | whileStatment
  156. {
  157. $$ = $1;
  158. }
  159. | doWhileStatment
  160. {
  161. $$ = $1;
  162. }
  163. | returnStatment
  164. {
  165. $$ = $1;
  166. }
  167. | block
  168. {
  169. $$ = $1;
  170. }
  171. | expressionStatment
  172. {
  173. $$ = $1;
  174. }
  175. | includeStatment
  176. {
  177. $$ = $1;
  178. }
  179. ;
  180. functionDefinitionStatment
  181. : 'function' IDENTIFIER '(' identifierList ')' block
  182. {
  183. $$ = { type: "FUNCTIONDEF", name: $2, params: $4.identifiers, block: $6};
  184. setLines($$, @1, @6);
  185. }
  186. | 'function' IDENTIFIER '(' ')' block
  187. {
  188. $$ = { type: "FUNCTIONDEF", name: $2, params: [], block: $5 };
  189. setLines($$, @1, @5);
  190. }
  191. ;
  192. templateDefinitionStatment
  193. : 'template' IDENTIFIER '(' identifierList ')' block
  194. {
  195. $$ = { type: "TEMPLATEDEF", name: $2, params: $4.identifiers, block: $6 };
  196. setLines($$, @1, @6);
  197. }
  198. | 'template' IDENTIFIER '(' ')' block
  199. {
  200. $$ = { type: "TEMPLATEDEF", name: $2, params: [], block: $5 };
  201. setLines($$, @1, @5);
  202. }
  203. ;
  204. identifierList
  205. : identifierList ',' IDENTIFIER
  206. {
  207. $1.identifiers.push($3);
  208. setLines($1, @1, @3);
  209. }
  210. | IDENTIFIER %prec EMPTY
  211. {
  212. $$ = { type: "IDENTIFIERLIST", identifiers: [$1] };
  213. setLines($$, @1);
  214. }
  215. ;
  216. ifStatment
  217. : 'if' '(' expression ')' statment 'else' statment
  218. {
  219. if ($3.type == "NUMBER") {
  220. $$ = !$3.value.eq(0) ? $5 : $7;
  221. } else {
  222. $$ = { type: "IF", condition: $3, then: $5, else: $7 };
  223. }
  224. setLines($$, @1, @7);
  225. }
  226. | 'if' '(' expression ')' statment
  227. {
  228. if ($3.type == "NUMBER") {
  229. $$ = !$3.value.eq(0) ? $5 : { type: "NUMBER", value: bigInt(0) };
  230. } else {
  231. $$ = { type: "IF", condition: $3, then: $5 };
  232. }
  233. setLines($$, @1, @5);
  234. }
  235. ;
  236. forStatment
  237. : 'for' '(' expression ';' expression ';' expression ')' statment
  238. {
  239. $$ = { type: "FOR", init: $3, condition: $5, step: $7, body: $9 };
  240. setLines($$, @1, @9);
  241. }
  242. ;
  243. whileStatment
  244. : 'while' '(' expression ')' statment
  245. {
  246. $$ = { type: "WHILE", condition: $3, body: $5 };
  247. setLines($$, @1, @5);
  248. }
  249. ;
  250. doWhileStatment
  251. : 'do' statment 'while' '(' expression ')'
  252. {
  253. $$ = { type: "DOWHILE", condition: $5, body: $2 };
  254. setLines($$, @1, @6);
  255. }
  256. ;
  257. returnStatment
  258. : 'return' expression ';'
  259. {
  260. $$ = { type: "RETURN", value: $2 };
  261. setLines($$, @1, @3);
  262. }
  263. | 'return' expression %prec ';'
  264. {
  265. $$ = { type: "RETURN", value: $2 }
  266. setLines($$, @1, @2);
  267. }
  268. ;
  269. includeStatment
  270. : 'include' STRING ';'
  271. {
  272. $$ = { type: "INCLUDE", file: $2 };
  273. setLines($$, @1, @3);
  274. }
  275. | 'include' STRING %prec ';'
  276. {
  277. $$ = { type: "INCLUDE", file: $2 }
  278. setLines($$, @1, @2);
  279. }
  280. ;
  281. block
  282. : '{' statmentList '}'
  283. {
  284. $$ = { type: "BLOCK", statements: $2.statments };
  285. setLines($$, @1, @3);
  286. }
  287. ;
  288. expressionStatment
  289. : expression ';' %prec ';'
  290. {
  291. $$ = $1;
  292. }
  293. | expression %prec ';'
  294. {
  295. $$ = $1;
  296. }
  297. ;
  298. expression
  299. : e17 %prec EMPTY
  300. {
  301. $$ = $1;
  302. }
  303. ;
  304. e17
  305. : leftHandExpression '=' e17
  306. {
  307. $$ = { type: "OP", op: "=", values: [$1, $3] };
  308. setLines($$, @1, @3);
  309. }
  310. | leftHandExpression '+=' e17
  311. {
  312. $$ = { type: "OP", op: "+=", values: [$1, $3] };
  313. setLines($$, @1, @3);
  314. }
  315. | leftHandExpression '-=' e17
  316. {
  317. $$ = { type: "OP", op: "-=", values: [$1, $3] };
  318. setLines($$, @1, @3);
  319. }
  320. | leftHandExpression '*=' e17
  321. {
  322. $$ = { type: "OP", op: "*=", values: [$1, $3] };
  323. setLines($$, @1, @3);
  324. }
  325. | leftHandExpression '/=' e17
  326. {
  327. $$ = { type: "OP", op: "/=", values: [$1, $3] };
  328. setLines($$, @1, @3);
  329. }
  330. | leftHandExpression '%=' e17
  331. {
  332. $$ = { type: "OP", op: "%=", values: [$1, $3] };
  333. setLines($$, @1, @3);
  334. }
  335. | leftHandExpression '<<=' e17
  336. {
  337. $$ = { type: "OP", op: "<<=", values: [$1, $3] };
  338. setLines($$, @1, @3);
  339. }
  340. | leftHandExpression '>>=' e17
  341. {
  342. $$ = { type: "OP", op: ">>=", values: [$1, $3] };
  343. setLines($$, @1, @3);
  344. }
  345. | leftHandExpression '&=' e17
  346. {
  347. $$ = { type: "OP", op: "&=", values: [$1, $3] };
  348. setLines($$, @1, @3);
  349. }
  350. | leftHandExpression '|=' e17
  351. {
  352. $$ = { type: "OP", op: "|=", values: [$1, $3] };
  353. setLines($$, @1, @3);
  354. }
  355. | leftHandExpression '^=' e17
  356. {
  357. $$ = { type: "OP", op: "^=", values: [$1, $3] };
  358. setLines($$, @1, @3);
  359. }
  360. | leftHandExpression '<==' e17
  361. {
  362. $$ = { type: "OP", op: "<==", values: [$1, $3] };
  363. setLines($$, @1, @3);
  364. }
  365. | e17 '==>' leftHandExpression
  366. {
  367. $$ = { type: "OP", op: "<==", values: [$3, $1] };
  368. setLines($$, @1, @3);
  369. }
  370. | leftHandExpression '<--' e17
  371. {
  372. $$ = { type: "OP", op: "<--", values: [$1, $3] };
  373. setLines($$, @1, @3);
  374. }
  375. | e17 '-->' leftHandExpression
  376. {
  377. $$ = { type: "OP", op: "<--", values: [$3, $1] };
  378. setLines($$, @1, @3);
  379. }
  380. | e16 '===' e17
  381. {
  382. $$ = { type: "OP", op: "===", values: [$1, $3] };
  383. setLines($$, @1, @3);
  384. }
  385. | e17 '?' e17 ':' e17 %prec TERCOND
  386. {
  387. if ($1.type == "NUMBER") {
  388. $$ = !$1.value.eq(0) ? $3 : $5;
  389. } else {
  390. $$ = { type: "OP", op: "?", values: [$1, $3, $5] };
  391. }
  392. setLines($$, @1, @5);
  393. }
  394. | e16 %prec EMPTY
  395. {
  396. $$ = $1;
  397. }
  398. ;
  399. e16
  400. : rightArray
  401. {
  402. $$ = $1;
  403. }
  404. | e15 %prec EMPTY
  405. {
  406. $$ = $1;
  407. }
  408. ;
  409. e15
  410. : e15 '||' e14
  411. {
  412. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  413. $$ = { type: "NUMBER", value: !$1.value.eq(0) || !$3.value.eq(0) ? bigInt(1) : bigInt(0) };
  414. } else {
  415. $$ = { type: "OP", op: "||", values: [$1, $3] };
  416. }
  417. setLines($$, @1, @3);
  418. }
  419. | e14 %prec EMPTY
  420. {
  421. $$ = $1;
  422. }
  423. ;
  424. e14
  425. : e14 '&&' e13
  426. {
  427. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  428. $$ = { type: "NUMBER", value: !$1.value.eq(0) && !$3.value.eq(0) ? bigInt(1) : bigInt(0) };
  429. } else {
  430. $$ = { type: "OP", op: "&&", values: [$1, $3] };
  431. }
  432. setLines($$, @1, @3);
  433. }
  434. | e13 %prec EMPTY
  435. {
  436. $$ = $1;
  437. }
  438. ;
  439. e13
  440. : e13 '|' e12
  441. {
  442. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  443. $$ = { type: "NUMBER", value: $1.value.or($3.value).and(__MASK__) };
  444. } else {
  445. $$ = { type: "OP", op: "|", values: [$1, $3] };
  446. }
  447. setLines($$, @1, @3);
  448. }
  449. | e12 %prec EMPTY
  450. {
  451. $$ = $1;
  452. }
  453. ;
  454. e12
  455. : e12 '^' e11
  456. {
  457. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  458. $$ = { type: "NUMBER", value: $1.value.or($3.value).and(__MASK__) };
  459. } else {
  460. $$ = { type: "OP", op: "^", values: [$1, $3] };
  461. }
  462. setLines($$, @1, @3);
  463. }
  464. | e11 %prec EMPTY
  465. {
  466. $$ = $1;
  467. }
  468. ;
  469. e11
  470. : e11 '&' e10
  471. {
  472. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  473. $$ = { type: "NUMBER", value: $1.value.and($3.value).and(__MASK__) };
  474. } else {
  475. $$ = { type: "OP", op: "&", values: [$1, $3] };
  476. }
  477. setLines($$, @1, @3);
  478. }
  479. | e10 %prec EMPTY
  480. {
  481. $$ = $1;
  482. }
  483. ;
  484. e10
  485. : e10 '==' e9
  486. {
  487. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  488. $$ = { type: "NUMBER", value: $1.value.equals($3.value) ? bigInt(1) : bigInt(0) };
  489. } else {
  490. $$ = { type: "OP", op: "==", values: [$1, $3] };
  491. }
  492. setLines($$, @1, @3);
  493. }
  494. | e10 '!=' e9
  495. {
  496. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  497. $$ = { type: "NUMBER", value: $1.value.eq($3.value) ? bigInt(0) : bigInt(1) };
  498. } else {
  499. $$ = { type: "OP", op: "!=", values: [$1, $3] };
  500. }
  501. setLines($$, @1, @3);
  502. }
  503. | e9 %prec EMPTY
  504. {
  505. $$ = $1
  506. }
  507. ;
  508. e9
  509. : e9 '<=' e7
  510. {
  511. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  512. $$ = { type: "NUMBER", value: $1.value.lesserOrEquals($3.value) ? bigInt(1) : bigInt(0) };
  513. } else {
  514. $$ = { type: "OP", op: "<=", values: [$1, $3] };
  515. }
  516. setLines($$, @1, @3);
  517. }
  518. | e9 '>=' e7
  519. {
  520. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  521. $$ = { type: "NUMBER", value: $1.value.greaterOrEquals($3.value) ? bigInt(1) : bigInt(0) };
  522. } else {
  523. $$ = { type: "OP", op: ">=", values: [$1, $3] };
  524. }
  525. setLines($$, @1, @3);
  526. }
  527. | e9 '<' e7
  528. {
  529. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  530. $$ = { type: "NUMBER", value: $1.value.lesser($3.value) ? bigInt(1) : bigInt(0) };
  531. } else {
  532. $$ = { type: "OP", op: "<", values: [$1, $3] };
  533. }
  534. setLines($$, @1, @3);
  535. }
  536. | e9 '>' e7
  537. {
  538. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  539. $$ = { type: "NUMBER", value: $1.value.greater($3.value) ? bigInt(1) : bigInt(0) };
  540. } else {
  541. $$ = { type: "OP", op: ">", values: [$1, $3] };
  542. }
  543. setLines($$, @1, @3);
  544. }
  545. | e7 %prec EMPTY
  546. {
  547. $$ = $1
  548. }
  549. ;
  550. e7
  551. : e7 '<<' e6
  552. {
  553. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  554. let v = $3.value.greater(256) ? 256 : $3.value.value;
  555. $$ = { type: "NUMBER", value: $1.value.shiftLeft(v).and(__MASK__) };
  556. } else {
  557. $$ = { type: "OP", op: "<<", values: [$1, $3] };
  558. }
  559. setLines($$, @1, @3);
  560. }
  561. | e7 '>>' e6
  562. {
  563. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  564. let v = $3.value.greater(256) ? 256 : $3.value.value;
  565. $$ = {t1ype: "NUMBER", value: $1.value.shiftRight(v).and(__MASK__) };
  566. } else {
  567. $$ = { type: "OP", op: ">>", values: [$1, $3] };
  568. }
  569. setLines($$, @1, @3);
  570. }
  571. | e6 %prec EMPTY
  572. {
  573. $$ = $1;
  574. }
  575. ;
  576. e6
  577. : e6 '+' e5
  578. {
  579. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  580. $$ = { type: "NUMBER", value: ($1.value.plus($3.value)).mod(__P__) };
  581. } else {
  582. $$ = { type: "OP", op: "+", values: [$1, $3] };
  583. }
  584. setLines($$, @1, @3);
  585. }
  586. | e6 '-' e5
  587. {
  588. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  589. $$ = { type: "NUMBER", value: ($1.value.plus(__P__).minus($3.value)).mod(__P__) };
  590. } else {
  591. $$ = { type: "OP", op: "-", values: [$1, $3] };
  592. }
  593. setLines($$, @1, @3);
  594. }
  595. | e5 %prec EMPTY
  596. {
  597. $$ = $1;
  598. }
  599. ;
  600. e5
  601. : e5 '*' e4
  602. {
  603. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  604. $$ = { type: "NUMBER", value: ($1.value.times($3.value)).mod(__P__) };
  605. } else {
  606. $$ = { type: "OP", op: "*", values: [$1, $3] };
  607. }
  608. setLines($$, @1, @3);
  609. }
  610. | e5 '/' e4
  611. {
  612. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  613. $$ = { type: "NUMBER", value: ($1.value.times($3.value.modInv(__P__))).mod(__P__) };
  614. } else {
  615. $$ = { type: "OP", op: "/", values: [$1, $3] };
  616. }
  617. setLines($$, @1, @3);
  618. }
  619. | e5 '%' e4
  620. {
  621. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  622. $$ = { type: "NUMBER", value: $1.value.mod($3.value) };
  623. } else {
  624. $$ = { type: "OP", op: "%", values: [$1, $3] };
  625. }
  626. setLines($$, @1, @3);
  627. }
  628. | e4 %prec EMPTY
  629. {
  630. $$ = $1;
  631. }
  632. ;
  633. e4
  634. : e4 '**' e3
  635. {
  636. if (($1.type == "NUMBER") && ($3.type == "NUMBER")) {
  637. $$ = { type: "NUMBER", value: $1.value.modPow($3.value, __P__) };
  638. } else {
  639. $$ = { type: "OP", op: "**", values: [$1, $3] };
  640. }
  641. setLines($$, @1, @3);
  642. }
  643. | e3 %prec EMPTY
  644. {
  645. $$ = $1;
  646. }
  647. ;
  648. e3
  649. : '++' leftHandExpression
  650. {
  651. $$ = { type: "OP", op: "PLUSPLUSLEFT", values: [$2] };
  652. setLines($$, @1, @2);
  653. }
  654. | '--' leftHandExpression
  655. {
  656. $$ = { type: "OP", op: "MINUSMINUSLEFT", values: [$2] };
  657. setLines($$, @1, @2);
  658. }
  659. | '+' e3 %prec UPLUS
  660. {
  661. $$ = $2;
  662. setLines($$, @1, @2);
  663. }
  664. | '-' e3 %prec UMINUS
  665. {
  666. if ($2.type == "NUMBER") {
  667. $$ = { type: "NUMBER", value: __P__.minus($2.value).mod(__P__) };
  668. } else {
  669. $$ = { type: "OP", op: "UMINUS", values: [$2] };
  670. }
  671. setLines($$, @1, @2);
  672. }
  673. | '!' e3
  674. {
  675. if ($2.type == "NUMBER") {
  676. $$ = { type: "NUMBER", value: $2.value.eq(0) ? bigInt(1) : bigInt(0) };
  677. } else {
  678. $$ = { type: "OP", op: "!", values: [$2] };
  679. }
  680. setLines($$, @1, @2);
  681. }
  682. | '~' e3
  683. {
  684. if ($2.type == "NUMBER") {
  685. $$ = { type: "NUMBER", value: $2.value.xor(__MASK__) };
  686. } else {
  687. $$ = { type: "OP", op: "~", values: [$2] };
  688. }
  689. setLines($$, @1, @2);
  690. }
  691. | e2 %prec EMPTY
  692. {
  693. $$ = $1;
  694. }
  695. ;
  696. e2
  697. : leftHandExpression '++' %prec PLUSPLUSRIGHT
  698. {
  699. $$ = {type: "OP", op: "PLUSPLUSRIGHT", values: [$1] };
  700. setLines($$, @1, @2);
  701. }
  702. | leftHandExpression '--' %prec MINUSMINUSRIGHT
  703. {
  704. $$ = {type: "OP", op: "MINUSMINUSRIGHT", values: [$1] };
  705. setLines($$, @1, @2);
  706. }
  707. | functionCall
  708. {
  709. $$ = $1;
  710. }
  711. | e0 %prec EMPTY
  712. {
  713. $$ = $1;
  714. }
  715. ;
  716. e0
  717. : leftHandExpression %prec EMPTY
  718. {
  719. $$ = $1
  720. }
  721. | DECNUMBER
  722. {
  723. $$ = {type: "NUMBER", value: bigInt($1).mod(__P__) }
  724. setLines($$, @1);
  725. }
  726. | HEXNUMBER
  727. {
  728. $$ = {type: "NUMBER", value: bigInt($1.substr(2).toUpperCase(), 16).mod(__P__) }
  729. setLines($$, @1);
  730. }
  731. | '(' expression ')' %prec EMPTY
  732. {
  733. $$ = $2;
  734. setLines($$, @1, @3);
  735. }
  736. ;
  737. leftHandExpression
  738. : simpleLeftHandExpression '.' simpleLeftHandExpression %prec EMPTY
  739. {
  740. $$ = {type: "PIN", component: $1, pin: $3 };
  741. setLines($$, @1, @3);
  742. }
  743. | declaration %prec DECL
  744. {
  745. $$ = $1
  746. }
  747. | simpleLeftHandExpression %prec EMPTY
  748. {
  749. $$ = $1
  750. }
  751. ;
  752. declaration
  753. : 'var' simpleLeftHandExpression %prec DECL
  754. {
  755. $$ = {type: "DECLARE", declareType: "VARIABLE", name: $2}
  756. setLines($$, @1, @2);
  757. }
  758. | 'signal' simpleLeftHandExpression %prec DECL
  759. {
  760. $$ = {type: "DECLARE", declareType: "SIGNAL", name: $2}
  761. setLines($$, @1, @2);
  762. }
  763. | 'signal' 'input' simpleLeftHandExpression %prec DECL
  764. {
  765. $$ = {type: "DECLARE", declareType: "SIGNALIN", name: $3};
  766. setLines($$, @1, @3);
  767. }
  768. | 'signal' 'output' simpleLeftHandExpression %prec DECL
  769. {
  770. $$ = {type: "DECLARE", declareType: "SIGNALOUT", name: $3};
  771. setLines($$, @1, @3);
  772. }
  773. | 'component' simpleLeftHandExpression %prec DECL
  774. {
  775. $$ = {type: "DECLARE", declareType: "COMPONENT", name: $2}
  776. setLines($$, @1, @2);
  777. }
  778. ;
  779. simpleLeftHandExpression
  780. : simpleLeftHandExpression array
  781. {
  782. for (let i=0; i< $2.values.length; i++) {
  783. $1.selectors.push($2.values[i]);
  784. }
  785. setLines($1, @1, @2);
  786. }
  787. | IDENTIFIER %prec EMPTY
  788. {
  789. $$ = {type: "VARIABLE", name: $1 , selectors: []};
  790. setLines($$, @1);
  791. }
  792. ;
  793. functionCall
  794. : IDENTIFIER '(' expressionList ')'
  795. {
  796. $$ = {type: "FUNCTIONCALL", name: $1, params: $3.expressions}
  797. setLines($$, @1, @4);
  798. }
  799. | IDENTIFIER '(' ')'
  800. {
  801. $$ = {type: "FUNCTIONCALL", name: $1, params: []}
  802. setLines($$, @1, @3);
  803. }
  804. ;
  805. expressionList
  806. : expressionList ',' expression
  807. {
  808. $1.expressions.push($3);
  809. setLines($$, @1, @3);
  810. }
  811. | expression %prec ','
  812. {
  813. $$ = {type: "EXPRESSIONLST", expressions: [$1]};
  814. setLines($$, @1);
  815. }
  816. ;
  817. rightArray
  818. : array %prec EMPTY
  819. {
  820. $$ = $1;
  821. }
  822. ;
  823. array
  824. : '[' expressionList ']'
  825. {
  826. $$ = { type: "ARRAY", values: $2.expressions};
  827. setLines($$, @1, @3);
  828. }
  829. ;