finsh_parser.c 21 KB

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