-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.hpp
50 lines (47 loc) · 1.32 KB
/
syntax.hpp
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
#pragma once
#include <vector>
#include <stack>
#include "classes.hpp"
#include "error.hpp"
namespace pl {
auto checkSyntax(std::vector<RtData>& this_line) -> int {
std::stack<std::string> StBracket;
for (size_t i = 0; i < this_line.size(); i++) {
auto now = this_line[i];
auto before = this_line[i];
if (i > 0) before = this_line[i - 1];
if (now.type == 1) {
if (i > 0) {
if (before.data != ")" && before.data != "}") {
if (before.type == 1) CpError("Invalid Syntax");
}
}
if (now.data == "(") StBracket.push("(");
if (now.data == "{") StBracket.push("{");
if (now.data == ")") {
if (before.data == "(")
CpError("() is empty.");
if (StBracket.empty()) CpError("() is not matched.");
if (StBracket.top() != "(") CpError("() is not matched.");;
StBracket.pop();
}
if (now.data == "}") {
if (before.data == "{")
CpError("{} is empty.");
if (StBracket.empty()) CpError("{} is not matched.");
if (StBracket.top() != "{") CpError("{} is not matched.");
StBracket.pop();
}
}
else {
if (i > 0) {
if (before.type != 1) CpError("Invalid Syntax");
}
}
}
if (!StBracket.empty()) {
if (StBracket.top() == "{") CpError("{} is not matched.");
if (StBracket.top() == "(") CpError("() is not matched.");
}
}
}