aboutsummaryrefslogtreecommitdiff
path: root/src/parser.c
blob: c32202e3a439a942d422ec6c5d7c67a85ddf348f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "include/token.h"
#include "include/util.h"
#include "include/tree.h"
#include "include/lexer.h"
#include "include/parser.h"

parser_t* parser_init(lexer_t* lexer) {
   parser_t* parser;

   parser = calloc(1, sizeof(struct PARSER_STRUC));
   parser->lexer = lexer;
   parser->token = lexer_get_next_token(lexer);

   return parser;
}

void parser_destroy(parser_t* parser) { free(parser); }

void parser_token_expect(parser_t* parser, int tokens, ...) {
   va_list ap;
   va_start(ap, tokens);
   for (int i = 0; i < tokens; ++i) {
      va_arg(ap, int) == parser->token->type?
         parser->token = lexer_get_next_token(parser->lexer):
         die(
            "unexpected token\n\ttype: [%s]\n\tvalue: [%s]", 
            token_get_type(parser->token->type),
            parser->token->value
         );
   }
   va_end(ap);
}

tree_t* parser_parse(parser_t* parser) { return parser_parse_exprs(parser); }

tree_t* parser_parse_expr(parser_t* parser) {
   switch (parser->token->type) {
      case TOKEN_DEF_TAG:
         return parser_parse_def(parser);
         break;
      case TOKEN_KEYWORD:
         return parser_parse_call(parser);
         break;
      default:
         die("BAD TOKEN"); /* TODO: make more informative error msgs */
   }

   return NULL; /* unreachable */
                /* hopefully */
}

tree_t* parser_parse_exprs(parser_t* parser) {
   tree_t* comp;
   tree_t* statement;

   comp = tree_init(TREE_COMP);
   comp->data.comp.value = calloc(1, sizeof(struct TREE_STRUC*));

   statement = parser_parse_expr(parser);
   comp->data.comp.value[0] = statement;
   while (parser->token->type == TOKEN_STMNT_END) {
      tree_t* statement;

      parser_token_expect(parser, TOKEN_STMNT_END);

      statement = parser_parse_expr(parser);
      comp->data.comp.size ++;
      comp->data.comp.value = realloc(
         comp->data.comp.value,
         comp->data.comp.size * sizeof(struct TREE_STRUC)
      );
      comp->data.comp.value[comp->data.comp.size - 1] = statement;
   }

   return comp;
}

tree_t* parser_parse_blocks(parser_t* parser) {

}

tree_t* parser_parse_block(parser_t* parser) {

}

tree_t* parser_parse_def(parser_t* parser) {
   tree_t* def;

   def->type = TREE_DEF;

   while (parser->token->type == TOKEN_DEF_TAG) {
      def->data.def.tags_size ++;
      def->data.def.tags = realloc(
         def->data.def.args,
         def->data.def.args_size + sizeof(parser->token->value)
      );
   } 

}

tree_t* parser_parse_call(parser_t* parser) {
}

tree_t* parser_parse_type_str(parser_t* parser) {
   tree_t* tree;
   
   tree->type = TREE_TYPE_STR;
   tree->data.type_str.value = parser->token->value;

   return tree;
}

tree_t* parser_parse_type_int(parser_t* parser) {
   tree_t* tree;

   tree->type = TREE_TYPE_INT;
   tree->data.type_int.value = strtol(parser->token->value, NULL, 10);

   return tree;
}