gmParser.y 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. _____ __ ___ __ ____ _ __
  3. / ___/__ ___ _ ___ / |/ /__ ___ / /_____ __ __/ __/_______(_)__ / /_
  4. / (_ / _ `/ ' \/ -_) /|_/ / _ \/ _ \/ '_/ -_) // /\ \/ __/ __/ / _ \/ __/
  5. \___/\_,_/_/_/_/\__/_/ /_/\___/_//_/_/\_\\__/\_, /___/\__/_/ /_/ .__/\__/
  6. /___/ /_/
  7. See Copyright Notice in gmMachine.h
  8. */
  9. %{
  10. #define YYPARSER
  11. #include "gmConfig.h"
  12. #include "gmCodeTree.h"
  13. #define YYSTYPE gmCodeTreeNode *
  14. extern gmCodeTreeNode * g_codeTree;
  15. #define GM_BISON_DEBUG
  16. #ifdef GM_BISON_DEBUG
  17. #define YYDEBUG 1
  18. #define YYERROR_VERBOSE
  19. #endif // GM_BISON_DEBUG
  20. //
  21. // HELPERS
  22. //
  23. void ATTACH(gmCodeTreeNode * &a_res, gmCodeTreeNode * a_a, gmCodeTreeNode * a_b)
  24. {
  25. YYSTYPE t = a_a;
  26. if(t != NULL)
  27. {
  28. while(t->m_sibling != NULL)
  29. {
  30. t = t->m_sibling;
  31. }
  32. t->m_sibling = a_b;
  33. if(a_b) { a_b->m_parent = t; }
  34. a_res = a_a;
  35. }
  36. else
  37. {
  38. a_res = a_b;
  39. }
  40. }
  41. gmCodeTreeNode * CreateOperation(int a_subTypeType, gmCodeTreeNode * a_left = NULL, gmCodeTreeNode * a_right = NULL)
  42. {
  43. gmCodeTreeNode * node = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_OPERATION, gmlineno, a_subTypeType);
  44. node->SetChild(0, a_left);
  45. node->SetChild(1, a_right);
  46. return node;
  47. }
  48. gmCodeTreeNode * CreateAsignExpression(int a_subTypeType, gmCodeTreeNode * a_left, gmCodeTreeNode * a_right)
  49. {
  50. // we need to evaluate the complexety of the l-value... if it is a function call, index or dot to the left of a dot or index, we need to cache
  51. // into a hidden variable.
  52. // todo
  53. gmCodeTreeNode * opNode = CreateOperation(a_subTypeType, a_left, a_right);
  54. return CreateOperation(CTNOT_ASSIGN, a_left, opNode);
  55. }
  56. %}
  57. %token KEYWORD_LOCAL
  58. %token KEYWORD_GLOBAL
  59. %token KEYWORD_MEMBER
  60. %token KEYWORD_AND
  61. %token KEYWORD_OR
  62. %token KEYWORD_IF
  63. %token KEYWORD_ELSE
  64. %token KEYWORD_WHILE
  65. %token KEYWORD_FOR
  66. %token KEYWORD_FOREACH
  67. %token KEYWORD_IN
  68. %token KEYWORD_BREAK
  69. %token KEYWORD_CONTINUE
  70. %token KEYWORD_NULL
  71. %token KEYWORD_DOWHILE
  72. %token KEYWORD_RETURN
  73. %token KEYWORD_FUNCTION
  74. %token KEYWORD_TABLE
  75. %token KEYWORD_THIS
  76. %token KEYWORD_TRUE
  77. %token KEYWORD_FALSE
  78. %token KEYWORD_FORK
  79. %token IDENTIFIER
  80. %token CONSTANT_HEX
  81. %token CONSTANT_BINARY
  82. %token CONSTANT_INT
  83. %token CONSTANT_CHAR
  84. %token CONSTANT_FLOAT
  85. %token CONSTANT_FLOAT
  86. %token CONSTANT_STRING
  87. %token SYMBOL_ASGN_BSR
  88. %token SYMBOL_ASGN_BSL
  89. %token SYMBOL_ASGN_ADD
  90. %token SYMBOL_ASGN_MINUS
  91. %token SYMBOL_ASGN_TIMES
  92. %token SYMBOL_ASGN_DIVIDE
  93. %token SYMBOL_ASGN_REM
  94. %token SYMBOL_ASGN_BAND
  95. %token SYMBOL_ASGN_BOR
  96. %token SYMBOL_ASGN_BXOR
  97. %token SYMBOL_RIGHT_SHIFT
  98. %token SYMBOL_LEFT_SHIFT
  99. %token SYMBOL_LTE
  100. %token SYMBOL_GTE
  101. %token SYMBOL_EQ
  102. %token SYMBOL_NEQ
  103. %token TOKEN_ERROR
  104. %start program
  105. %%
  106. program
  107. : statement_list
  108. {
  109. g_codeTree = $1;
  110. }
  111. ;
  112. statement_list
  113. : statement
  114. {
  115. $$ = $1;
  116. }
  117. | statement_list statement
  118. {
  119. ATTACH($$, $1, $2);
  120. }
  121. ;
  122. statement
  123. : expression_statement
  124. {
  125. $$ = $1;
  126. }
  127. | var_statement
  128. {
  129. $$ = $1;
  130. }
  131. | selection_statement
  132. {
  133. $$ = $1;
  134. }
  135. | iteration_statement
  136. {
  137. $$ = $1;
  138. }
  139. | jump_statement
  140. {
  141. $$ = $1;
  142. }
  143. | function_statement
  144. {
  145. $$ = $1;
  146. }
  147. ;
  148. compound_statement
  149. : '{' '}'
  150. {
  151. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_COMPOUND, gmlineno);
  152. }
  153. | '{' statement_list '}'
  154. {
  155. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_COMPOUND, gmlineno);
  156. $$->SetChild(0, $2);
  157. }
  158. ;
  159. function_statement
  160. :
  161. KEYWORD_FUNCTION identifier '(' ')' compound_statement
  162. {
  163. gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
  164. func->SetChild(1, $5);
  165. $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_VARIABLE, gmlineno, (int)GMMACHINE_DEFAULT_FUNCTION);
  166. $$->SetChild(0, $2);
  167. ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $2, func));
  168. }
  169. |
  170. KEYWORD_FUNCTION identifier '(' parameter_list ')' compound_statement
  171. {
  172. gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
  173. func->SetChild(0, $4);
  174. func->SetChild(1, $6);
  175. $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_VARIABLE, gmlineno, (int)GMMACHINE_DEFAULT_FUNCTION);
  176. $$->SetChild(0, $2);
  177. ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $2, func));
  178. }
  179. ;
  180. var_statement
  181. : var_type identifier ';'
  182. {
  183. $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_VARIABLE, gmlineno, (int) $1);
  184. $$->SetChild(0, $2);
  185. }
  186. | var_type identifier '=' constant_expression ';'
  187. {
  188. $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_VARIABLE, gmlineno, (int) $1);
  189. $$->SetChild(0, $2);
  190. ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $2, $4));
  191. }
  192. | var_type KEYWORD_FUNCTION identifier '(' ')' compound_statement
  193. {
  194. gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
  195. func->SetChild(1, $6);
  196. $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_VARIABLE, gmlineno, (int) $1);
  197. $$->SetChild(0, $3);
  198. ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $3, func));
  199. }
  200. | var_type KEYWORD_FUNCTION identifier '(' parameter_list ')' compound_statement
  201. {
  202. gmCodeTreeNode* func = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
  203. func->SetChild(0, $5);
  204. func->SetChild(1, $7);
  205. $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_VARIABLE, gmlineno, (int) $1);
  206. $$->SetChild(0, $3);
  207. ATTACH($$, $$, CreateOperation(CTNOT_ASSIGN, $3, func));
  208. }
  209. ;
  210. var_type
  211. : KEYWORD_LOCAL
  212. {
  213. $$ = (YYSTYPE) CTVT_LOCAL;
  214. }
  215. | KEYWORD_GLOBAL
  216. {
  217. $$ = (YYSTYPE) CTVT_GLOBAL;
  218. }
  219. | KEYWORD_MEMBER
  220. {
  221. $$ = (YYSTYPE) CTVT_MEMBER;
  222. }
  223. ;
  224. expression_statement
  225. : ';'
  226. {
  227. $$ = NULL;
  228. }
  229. | assignment_expression ';'
  230. {
  231. $$ = $1;
  232. }
  233. ;
  234. selection_statement
  235. : KEYWORD_IF '(' constant_expression ')' compound_statement
  236. {
  237. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_IF, ($3) ? $3->m_lineNumber : gmlineno);
  238. $$->SetChild(0, $3);
  239. $$->SetChild(1, $5);
  240. }
  241. | KEYWORD_IF '(' constant_expression ')' compound_statement KEYWORD_ELSE compound_statement
  242. {
  243. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_IF, ($3) ? $3->m_lineNumber : gmlineno);
  244. $$->SetChild(0, $3);
  245. $$->SetChild(1, $5);
  246. $$->SetChild(2, $7);
  247. }
  248. | KEYWORD_IF '(' constant_expression ')' compound_statement KEYWORD_ELSE selection_statement
  249. {
  250. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_IF, ($3) ? $3->m_lineNumber : gmlineno);
  251. $$->SetChild(0, $3);
  252. $$->SetChild(1, $5);
  253. $$->SetChild(2, $7);
  254. }
  255. | KEYWORD_FORK identifier compound_statement
  256. {
  257. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_FORK, ($2) ? $2->m_lineNumber : gmlineno );
  258. $$->SetChild(0, $3 );
  259. $$->SetChild(1, $2 );
  260. }
  261. | KEYWORD_FORK compound_statement
  262. {
  263. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_FORK, ($2) ? $2->m_lineNumber : gmlineno );
  264. $$->SetChild(0, $2 );
  265. }
  266. ;
  267. iteration_statement
  268. : KEYWORD_WHILE '(' constant_expression ')' compound_statement
  269. {
  270. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_WHILE, ($3) ? $3->m_lineNumber : gmlineno);
  271. $$->SetChild(0, $3);
  272. $$->SetChild(1, $5);
  273. }
  274. | KEYWORD_DOWHILE '(' constant_expression ')' compound_statement
  275. {
  276. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_DOWHILE, ($3) ? $3->m_lineNumber : gmlineno);
  277. $$->SetChild(0, $3);
  278. $$->SetChild(1, $5);
  279. }
  280. | KEYWORD_FOR '(' expression_statement constant_expression_statement ')' compound_statement
  281. {
  282. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_FOR, ($3) ? $3->m_lineNumber : gmlineno);
  283. $$->SetChild(0, $3);
  284. $$->SetChild(1, $4);
  285. $$->SetChild(3, $6);
  286. }
  287. | KEYWORD_FOR '(' expression_statement constant_expression_statement assignment_expression ')' compound_statement
  288. {
  289. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_FOR, ($3) ? $3->m_lineNumber : gmlineno);
  290. $$->SetChild(0, $3);
  291. $$->SetChild(1, $4);
  292. $$->SetChild(2, $5);
  293. $$->SetChild(3, $7);
  294. }
  295. | KEYWORD_FOREACH '(' identifier KEYWORD_IN constant_expression ')' compound_statement
  296. {
  297. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_FOREACH, ($5) ? $5->m_lineNumber : gmlineno);
  298. $$->SetChild(0, $5);
  299. $$->SetChild(1, $3);
  300. $$->SetChild(3, $7);
  301. }
  302. | KEYWORD_FOREACH '(' identifier KEYWORD_AND identifier KEYWORD_IN constant_expression')' compound_statement
  303. {
  304. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_FOREACH, ($7) ? $7->m_lineNumber : gmlineno);
  305. $$->SetChild(0, $7);
  306. $$->SetChild(1, $5);
  307. $$->SetChild(2, $3);
  308. $$->SetChild(3, $9);
  309. }
  310. ;
  311. jump_statement
  312. : KEYWORD_CONTINUE ';'
  313. {
  314. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_CONTINUE, gmlineno);
  315. }
  316. | KEYWORD_BREAK ';'
  317. {
  318. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_BREAK, gmlineno);
  319. }
  320. | KEYWORD_RETURN ';'
  321. {
  322. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_RETURN, gmlineno);
  323. }
  324. | KEYWORD_RETURN constant_expression ';'
  325. {
  326. $$ = gmCodeTreeNode::Create(CTNT_STATEMENT, CTNST_RETURN, gmlineno);
  327. $$->SetChild(0, $2);
  328. }
  329. ;
  330. assignment_expression
  331. : logical_or_expression
  332. {
  333. $$ = $1;
  334. if($$)
  335. {
  336. $$->m_flags |= gmCodeTreeNode::CTN_POP;
  337. }
  338. }
  339. | postfix_expression '=' logical_or_expression
  340. {
  341. $$ = CreateOperation(CTNOT_ASSIGN, $1, $3);
  342. }
  343. | postfix_expression SYMBOL_ASGN_BSR logical_or_expression
  344. {
  345. $$ = CreateAsignExpression(CTNOT_SHIFT_RIGHT, $1, $3);
  346. }
  347. | postfix_expression SYMBOL_ASGN_BSL logical_or_expression
  348. {
  349. $$ = CreateAsignExpression(CTNOT_SHIFT_LEFT, $1, $3);
  350. }
  351. | postfix_expression SYMBOL_ASGN_ADD logical_or_expression
  352. {
  353. $$ = CreateAsignExpression(CTNOT_ADD, $1, $3);
  354. }
  355. | postfix_expression SYMBOL_ASGN_MINUS logical_or_expression
  356. {
  357. $$ = CreateAsignExpression(CTNOT_MINUS, $1, $3);
  358. }
  359. | postfix_expression SYMBOL_ASGN_TIMES logical_or_expression
  360. {
  361. $$ = CreateAsignExpression(CTNOT_TIMES, $1, $3);
  362. }
  363. | postfix_expression SYMBOL_ASGN_DIVIDE logical_or_expression
  364. {
  365. $$ = CreateAsignExpression(CTNOT_DIVIDE, $1, $3);
  366. }
  367. | postfix_expression SYMBOL_ASGN_REM logical_or_expression
  368. {
  369. $$ = CreateAsignExpression(CTNOT_REM, $1, $3);
  370. }
  371. | postfix_expression SYMBOL_ASGN_BAND logical_or_expression
  372. {
  373. $$ = CreateAsignExpression(CTNOT_BIT_AND, $1, $3);
  374. }
  375. | postfix_expression SYMBOL_ASGN_BOR logical_or_expression
  376. {
  377. $$ = CreateAsignExpression(CTNOT_BIT_OR, $1, $3);
  378. }
  379. | postfix_expression SYMBOL_ASGN_BXOR logical_or_expression
  380. {
  381. $$ = CreateAsignExpression(CTNOT_BIT_XOR, $1, $3);
  382. }
  383. ;
  384. constant_expression_statement
  385. : ';'
  386. {
  387. $$ = NULL;
  388. }
  389. | constant_expression ';'
  390. {
  391. $$ = $1;
  392. }
  393. ;
  394. constant_expression
  395. : logical_or_expression
  396. {
  397. $$ = $1;
  398. }
  399. ;
  400. logical_or_expression
  401. : logical_and_expression
  402. {
  403. $$ = $1;
  404. }
  405. | logical_or_expression KEYWORD_OR logical_and_expression
  406. {
  407. $$ = CreateOperation(CTNOT_OR, $1, $3);
  408. }
  409. ;
  410. logical_and_expression
  411. : inclusive_or_expression
  412. {
  413. $$ = $1;
  414. }
  415. | logical_and_expression KEYWORD_AND inclusive_or_expression
  416. {
  417. $$ = CreateOperation(CTNOT_AND, $1, $3);
  418. }
  419. ;
  420. inclusive_or_expression
  421. : exclusive_or_expression
  422. {
  423. $$ = $1;
  424. }
  425. | inclusive_or_expression '|' exclusive_or_expression
  426. {
  427. $$ = CreateOperation(CTNOT_BIT_OR, $1, $3);
  428. $$->ConstantFold();
  429. }
  430. ;
  431. exclusive_or_expression
  432. : and_expression
  433. {
  434. $$ = $1;
  435. }
  436. | exclusive_or_expression '^' and_expression
  437. {
  438. $$ = CreateOperation(CTNOT_BIT_XOR, $1, $3);
  439. $$->ConstantFold();
  440. }
  441. ;
  442. and_expression
  443. : equality_expression
  444. {
  445. $$ = $1;
  446. }
  447. | and_expression '&' equality_expression
  448. {
  449. $$ = CreateOperation(CTNOT_BIT_AND, $1, $3);
  450. $$->ConstantFold();
  451. }
  452. ;
  453. equality_expression
  454. : relational_expression
  455. {
  456. $$ = $1;
  457. }
  458. | equality_expression SYMBOL_EQ relational_expression
  459. {
  460. $$ = CreateOperation(CTNOT_EQ, $1, $3);
  461. }
  462. | equality_expression SYMBOL_NEQ relational_expression
  463. {
  464. $$ = CreateOperation(CTNOT_NEQ, $1, $3);
  465. }
  466. ;
  467. relational_expression
  468. : shift_expression
  469. {
  470. $$ = $1;
  471. }
  472. | relational_expression '<' shift_expression
  473. {
  474. $$ = CreateOperation(CTNOT_LT, $1, $3);
  475. }
  476. | relational_expression '>' shift_expression
  477. {
  478. $$ = CreateOperation(CTNOT_GT, $1, $3);
  479. }
  480. | relational_expression SYMBOL_LTE shift_expression
  481. {
  482. $$ = CreateOperation(CTNOT_LTE, $1, $3);
  483. }
  484. | relational_expression SYMBOL_GTE shift_expression
  485. {
  486. $$ = CreateOperation(CTNOT_GTE, $1, $3);
  487. }
  488. ;
  489. shift_expression
  490. : additive_expression
  491. {
  492. $$ = $1;
  493. }
  494. | shift_expression SYMBOL_LEFT_SHIFT additive_expression
  495. {
  496. $$ = CreateOperation(CTNOT_SHIFT_LEFT, $1, $3);
  497. $$->ConstantFold();
  498. }
  499. | shift_expression SYMBOL_RIGHT_SHIFT additive_expression
  500. {
  501. $$ = CreateOperation(CTNOT_SHIFT_RIGHT, $1, $3);
  502. $$->ConstantFold();
  503. }
  504. ;
  505. additive_expression
  506. : multiplicative_expression
  507. {
  508. $$ = $1;
  509. }
  510. | additive_expression '+' multiplicative_expression
  511. {
  512. $$ = CreateOperation(CTNOT_ADD, $1, $3);
  513. $$->ConstantFold();
  514. }
  515. | additive_expression '-' multiplicative_expression
  516. {
  517. $$ = CreateOperation(CTNOT_MINUS, $1, $3);
  518. $$->ConstantFold();
  519. }
  520. ;
  521. multiplicative_expression
  522. : unary_expression
  523. {
  524. $$ = $1;
  525. }
  526. | multiplicative_expression '*' unary_expression
  527. {
  528. $$ = CreateOperation(CTNOT_TIMES, $1, $3);
  529. $$->ConstantFold();
  530. }
  531. | multiplicative_expression '/' unary_expression
  532. {
  533. $$ = CreateOperation(CTNOT_DIVIDE, $1, $3);
  534. $$->ConstantFold();
  535. }
  536. | multiplicative_expression '%' unary_expression
  537. {
  538. $$ = CreateOperation(CTNOT_REM, $1, $3);
  539. $$->ConstantFold();
  540. }
  541. ;
  542. unary_expression
  543. : postfix_expression
  544. {
  545. $$ = $1;
  546. }
  547. | unary_operator unary_expression
  548. {
  549. $$ = $1;
  550. $$->SetChild(0, $2);
  551. $$->ConstantFold();
  552. }
  553. ;
  554. unary_operator
  555. : '+'
  556. {
  557. $$ = CreateOperation(CTNOT_UNARY_PLUS);
  558. }
  559. | '-'
  560. {
  561. $$ = CreateOperation(CTNOT_UNARY_MINUS);
  562. }
  563. | '~'
  564. {
  565. $$ = CreateOperation(CTNOT_UNARY_COMPLEMENT);
  566. }
  567. | '!'
  568. {
  569. $$ = CreateOperation(CTNOT_UNARY_NOT);
  570. }
  571. ;
  572. postfix_expression
  573. : primary_expression
  574. {
  575. $$ = $1;
  576. }
  577. | postfix_expression '[' constant_expression ']'
  578. {
  579. $$ = CreateOperation(CTNOT_ARRAY_INDEX, $1, $3);
  580. }
  581. | postfix_expression '(' ')'
  582. {
  583. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_CALL, gmlineno);
  584. $$->SetChild(0, $1);
  585. }
  586. | postfix_expression '(' argument_expression_list ')'
  587. {
  588. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_CALL, gmlineno);
  589. $$->SetChild(0, $1);
  590. $$->SetChild(1, $3);
  591. }
  592. | postfix_expression ':' identifier '(' ')'
  593. {
  594. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_CALL, gmlineno);
  595. $$->SetChild(0, $3);
  596. $$->SetChild(2, $1);
  597. }
  598. | postfix_expression ':' identifier '(' argument_expression_list ')'
  599. {
  600. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_CALL, gmlineno);
  601. $$->SetChild(0, $3);
  602. $$->SetChild(1, $5);
  603. $$->SetChild(2, $1);
  604. }
  605. | postfix_expression '.' identifier
  606. {
  607. $$ = CreateOperation(CTNOT_DOT, $1, $3);
  608. }
  609. ;
  610. argument_expression_list
  611. : constant_expression
  612. {
  613. $$ = $1;
  614. }
  615. | argument_expression_list ',' constant_expression
  616. {
  617. ATTACH($$, $1, $3);
  618. }
  619. ;
  620. table_constructor
  621. : KEYWORD_TABLE '(' ')'
  622. {
  623. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_TABLE, gmlineno);
  624. }
  625. | KEYWORD_TABLE '(' field_list ')'
  626. {
  627. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_TABLE, gmlineno);
  628. $$->SetChild(0, $3);
  629. }
  630. | '{' '}'
  631. {
  632. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_TABLE, gmlineno);
  633. }
  634. | '{' field_list '}'
  635. {
  636. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_TABLE, gmlineno);
  637. $$->SetChild(0, $2);
  638. }
  639. | '{' field_list ',' '}'
  640. {
  641. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_TABLE, gmlineno);
  642. $$->SetChild(0, $2);
  643. }
  644. ;
  645. function_constructor
  646. : KEYWORD_FUNCTION '(' parameter_list ')' compound_statement
  647. {
  648. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
  649. $$->SetChild(0, $3);
  650. $$->SetChild(1, $5);
  651. }
  652. | KEYWORD_FUNCTION '(' ')' compound_statement
  653. {
  654. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_FUNCTION, gmlineno);
  655. $$->SetChild(1, $4);
  656. }
  657. ;
  658. field_list
  659. : field
  660. {
  661. $$ = $1;
  662. }
  663. | field_list ',' field
  664. {
  665. ATTACH($$, $1, $3);
  666. }
  667. ;
  668. field
  669. : constant_expression
  670. {
  671. $$ = $1;
  672. }
  673. | identifier '=' constant_expression
  674. {
  675. $$ = CreateOperation(CTNOT_ASSIGN_FIELD, $1, $3);
  676. }
  677. ;
  678. parameter_list
  679. : parameter
  680. {
  681. $$ = $1;
  682. }
  683. | parameter_list ',' parameter
  684. {
  685. ATTACH($$, $1, $3);
  686. }
  687. ;
  688. parameter
  689. : identifier
  690. {
  691. $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_PARAMETER, gmlineno);
  692. $$->SetChild(0, $1);
  693. }
  694. | identifier '=' constant_expression
  695. {
  696. $$ = gmCodeTreeNode::Create(CTNT_DECLARATION, CTNDT_PARAMETER, gmlineno);
  697. $$->SetChild(0, $1);
  698. $$->SetChild(1, $3);
  699. }
  700. ;
  701. primary_expression
  702. : identifier
  703. {
  704. $$ = $1;
  705. }
  706. | '.' identifier
  707. {
  708. $$ = $2;
  709. $$->m_flags |= gmCodeTreeNode::CTN_MEMBER;
  710. }
  711. | KEYWORD_THIS
  712. {
  713. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_THIS, gmlineno);
  714. }
  715. | constant
  716. {
  717. $$ = $1;
  718. }
  719. | table_constructor
  720. {
  721. $$ = $1;
  722. }
  723. | function_constructor
  724. {
  725. $$ = $1;
  726. }
  727. | '(' constant_expression ')'
  728. {
  729. $$ = $2;
  730. }
  731. ;
  732. identifier
  733. : IDENTIFIER
  734. {
  735. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_IDENTIFIER, gmlineno);
  736. $$->m_data.m_string = (char *) gmCodeTree::Get().Alloc((int)strlen(gmtext) + 1);
  737. strcpy($$->m_data.m_string, gmtext);
  738. }
  739. ;
  740. constant
  741. : CONSTANT_HEX
  742. {
  743. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_CONSTANT, gmlineno, CTNCT_INT);
  744. $$->m_data.m_iValue = strtoul(gmtext + 2, NULL, 16);
  745. }
  746. | CONSTANT_BINARY
  747. {
  748. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_CONSTANT, gmlineno, CTNCT_INT);
  749. $$->m_data.m_iValue = strtoul(gmtext + 2, NULL, 2);
  750. }
  751. | CONSTANT_INT
  752. {
  753. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_CONSTANT, gmlineno, CTNCT_INT);
  754. $$->m_data.m_iValue = atoi(gmtext);
  755. }
  756. | KEYWORD_TRUE
  757. {
  758. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_CONSTANT, gmlineno, CTNCT_INT);
  759. $$->m_data.m_iValue = 1;
  760. }
  761. | KEYWORD_FALSE
  762. {
  763. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_CONSTANT, gmlineno, CTNCT_INT);
  764. $$->m_data.m_iValue = 0;
  765. }
  766. | CONSTANT_CHAR
  767. {
  768. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_CONSTANT, gmlineno, CTNCT_INT);
  769. char * c = (char *) gmCodeTree::Get().Alloc((int)strlen(gmtext) + 1);
  770. strcpy(c, gmtext);
  771. int result = 0;
  772. int shr = 0;
  773. while(*c)
  774. {
  775. if(c[0] == '\'')
  776. {
  777. ++c;
  778. continue;
  779. }
  780. else if(c[0] == '\\')
  781. {
  782. if(shr) result <<= 8;
  783. switch(c[1])
  784. {
  785. case 'a' : result |= (unsigned char) '\a'; break;
  786. case 'b' : result |= (unsigned char) '\b'; break;
  787. case 'f' : result |= (unsigned char) '\f'; break;
  788. case 'n' : result |= (unsigned char) '\n'; break;
  789. case 'r' : result |= (unsigned char) '\r'; break;
  790. case 't' : result |= (unsigned char) '\t'; break;
  791. case 'v' : result |= (unsigned char) '\v'; break;
  792. case '\'' : result |= (unsigned char) '\''; break;
  793. case '\"' : result |= (unsigned char) '\"'; break;
  794. case '\\' : result |= (unsigned char) '\\'; break;
  795. default: result |= (unsigned char) c[1];
  796. }
  797. ++shr;
  798. c += 2;
  799. continue;
  800. }
  801. if(shr) result <<= 8;
  802. result |= (unsigned char) *(c++);
  803. ++shr;
  804. }
  805. if(shr > 4 && gmCodeTree::Get().GetLog()) gmCodeTree::Get().GetLog()->LogEntry("truncated char, line %d", gmlineno);
  806. $$->m_data.m_iValue = result;
  807. }
  808. | CONSTANT_FLOAT
  809. {
  810. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_CONSTANT, gmlineno, CTNCT_FLOAT);
  811. $$->m_data.m_fValue = (float) atof(gmtext);
  812. }
  813. | constant_string_list
  814. {
  815. $$ = $1;
  816. }
  817. | KEYWORD_NULL
  818. {
  819. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_CONSTANT, gmlineno, CTNCT_NULL);
  820. $$->m_data.m_iValue = 0;
  821. }
  822. ;
  823. constant_string_list
  824. : CONSTANT_STRING
  825. {
  826. $$ = gmCodeTreeNode::Create(CTNT_EXPRESSION, CTNET_CONSTANT, gmlineno, CTNCT_STRING);
  827. $$->m_data.m_string = (char *) gmCodeTree::Get().Alloc((int)strlen(gmtext) + 1);
  828. strcpy($$->m_data.m_string, gmtext);
  829. if(gmtext[0] == '"')
  830. {
  831. gmProcessDoubleQuoteString($$->m_data.m_string);
  832. }
  833. else if(gmtext[0] == '`')
  834. {
  835. gmProcessSingleQuoteString($$->m_data.m_string);
  836. }
  837. }
  838. | constant_string_list CONSTANT_STRING
  839. {
  840. $$ = $1;
  841. int alen = (int)strlen($$->m_data.m_string);
  842. int blen = (int)strlen(gmtext);
  843. char * str = (char *) gmCodeTree::Get().Alloc(alen + blen + 1);
  844. if(str)
  845. {
  846. memcpy(str, $1->m_data.m_string, alen);
  847. memcpy(str + alen, gmtext, blen);
  848. str[alen + blen] = '\0';
  849. if(str[alen] == '"')
  850. {
  851. gmProcessDoubleQuoteString(str + alen);
  852. }
  853. else if(str[alen] == '`')
  854. {
  855. gmProcessSingleQuoteString(str + alen);
  856. }
  857. $$->m_data.m_string = str;
  858. }
  859. }
  860. ;
  861. %%
  862. #include <stdio.h>