Skip to content

Commit 5dade08

Browse files
committed
Format files
1 parent 221f7d9 commit 5dade08

File tree

11 files changed

+409
-414
lines changed

11 files changed

+409
-414
lines changed

analyser/analyser.cpp

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ std::optional<CompilationError> Analyser::analyseProgram() {
2424

2525
// <主过程>
2626
auto err = analyseMain();
27-
if (err.has_value())
28-
return err;
27+
if (err.has_value()) return err;
2928

3029
// 'end'
3130
auto ed = nextToken();
@@ -57,8 +56,7 @@ std::optional<CompilationError> Analyser::analyseConstantDeclaration() {
5756
while (true) {
5857
// 预读一个 token,不然不知道是否应该用 <常量声明> 推导
5958
auto next = nextToken();
60-
if (!next.has_value())
61-
return {};
59+
if (!next.has_value()) return {};
6260
// 如果是 const 那么说明应该推导 <常量声明> 否则直接返回
6361
if (next.value().GetType() != TokenType::CONST) {
6462
unreadToken();
@@ -84,8 +82,7 @@ std::optional<CompilationError> Analyser::analyseConstantDeclaration() {
8482
// <常表达式>
8583
int32_t val;
8684
auto err = analyseConstantExpression(val);
87-
if (err.has_value())
88-
return err;
85+
if (err.has_value()) return err;
8986

9087
// ';'
9188
next = nextToken();
@@ -130,8 +127,7 @@ std::optional<CompilationError> Analyser::analyseStatementSequence() {
130127
while (true) {
131128
// 预读
132129
auto next = nextToken();
133-
if (!next.has_value())
134-
return {};
130+
if (!next.has_value()) return {};
135131
unreadToken();
136132
if (next.value().GetType() != TokenType::IDENTIFIER &&
137133
next.value().GetType() != TokenType::PRINT &&
@@ -140,19 +136,19 @@ std::optional<CompilationError> Analyser::analyseStatementSequence() {
140136
}
141137
std::optional<CompilationError> err;
142138
switch (next.value().GetType()) {
143-
// 这里需要你针对不同的预读结果来调用不同的子程序
144-
// 注意我们没有针对空语句单独声明一个函数,因此可以直接在这里返回
145-
default:
146-
break;
139+
// 这里需要你针对不同的预读结果来调用不同的子程序
140+
// 注意我们没有针对空语句单独声明一个函数,因此可以直接在这里返回
141+
default:
142+
break;
147143
}
148144
}
149145
return {};
150146
}
151147

152148
// <常表达式> ::= [<符号>]<无符号整数>
153149
// 需要补全
154-
std::optional<CompilationError>
155-
Analyser::analyseConstantExpression(int32_t &out) {
150+
std::optional<CompilationError> Analyser::analyseConstantExpression(
151+
int32_t &out) {
156152
// out 是常表达式的结果
157153
// 这里你要分析常表达式并且计算结果
158154
// 注意以下均为常表达式
@@ -165,15 +161,13 @@ Analyser::analyseConstantExpression(int32_t &out) {
165161
std::optional<CompilationError> Analyser::analyseExpression() {
166162
// <项>
167163
auto err = analyseItem();
168-
if (err.has_value())
169-
return err;
164+
if (err.has_value()) return err;
170165

171166
// {<加法型运算符><项>}
172167
while (true) {
173168
// 预读
174169
auto next = nextToken();
175-
if (!next.has_value())
176-
return {};
170+
if (!next.has_value()) return {};
177171
auto type = next.value().GetType();
178172
if (type != TokenType::PLUS_SIGN && type != TokenType::MINUS_SIGN) {
179173
unreadToken();
@@ -182,8 +176,7 @@ std::optional<CompilationError> Analyser::analyseExpression() {
182176

183177
// <项>
184178
err = analyseItem();
185-
if (err.has_value())
186-
return err;
179+
if (err.has_value()) return err;
187180

188181
// 根据结果生成指令
189182
if (type == TokenType::PLUS_SIGN)
@@ -217,8 +210,7 @@ std::optional<CompilationError> Analyser::analyseOutputStatement() {
217210

218211
// <表达式>
219212
auto err = analyseExpression();
220-
if (err.has_value())
221-
return err;
213+
if (err.has_value()) return err;
222214

223215
// ')'
224216
next = nextToken();
@@ -267,31 +259,28 @@ std::optional<CompilationError> Analyser::analyseFactor() {
267259
return std::make_optional<CompilationError>(
268260
_current_pos, ErrorCode::ErrIncompleteExpression);
269261
switch (next.value().GetType()) {
270-
// 这里和 <语句序列> 类似,需要根据预读结果调用不同的子程序
271-
// 但是要注意 default 返回的是一个编译错误
272-
default:
273-
return std::make_optional<CompilationError>(
274-
_current_pos, ErrorCode::ErrIncompleteExpression);
262+
// 这里和 <语句序列> 类似,需要根据预读结果调用不同的子程序
263+
// 但是要注意 default 返回的是一个编译错误
264+
default:
265+
return std::make_optional<CompilationError>(
266+
_current_pos, ErrorCode::ErrIncompleteExpression);
275267
}
276268

277269
// 取负
278-
if (prefix == -1)
279-
_instructions.emplace_back(Operation::SUB, 0);
270+
if (prefix == -1) _instructions.emplace_back(Operation::SUB, 0);
280271
return {};
281272
}
282273

283274
std::optional<Token> Analyser::nextToken() {
284-
if (_offset == _tokens.size())
285-
return {};
275+
if (_offset == _tokens.size()) return {};
286276
// 考虑到 _tokens[0..._offset-1] 已经被分析过了
287277
// 所以我们选择 _tokens[0..._offset-1] 的 EndPos 作为当前位置
288278
_current_pos = _tokens[_offset].GetEndPos();
289279
return _tokens[_offset++];
290280
}
291281

292282
void Analyser::unreadToken() {
293-
if (_offset == 0)
294-
DieAndPrint("analyser unreads token from the begining.");
283+
if (_offset == 0) DieAndPrint("analyser unreads token from the begining.");
295284
_current_pos = _tokens[_offset - 1].GetEndPos();
296285
_offset--;
297286
}
@@ -335,4 +324,4 @@ bool Analyser::isInitializedVariable(const std::string &s) {
335324
bool Analyser::isConstant(const std::string &s) {
336325
return _consts.find(s) != _consts.end();
337326
}
338-
} // namespace miniplc0
327+
} // namespace miniplc0

analyser/analyser.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
#pragma once
22

3-
#include "error/error.h"
4-
#include "instruction/instruction.h"
5-
#include "tokenizer/token.h"
6-
7-
#include <cstddef> // for std::size_t
3+
#include <cstddef> // for std::size_t
84
#include <cstdint>
95
#include <map>
106
#include <optional>
117
#include <utility>
128
#include <vector>
139

10+
#include "error/error.h"
11+
#include "instruction/instruction.h"
12+
#include "tokenizer/token.h"
13+
14+
1415
namespace miniplc0 {
1516

1617
class Analyser final {
17-
private:
18+
private:
1819
using uint64_t = std::uint64_t;
1920
using int64_t = std::int64_t;
2021
using uint32_t = std::uint32_t;
2122
using int32_t = std::int32_t;
2223

23-
public:
24+
public:
2425
Analyser(std::vector<Token> v)
25-
: _tokens(std::move(v)), _offset(0), _instructions({}),
26-
_current_pos(0, 0), _uninitialized_vars({}), _vars({}), _consts({}),
26+
: _tokens(std::move(v)),
27+
_offset(0),
28+
_instructions({}),
29+
_current_pos(0, 0),
30+
_uninitialized_vars({}),
31+
_vars({}),
32+
_consts({}),
2733
_nextTokenIndex(0) {}
2834
Analyser(Analyser &&) = delete;
2935
Analyser(const Analyser &) = delete;
@@ -33,7 +39,7 @@ class Analyser final {
3339
std::pair<std::vector<Instruction>, std::optional<CompilationError>>
3440
Analyse();
3541

36-
private:
42+
private:
3743
// 所有的递归子程序
3844

3945
// <程序>
@@ -86,7 +92,7 @@ class Analyser final {
8692
// 获得 {变量,常量} 在栈上的偏移
8793
int32_t getIndex(const std::string &);
8894

89-
private:
95+
private:
9096
std::vector<Token> _tokens;
9197
std::size_t _offset;
9298
std::vector<Instruction> _instructions;
@@ -103,4 +109,4 @@ class Analyser final {
103109
// 下一个 token 在栈的偏移
104110
int32_t _nextTokenIndex;
105111
};
106-
} // namespace miniplc0
112+
} // namespace miniplc0

error/error.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <string>
66
#include <utility>
77

8-
98
namespace miniplc0 {
109

1110
inline void DieAndPrint(std::string condition) {
@@ -20,12 +19,12 @@ inline void DieAndPrint(std::string condition) {
2019

2120
// To keep it simple, we don't create an error system.
2221
enum ErrorCode {
23-
ErrNoError, // Should be only used internally.
22+
ErrNoError, // Should be only used internally.
2423
ErrStreamError,
2524
ErrEOF,
2625
ErrInvalidInput,
2726
ErrInvalidIdentifier,
28-
ErrIntegerOverflow, // int32_t overflow.
27+
ErrIntegerOverflow, // int32_t overflow.
2928
ErrNoBegin,
3029
ErrNoEnd,
3130
ErrNeedIdentifier,
@@ -42,10 +41,10 @@ enum ErrorCode {
4241
};
4342

4443
class CompilationError final {
45-
private:
44+
private:
4645
using uint64_t = std::uint64_t;
4746

48-
public:
47+
public:
4948
friend void swap(CompilationError &lhs, CompilationError &rhs);
5049

5150
CompilationError(uint64_t line, uint64_t column, ErrorCode err)
@@ -71,7 +70,7 @@ class CompilationError final {
7170
std::pair<uint64_t, uint64_t> GetPos() const { return _pos; }
7271
ErrorCode GetCode() const { return _err; }
7372

74-
private:
73+
private:
7574
std::pair<uint64_t, uint64_t> _pos;
7675
ErrorCode _err;
7776
};
@@ -81,4 +80,4 @@ inline void swap(CompilationError &lhs, CompilationError &rhs) {
8180
swap(lhs._pos, rhs._pos);
8281
swap(lhs._err, rhs._err);
8382
}
84-
} // namespace miniplc0
83+
} // namespace miniplc0

0 commit comments

Comments
 (0)