1
0

finsh_parser.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /*
  2. * script parser for finsh shell.
  3. *
  4. * COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
  5. *
  6. * This file is part of RT-Thread (http://www.rt-thread.org)
  7. * Maintainer: bernard.xiong <bernard.xiong at gmail.com>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. *
  25. * Change Logs:
  26. * Date Author Notes
  27. * 2010-03-22 Bernard first version
  28. * 2013-10-09 Bernard fix the command line too long issue.
  29. */
  30. #include <finsh.h>
  31. #include "finsh_token.h"
  32. #include "finsh_node.h"
  33. #include "finsh_error.h"
  34. #include "finsh_parser.h"
  35. #include "finsh_var.h"
  36. /*
  37. * the structure of abstract syntax tree:
  38. * root____________
  39. * | \
  40. * child__ sibling__
  41. * | \ | \
  42. * child sibling child sibling
  43. * ...
  44. */
  45. static enum finsh_type proc_type(struct finsh_parser* self);
  46. static int proc_identifier(struct finsh_parser* self, char* id);
  47. static struct finsh_node* proc_variable_decl(struct finsh_parser* self);
  48. static struct finsh_node* proc_expr(struct finsh_parser* self);
  49. static struct finsh_node* proc_assign_expr(struct finsh_parser* self);
  50. static struct finsh_node* proc_inclusive_or_expr(struct finsh_parser* self);
  51. static struct finsh_node* proc_exclusive_or_expr(struct finsh_parser* self);
  52. static struct finsh_node* proc_and_expr(struct finsh_parser* self);
  53. static struct finsh_node* proc_shift_expr(struct finsh_parser* self);
  54. static struct finsh_node* proc_additive_expr(struct finsh_parser* self);
  55. static struct finsh_node* proc_multiplicative_expr(struct finsh_parser* self);
  56. static struct finsh_node* proc_cast_expr(struct finsh_parser* self);
  57. static struct finsh_node* proc_unary_expr(struct finsh_parser* self);
  58. static struct finsh_node* proc_postfix_expr(struct finsh_parser* self);
  59. static struct finsh_node* proc_primary_expr(struct finsh_parser* self);
  60. static struct finsh_node* proc_param_list(struct finsh_parser* self);
  61. static struct finsh_node* proc_expr_statement(struct finsh_parser* self);
  62. static struct finsh_node* make_sys_node(u_char type, struct finsh_node* node1,
  63. struct finsh_node* node2);
  64. /* check token */
  65. #define check_token(token, lex, type) if ( (token) != (type) ) \
  66. { \
  67. finsh_error_set(FINSH_ERROR_INVALID_TOKEN); \
  68. finsh_token_replay(lex); \
  69. }
  70. /* is the token a data type? */
  71. #define is_base_type(token) ((token) == finsh_token_type_void \
  72. || (token) == finsh_token_type_char \
  73. || (token) == finsh_token_type_short \
  74. || (token) == finsh_token_type_int \
  75. || (token) == finsh_token_type_long)
  76. /* get the next token */
  77. #define next_token(token, lex) (token) = finsh_token_token(lex)
  78. /* match a specified token */
  79. #define match_token(token, lex, type) next_token(token, lex); \
  80. check_token(token, lex, type)
  81. /*
  82. process for function and variable declaration.
  83. decl_variable -> type declaration_list ';'
  84. declarator_list -> declarator_list ',' declarator
  85. | declarator
  86. declarator -> identifier
  87. | identifier ASSIGN expr_assign
  88. */
  89. static struct finsh_node* proc_variable_decl(struct finsh_parser* self)
  90. {
  91. enum finsh_token_type token;
  92. enum finsh_type type;
  93. char id[FINSH_NAME_MAX + 1];
  94. struct finsh_node *node;
  95. struct finsh_node *end;
  96. struct finsh_node *assign;
  97. node = NULL;
  98. end = NULL;
  99. /* get type */
  100. type = proc_type(self);
  101. /*process id.*/
  102. if (proc_identifier(self, id) == 0)
  103. {
  104. /* if add variable failed */
  105. if (finsh_var_insert(id, type) < 0)
  106. {
  107. finsh_error_set(FINSH_ERROR_VARIABLE_EXIST);
  108. }
  109. }
  110. next_token(token, &(self->token));
  111. switch ( token )
  112. {
  113. case finsh_token_type_comma:/*',', it's a variable_list declaration.*/
  114. if (proc_identifier(self, id) == 0)
  115. {
  116. /* if add variable failed */
  117. if (finsh_var_insert(id, type) < 0)
  118. {
  119. finsh_error_set(FINSH_ERROR_VARIABLE_EXIST);
  120. }
  121. }
  122. next_token(token, &(self->token));
  123. if ( token == finsh_token_type_assign )
  124. {
  125. /* get the right side of assign expression */
  126. assign = proc_assign_expr(self);
  127. if (assign != NULL)
  128. {
  129. struct finsh_node* idnode;
  130. idnode = finsh_node_new_id(id);
  131. end = make_sys_node(FINSH_NODE_SYS_ASSIGN, idnode, assign);
  132. node = end;
  133. next_token(token, &(self->token));
  134. }
  135. }
  136. while ( token == finsh_token_type_comma )
  137. {
  138. if (proc_identifier(self, id) == 0)
  139. {
  140. /* if add variable failed */
  141. if (finsh_var_insert(id, type) < 0)
  142. {
  143. finsh_error_set(FINSH_ERROR_VARIABLE_EXIST);
  144. }
  145. }
  146. next_token(token, &(self->token));
  147. if ( token == finsh_token_type_assign )
  148. {
  149. /* get the right side of assign expression */
  150. assign = proc_assign_expr(self);
  151. if (assign != NULL)
  152. {
  153. struct finsh_node* idnode;
  154. idnode = finsh_node_new_id(id);
  155. /* make assign expression node */
  156. if (node != NULL)
  157. {
  158. finsh_node_sibling(end) = make_sys_node(FINSH_NODE_SYS_ASSIGN, idnode, assign);
  159. end = finsh_node_sibling(end);
  160. }
  161. else
  162. {
  163. end = make_sys_node(FINSH_NODE_SYS_ASSIGN, idnode, assign);
  164. node = end;
  165. }
  166. next_token(token, &(self->token));
  167. }
  168. }
  169. }
  170. check_token(token, &(self->token), finsh_token_type_semicolon);
  171. return node;
  172. case finsh_token_type_assign:/*'=', it's a variable with assign declaration.*/
  173. {
  174. struct finsh_node *idnode;
  175. assign = proc_assign_expr(self);
  176. if (assign != NULL)
  177. {
  178. idnode = finsh_node_new_id(id);
  179. /* make assign expression node */
  180. end = make_sys_node(FINSH_NODE_SYS_ASSIGN, idnode, assign);
  181. node = end;
  182. next_token(token, &(self->token));
  183. }
  184. while ( token == finsh_token_type_comma )
  185. {
  186. if (proc_identifier(self, id) == 0)
  187. {
  188. /* if add variable failed */
  189. if (finsh_var_insert(id, type) < 0)
  190. {
  191. finsh_error_set(FINSH_ERROR_VARIABLE_EXIST);
  192. }
  193. }
  194. next_token(token, &(self->token));
  195. if (token == finsh_token_type_assign)
  196. {
  197. /* get the right side of assign expression */
  198. assign = proc_assign_expr(self);
  199. if (assign != NULL)
  200. {
  201. idnode = finsh_node_new_id(id);
  202. /* make assign expression node */
  203. if (node != NULL)
  204. {
  205. finsh_node_sibling(end) = make_sys_node(FINSH_NODE_SYS_ASSIGN, idnode, assign);
  206. end = finsh_node_sibling(end);
  207. }
  208. else
  209. {
  210. end = make_sys_node(FINSH_NODE_SYS_ASSIGN, idnode, assign);
  211. node = end;
  212. }
  213. next_token(token, &(self->token));
  214. }
  215. }
  216. }
  217. check_token(token, &(self->token), finsh_token_type_semicolon);
  218. return node;
  219. }
  220. case finsh_token_type_semicolon:/*';', it's a variable declaration.*/
  221. return node;
  222. default:
  223. finsh_error_set(FINSH_ERROR_EXPECT_TYPE);
  224. return NULL;
  225. }
  226. }
  227. /*
  228. type -> type_prefix type_basic | type_basic
  229. type_prefix -> UNSIGNED
  230. type_basic -> VOID
  231. | CHAR
  232. | SHORT
  233. | INT
  234. | STRING
  235. */
  236. static enum finsh_type proc_type(struct finsh_parser* self)
  237. {
  238. enum finsh_type type;
  239. enum finsh_token_type token;
  240. /* set init type */
  241. type = finsh_type_unknown;
  242. next_token(token, &(self->token));
  243. if ( is_base_type(token) ) /* base_type */
  244. {
  245. switch (token)
  246. {
  247. case finsh_token_type_void:
  248. type = finsh_type_void;
  249. break;
  250. case finsh_token_type_char:
  251. type = finsh_type_char;
  252. break;
  253. case finsh_token_type_short:
  254. type = finsh_type_short;
  255. break;
  256. case finsh_token_type_int:
  257. type = finsh_type_int;
  258. break;
  259. case finsh_token_type_long:
  260. type = finsh_type_long;
  261. break;
  262. default:
  263. goto __return;
  264. }
  265. }
  266. else if ( token == finsh_token_type_unsigned ) /* unsigned base_type */
  267. {
  268. next_token(token, &(self->token));
  269. if ( is_base_type(token) )
  270. {
  271. switch (token)
  272. {
  273. case finsh_token_type_char:
  274. type = finsh_type_uchar;
  275. break;
  276. case finsh_token_type_short:
  277. type = finsh_type_ushort;
  278. break;
  279. case finsh_token_type_int:
  280. type = finsh_type_uint;
  281. break;
  282. case finsh_token_type_long:
  283. type = finsh_type_ulong;
  284. break;
  285. default:
  286. goto __return;
  287. }
  288. }
  289. else
  290. {
  291. finsh_token_replay(&(self->token));
  292. finsh_error_set(FINSH_ERROR_EXPECT_TYPE);
  293. }
  294. }
  295. else
  296. {
  297. goto __return;
  298. }
  299. /* parse for pointer */
  300. next_token(token, &(self->token));
  301. if (token == finsh_token_type_mul)
  302. {
  303. switch (type)
  304. {
  305. case finsh_type_void:
  306. type = finsh_type_voidp;
  307. break;
  308. case finsh_type_char:
  309. case finsh_type_uchar:
  310. type = finsh_type_charp;
  311. break;
  312. case finsh_type_short:
  313. case finsh_type_ushort:
  314. type = finsh_type_shortp;
  315. break;
  316. case finsh_type_int:
  317. case finsh_type_uint:
  318. type = finsh_type_intp;
  319. break;
  320. case finsh_type_long:
  321. case finsh_type_ulong:
  322. type = finsh_type_longp;
  323. break;
  324. default:
  325. type = finsh_type_voidp;
  326. break;
  327. }
  328. }
  329. else finsh_token_replay(&(self->token));
  330. return type;
  331. __return:
  332. finsh_token_replay(&(self->token));
  333. finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE);
  334. return type;
  335. }
  336. /*
  337. identifier -> IDENTIFIER
  338. */
  339. static int proc_identifier(struct finsh_parser* self, char* id)
  340. {
  341. enum finsh_token_type token;
  342. match_token(token, &(self->token), finsh_token_type_identifier);
  343. strncpy(id, (char*)self->token.string, FINSH_NAME_MAX);
  344. return 0;
  345. }
  346. /*
  347. statement_expr -> ';'
  348. | expr ';'
  349. */
  350. static struct finsh_node* proc_expr_statement(struct finsh_parser* self)
  351. {
  352. enum finsh_token_type token;
  353. struct finsh_node* expr;
  354. expr = NULL;
  355. next_token(token, &(self->token));
  356. if ( token != finsh_token_type_semicolon )
  357. {
  358. finsh_token_replay(&(self->token));
  359. expr = proc_expr(self);
  360. match_token(token, &(self->token), finsh_token_type_semicolon);
  361. }
  362. return expr;
  363. }
  364. /*
  365. expr -> expr_assign
  366. */
  367. static struct finsh_node* proc_expr(struct finsh_parser* self)
  368. {
  369. return proc_assign_expr(self);
  370. }
  371. /*
  372. expr_assign -> expr_inclusive_or
  373. | expr_unary ASSIGN expr_assign
  374. */
  375. static struct finsh_node* proc_assign_expr(struct finsh_parser* self)
  376. {
  377. enum finsh_token_type token;
  378. struct finsh_node* or;
  379. struct finsh_node* assign;
  380. or = proc_inclusive_or_expr(self);
  381. next_token(token, &(self->token));
  382. if (token == finsh_token_type_assign)
  383. {
  384. assign = proc_assign_expr(self);
  385. return make_sys_node(FINSH_NODE_SYS_ASSIGN, or, assign);
  386. }
  387. else finsh_token_replay(&(self->token));
  388. return or;
  389. }
  390. /*
  391. expr_inclusive_or -> expr_exclusive_or
  392. | expr_inclusive_or '|' expr_exclusive_or
  393. */
  394. static struct finsh_node* proc_inclusive_or_expr(struct finsh_parser* self)
  395. {
  396. enum finsh_token_type token;
  397. struct finsh_node* xor;
  398. struct finsh_node* xor_new;
  399. xor = proc_exclusive_or_expr(self);
  400. next_token(token, &(self->token));
  401. while ( token == finsh_token_type_or )
  402. {
  403. xor_new = proc_exclusive_or_expr(self);
  404. if (xor_new == NULL) finsh_error_set(FINSH_ERROR_EXPECT_OPERATOR);
  405. else xor = make_sys_node(FINSH_NODE_SYS_OR, xor, xor_new);
  406. next_token(token, &(self->token));
  407. }
  408. finsh_token_replay(&(self->token));
  409. return xor;
  410. }
  411. /*
  412. expr_exclusive_or -> expr_and
  413. | expr_exclusive '^' expr_and
  414. */
  415. static struct finsh_node* proc_exclusive_or_expr(struct finsh_parser* self)
  416. {
  417. enum finsh_token_type token;
  418. struct finsh_node* and;
  419. struct finsh_node* and_new;
  420. and = proc_and_expr(self);
  421. next_token(token, &(self->token));
  422. while ( token == finsh_token_type_xor )
  423. {
  424. and_new = proc_and_expr(self);
  425. if (and_new == NULL) finsh_error_set(FINSH_ERROR_EXPECT_OPERATOR);
  426. else and = make_sys_node(FINSH_NODE_SYS_XOR, and, and_new);
  427. next_token(token, &(self->token));
  428. }
  429. finsh_token_replay(&(self->token));
  430. return and;
  431. }
  432. /*
  433. expr_and -> expr_shift
  434. | expr_and '&' expr_shift
  435. */
  436. static struct finsh_node* proc_and_expr(struct finsh_parser* self)
  437. {
  438. enum finsh_token_type token;
  439. struct finsh_node* shift;
  440. struct finsh_node* shift_new;
  441. shift = proc_shift_expr(self);
  442. next_token(token, &(self->token));
  443. while ( token == finsh_token_type_and )
  444. {
  445. shift_new = proc_shift_expr(self);
  446. if (shift_new == NULL) finsh_error_set(FINSH_ERROR_EXPECT_OPERATOR);
  447. else shift = make_sys_node(FINSH_NODE_SYS_AND, shift, shift_new);
  448. next_token(token, &(self->token));
  449. }
  450. finsh_token_replay(&(self->token));
  451. return shift;
  452. }
  453. /*
  454. expr_shift -> expr_additive
  455. | expr_shift '<<' expr_additive
  456. | expr_shift '>>' expr_additive
  457. */
  458. static struct finsh_node* proc_shift_expr(struct finsh_parser* self)
  459. {
  460. enum finsh_token_type token;
  461. struct finsh_node* add;
  462. struct finsh_node* add_new;
  463. add = proc_additive_expr(self);
  464. next_token(token, &(self->token));
  465. while ( token == finsh_token_type_shl || token == finsh_token_type_shr)
  466. {
  467. add_new = proc_additive_expr(self);
  468. if (add_new == NULL) finsh_error_set(FINSH_ERROR_EXPECT_OPERATOR);
  469. else
  470. {
  471. switch (token)
  472. {
  473. case finsh_token_type_shl:
  474. add = make_sys_node(FINSH_NODE_SYS_SHL, add, add_new);
  475. break;
  476. case finsh_token_type_shr:
  477. add = make_sys_node(FINSH_NODE_SYS_SHR, add, add_new);
  478. break;
  479. default:
  480. finsh_error_set(FINSH_ERROR_EXPECT_OPERATOR);
  481. break;
  482. }
  483. }
  484. next_token(token, &(self->token));
  485. }
  486. finsh_token_replay(&(self->token));
  487. return add;
  488. }
  489. /*
  490. expr_additive -> expr_multiplicative
  491. | expr_additive SUB expr_multiplicative
  492. | expr_additive ADD expr_multiplicative
  493. */
  494. static struct finsh_node* proc_additive_expr(struct finsh_parser* self)
  495. {
  496. enum finsh_token_type token;
  497. struct finsh_node* mul;
  498. struct finsh_node* mul_new;
  499. mul = proc_multiplicative_expr(self);
  500. next_token(token, &(self->token));
  501. while ( token == finsh_token_type_sub || token == finsh_token_type_add )
  502. {
  503. mul_new = proc_multiplicative_expr(self);
  504. if (mul_new == NULL) finsh_error_set(FINSH_ERROR_EXPECT_OPERATOR);
  505. else
  506. {
  507. switch (token)
  508. {
  509. case finsh_token_type_sub:
  510. mul = make_sys_node(FINSH_NODE_SYS_SUB, mul, mul_new);
  511. break;
  512. case finsh_token_type_add:
  513. mul = make_sys_node(FINSH_NODE_SYS_ADD, mul, mul_new);
  514. break;
  515. default:
  516. finsh_error_set(FINSH_ERROR_EXPECT_OPERATOR);
  517. break;
  518. }
  519. }
  520. next_token(token, &(self->token));
  521. }
  522. finsh_token_replay(&(self->token));
  523. return mul;
  524. }
  525. /*
  526. expr_multiplicative -> expr_cast
  527. | expr_multiplicative '*' expr_cast
  528. | expr_multiplicative '/' expr_cast
  529. | expr_multiplicative '%' expr_cast
  530. */
  531. static struct finsh_node* proc_multiplicative_expr(struct finsh_parser* self)
  532. {
  533. enum finsh_token_type token;
  534. struct finsh_node* cast;
  535. struct finsh_node* cast_new;
  536. cast = proc_cast_expr(self);
  537. next_token(token, &(self->token));
  538. while (token == finsh_token_type_mul ||
  539. token == finsh_token_type_div ||
  540. token == finsh_token_type_mod )
  541. {
  542. cast_new = proc_cast_expr(self);
  543. if (cast_new == NULL) finsh_error_set(FINSH_ERROR_EXPECT_OPERATOR);
  544. else
  545. {
  546. switch (token)
  547. {
  548. case finsh_token_type_mul:
  549. cast = make_sys_node(FINSH_NODE_SYS_MUL, cast, cast_new);
  550. break;
  551. case finsh_token_type_div:
  552. cast = make_sys_node(FINSH_NODE_SYS_DIV, cast, cast_new);
  553. break;
  554. case finsh_token_type_mod:
  555. cast = make_sys_node(FINSH_NODE_SYS_MOD, cast, cast_new);
  556. break;
  557. default:
  558. finsh_error_set(FINSH_ERROR_EXPECT_OPERATOR);
  559. break;
  560. }
  561. }
  562. next_token(token, &(self->token));
  563. }
  564. finsh_token_replay(&(self->token));
  565. return cast;
  566. }
  567. /*
  568. 20060313, add recast parse
  569. expr_cast -> expr_unary
  570. | '(' type ')' expr_cast
  571. */
  572. static struct finsh_node* proc_cast_expr(struct finsh_parser* self)
  573. {
  574. enum finsh_token_type token;
  575. enum finsh_type type;
  576. struct finsh_node* cast;
  577. next_token(token, &(self->token));
  578. if (token == finsh_token_type_left_paren)
  579. {
  580. type = proc_type(self);
  581. match_token(token, &(self->token), finsh_token_type_right_paren);
  582. cast = proc_cast_expr(self);
  583. if (cast != NULL)
  584. {
  585. cast->data_type = type;
  586. return cast;
  587. }
  588. }
  589. finsh_token_replay(&(self->token));
  590. return proc_unary_expr(self);
  591. }
  592. /*
  593. 20050921, add '*' and '&'
  594. expr_unary -> expr_postfix
  595. | ADD expr_cast
  596. | INC expr_cast
  597. | SUB expr_cast
  598. | DEC expr_cast
  599. | '~' expr_cast
  600. | '*' expr_cast
  601. | '&' expr_cast
  602. */
  603. static struct finsh_node* proc_unary_expr(struct finsh_parser* self)
  604. {
  605. enum finsh_token_type token;
  606. struct finsh_node *cast;
  607. next_token(token, &(self->token));
  608. switch (token)
  609. {
  610. case finsh_token_type_add: /* + */
  611. cast = proc_cast_expr(self);
  612. return cast;
  613. case finsh_token_type_inc: /* ++ */
  614. cast = proc_cast_expr(self);
  615. return make_sys_node(FINSH_NODE_SYS_PREINC, cast, NULL);
  616. case finsh_token_type_sub: /* - */
  617. cast = proc_cast_expr(self);
  618. return make_sys_node(FINSH_NODE_SYS_SUB, finsh_node_new_long(0), cast);
  619. case finsh_token_type_dec: /* -- */
  620. cast = proc_cast_expr(self);
  621. return make_sys_node(FINSH_NODE_SYS_PREDEC, cast, NULL);
  622. case finsh_token_type_bitwise: /* ~ */
  623. cast = proc_cast_expr(self);
  624. return make_sys_node(FINSH_NODE_SYS_BITWISE, cast, NULL);
  625. case finsh_token_type_mul: /* * */
  626. cast = proc_cast_expr(self);
  627. return make_sys_node(FINSH_NODE_SYS_GETVALUE, cast, NULL);
  628. case finsh_token_type_and: /* & */
  629. cast = proc_cast_expr(self);
  630. return make_sys_node(FINSH_NODE_SYS_GETADDR, cast, NULL);
  631. default:
  632. finsh_token_replay(&(self->token));
  633. return proc_postfix_expr(self);
  634. }
  635. }
  636. /*
  637. expr_postfix -> expr_primary
  638. | expr_postfix INC
  639. | expr_postfix DEC
  640. | expr_postfix '(' param_list ')'
  641. */
  642. static struct finsh_node* proc_postfix_expr(struct finsh_parser* self)
  643. {
  644. enum finsh_token_type token;
  645. struct finsh_node* postfix;
  646. postfix = proc_primary_expr(self);
  647. next_token(token, &(self->token));
  648. while ( token == finsh_token_type_inc ||
  649. token == finsh_token_type_dec ||
  650. token == finsh_token_type_left_paren )
  651. {
  652. switch (token)
  653. {
  654. case finsh_token_type_inc :/* '++' */
  655. postfix = make_sys_node(FINSH_NODE_SYS_INC, postfix, NULL);
  656. break;
  657. case finsh_token_type_dec :/* '--' */
  658. postfix = make_sys_node(FINSH_NODE_SYS_DEC, postfix, NULL);
  659. break;
  660. case finsh_token_type_left_paren :/* '(' */
  661. {
  662. struct finsh_node* param_list;
  663. param_list = NULL;
  664. next_token(token, &(self->token));
  665. if (token != finsh_token_type_right_paren)
  666. {
  667. finsh_token_replay(&(self->token));
  668. param_list = proc_param_list(self);
  669. match_token(token, &(self->token), finsh_token_type_right_paren);
  670. }
  671. postfix = make_sys_node(FINSH_NODE_SYS_FUNC, postfix, param_list);
  672. }
  673. break;
  674. default:
  675. break;
  676. }
  677. next_token(token, &(self->token));
  678. }
  679. finsh_token_replay(&(self->token));
  680. return postfix;
  681. }
  682. /*
  683. expr_primary -> literal
  684. | '(' expr ')'
  685. | identifier
  686. */
  687. static struct finsh_node* proc_primary_expr(struct finsh_parser* self)
  688. {
  689. enum finsh_token_type token;
  690. struct finsh_node* expr;
  691. next_token(token, &(self->token));
  692. switch ( token )
  693. {
  694. case finsh_token_type_identifier:
  695. {
  696. char id[FINSH_NAME_MAX + 1];
  697. finsh_token_replay(&(self->token));
  698. proc_identifier(self, id);
  699. return finsh_node_new_id(id);
  700. }
  701. case finsh_token_type_left_paren:
  702. expr = proc_expr(self);
  703. match_token(token, &(self->token), finsh_token_type_right_paren);
  704. return expr;
  705. case finsh_token_type_value_int:
  706. return finsh_node_new_int(self->token.value.int_value);
  707. case finsh_token_type_value_long:
  708. return finsh_node_new_long(self->token.value.long_value);
  709. case finsh_token_type_value_char:
  710. return finsh_node_new_char(self->token.value.char_value);
  711. case finsh_token_type_value_string:
  712. return finsh_node_new_string((char*)self->token.string);
  713. case finsh_token_type_value_null:
  714. return finsh_node_new_ptr(NULL);
  715. default:
  716. finsh_error_set(FINSH_ERROR_INVALID_TOKEN);
  717. break;
  718. }
  719. return NULL;
  720. }
  721. /*
  722. param_list -> empty
  723. | expr_assign
  724. | param_list ',' expr_assign
  725. */
  726. static struct finsh_node* proc_param_list(struct finsh_parser* self)
  727. {
  728. enum finsh_token_type token;
  729. struct finsh_node *node, *assign;
  730. assign = proc_assign_expr(self);
  731. if (assign == NULL) return NULL;
  732. node = assign;
  733. next_token(token, &(self->token));
  734. while (token == finsh_token_type_comma )
  735. {
  736. finsh_node_sibling(assign) = proc_assign_expr(self);
  737. if (finsh_node_sibling(assign) != NULL) assign = finsh_node_sibling(assign);
  738. else finsh_error_set(FINSH_ERROR_EXPECT_OPERATOR);
  739. next_token(token, &(self->token));
  740. }
  741. finsh_token_replay(&(self->token));
  742. return node;
  743. }
  744. /*
  745. make a new node as following tree:
  746. new_node
  747. |
  748. node1__
  749. \
  750. node2
  751. */
  752. static struct finsh_node* make_sys_node(u_char type, struct finsh_node* node1, struct finsh_node* node2)
  753. {
  754. struct finsh_node* node;
  755. node = finsh_node_allocate(type);
  756. if ((node1 != NULL) && (node != NULL))
  757. {
  758. finsh_node_child(node) = node1;
  759. finsh_node_sibling(node1) = node2;
  760. }
  761. else finsh_error_set(FINSH_ERROR_NULL_NODE);
  762. return node;
  763. }
  764. /*
  765. start -> statement_expr | decl_variable
  766. */
  767. void finsh_parser_run(struct finsh_parser* self, const u_char* string)
  768. {
  769. enum finsh_token_type token;
  770. struct finsh_node *node;
  771. node = NULL;
  772. /* init parser */
  773. self->parser_string = (u_char*)string;
  774. /* init token */
  775. finsh_token_init(&(self->token), self->parser_string);
  776. /* get next token */
  777. next_token(token, &(self->token));
  778. while (token != finsh_token_type_eof && token != finsh_token_type_bad)
  779. {
  780. switch (token)
  781. {
  782. case finsh_token_type_identifier:
  783. /* process expr_statement */
  784. finsh_token_replay(&(self->token));
  785. if (self->root != NULL)
  786. {
  787. finsh_node_sibling(node) = proc_expr_statement(self);
  788. if (finsh_node_sibling(node) != NULL)
  789. node = finsh_node_sibling(node);
  790. }
  791. else
  792. {
  793. node = proc_expr_statement(self);
  794. self->root = node;
  795. }
  796. break;
  797. default:
  798. if (is_base_type(token) || token == finsh_token_type_unsigned)
  799. {
  800. /* variable decl */
  801. finsh_token_replay(&(self->token));
  802. if (self->root != NULL)
  803. {
  804. finsh_node_sibling(node) = proc_variable_decl(self);
  805. if (finsh_node_sibling(node) != NULL)
  806. node = finsh_node_sibling(node);
  807. }
  808. else
  809. {
  810. node = proc_variable_decl(self);
  811. self->root = node;
  812. }
  813. }
  814. else
  815. {
  816. /* process expr_statement */
  817. finsh_token_replay(&(self->token));
  818. if (self->root != NULL)
  819. {
  820. finsh_node_sibling(node) = proc_expr_statement(self);
  821. if (finsh_node_sibling(node) != NULL)
  822. node = finsh_node_sibling(node);
  823. else next_token(token, &(self->token));
  824. }
  825. else
  826. {
  827. node = proc_expr_statement(self);
  828. self->root = node;
  829. }
  830. }
  831. break;
  832. }
  833. /* no root found, break out */
  834. if (self->root == NULL) break;
  835. /* get next token */
  836. next_token(token, &(self->token));
  837. }
  838. }
  839. int finsh_parser_init(struct finsh_parser* self)
  840. {
  841. memset(self, 0, sizeof(struct finsh_parser));
  842. return 0;
  843. }