Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.79 KB | None | 0 0
  1. #include "includes/parse.h"
  2. #include "includes/token.h"
  3. #include "includes/ast_node_binop.h"
  4. #include "includes/ast_node_unaryop.h"
  5. #include "includes/ast_node_number.h"
  6. #include "includes/Vector.h"
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9.  
  10.  
  11. parse_state* parse_init(lex_state* lex) {
  12.     parse_state* state;
  13.     state = malloc(sizeof(struct PARSE_STATE_STRUCT));
  14.     state->lex = lex;
  15.     state->current_token = lex_get_next_token(state->lex);
  16.  
  17.     return state;
  18. };
  19.  
  20. void parse_eat(parse_state* state, int token_type) {
  21.     if (state->current_token->type == token_type) {
  22.         state->current_token = lex_get_next_token(state->lex);
  23.     } else {
  24.         printf("Unexpected token_type: `%d`", token_type);
  25.         // TODO: error
  26.     }
  27. };
  28.  
  29. ast_node* parse_factor(parse_state* state) {
  30.     token* tok = state->current_token;
  31.  
  32.     if (tok->type == _OP_PLUS) {
  33.         parse_eat(state, _OP_PLUS);
  34.         ast_node_unaryop* node = init_ast_node_unaryop(tok, parse_factor(state));
  35.         // invalid pointer type
  36.         return (ast_node*) node;
  37.  
  38.     } else if (tok->type == _OP_SUBTRACT) {
  39.         parse_eat(state, _OP_SUBTRACT);
  40.         ast_node_unaryop* node = init_ast_node_unaryop(tok, parse_factor(state));
  41.         // invalid pointer type
  42.         return (ast_node*) node;
  43.  
  44.     } else if (tok->type == _NOT_EQUALS) {
  45.         parse_eat(state, _NOT_EQUALS);
  46.         ast_node_unaryop* node = init_ast_node_unaryop(tok, parse_factor(state));
  47.         // invalid pointer type
  48.         return (ast_node*) node;
  49.  
  50.     } else if (tok->type == _TYPE_NUMBER) {
  51.         parse_eat(state, _TYPE_NUMBER);
  52.         ast_node_number* node = init_ast_node_number(tok);
  53.         // invalid pointer type
  54.         return (ast_node*) node;
  55.  
  56.     }/* else {
  57.         ast_node* node = parse_variable(state);
  58.         return node;
  59.         }*/
  60.  
  61.     return (ast_node*) parse_expr(state);
  62. }
  63.  
  64. ast_node* parse_term(parse_state* state) {
  65.     token* tok = (void*)0;
  66.  
  67.     ast_node* node = parse_factor(state);
  68.  
  69.     while (
  70.             state->current_token->type == _OP_MULTIPLY ||
  71.             state->current_token->type == _OP_DIVIDE
  72.           ) {
  73.         tok = state->current_token;
  74.  
  75.         if (tok->type == _OP_MULTIPLY) {
  76.             parse_eat(state, _OP_MULTIPLY);
  77.         } else if (tok->type == _OP_DIVIDE) {
  78.             parse_eat(state, _OP_DIVIDE);
  79.         }
  80.  
  81.         // this probably wont work since `node` is of pointer type ast_node*
  82.         node = (ast_node*) init_ast_node_binop(tok, node, parse_factor(state));
  83.     }
  84.  
  85.     return (ast_node*) node;
  86. }
  87.  
  88. ast_node* parse_expr(parse_state* state) {
  89.     token* tok = (void*)0;
  90.  
  91.     // invalid pointer type
  92.     ast_node* node = parse_term(state);
  93.  
  94.     while(
  95.             state->current_token->type == _OP_PLUS ||
  96.             state->current_token->type == _OP_SUBTRACT ||
  97.             state->current_token->type == _NOT_EQUALS ||
  98.             state->current_token->type == _LESS_THAN ||
  99.             state->current_token->type == _LARGER_THAN ||
  100.             state->current_token->type == _LESS_OR_EQUALS ||
  101.             state->current_token->type == _LARGER_OR_EQUALS ||
  102.             state->current_token->type == _EQUALS ||
  103.             state->current_token->type == _DOT
  104.          ) {
  105.         tok = state->current_token;
  106.  
  107.         if (tok->type == _OP_PLUS) {
  108.             parse_eat(state, _OP_PLUS);
  109.         } else if (tok->type == _OP_SUBTRACT) {
  110.             parse_eat(state, _OP_SUBTRACT);
  111.         } else if (tok->type == _NOT_EQUALS) {
  112.             parse_eat(state, _NOT_EQUALS);
  113.         } else if (tok->type == _LESS_THAN) {
  114.             parse_eat(state, _LESS_THAN);
  115.         } else if (tok->type == _LARGER_THAN) {
  116.             parse_eat(state, _LARGER_THAN);
  117.         } else if (tok->type == _LESS_OR_EQUALS) {
  118.             parse_eat(state, _LESS_OR_EQUALS);
  119.         } else if (tok->type == _LARGER_OR_EQUALS) {
  120.             parse_eat(state, _LARGER_OR_EQUALS);
  121.         } else if (tok->type == _EQUALS) {
  122.             parse_eat(state, _EQUALS);
  123.         } else if (tok->type == _DOT) {
  124.             parse_eat(state, _DOT);
  125.         }
  126.  
  127.         // invalid pointer type
  128.         node = (ast_node*) init_ast_node_binop(tok, node, parse_term(state));
  129.     };
  130.  
  131.     return node;
  132. }
  133.  
  134. ast_node* parse_statement(parse_state* state) {
  135.     // we only know about expressions as of now
  136.     return parse_expr(state);
  137. };
  138.  
  139. Vector parse_statement_list(parse_state* state) {
  140.     Vector nodes;
  141.     vector_init(&nodes);
  142.  
  143.     ast_node* node = parse_statement(state);
  144.  
  145.     vector_append(&nodes, node);
  146.  
  147.     while (state->current_token->type == _SEMI) {
  148.         parse_eat(state, _SEMI);
  149.         vector_append(&nodes, parse_statement(state));
  150.     }
  151.  
  152.     return nodes;
  153. };
  154.  
  155. ast_node* parse_parse(parse_state* state) {
  156.     parse_statement_list(state);
  157.  
  158.     return (void*)0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement