This repository was archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr_tree.cpp
More file actions
98 lines (82 loc) · 3.04 KB
/
expr_tree.cpp
File metadata and controls
98 lines (82 loc) · 3.04 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
98
// Implementation of ExprTree subclasses
//
// Created by: David Bell
// April 26th, 2012
#include "expr_tree.h"
#include "stdio.h"
/* Root */
Root::Root(ExprTree *e) : expr(e) { e->parent = this; }
Root::~Root() { delete expr; }
void Root::visitSubExpr(Visitor *v) { expr->accept(v); }
void Root::accept(Visitor *v) { v->visitRoot(this); }
bool Root::isIntExpr() { return expr->isIntExpr(); }
int Root::resolveValue() { return expr->resolveValue(); }
/* Ident */
Ident::Ident(char c) : identifier(c) {}
Ident::~Ident() {}
void Ident::visitSubExpr(Visitor *v) { return; }
void Ident::accept(Visitor *v) { v->visitIdent(this); }
bool Ident::isIntExpr() { return false; }
int Ident::resolveValue() { return NULL; }
/* Int */
Int::Int(int i) : value(i) {}
Int::~Int() {}
void Int::visitSubExpr(Visitor *v) { return; }
void Int::accept(Visitor *v) { v->visitInt(this); }
bool Int::isIntExpr() { return true; }
int Int::resolveValue() { return value; }
/* Div */
Div::Div(ExprTree *lhs, ExprTree *rhs) : op1(lhs), op2(rhs)
{
op1->parent = this;
op2->parent = this;
}
Div::~Div() { delete op1; delete op2; }
void Div::visitSubExpr(Visitor *v) { op1->accept(v); op2->accept(v); }
void Div::accept(Visitor *v) { v->visitDiv(this); }
bool Div::isIntExpr()
{
if (op1->isIntExpr() and op2->isIntExpr() and op1->resolveValue() % op2->resolveValue() == 0)
return true;
return false;
}
int Div::resolveValue() { return op1->resolveValue() / op2->resolveValue(); }
/* Times */
Times::Times(ExprTree *lhs, ExprTree *rhs) : op1(lhs), op2(rhs) {}
Times::~Times() { delete op1; delete op2; }
void Times::visitSubExpr(Visitor *v) { op1->accept(v); op2->accept(v); }
void Times::accept(Visitor *v) { v->visitTimes(this); }
bool Times::isIntExpr() { return op1->isIntExpr() and op2->isIntExpr(); }
int Times::resolveValue() { return op1->resolveValue() * op2->resolveValue(); }
/* Plus */
Plus::Plus(ExprTree *lhs, ExprTree *rhs) : op1(lhs), op2(rhs)
{
op1->parent = this;
op2->parent = this;
}
Plus::~Plus() { delete op1; delete op2; }
void Plus::visitSubExpr(Visitor *v) { op1->accept(v); op2->accept(v); }
void Plus::accept(Visitor *v) { v->visitPlus(this); }
bool Plus::isIntExpr() { return op1->isIntExpr() and op2->isIntExpr(); }
int Plus::resolveValue() { return op1->resolveValue() + op2->resolveValue(); }
/* Minus */
Minus::Minus(ExprTree *lhs, ExprTree *rhs) : op1(lhs), op2(rhs)
{
op1->parent = this;
op2->parent = this;
}
Minus::~Minus() { delete op1; delete op2; }
void Minus::visitSubExpr(Visitor *v) { op1->accept(v); op2->accept(v); }
void Minus::accept(Visitor *v) { v->visitMinus(this); }
bool Minus::isIntExpr() { return op1->isIntExpr() and op2->isIntExpr(); }
int Minus::resolveValue() { return op1->resolveValue() - op2->resolveValue(); }
/* UMinus */
UMinus::UMinus(ExprTree *rhs) : op1(rhs)
{
op1->parent = this;
}
UMinus::~UMinus() { delete op1; }
void UMinus::visitSubExpr(Visitor *v) { op1->accept(v); }
void UMinus::accept(Visitor *v) { v->visitUMinus(this); }
bool UMinus::isIntExpr() { return op1->isIntExpr(); }
int UMinus::resolveValue() { return -(op1->resolveValue()); }