-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathparser.h
98 lines (91 loc) · 2 KB
/
parser.h
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
#ifndef PARSER_H
#define PARSER_H
#include "lexer.h"
// Node types
typedef enum {
NODE_PROGRAM,
NODE_FUNCTION_DECLARATION,
NODE_VARIABLE_DECLARATION,
NODE_TYPE,
NODE_IDENTIFIER,
NODE_BLOCK,
NODE_IF_STATEMENT,
NODE_WHILE_STATEMENT,
NODE_RETURN_STATEMENT,
NODE_EXPRESSION_STATEMENT,
NODE_BINARY,
NODE_UNARY,
NODE_PRIMARY,
NODE_ASSIGNMENT,
NODE_LITERAL
} NodeType;
// Forward declaration of Node
typedef struct Node Node;
// Node structure
struct Node {
NodeType type;
Token token;
union {
struct {
Node** declarations;
int declaration_count;
} program;
struct {
Node* type;
Node* identifier;
Node** parameters;
int parameter_count;
Node* body;
} function_declaration;
struct {
Node* type;
Node* identifier;
Node* initializer;
} variable_declaration;
struct {
Node** statements;
int statement_count;
} block;
struct {
Node* condition;
Node* then_branch;
Node* else_branch;
} if_statement;
struct {
Node* condition;
Node* body;
} while_statement;
struct {
Node* expression;
} return_statement;
struct {
Node* expression;
} expression_statement;
struct {
Node* left;
Node* right;
} binary;
struct {
Node* operand;
} unary;
struct {
Token value;
} literal;
struct {
Node* left;
Node* right;
} assignment;
} as;
};
// Parser structure
typedef struct {
Lexer* lexer;
Token current;
Token previous;
int hadError;
int panicMode;
} Parser;
// Function prototypes
void initParser(Parser* parser, Lexer* lexer);
Node* parseProgram(Parser* parser);
#endif // PARSER_H