summaryrefslogtreecommitdiff
path: root/src/parser.c
blob: ba9788a01fdfd9c5d16bc895e75aa9343512a95f (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "./include/parser.h"
#include "./include/hash_table.h"
#include "./include/lexer.h"
#include "./include/macros.h"
#include "./include/token.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

parser_t *init_parser(lexer_t *lexer) {
  parser_t *p = (parser_t *)malloc(sizeof(parser_t));
  if (p == NULL)
    die("malloc on parser");

  p->i = 0;
  p->tokens = malloc(sizeof(token_t *));
  p->symbol_table = init_hash_table(100);
  p->finished = false;
  if (p->tokens == NULL)
    die("malloc on p->tokens");

  int size = 1;
  token_t *t = lexer_collect_next(lexer);
  p->tokens[size - 1] = t;

  while (true) {
    t = lexer_collect_next(lexer);
    size++;
    p->tokens = realloc(p->tokens, size * sizeof(token_t *));
    p->tokens[size - 1] = t;
    if (t == NULL)
      break;
  }

  p->size = size;
  return p;
}

void parser_move(parser_t *parser) {
  if (parser->i != parser->size - 1)
    parser->i++;
  else
    parser->finished = true;
}

void parser_eat(parser_t *parser, int type) {
  parser_move(parser);
  if (parser->tokens[parser->i] == NULL)
    printf("weird.\n");
  if (parser->tokens[parser->i]->type != type)
    parser_error(parser);
}

ast_t *parse_bool(parser_t *parser) {
  token_t *t = parser->tokens[parser->i];
  parser_move(parser);
  if (strcmp("T", t->value) == 0)
    return init_ast_bool(true);
  else
    return init_ast_bool(false);
}

static char *escape_string(char *str) { return str; }

ast_t *parse_string(parser_t *parser) {
  char *str = parser->tokens[parser->i]->value;
  parser_move(parser);
  return init_ast_string(escape_string(str));
}

ast_t *parse_int(parser_t *parser) {
  int ret = atoi(parser->tokens[parser->i]->value);
  parser_move(parser);
  return init_ast_int(ret);
}

ast_t *parse_float(parser_t *parser) {
  double ret = atof(parser->tokens[parser->i]->value);
  parser_move(parser);
  return init_ast_float(ret);
}

ast_t *parse_symbol(parser_t *parser) {
  char *str = parser->tokens[parser->i]->value;
  parser_move(parser);
  return init_ast_symbol(str);
}

ast_t *parse_function_args(parser_t *parser) {
  ast_t *car;
  ast_t *head = init_ast_pair(NULL, NULL);
  ast_t *cur = head;
  parser_move(parser);

  token_t *current_token = parser->tokens[parser->i];
  while (current_token->type != TOKEN_RPAREN) {
    if (current_token->type != TOKEN_ID)
      parser_error(parser);

    car = parse_symbol(parser);

    cur->car = car;
    cur->cdr = init_ast_pair(NULL, NULL);
    cur = cur->cdr;

    current_token = parser->tokens[parser->i];
  }

  parser_move(parser);
  return head;
}

ast_t *parse_function(parser_t *parser) {
  parser_eat(parser, TOKEN_LPAREN);
  ast_t *car = parse_function_args(parser);
  ast_t *cdr =
      parse_expr(parser); /* a function can contain a single expression */
  if (cdr == NULL)
    parser_error(parser);
  if (parser->tokens[parser->i]->type != TOKEN_RPAREN)
    parser_error(parser);
  parser_move(parser);
  return init_ast_function(car, cdr);
}

void parse_bind(parser_t *parser) {
  parser_move(parser);
  if (parser->tokens[parser->i]->type != TOKEN_ID)
    parser_error(parser);
  token_t *t = parser->tokens[parser->i];
  char *name = t->value;
  printf("%s\n", name);
  parser_move(parser);
  ast_t *expr = parse_expr(parser); /* unevaluated expr will be evaluated when
                                       hash table transfers to visitor JIT */
  if (expr == NULL)
    parser_error(parser);
  hash_table_add(parser->symbol_table, name, expr);
  printf("after add\n");
  if (parser->tokens[parser->i]->type != TOKEN_RPAREN)
    parser_error(parser);

  parser_move(parser);
}

ast_t *parse_list(parser_t *parser) {
  ast_t *car;
  ast_t *head = init_ast_pair(NULL, NULL);
  ast_t *cur = head;
  parser_move(parser);

  bool first_entry = true;
  token_t *current_token = parser->tokens[parser->i];
  while (current_token->type != TOKEN_RPAREN) {
    if (current_token->type == TOKEN_ID) {
      if (strcmp(current_token->value, "lambda") == 0 && first_entry) {
        printf("lambda here\n");
        return parse_function(parser);
      } else if (strcmp(current_token->value, "bind") == 0 && first_entry) {
        parse_bind(parser);
        return NULL;
      } else {
        car = parse_symbol(parser);
      }
    } else {
      car = parse_expr(parser);
      if (car == NULL)
        parser_error(parser);
    }
    cur->car = car;
    cur->cdr = init_ast_pair(NULL, NULL);
    cur = cur->cdr;
    first_entry = false;
    current_token = parser->tokens[parser->i];
  }
  parser_move(parser);
  return head;
}

ast_t *parse_quote(parser_t *parser) {
  parser_move(parser);
  ast_t *car = init_ast_symbol("quote");
  ast_t *expr = parse_expr(parser);
  if (expr == NULL)
    parser_error(parser);
  ast_t *ret = init_ast_pair(
      car, init_ast_pair(
               expr, init_ast_pair(NULL, NULL))); /* Converts ' to `quote` */
  return ret;
}

ast_t *parse_expr(parser_t *parser) {
  token_t *t = parser->tokens[parser->i];
  if (t->type == TOKEN_STRING)
    return parse_string(parser);
  else if (t->type == TOKEN_INT)
    return parse_int(parser);
  else if (t->type == TOKEN_FLOAT)
    return parse_float(parser);
  else if (t->type == TOKEN_BOOL)
    return parse_bool(parser);
  else if (t->type == TOKEN_LPAREN)
    return parse_list(parser);
  else if (t->type == TOKEN_QUOTE)
    return parse_quote(parser);
  else if (t->type == TOKEN_ID)
    return parse_symbol(parser);
  else if (t->type == TOKEN_LPAREN)
    return parse_list(parser);
  else {
    printf("DEBUG\n");
    printf("%d", t->type);
    parser_error(parser);
  }
  return NULL;
}

ast_t *parse_all(parser_t *parser) {
  token_t *t = parser->tokens[parser->i];
  ast_t **asts = malloc(sizeof(ast_t *));
  ast_t *cur;
  int i = 0;
  while (t != NULL) {
    cur = parse_expr(parser);
    if (cur == NULL) {
      printf("this is happening\n");
      t = parser->tokens[parser->i];
      continue;
    }
    i++;
    asts = realloc(asts, i * sizeof(ast_t *));
    asts[i - 1] = cur;
    t = parser->tokens[parser->i];
  }
  return init_ast_root(asts, i);
}

void parser_error(parser_t *parser) {
  printf("PARSER ERROR: something went wrong.\n");
  exit(1);
}