finsh_parser.c 26 KB

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