This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
generated from dthain/compilerbook-starter-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.h
executable file
·93 lines (77 loc) · 1.86 KB
/
expr.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
#ifndef EXPR_H
#define EXPR_H
typedef enum
{
/* Arithmetic operator nodes */
EXPR_ADD,
EXPR_SUB,
EXPR_MUL,
EXPR_DIV,
EXPR_MOD,
EXPR_EXP,
/* Comparison and logic operator nodes */
EXPR_GT,
EXPR_GEQ,
EXPR_LT,
EXPR_LEQ,
EXPR_EQ,
EXPR_NEQ,
EXPR_AND,
EXPR_OR,
/* Other binary operator nodes */
EXPR_CALL,
EXPR_INDEX,
EXPR_ASSIGN,
/* Unary operator nodes */
EXPR_NEG,
EXPR_NOT,
EXPR_INCREMENT,
EXPR_DECREMENT,
/* Leaf nodes */
EXPR_INTEGER_LITERAL,
EXPR_FLOAT_LITERAL,
EXPR_BOOLEAN_LITERAL,
EXPR_CHAR_LITERAL,
EXPR_STRING_LITERAL,
EXPR_NAME,
/* Special nodes */
EXPR_LIST
} expr_t;
struct expr
{
/* Used by all kinds of expressions */
expr_t kind;
struct expr* left;
struct expr* right;
int reg; // Used by codegen
/* Used by various leaf expressions */
struct symbol* symbol;
const char* name;
int integer_literal; // Used by INTEGER and BOOLEAN
float float_literal;
char char_literal;
const char* string_literal;
};
/* Creating non-leaf nodes */
struct expr* expr_create(expr_t kind, struct expr* left, struct expr* right);
struct expr* expr_create_unary(expr_t kind, struct expr* operand);
/* Creating leaf nodes */
struct expr* expr_create_name(const char* n);
struct expr* expr_create_integer_literal(int i);
struct expr* expr_create_float_literal(float f);
struct expr* expr_create_boolean_literal(int b);
struct expr* expr_create_char_literal(char c);
struct expr* expr_create_string_literal(const char* str);
/** Print the expression */
void expr_print(const struct expr* e);
/** Resolve the expression */
void expr_resolve(struct expr* e);
/**
* Typechecking the expression.
* @return Type of expr if successful, of left child otherwise.
*/
struct type* expr_typecheck(const struct expr* e);
/** @return 1 if the expression is constant, 0 otherwise */
int expr_is_constant(const struct expr* e);
void expr_codegen(struct expr* e);
#endif