Guest User

Untitled

a guest
Feb 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <vector>
  4. #include <string>
  5. #include <gmp.h>
  6. #include <mpfr.h>
  7. #include "tokens.hpp"
  8.  
  9. //------------------------------------------------------------------------------
  10. // token_t
  11. //------------------------------------------------------------------------------
  12.  
  13. union value_t {
  14. char *xstr;
  15. mpz_t xint;
  16. mpfr_t xfloat;
  17. };
  18.  
  19. void init_value(value_t &v, const char *s, int type);
  20. void free_value(value_t &v, int type);
  21. void copy_value(value_t &v, const value_t *r, int type);
  22.  
  23. struct source_loc_t {
  24. unsigned int line;
  25. unsigned int col;
  26. };
  27.  
  28. struct token_t {
  29. int type;
  30. value_t val;
  31. source_loc_t pos;
  32.  
  33. token_t(const char *s, int type, unsigned int line, unsigned int col);
  34. token_t(const token_t &r);
  35. ~token_t();
  36.  
  37. private:
  38. token_t &operator=(const token_t &r); // copy protection
  39. };
  40.  
  41. //------------------------------------------------------------------------------
  42. // lexer_parser_t
  43. //------------------------------------------------------------------------------
  44.  
  45. struct lexer_parser_t {
  46. char *linestart;
  47. unsigned int line;
  48. std::string interp;
  49. void *parser;
  50.  
  51. lexer_parser_t();
  52. ~lexer_parser_t();
  53.  
  54. void append_hex(const char *p, size_t len);
  55. void append_oct(const char *p);
  56. void append_escaped(const char c);
  57.  
  58. void tok_int(char *beg, char *end);
  59. void tok_float(char *beg, char *end);
  60. void tok_string(int type);
  61. void tok_op(int optype);
  62. void tok_send(token_t *t);
  63.  
  64. void parse(std::vector<char> &buf);
  65. };
  66.  
  67. //------------------------------------------------------------------------------
  68. // node_t
  69. //------------------------------------------------------------------------------
  70.  
  71. struct node_t {
  72. enum node_type_t {
  73. PROGRAM,
  74. EXPR_STMT,
  75. ASSIGN_STMT,
  76.  
  77. BINARY_EXPR,
  78. UNARY_EXPR,
  79. BASIC_LIT_EXPR,
  80. IDENT_EXPR,
  81. SELECTOR_EXPR,
  82. INDEX_EXPR,
  83. CALL_EXPR,
  84. };
  85.  
  86. node_type_t type;
  87.  
  88. node_t(node_type_t t);
  89. virtual ~node_t();
  90. virtual std::string to_string();
  91. };
  92.  
  93. void print_ast(node_t *n);
  94.  
  95. typedef std::vector<node_t*> node_vector_t;
  96.  
  97. //------------------------------------------------------------------------------
  98. // program_t
  99. //------------------------------------------------------------------------------
  100. struct program_t : node_t {
  101. node_vector_t statements;
  102.  
  103. program_t(node_vector_t *stmts);
  104. ~program_t();
  105. std::string to_string();
  106. };
  107.  
  108. //------------------------------------------------------------------------------
  109. // expr_stmt_t
  110. //------------------------------------------------------------------------------
  111. struct expr_stmt_t : node_t {
  112. node_t *e;
  113.  
  114. expr_stmt_t(node_t *e);
  115. ~expr_stmt_t();
  116. std::string to_string();
  117. };
  118.  
  119. //------------------------------------------------------------------------------
  120. // assign_stmt_t
  121. //------------------------------------------------------------------------------
  122. struct assign_stmt_t : node_t {
  123. int tok;
  124. node_vector_t lhs;
  125. node_vector_t rhs;
  126. source_loc_t pos;
  127.  
  128. assign_stmt_t(node_vector_t *lhs, node_vector_t *rhs, token_t *t);
  129. ~assign_stmt_t();
  130. std::string to_string();
  131. };
  132.  
  133. //------------------------------------------------------------------------------
  134. // binary_expr_t
  135. //------------------------------------------------------------------------------
  136.  
  137. struct binary_expr_t : node_t {
  138. int tok;
  139. node_t *lhs;
  140. node_t *rhs;
  141. source_loc_t pos;
  142.  
  143. binary_expr_t(node_t *l, node_t *r, token_t *t);
  144. ~binary_expr_t();
  145. std::string to_string();
  146. };
  147.  
  148. //------------------------------------------------------------------------------
  149. // unary_expr_t
  150. //------------------------------------------------------------------------------
  151.  
  152. struct unary_expr_t : node_t {
  153. int tok;
  154. node_t *e;
  155. source_loc_t pos;
  156.  
  157. unary_expr_t(node_t *e, token_t *t);
  158. ~unary_expr_t();
  159. std::string to_string();
  160. };
  161.  
  162. //------------------------------------------------------------------------------
  163. // basic_lit_expr_t
  164. //------------------------------------------------------------------------------
  165.  
  166. struct basic_lit_expr_t : node_t {
  167. int tok;
  168. value_t val;
  169. source_loc_t pos;
  170.  
  171. basic_lit_expr_t(token_t *t);
  172. ~basic_lit_expr_t();
  173. std::string to_string();
  174. };
  175.  
  176. //------------------------------------------------------------------------------
  177. // ident_expr_t
  178. //------------------------------------------------------------------------------
  179.  
  180. struct ident_expr_t : node_t {
  181. std::string val;
  182. source_loc_t pos;
  183.  
  184. ident_expr_t(token_t *t);
  185. std::string to_string();
  186. };
  187.  
  188. //------------------------------------------------------------------------------
  189. // selector_expr_t
  190. //------------------------------------------------------------------------------
  191.  
  192. struct selector_expr_t : node_t {
  193. node_t *e;
  194. std::string sel;
  195. source_loc_t pos;
  196.  
  197. selector_expr_t(node_t *e, token_t *ident);
  198. ~selector_expr_t();
  199. std::string to_string();
  200. };
  201.  
  202. //------------------------------------------------------------------------------
  203. // index_expr_t
  204. //------------------------------------------------------------------------------
  205.  
  206. struct index_expr_t : node_t {
  207. node_t *e;
  208. node_t *i;
  209. source_loc_t lsb_pos;
  210. source_loc_t rsb_pos;
  211.  
  212. index_expr_t(node_t *e, node_t *i, token_t *l, token_t *r);
  213. ~index_expr_t();
  214. std::string to_string();
  215. };
  216.  
  217. //------------------------------------------------------------------------------
  218. // call_expr_t
  219. //------------------------------------------------------------------------------
  220.  
  221. struct call_expr_t : node_t {
  222. node_t *e;
  223. node_vector_t args;
  224. source_loc_t lp_pos;
  225. source_loc_t rp_pos;
  226.  
  227. call_expr_t(node_t *e, node_vector_t *args, token_t *l, token_t *r);
  228. ~call_expr_t();
  229. std::string to_string();
  230. };
Add Comment
Please, Sign In to add comment