Repository navigation
Expand file tree
/
Copy pathCompiler.h
More file actions
233 lines (197 loc) · 6.52 KB
/
Copy pathCompiler.h
File metadata and controls
233 lines (197 loc) · 6.52 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifndef TITANPLUSPLUS_COMPILER_H
#define TITANPLUSPLUS_COMPILER_H
/**
* @file Compile.h
* @author Bryn McKerracher
* @date 7/10/2021
* @brief Contains the Compiler class header.
*/
#include <string>
#include <iostream>
#include <cstdlib>
#include <functional>
#include <vector>
#include "Batch.h"
#include "Token.h"
#include "Scanner.h"
#include "Parser.h"
#include "Memory.h"
#include "Debug.h"
//Enable logging of batch contents.
#define DEBUG_PRINT_CODE
/**
* @class Compiler
* @brief
*/
class Compiler {
public:
/**
* Initialises the parse rules.
*/
Compiler();
/**
* Compiles a string of Titan source code into bytecode
* to be interpreted by the VM.
*
* @brief Compiles a string of Titan source code.
* @param titanCode A string containing Titan source code.
* @param batch The batch to compile the bytecode into.
* @return True if no compilation errors were found, otherwise false.
*/
bool compile(const std::string& titanCode, Batch& batch);
protected:
///Ordering of precedence values for parsing.
enum Precedence {
NONE,
ASSIGNMENT, // =
OR, // or
AND, // and
EQUALITY, // == !=
COMPARISON, // < > <= >=
TERM, // + -
FACTOR, // * /
UNARY, // ! -
CALL, // . ()
PRIMARY
};
typedef std::function<void()> ParseFunction; ///< Alias for use in Pratt parsing.
struct ParseRule {
ParseFunction prefix; ///< Prefix behaviour.
ParseFunction infix; ///< Infix behaviour.
Compiler::Precedence precedence; ///< Precedence for parsing behaviour.
};
std::string titanSourceCode; ///< A copy of the source code we're compiling.
Scanner scanner; ///< Scans tokens from source code during compilation.
Parser parser; ///< Parses tokens produced by the scanner.
Batch* currentBatch = nullptr; ///< Current batch being compiled.
ParseRule parseRules[Token::Type::SIZE]; ///< List of parsing rules indexed by token type.
/**
* @brief Returns a pointer to the parsing rule associated with the given token type.
* @param type The token type to get the associated rule for.
* @return A pointer to the parsing rule associated with the given token type.
*/
ParseRule* getRule(Token::Type type);
/**
* @brief Asks the given scanner to generate the next token in the stream.
*/
void advance();
/**
* @brief Displays line and token information about a compilation error.
* @param token The token that caused the compilation error.
* @param message The error message to be disaplayed.
*/
void error(Token& token, const std::string& message);
/**
* @brief Tries to consume a token of the expected type from the source, reports an error if it can't.
* @param tokenType The expected type of token to consume.
* @param message An error message to display is the next token isn't of the expected type.
*/
void consume(Token::Type tokenType, const std::string& message);
/**
* @brief Writes an Op to the current batch.
* @param op The op to be written to the current batch.
*/
void emitOp(Op::Code op);
/**
* @brief Writes a pair of Ops to the current batch.
* @param a The first Op to be written.
* @param b The second Op to be written.
*/
void emitOps(Op::Code a, Op::Code b);
/**
* @brief Parses the next expression in the source code.
*/
void expression();
/**
* @brief Parses a double from the current position in the source code.
*/
void number();
/**
* @brief Parses a bracket grouping.
*/
void grouping();
/**
* @brief Parses a unary expression.
*/
void unary();
/**
* @brief Parses an expression with two operands.
*/
void binary();
/**
* @brief Parses a literal value.
*/
void literal();
/**
* @brief Parses a string.
*/
void string();
/**
* @brief Parses a matrix.
*/
void matrix();
/**
* @brief Parses a declaration.
*/
void declaration();
/**
* @brief Parses a statement.
*/
void statement();
/**
* @brief Parses an expression and pops the resulting value from the stack.
*/
void expressionStatement();
/**
* @brief Parses a variable declaration.
*/
void variableDeclaration();
/**
* @brief Parses a variable.
*/
void variable();
/**
* @brief Creates a sequence of instructions for loading a named variable.
* @param token The token to parse the variable from.
*/
void namedVariable(const Token& token);
/**
* @brief Recovers the parser from panic mode to prevent cascade errors.
*/
void synchronise();
/**
* @brief Parses a token from source code at the given precedence.
* @param precedence Parses a token from source with the given precedence.
*/
void parsePrecedence(Precedence precedence);
/**
* @brief Parses a variable name from the source stream.
* @param errorMessage Message to be displayed if parsing fails.
* @return The index of the new variable's name in the constants array of the current batch.
*/
size_t parseVariable(const std::string& errorMessage);
/**
* @brief Creates a string constant and returns its index for use in defining variables.
* @param token Token to lex the constant's name from.
* @return The index of the string.
*/
size_t identifierConstant(const Token& token);
/**
* @brief Writes Op::Codes to the current batch to define a global variable, given the index to its name.
* @param global The index of the global variable's name in the batch's constant pool.
*/
void defineVariable(size_t global);
/**
* @brief Adds an Op sequence to the current batch to store a constant.
* @param value The value to be added.
* @return The index of the constant in the current batch's constant table.
*/
size_t emitConstant(const Value& value);
/**
* @brief Consumes a token if it has the given type, otherwise returns false.
* @param type The type to check the next token for.
* @return True if the next token matches the given type, otherwise false.
*/
bool matchType(Token::Type type);
};
#endif //TITANPLUSPLUS_COMPILER_H