-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.h
141 lines (127 loc) · 3.33 KB
/
expression.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
140
141
#pragma once
#include <iostream>
#include <vector>
#include "lexer.h"
#include "asm_generator.h"
#define MAX_PRIORITY 16
#define MIN_PRIORITY 1
class sym_type;
class sym_table;
class symbol;
class expr_bin_op;
class expr_postfix_unar_op;
class expr_prefix_unar_op;
int get_priority(token tk, bool unar = 0);
class expr{
protected:
sym_type *type; /* return value type */
token tk;
string op;
public:
expr();
friend class parser;
friend class expr_bin_op;
friend class expr_postfix_unar_op;
friend class stmt_printf;
friend class expr_prefix_unar_op;
virtual void print(ostream &os, int level) = 0;
void print_level(ostream &os, int level);
int operator<<(int);
bool of_ctype(string s);
virtual void generate(asm_cmd_list *){}
virtual void generate_addr(asm_cmd_list *){}
};
class expr_bin_op: public expr{
protected:
expr *left, *right;
public:
expr_bin_op(expr *l, expr *r, token t, sym_type *st);
expr_bin_op(expr *l, expr *r, token t);
expr_bin_op(expr *l, expr *r, string op, sym_type *st);
expr_bin_op(expr *l, expr *r, string op);
void print(ostream &os, int level) override;
void generate(asm_cmd_list *) override;
void generate_addr(asm_cmd_list *) override;
void generate_simple_bin_op(asm_cmd_list *, token_t); /* +, -, *, / */
void generate_rel_bin_op(asm_cmd_list *);
void generate_assign_bin_op(asm_cmd_list *);
void generate_array_elem(asm_cmd_list *);
};
class expr_prefix_unar_op: public expr{
expr *ex;
public:
expr_prefix_unar_op(expr *e, token t);
void print(ostream &os, int level);
void generate(asm_cmd_list *) override;
void generate_addr(asm_cmd_list *) override;
};
class expr_postfix_unar_op: public expr{
expr *ex;
public:
expr_postfix_unar_op(expr *e, token t);
void print(ostream &os, int level);
void generate(asm_cmd_list *) override;
};
class expr_literal: public expr{
public:
expr_literal(token t, sym_type *);
expr_literal(token t);
void print(ostream &os, int level) override;
void generate(asm_cmd_list *) override;
};
class expr_var: public expr{
protected:
string name;
symbol *sym;
public:
expr_var(){}
int get_size_of_elem();
};
class expr_local_var: public expr_var{
public:
friend class expr_bin_op;
expr_local_var(symbol *);
expr_local_var(string, sym_type *);
void print(ostream &os, int level) override;
void generate(asm_cmd_list *) override;
void generate_addr(asm_cmd_list *) override;
};
class expr_global_var: public expr_var{
public:
friend class expr_bin_op;
expr_global_var(string, sym_type *);
expr_global_var(symbol *);
void print(ostream &os, int level) override;
void generate(asm_cmd_list *) override;
void generate_addr(asm_cmd_list *) override;
};
class expr_tern_op: public expr{
expr *left, *middle, *right;
public:
friend class parser;
expr_tern_op(expr* l, expr *m, expr *r, string s);
void print(ostream &os, int level);
};
class expr_function:public expr{
expr *fid;
vector<expr *>fargs;
public:
expr_function(expr *id, const vector<expr *> &args);
void print(ostream &os, int level);
void generate(asm_cmd_list *) override;
};
class struct_access:public expr{
expr *left;
token tk;
expr *field;
public:
struct_access(expr *l, expr *r, token t);
void print(ostream &os, int level);
};
class expr_cast2type: public expr{
expr *ex;
public:
expr_cast2type(string, expr *, sym_table *);
expr_cast2type(sym_type *, expr *);
void print(ostream &os, int level);
};