-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast_expr.cc
executable file
·132 lines (106 loc) · 3.38 KB
/
ast_expr.cc
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
/* File: ast_expr.cc
* -----------------
* Implementation of expression node classes.
*/
#include "ast_expr.h"
#include "ast_type.h"
#include "ast_decl.h"
#include <string.h>
IntConstant::IntConstant(yyltype loc, int val) : Expr(loc) {
value = val;
}
void IntConstant::PrintChildren(int indentLevel) {
printf("%d", value);
}
DoubleConstant::DoubleConstant(yyltype loc, double val) : Expr(loc) {
value = val;
}
void DoubleConstant::PrintChildren(int indentLevel) {
printf("%g", value);
}
BoolConstant::BoolConstant(yyltype loc, bool val) : Expr(loc) {
value = val;
}
void BoolConstant::PrintChildren(int indentLevel) {
printf("%s", value ? "true" : "false");
}
StringConstant::StringConstant(yyltype loc, const char *val) : Expr(loc) {
Assert(val != NULL);
value = strdup(val);
}
void StringConstant::PrintChildren(int indentLevel) {
printf("%s",value);
}
Operator::Operator(yyltype loc, const char *tok) : Node(loc) {
Assert(tok != NULL);
strncpy(tokenString, tok, sizeof(tokenString));
}
void Operator::PrintChildren(int indentLevel) {
printf("%s",tokenString);
}
CompoundExpr::CompoundExpr(Expr *l, Operator *o, Expr *r)
: Expr(Join(l->GetLocation(), r->GetLocation())) {
Assert(l != NULL && o != NULL && r != NULL);
(op=o)->SetParent(this);
(left=l)->SetParent(this);
(right=r)->SetParent(this);
}
CompoundExpr::CompoundExpr(Operator *o, Expr *r)
: Expr(Join(o->GetLocation(), r->GetLocation())) {
Assert(o != NULL && r != NULL);
left = NULL;
(op=o)->SetParent(this);
(right=r)->SetParent(this);
}
void CompoundExpr::PrintChildren(int indentLevel) {
if (left) left->Print(indentLevel+1);
op->Print(indentLevel+1);
right->Print(indentLevel+1);
}
ArrayAccess::ArrayAccess(yyltype loc, Expr *b, Expr *s) : LValue(loc) {
(base=b)->SetParent(this);
(subscript=s)->SetParent(this);
}
void ArrayAccess::PrintChildren(int indentLevel) {
base->Print(indentLevel+1);
subscript->Print(indentLevel+1, "(subscript) ");
}
FieldAccess::FieldAccess(Expr *b, Identifier *f)
: LValue(b? Join(b->GetLocation(), f->GetLocation()) : *f->GetLocation()) {
Assert(f != NULL); // b can be be NULL (just means no explicit base)
base = b;
if (base) base->SetParent(this);
(field=f)->SetParent(this);
}
void FieldAccess::PrintChildren(int indentLevel) {
if (base) base->Print(indentLevel+1);
field->Print(indentLevel+1);
}
Call::Call(yyltype loc, Expr *b, Identifier *f, List<Expr*> *a) : Expr(loc) {
Assert(f != NULL && a != NULL); // b can be be NULL (just means no explicit base)
base = b;
if (base) base->SetParent(this);
(field=f)->SetParent(this);
(actuals=a)->SetParentAll(this);
}
void Call::PrintChildren(int indentLevel) {
if (base) base->Print(indentLevel+1);
field->Print(indentLevel+1);
actuals->PrintAll(indentLevel+1, "(actuals) ");
}
NewExpr::NewExpr(yyltype loc, NamedType *c) : Expr(loc) {
Assert(c != NULL);
(cType=c)->SetParent(this);
}
void NewExpr::PrintChildren(int indentLevel) {
cType->Print(indentLevel+1);
}
NewArrayExpr::NewArrayExpr(yyltype loc, Expr *sz, Type *et) : Expr(loc) {
Assert(sz != NULL && et != NULL);
(size=sz)->SetParent(this);
(elemType=et)->SetParent(this);
}
void NewArrayExpr::PrintChildren(int indentLevel) {
size->Print(indentLevel+1);
elemType->Print(indentLevel+1);
}