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.

898 lines
23 KiB

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