-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.h
More file actions
executable file
·97 lines (85 loc) · 2.45 KB
/
ast.h
File metadata and controls
executable file
·97 lines (85 loc) · 2.45 KB
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
#include <stdlib.h>
#include <llvm-c/Core.h>
#include "y.tab.h"
const char *type_name(enum value_type t);
enum expr_type {
BOOL_LIT,
LITERAL,
DECIMAL,
VARIABLE,
BIN_OP,
};
struct expr {
enum expr_type type;
union {
int value; // for type == LITERAL || type == BOOL_LIT
int value_dec; // for type == DECIMAL
size_t id; // for type == VARIABLE
struct {
struct expr *lhs;
struct expr *rhs;
int op;
} binop; // for type == BIN_OP
};
};
struct expr* bool_lit(int v);
struct expr* literal(int v);
struct expr* decimal(float v);
struct expr* variable(size_t id);
struct expr* binop(struct expr *lhs, int op, struct expr *rhs);
void print_expr(struct expr *expr);
void emit_stack_machine(struct expr *expr);
int emit_reg_machine(struct expr *expr);
enum value_type check_types(struct expr *expr);
void free_expr(struct expr *expr);
enum stmt_type {
STMT_SEQ,
STMT_ASSIGN,
STMT_IF,
STMT_WHILE,
STMT_DOWHILE,
STMT_FOR,
STMT_PRINT,
};
struct stmt {
enum stmt_type type;
union {
struct {
size_t id;
struct expr *expr;
} assign; // for type == STMT_ASSIGN
struct {
struct stmt *fst, *snd;
} seq; // for type == STMT_SEQ
struct {
struct expr *cond;
struct stmt *if_body, *else_body;
} ifelse; // for type == STMT_IF
struct {
struct expr *cond;
struct stmt *body;
} while_, dowhile; // for type == STMT_WHILE, STMT_DOWHILE
struct {
struct stmt *init;
struct expr *cond;
struct stmt *body;
struct stmt *incr;
} for_; // for type == STMT_FOR
struct {
struct expr *expr;
} print; // for type == STMT_PRINT
};
};
struct stmt* make_seq(struct stmt *fst, struct stmt *snd);
struct stmt* make_assign(size_t id, struct expr *e);
struct stmt* make_while(struct expr *e, struct stmt *body);
struct stmt* make_dowhile(struct stmt *body, struct expr *e);
struct stmt* make_for(struct stmt *init, struct expr *cond, struct stmt *incr, struct stmt *body);
struct stmt* make_ifelse(struct expr *e, struct stmt *if_body, struct stmt *else_body);
struct stmt* make_if(struct expr *e, struct stmt *body);
struct stmt* make_print(struct expr *e);
void free_stmt(struct stmt *stmt);
void print_stmt(struct stmt *stmt, int indent);
int valid_stmt(struct stmt *stmt);
LLVMValueRef codegen_expr(struct expr *expr, LLVMModuleRef module, LLVMBuilderRef builder);
void codegen_stmt(struct stmt *stmt, LLVMModuleRef module, LLVMBuilderRef builder);