This repository was archived by the owner on May 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram_tree.h
More file actions
121 lines (101 loc) · 3.85 KB
/
Copy pathprogram_tree.h
File metadata and controls
121 lines (101 loc) · 3.85 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef __program_tree_h_
#define __program_tree_h_
#include <vector>
#include <memory>
namespace bf {
class Node {
public:
virtual bool modifiesStack() const = 0;
virtual bool isConstant() const = 0;
virtual int getValue() const = 0;
virtual char getOpeningBrace() const = 0;
virtual char getClosingBrace() const = 0;
};
class PlusOne : public Node {
public:
bool modifiesStack() const { return false; };
bool isConstant() const { return true; };
int getValue() const { return 1; };
char getOpeningBrace() const { return '('; };
char getClosingBrace() const { return ')'; };
friend std::shared_ptr<Node> createNilad(char);
};
class StackHeight : public Node {
public:
bool modifiesStack() const { return false; };
bool isConstant() const { return false; };
// since StackHeight is not constant getValue() doesn't matter
int getValue() const { return 0; };
char getOpeningBrace() const { return '['; };
char getClosingBrace() const { return ']'; };
friend std::shared_ptr<Node> createNilad(char);
};
class Pop : public Node {
public:
bool modifiesStack() const { return true; };
bool isConstant() const { return false; };
// since Pop is not constant getValue() doesn't matter
int getValue() const { return 0; };
char getOpeningBrace() const { return '{'; };
char getClosingBrace() const { return '}'; };
friend std::shared_ptr<Node> createNilad(char);
};
class StackSwap : public Node {
public:
bool modifiesStack() const { return true; };
bool isConstant() const { return true; };
int getValue() const { return 0; };
char getOpeningBrace() const { return '<'; };
char getClosingBrace() const { return '>'; };
friend std::shared_ptr<Node> createNilad(char);
};
class Monad : public Node {
bool modStack, constant;
int val;
std::vector<std::shared_ptr<Node> > containedOps;
protected:
Monad(const std::vector<std::shared_ptr<Node> >& contained);
public:
virtual bool modifiesStack() const { return modStack; };
virtual bool isConstant() const { return constant; };
virtual int getValue() const { return val; };
};
class Push : public Monad {
public:
Push(std::vector<std::shared_ptr<Node> > contained) : Monad(contained) {};
bool modifiesStack() const { return true; };
char getOpeningBrace() const { return '('; };
char getClosingBrace() const { return ')'; };
friend std::shared_ptr<Node> createMonad(char, const std::vector<std::shared_ptr<Node> >&);
};
class Negative : public Monad {
public:
Negative(std::vector<std::shared_ptr<Node> > contained) : Monad(contained) {};
int getValue() const { return -Monad::getValue(); };
char getOpeningBrace() const { return '['; };
char getClosingBrace() const { return ']'; };
friend std::shared_ptr<Node> createMonad(char, const std::vector<std::shared_ptr<Node> >&);
};
class Loop : public Monad {
public:
Loop(std::vector<std::shared_ptr<Node> > contained) : Monad(contained) {};
bool isConstant() const { return false; };
// since Loop is not constant getValue() doesn't matter
int getValue() const { return 0; };
char getOpeningBrace() const { return '{'; };
char getClosingBrace() const { return '}'; };
friend std::shared_ptr<Node> createMonad(char, const std::vector<std::shared_ptr<Node> >&);
};
class Zero : public Monad {
public:
Zero(std::vector<std::shared_ptr<Node> > contained) : Monad(contained) {};
bool isConstant() const { return true; };
int getValue() const { return 0; };
char getOpeningBrace() const { return '<'; };
char getClosingBrace() const { return '>'; };
friend std::shared_ptr<Node> createMonad(char, const std::vector<std::shared_ptr<Node> >&);
};
std::shared_ptr<Node> createNilad(char brace);
std::shared_ptr<Node> createMonad(char brace, const std::vector<std::shared_ptr<Node> >& contained);
}
#endif//__program_tree_h_