finsh_parser.c 22 KB

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