aboutsummaryrefslogtreecommitdiff
path: root/src/parser.h
blob: 8fc5d6c177bb43e0ed6c5b4f6d91b20df60da349 (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
#ifndef PARSER_H
#define PARSER_H

#include "value.h"
#include "lexer.h"

typedef struct parser *parser_t;

struct ast {
    enum ast_type {
        AST_SEXP,
        AST_VALUE,
        AST_TYPES // number of types
    } type;

    union {
        struct sexp {
            struct ast *children;
            size_t nchildren;

            struct sexp *prev;
        } sexp;

        value_t value;
    };
};

struct parser {
    struct sexp root;
    struct sexp *cur_sexp;

    struct quote_node {
        struct sexp *cur_sexp;
        struct quote_node *prev;
    } *quote_head;

    value_t begin_symbol_value;
    value_t quote_symbol_value;
};

// allocate a parser
// returns a parser on success and NULL on fail
parser_t parser_create();

// deallocate a parser
void parser_destroy(parser_t parser);

// reset a parser to its default state without destroying it
// returns 0 on success
void parser_reset(parser_t parser);

// self explanatory
void parser_print_ast(parser_t parser);

// turn the given lexer (which has already has tokens) into an ast
// returns 0 on success, > 0 when more tokens are needed,
//         and < 0 on a fatal error
int parser_parse_lexer(parser_t parser, lexer_t lexer);

#endif