summaryrefslogtreecommitdiffstats
path: root/source/code/parser.h
blob: 3f3c75cf0d3f938a7e263be6becf5329848f6d8d (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
#ifndef EDITOR_PARSER_H
#define EDITOR_PARSER_H

// typedef enum AstNodeType AstNodeType;
enum AstNodeType {
 Literal,
 IdentExpr, // TODO: evaluate this in an expression pass
 Binary,
 Assignment,
 Return,
 Function,
};

typedef struct AstNode AstNode;
struct AstNode {
 AstNode    *first;
 AstNode    *last;
 AstNode    *parent;
 AstNode    *next;
 AstNodeType type;
};

typedef struct Ast Ast;
struct Ast {
 AstNode     *root;
 AstNode     *current;
 String8List *symbols;
};

ReadOnly global_variable AstNode nil_ast_node = {
 &nil_ast_node, // first
 &nil_ast_node, // last
 &nil_ast_node, // parent
 &nil_ast_node, // next
};

ReadOnly global_variable Ast nil_ast = {
 &nil_ast_node, // root
 &nil_ast_node, // currrent
};

#define PeekForward(node, type, nil_node)                          \
 for (; (node) && (node) != &(nil_node); (node) = (node)->next) \
 if ((node)->token->type == (type))                         \
 break;

//-
internal Ast *parse_tokenized_tree_fmt(MemArena *arena, TokenTree *token_tree);
internal b32  is_ast_node_nil(AstNode *node);
internal void adopt_ast_node(AstNode *tree_node, AstNode *new_node);

//- syntax
global_variable String8 reserved_words[] = {

 str8("struct"),
 str8("while"),
 str8("return"),
 str8("for"),
 str8("import"),
 str8("switch"),
 str8("case"),
 str8("defer"),
 str8("enum"),
};

#define FillAstNodeFieldWIthNilNode(node, field)       \
 if (node->field == NULL || !is_ast_node_nil(node)) \
 node->field = &nil_ast_node;

internal void adopt_ast_node(AstNode *node, AstNode *new_node);

#endif // EDITOR_PARSER_H