-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.hpp
More file actions
145 lines (119 loc) · 4.01 KB
/
Copy pathast.hpp
File metadata and controls
145 lines (119 loc) · 4.01 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#pragma once
#include <memory>
#include <vector>
#include "lexer.hpp"
enum Movement {
move = Token::tok_move, turn_left = Token::tok_turn_left
};
/// ExprAST - Base class for all expression nodes
class ExprAST {
public:
virtual ~ExprAST() = default;
// Generates the actual assembly code
virtual void codegen(std::ostream &out) = 0;
// Helper function: counts the number of instructions in codegen
virtual int instructionCount() = 0;
// Debugging function: dumps the contents of the AST in readable format
virtual void dump(int indent) = 0;
};
/// MovementAST - Represents an movement, either move or turn left
class MovementAST : public ExprAST {
Movement movement;
public:
MovementAST(Movement movement) : movement(movement) {}
void codegen(std::ostream &out) override;
int instructionCount() override;
void dump(int indent) override;
};
/// FrontBlockedAST - Indicating front blocked
class FrontBlockedAST : public ExprAST {
public:
void codegen(std::ostream &out) override;
int instructionCount() override;
void dump(int indent) override;
};
/// NotExprAST - Indicating a not on a condition
class NotExprAST : public ExprAST {
std::unique_ptr<ExprAST> Cond;
public:
NotExprAST(std::unique_ptr<ExprAST> Cond) : Cond(std::move(Cond)) {}
void codegen(std::ostream &out) override;
int instructionCount() override;
void dump(int indent) override;
};
/// CondAST - Expression for conditions
class CondAST : public ExprAST {
std::unique_ptr<ExprAST> Cond;
public:
CondAST(std::unique_ptr<ExprAST> Cond) : Cond(std::move(Cond)) {}
void codegen(std::ostream &out) override;
int instructionCount() override;
void dump(int indent) override;
};
/// BinaryCondAST - Expression class for binary relations aka '&' and '|'
class BinaryCondAST : public ExprAST {
int Op;
std::unique_ptr<ExprAST> LHS, RHS;
public:
BinaryCondAST(int Op, std::unique_ptr<ExprAST> LHS,
std::unique_ptr<ExprAST> RHS)
: Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
void codegen(std::ostream &out) override;
int instructionCount() override;
void dump(int indent) override;
};
/// IfExprAST - Expression class for if/then/else
class IfExprAST : public ExprAST {
std::unique_ptr<ExprAST> Cond;
std::unique_ptr<ExprAST> Then, Else;
public:
IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then,
std::unique_ptr<ExprAST> Else)
: Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}
void codegen(std::ostream &out) override;
int instructionCount() override;
void dump(int indent) override;
};
/// WhileLoopAST - Expression class for while loops - aka loops with a condition
class WhileLoopAST : public ExprAST {
std::unique_ptr<ExprAST> Cond;
std::unique_ptr<ExprAST> Body;
public:
WhileLoopAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Body)
: Cond(std::move(Cond)), Body(std::move(Body)) {}
void codegen(std::ostream &out) override;
int instructionCount() override;
void dump(int indent) override;
};
/// ForLoopAST - Expression class indicating for loops loop over 'count' times
class ForLoopAST : public ExprAST {
int count;
std::unique_ptr<ExprAST> Body;
public:
ForLoopAST(int count, std::unique_ptr<ExprAST> Body)
: count(count), Body(std::move(Body)) {}
void codegen(std::ostream &out) override;
int instructionCount() override;
void dump(int indent) override;
};
/// BlockAST - AST representing an entire block of elements
class BlockAST : public ExprAST {
std::vector<std::unique_ptr<ExprAST>> actions;
public:
BlockAST(std::vector<std::unique_ptr<ExprAST>> actions)
: actions(std::move(actions)) {}
void codegen(std::ostream &out) override;
int instructionCount() override;
void dump(int indent) override;
};
/// ProgramAST - AST representing a program
class ProgramAST : public ExprAST {
std::unique_ptr<ExprAST> StartBlock;
public:
ProgramAST(std::unique_ptr<ExprAST> StartBlock)
: StartBlock(std::move(StartBlock)) {}
void codegen(std::ostream &out) override;
int instructionCount() override;
void dump() { dump(0); }
void dump(int indent) override;
};