Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <vector>
- #include <string>
- #include <gmp.h>
- #include <mpfr.h>
- #include "tokens.hpp"
- //------------------------------------------------------------------------------
- // token_t
- //------------------------------------------------------------------------------
- union value_t {
- char *xstr;
- mpz_t xint;
- mpfr_t xfloat;
- };
- void init_value(value_t &v, const char *s, int type);
- void free_value(value_t &v, int type);
- void copy_value(value_t &v, const value_t *r, int type);
- struct source_loc_t {
- unsigned int line;
- unsigned int col;
- };
- struct token_t {
- int type;
- value_t val;
- source_loc_t pos;
- token_t(const char *s, int type, unsigned int line, unsigned int col);
- token_t(const token_t &r);
- ~token_t();
- private:
- token_t &operator=(const token_t &r); // copy protection
- };
- //------------------------------------------------------------------------------
- // lexer_parser_t
- //------------------------------------------------------------------------------
- struct lexer_parser_t {
- char *linestart;
- unsigned int line;
- std::string interp;
- void *parser;
- lexer_parser_t();
- ~lexer_parser_t();
- void append_hex(const char *p, size_t len);
- void append_oct(const char *p);
- void append_escaped(const char c);
- void tok_int(char *beg, char *end);
- void tok_float(char *beg, char *end);
- void tok_string(int type);
- void tok_op(int optype);
- void tok_send(token_t *t);
- void parse(std::vector<char> &buf);
- };
- //------------------------------------------------------------------------------
- // node_t
- //------------------------------------------------------------------------------
- struct node_t {
- enum node_type_t {
- PROGRAM,
- EXPR_STMT,
- ASSIGN_STMT,
- BINARY_EXPR,
- UNARY_EXPR,
- BASIC_LIT_EXPR,
- IDENT_EXPR,
- SELECTOR_EXPR,
- INDEX_EXPR,
- CALL_EXPR,
- };
- node_type_t type;
- node_t(node_type_t t);
- virtual ~node_t();
- virtual std::string to_string();
- };
- void print_ast(node_t *n);
- typedef std::vector<node_t*> node_vector_t;
- //------------------------------------------------------------------------------
- // program_t
- //------------------------------------------------------------------------------
- struct program_t : node_t {
- node_vector_t statements;
- program_t(node_vector_t *stmts);
- ~program_t();
- std::string to_string();
- };
- //------------------------------------------------------------------------------
- // expr_stmt_t
- //------------------------------------------------------------------------------
- struct expr_stmt_t : node_t {
- node_t *e;
- expr_stmt_t(node_t *e);
- ~expr_stmt_t();
- std::string to_string();
- };
- //------------------------------------------------------------------------------
- // assign_stmt_t
- //------------------------------------------------------------------------------
- struct assign_stmt_t : node_t {
- int tok;
- node_vector_t lhs;
- node_vector_t rhs;
- source_loc_t pos;
- assign_stmt_t(node_vector_t *lhs, node_vector_t *rhs, token_t *t);
- ~assign_stmt_t();
- std::string to_string();
- };
- //------------------------------------------------------------------------------
- // binary_expr_t
- //------------------------------------------------------------------------------
- struct binary_expr_t : node_t {
- int tok;
- node_t *lhs;
- node_t *rhs;
- source_loc_t pos;
- binary_expr_t(node_t *l, node_t *r, token_t *t);
- ~binary_expr_t();
- std::string to_string();
- };
- //------------------------------------------------------------------------------
- // unary_expr_t
- //------------------------------------------------------------------------------
- struct unary_expr_t : node_t {
- int tok;
- node_t *e;
- source_loc_t pos;
- unary_expr_t(node_t *e, token_t *t);
- ~unary_expr_t();
- std::string to_string();
- };
- //------------------------------------------------------------------------------
- // basic_lit_expr_t
- //------------------------------------------------------------------------------
- struct basic_lit_expr_t : node_t {
- int tok;
- value_t val;
- source_loc_t pos;
- basic_lit_expr_t(token_t *t);
- ~basic_lit_expr_t();
- std::string to_string();
- };
- //------------------------------------------------------------------------------
- // ident_expr_t
- //------------------------------------------------------------------------------
- struct ident_expr_t : node_t {
- std::string val;
- source_loc_t pos;
- ident_expr_t(token_t *t);
- std::string to_string();
- };
- //------------------------------------------------------------------------------
- // selector_expr_t
- //------------------------------------------------------------------------------
- struct selector_expr_t : node_t {
- node_t *e;
- std::string sel;
- source_loc_t pos;
- selector_expr_t(node_t *e, token_t *ident);
- ~selector_expr_t();
- std::string to_string();
- };
- //------------------------------------------------------------------------------
- // index_expr_t
- //------------------------------------------------------------------------------
- struct index_expr_t : node_t {
- node_t *e;
- node_t *i;
- source_loc_t lsb_pos;
- source_loc_t rsb_pos;
- index_expr_t(node_t *e, node_t *i, token_t *l, token_t *r);
- ~index_expr_t();
- std::string to_string();
- };
- //------------------------------------------------------------------------------
- // call_expr_t
- //------------------------------------------------------------------------------
- struct call_expr_t : node_t {
- node_t *e;
- node_vector_t args;
- source_loc_t lp_pos;
- source_loc_t rp_pos;
- call_expr_t(node_t *e, node_vector_t *args, token_t *l, token_t *r);
- ~call_expr_t();
- std::string to_string();
- };
Add Comment
Please, Sign In to add comment