Advertisement
Camellias_

c is a fuckin mess

Oct 5th, 2019
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.72 KB | None | 0 0
  1. #include "include/parser.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. parser_T* init_parser(lexer_T* lexer)
  6. {
  7.     parser_T* parser = calloc(1, sizeof(struct PARSER_STRUCT));
  8.     parser->lexer = lexer;
  9.     parser->current_token = lexer_get_next_token(lexer);
  10.     parser->previous_token = parser->current_token;
  11.  
  12.     return parser;
  13. }
  14.  
  15. void parser_eat(parser_T* parser, int token_type)
  16. {
  17.     if(parser-> current_token = token_type)
  18.     {
  19.         parser->previous_token = parser->current_token;
  20.         parser->current_token = lexer_get_next_token(parser->lexer);
  21.     }
  22.     else
  23.     {
  24.         printf(
  25.             "Execpected token `%s`, with type %d",
  26.             parser->current_token->value,
  27.             parser->current_token->type
  28.         );
  29.  
  30.         exit(1);
  31.     }
  32.    
  33. }
  34.  
  35. AST_T* parser_parse(parser_T* parser)
  36. {
  37.     parser_parse_statements(parser);
  38. }
  39.  
  40. AST_T* parser_parse_statement(parser_T* parser)
  41. {
  42.     switch(parser->current_token->type)
  43.     {
  44.         case TOKEN_ID: return parser_parse_id(parser);
  45.     }
  46. }
  47.  
  48. AST_T* parser_parse_statements(parser_T* parser)
  49. {
  50.     AST_T* compound = init_ast(AST_COMPOUND);
  51.     compound->compound_value = calloc(1, sizeof(struct AST_STRUCT*));
  52.  
  53.     AST_T* ast_statement = parser_parse_statement(parser);
  54.     compound->compound_value[0] = ast_statement;
  55.     compound->compound_size += 1;
  56.  
  57.     while(parser->current_token->type == TOKEN_SEMI)
  58.     {
  59.         parser_eat(parser, TOKEN_SEMI);
  60.  
  61.         AST_T* ast_statement = parser_parse_statement(parser);
  62.         compound->compound_size += 1;
  63.         compound->compound_value = realloc
  64.         (
  65.             compound->compound_value,
  66.             compound->compound_size * sizeof(struct AST_STRUCT)
  67.         );
  68.         compound->compound_value[compound->compound_size - 1] = ast_statement;
  69.     }
  70.  
  71.     return compound;
  72. }
  73.  
  74. AST_T* parser_parse_expression(parser_T* parser)
  75. {
  76.     switch (parser->current_token->type)
  77.     {
  78.         case TOKEN_STRING: return parser_parse_string(parser);
  79.         case TOKEN_ID: return parser_parse_id(parser);
  80.     }
  81. }
  82.  
  83. AST_T* parser_parse_factor(parser_T* parser)
  84. {
  85. }
  86.  
  87. AST_T* parser_parse_term(parser_T* parser)
  88. {
  89. }
  90.  
  91. AST_T* parser_parse_function_call(parser_T* parser)
  92. {
  93.     AST_T* function_call = init_ast(AST_FUNCTION_CALL);
  94.     parser_eat(parser, TOKEN_LPAREN);
  95.  
  96.     function_call->function_call_name = parser->previous_token->value;
  97.     function_call->function_call_arguments = calloc(1, sizeof(struct AST_STRUCT*));
  98.  
  99.     AST_T* ast_expression = parser_parse_expression(parser);
  100.     function_call->function_call_arguments[0] = ast_expression;
  101.  
  102.     while(parser->current_token->type == TOKEN_COMMA)
  103.     {
  104.         parser_eat(parser, TOKEN_SEMI);
  105.  
  106.         AST_T* ast_expression = parser_parse_expression(parser);
  107.         function_call->function_call_arguments_size += 1;
  108.         function_call->function_call_arguments = realloc
  109.         (
  110.             function_call->function_call_arguments,
  111.             function_call->function_call_arguments_size * sizeof(struct AST_STRUCT)
  112.         );
  113.         function_call->function_call_arguments[function_call->function_call_arguments_size - 1] = ast_expression;
  114.     }
  115.  
  116.     parser_eat(parser, TOKEN_RPAREN);
  117.  
  118.     return function_call;
  119. }
  120.  
  121. AST_T* parser_parse_variable(parser_T* parser)
  122. {
  123.     char* token_value = parser->current_token->value;
  124.     parser_eat(parser, TOKEN_ID); //var name or function call
  125.  
  126.     if(parser->current_token->type == TOKEN_LPAREN)
  127.     {
  128.         return parser_parse_function_call(parser);
  129.     }
  130.    
  131.     AST_T* ast_variable = init_ast(AST_VARIABLE);
  132.     ast_variable->variable_name = token_value;
  133.  
  134.     return ast_variable;
  135. }
  136.  
  137. AST_T* parser_parse_variable_definition(parser_T* parser)
  138. {
  139.     parser_eat(parser, TOKEN_ID); //var
  140.     char* variable_definition_variable_name = parser->current_token->value;
  141.     parser_eat(parser, TOKEN_ID); //var name
  142.     parser_eat(parser, TOKEN_EQUALS);
  143.     AST_T* variable_definition_value = parser_parse_expression(parser);
  144.  
  145.     AST_T* variable_definition = init_ast(AST_VARIABLE_DEFINITION);
  146.     variable_definition->variable_definition_variable_name = variable_definition_variable_name;
  147.     variable_definition->variable_definition_value = variable_definition_value;
  148.  
  149.     return variable_definition;
  150. }
  151.  
  152. AST_T* parser_parse_string(parser_T* parser)
  153. {
  154.     AST_T* ast_string = init_ast(AST_STRING);
  155.     ast_string->string_value = parser->current_token->value;
  156.  
  157.     parser_eat(parser, TOKEN_STRING);
  158.  
  159.     return ast_string;
  160. }
  161.  
  162. AST_T* parser_parse_id(parser_T* parser)
  163. {
  164.     if(strcmp(parser->current_token->value, "var") == 0)
  165.     {
  166.         return parser_parse_variable_definition(parser);
  167.     }
  168.     else
  169.     {
  170.         return parser_parse_variable(parser);
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement