aboutsummaryrefslogtreecommitdiff
path: root/src/parser.c
blob: 145801880edd98691719e3ced8c5f74536c0caf2 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "common.h"
#include "parser.h"

// TODO: Do all the todos

// adds another child of type type to the sexp sexp 
// returns the index of the added type and < 0 on fail
static size_t sexp_add(struct sexp *sexp, enum ast_type type);

// self-explanatory
static void sexp_init(struct sexp *sexp);
static void sexp_print(struct sexp *sexp, int indent);
static void sexp_free(struct sexp *sexp);
static void ast_free(struct ast *ast);

static void quote_stack_push(struct quote_node **head, struct sexp *cur_sexp);
static struct sexp *quote_stack_pop(struct quote_node **head, int peek);

// returns 0 on success
enum paren_type { P_OPEN, P_CLOSE };
static int on_paren(parser_t parser, enum paren_type type); // 0 is open, 1 is close
static int on_quote(parser_t parent);
static int on_value(parser_t parent, value_t value);

#define TOKEN_CALLBACK_TBL(X, parser, token)           \
/*  X(test,                 execute on succes) */      \
    X(TOKEN_PARENTHS_OPEN,  on_paren(parser, P_OPEN))  \
    X(TOKEN_PARENTHS_CLOSE, on_paren(parser, P_CLOSE)) \
    X(TOKEN_SPECIAL_QUOTE,  on_quote(parser))          \
    X(TOKEN_VALUE,          on_value(parser, token->value))

#define FN(fn, arg) "%s", fn(arg, buf, buf_sz)

#define MANAGE_AST_TBL(X, ast)                                            \
    X(AST_SEXP,  sexp_free(&ast->sexp), "|")                              \
    X(AST_VALUE, value_destroy(ast->value), FN(value_string, ast->value)) \

// ---------- Exported Functions ---------- //

#define CASE_TYPE(type, callback)               \
    case type:                                  \
    if(callback) {                              \
        err(#callback ": failed");              \
        return -1;                              \
    } break;

int parser_parse_toklist(parser_t parser, toklist_t *toklist, ast_t *ast)
{
    if(parser->cur_sexp == NULL) {
        size_t index = sexp_add(&ast->sexp, AST_VALUE);
        ast->sexp.children[index].value = value_copy(parser->begin_symbol_value);
        
        parser->cur_sexp = &ast->sexp;
    }
    
    
    for(size_t i = 0; i < toklist->ntokens; i++) {
        struct token *token = &toklist->tokens[i];

        switch(token->type) {
            TOKEN_CALLBACK_TBL(CASE_TYPE, parser, token);
        default:
            err("parser_parse_lexer: Unknown token type given");
            break;
        }
        
        if(token->type != TOKEN_SPECIAL_QUOTE) 
           while(parser->cur_sexp == quote_stack_pop(&parser->quote_head, 1)) {
               on_paren(parser, P_CLOSE);
               quote_stack_pop(&parser->quote_head, 0);
           }
    }

    if(&ast->sexp == parser->cur_sexp) {
        return 0;
    } else {
        return 1;
    }
}

parser_t parser_create()
{   
    parser_t parser = xmalloc(sizeof(struct parser));

#define VALUE_CREATE(str)                  \
    value_create(VALUE_SYMBOL, str, &ret); \
    if(ret) {                              \
        err("value_create: failed");       \
        return NULL;                       \
    }

    int ret = 0;
    parser->begin_symbol_value = VALUE_CREATE("begin");
    parser->quote_symbol_value = VALUE_CREATE("quote");

    parser->quote_head = NULL;
    parser_reset(parser);

    return parser;
}

void parser_destroy(parser_t parser)
{
    if(!parser) return;

    value_destroy(parser->begin_symbol_value);
    value_destroy(parser->quote_symbol_value);
    
    parser_reset(parser);
    free(parser);
}

void parser_reset(parser_t parser)
{
    parser->cur_sexp = NULL;
    while(quote_stack_pop(&parser->quote_head, 0) != NULL);
}

void ast_reset(ast_t *ast)
{
    sexp_free(&ast->sexp);
    sexp_init(&ast->sexp);
}


void ast_print(ast_t *ast)
{
    sexp_print(&ast->sexp, 0);
}

// ---------- Callback Functions ---------- //

static int on_paren(parser_t parser, enum paren_type type)
{    
    if(type == P_CLOSE) {
        parser->cur_sexp = parser->cur_sexp->prev;
    } else if(type == P_OPEN) {
        size_t index = sexp_add(parser->cur_sexp, AST_SEXP);

        struct sexp *prev = parser->cur_sexp;
        struct sexp *new = &prev->children[index].sexp;
        
        sexp_init(new);
        
        parser->cur_sexp = new;
        parser->cur_sexp->prev = prev;
    }

    return 0;
}

static int on_quote(parser_t parser)
{
    // new sexp
    on_paren(parser, P_OPEN);

    // add symbol to the sexp
    on_value(parser, parser->quote_symbol_value);

    // save the current quote sexp
    quote_stack_push(&parser->quote_head, parser->cur_sexp);
    return 0;
}

static int on_value(parser_t parser, value_t value)
{    
    size_t index = sexp_add(parser->cur_sexp, AST_VALUE);

    value_t copied = value_copy(value);

    parser->cur_sexp->children[index].value = copied;
    return 0;
}

// ---------- Parser Functions ---------- //

static size_t sexp_add(struct sexp *sexp, enum ast_type type)
{   
    sexp->children = xrealloc(sexp->children, sizeof(struct ast) * (sexp->nchildren+1));

    sexp->children[sexp->nchildren].type = type;
    return sexp->nchildren++;
}

static void sexp_init(struct sexp *sexp)
{
    sexp->children = NULL;
    sexp->nchildren = 0;
    sexp->prev = NULL;
}

static void sexp_print(struct sexp *sexp, int indent)
{
    // (void)indent;
    // printf("( ");
    for(size_t i = 0; i < sexp->nchildren; i++) {
        struct ast *child = &sexp->children[i];

        if(child->type == AST_SEXP) {
            sexp_print(&child->sexp, indent + 2);
            continue;
        }

        char buf[LEXER_IDEN_CAP];
        size_t buf_sz = LEXER_IDEN_CAP;

        for(int i = 0; i < indent; i++) printf(" ");
        printf("%s\n", value_string(child->value, buf, buf_sz));
    }
    // printf(")");
}

static void sexp_free(struct sexp *sexp)
{
    for(size_t i = 0; i < sexp->nchildren; i++){
        ast_free(&sexp->children[i]);
    }
    
    if(sexp->children) free(sexp->children);
}

#define CASE_FREE(type, free_func, print_func)  \
    case type: free_func; return;

static void ast_free(struct ast *ast)
{
    if(!ast) return;

    switch(ast->type) {
        MANAGE_AST_TBL(CASE_FREE, ast)
    default:
        err("ast_free: Unknown ast type given");
        break;
    }
}

static void quote_stack_push(struct quote_node **head, struct sexp *cur_sexp)
{
    struct quote_node *node = xmalloc(sizeof(struct quote_node));

    node->prev = *head;
    node->cur_sexp = cur_sexp;
    *head = node;
}

static struct sexp *quote_stack_pop(struct quote_node **head, int peek)
{
    if(*head == NULL) {
        return NULL;
    }

    struct sexp *sexp = (*head)->cur_sexp;
    
    if(!peek) {
        struct quote_node *prev = (*head)->prev;
        free(*head);
        *head = prev;
    }
    
    return sexp;
}