-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.h
139 lines (126 loc) · 3.47 KB
/
ast.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
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
/* ast.h */
#ifndef AST_H
#define AST_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/* Forward declaration */
typedef struct ASTNode ASTNode;
typedef enum
{
OP_PLUS,
OP_MINUS,
OP_TIMES,
OP_DIVIDE,
OP_MOD,
OP_LT,
OP_GT,
OP_LE,
OP_GE,
OP_EQ,
OP_NE,
OP_AND,
OP_OR,
OP_NEG // For unary minus
// Add other operators as needed
} OperatorType;
/* AST node types */
typedef enum
{
NODE_NUMBER,
NODE_IDENTIFIER,
NODE_ASSIGNMENT,
NODE_OPERATION,
NODE_UNARY_OPERATION,
NODE_FOR_STATEMENT,
NODE_PRINT_STATEMENT,
NODE_ERROR_STATEMENT,
NODE_STATEMENT_LIST,
NODE_IF_STATEMENT,
NODE_STRING_LITERAL,
NODE_SWITCH_STATEMENT,
NODE_CASE,
NODE_DEFAULT_CASE,
NODE_BREAK_STATEMENT,
} NodeType;
/* Structures */
typedef struct StatementList
{
ASTNode *statement;
struct StatementList *next;
} StatementList;
typedef struct
{
ASTNode *condition;
ASTNode *then_branch;
ASTNode *else_branch;
} IfStatementNode;
typedef struct CaseNode
{
ASTNode *value;
ASTNode *statements;
struct CaseNode *next;
} CaseNode;
typedef struct
{
ASTNode *expression; // The switch expression
CaseNode *cases; // Linked list of cases
} SwitchNode;
/* AST node structure */
struct ASTNode
{
NodeType type;
union
{
int value; // For numbers
char *name; // For identifiers
struct
{ // For binary operations and assignments
ASTNode *left;
ASTNode *right;
OperatorType op; // Operator character like '+', '-', etc.
} op;
struct
{ // For unary operations
ASTNode *operand;
char op; // Operator character like '-', etc.
} unary;
struct
{ // For 'for' statements
ASTNode *init;
ASTNode *cond;
ASTNode *incr;
ASTNode *body;
} for_stmt;
StatementList *statements; // For statement lists
IfStatementNode if_stmt; // For if statements
SwitchNode switch_stmt;
CaseNode case_node;
ASTNode *break_stmt; // For break statements, can be NULL
// Add other nodes as needed
} data;
};
/* Function prototypes */
ASTNode *create_number_node(int value);
ASTNode *create_identifier_node(char *name);
ASTNode *create_assignment_node(char *name, ASTNode *expr);
ASTNode *create_operation_node(OperatorType op, ASTNode *left, ASTNode *right);
ASTNode *create_unary_operation_node(OperatorType op, ASTNode *operand);
ASTNode *create_for_statement_node(ASTNode *init, ASTNode *cond, ASTNode *incr, ASTNode *body);
ASTNode *create_print_statement_node(ASTNode *expr);
ASTNode *create_error_statement_node(ASTNode *expr);
ASTNode *create_statement_list(ASTNode *statement, ASTNode *next_statement);
ASTNode *create_if_statement_node(ASTNode *condition, ASTNode *then_branch, ASTNode *else_branch);
ASTNode *create_string_literal_node(char *string);
ASTNode *create_switch_statement_node(ASTNode *expression, CaseNode *cases);
CaseNode *create_case_node(ASTNode *value, ASTNode *statements);
CaseNode *create_default_case_node(ASTNode *statements);
CaseNode *append_case_list(CaseNode *list, CaseNode *case_node);
ASTNode *create_break_node();
int evaluate_expression(ASTNode *node);
void execute_statement(ASTNode *node);
void execute_statements(ASTNode *node);
void execute_for_statement(ASTNode *node);
void free_ast(ASTNode *node);
#endif /* AST_H */