-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariableObject.hpp
142 lines (132 loc) · 5.03 KB
/
VariableObject.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
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
#ifndef NEKO_INTERPRETER_VARIABLEOBJECT_HPP
#define NEKO_INTERPRETER_VARIABLEOBJECT_HPP
#include "token.hpp"
#include "exceptions.hpp"
#include "expressions.hpp"
bool operator>(VariableObject a, VariableObject b) {
return a.name > b.name;
}
bool operator<(VariableObject a, VariableObject b) {
return a.name < b.name;
}
bool operator==(VariableObject a, VariableObject b) {
return a.name == b.name;
}
bool isDeclaredClass(Token token) {
return Classes.find(token.source) != Classes.end();
}
bool isAssignmentOperator(Item item) {
return item.token.type == AssignmentOperator;
}
VariableAssignmentReturned parseVariableAssignment(const vector<Token> &input, int &index) {
if (input.size() < 3) {
return Exception(VariableDeclarationError, getLineIndex(input, index));
}
if (input[index].type != Name) {
return Exception(VariableAssignmentError, getLineIndex(input, index));
}
string name = input[index].source;
if (nameDeclaration(name) == Undeclared) {
return Exception(VariableAssignmentError, getLineIndex(input, index));
}
if (not scopeManager.get(name).isMutable) {
return Exception(ConstAssignment, getLineIndex(input, index));
}
index = nextIndex(input, index);
if (input[index].type != AssignmentOperator) {
return Exception(VariableAssignmentError, getLineIndex(input, index));
}
Token assignmentOperator = input[index];
index = nextIndex(input, index);
ParseExpressionReturned result = parseExpression(input, index);
if (result.exception.type != Nothing) {
return Exception(result.exception.type, getLineIndex(input, index));
}
CalculateReturned calculateReturned = Calculate(result.source);
if (calculateReturned.exception.type != Nothing) {
return Exception(calculateReturned.exception.type, getLineIndex(input, index));
}
if (assignmentOperator.source == "=") {
VariableObject v = scopeManager.get(name);
if (not v.isAnyType() and not v.containType(calculateReturned.item.type)) {
return Exception(TypeError, getLineIndex(input, index));
}
scopeManager.setItem(name, calculateReturned.item);
} else {
string s = assignmentOperator.source;
auto processed = Process(calculateReturned.item, scopeManager.get(name).item,
getToken(sliceString(s, 0, s.size() - 2)));
if (processed.exception.type != Nothing) {
return Exception(processed.exception.type, getLineIndex(input, index));
}
VariableObject v = scopeManager.get(name);
if (v.isAnyType() and v.containType(processed.item.type)) {
return Exception(TypeError, getLineIndex(input, index));
}
scopeManager.setItem(name, processed.item);
}
return scopeManager.get(name).item;
}
Exception parseVariableDeclaration(const vector<Token> &input, int &index) {
if (input.size() < 3) {
return Exception(VariableDeclarationError, getLineIndex(input, index));
}
VariableObject variable;
if (contain({"var", "val"}, input[index].source)) {
variable.isMutable = input[index].source == "var";
index = nextIndex(input, index);
}
if (input[index].type != Name) {
return Exception(VariableDeclarationError, getLineIndex(input, index));
}
variable.name = input[index].source;
index = nextIndex(input, index);
if (not contain({"=", ":=", ":"}, input[index].source)) {
return Exception(VariableDeclarationError, getLineIndex(input, index));
}
// типы переменной
if (input[index].source == ":") {
index = nextIndex(input, index);
if (not contain({Name}, input[index].type)) {
return Exception(VariableDeclarationError, getLineIndex(input, index));
}
if (not isBuildInType(input[index]) and not isDeclaredClass(input[index])) {
return Exception(UnknownTypeError, getLineIndex(input, index));
}
variable.addType(input[index].source);
index = nextIndex(input, index);
while (not contain({"=", ":="}, input[index].source)) {
if (input[index].source != ",") {
return Exception(VariableDeclarationError, getLineIndex(input, index));
}
index = nextIndex(input, index);
if (not contain({Name}, input[index].type)) {
return Exception(VariableDeclarationError, getLineIndex(input, index));
}
if (not isBuildInType(input[index]) and not isDeclaredClass(input[index])) {
return Exception(UnknownTypeError, getLineIndex(input, index));
}
variable.addType(input[index].source);
index = nextIndex(input, index);
}
}
index = nextIndex(input, index);
ParseExpressionReturned result = parseExpression(input, index);
if (result.exception.type != Nothing) {
return Exception(result.exception.type, getLineIndex(input, index));
}
CalculateReturned calculateReturned = Calculate(result.source);
if (calculateReturned.exception.type != Nothing) {
return Exception(calculateReturned.exception.type, getLineIndex(input, index));
}
if (not variable.isAnyType() and not variable.containType(calculateReturned.item.type)) {
return Exception(TypeError, getLineIndex(input, index));
}
variable.item = calculateReturned.item;
Exception exception = scopeManager.add(variable);
if (exception.type != Nothing) {
return exception;
}
return Exception(Nothing);
}
#endif //NEKO_INTERPRETER_VARIABLEOBJECT_HPP