-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkShop.cpp
More file actions
200 lines (162 loc) · 6.63 KB
/
workShop.cpp
File metadata and controls
200 lines (162 loc) · 6.63 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
#include "workShop.h"
#include "predefineSymbol.h"
#include "binaryOpNode.h"
#include "identifierNode.h"
#include "intergerLiteralNode.h"
#include "vmCodeGenerator.h"
#include "parser.h"
#include "lrParserGenerator.h"
#include "grammar.h"
#include "stackItem.h"
WorkShop::WorkShop()
{
}
WorkShop& WorkShop::getInstance()
{
static WorkShop instance;
return instance;
}
std::vector<std::string> WorkShop::loadProgram(const std::string &source)
{
Lexer lexer(source);
std::vector<Token> tokens;
while(auto token = lexer.getNextToken())
{
std::cout << tokenTypeToString(token->type) << " : " << token->lexeme << std::endl;
tokens.push_back(*token);
if(token->type == _TokenType_::EOF_TOKEN)
{
break;
}
}
tokens.push_back(Token{_TokenType_::EOF_TOKEN, ""});
GrammarSymbol nt_Expr{"Expr", SymbolType::NonTerminal};
GrammarSymbol nt_Term{"Term", SymbolType::NonTerminal};
GrammarSymbol nt_Factor{"Factor", SymbolType::NonTerminal};
std::vector<ProductionRule> rules;
rules.emplace_back(nt_Expr,
std::vector<GrammarSymbol>{nt_Expr, PredefineSymbol::SYMBOL_PLUS, nt_Term},
[](std::vector<StackItem> &&stackItems) -> std::unique_ptr<AstNode>
{
if (stackItems.size() != 3)
{
throw std::runtime_error("Expr production rule error");
}
return std::make_unique<BinaryOpNode>(
std::move(std::get<std::unique_ptr<AstNode>>(stackItems[0].value)),
std::move(std::get<std::unique_ptr<AstNode>>(stackItems[2].value)),
BinaryOpType::ADD);
});
rules.emplace_back(nt_Expr,
std::vector<GrammarSymbol>{nt_Term},
[](std::vector<StackItem> &&stackItems) -> std::unique_ptr<AstNode>
{
if (stackItems.size() != 1)
{
throw std::runtime_error("Expr production rule error");
}
return std::move(std::get<std::unique_ptr<AstNode>>(stackItems[0].value));
});
rules.emplace_back(nt_Term,
std::vector<GrammarSymbol>{nt_Term, PredefineSymbol::SYMBOL_MULTIPLY, nt_Factor},
[](std::vector<StackItem> &&stackItems) -> std::unique_ptr<AstNode>
{
if (stackItems.size() != 3)
{
throw std::runtime_error("Term production rule error");
}
return std::make_unique<BinaryOpNode>(
std::move(std::get<std::unique_ptr<AstNode>>(stackItems[0].value)),
std::move(std::get<std::unique_ptr<AstNode>>(stackItems[2].value)),
BinaryOpType::MULTIPLY);
});
rules.emplace_back(nt_Term,
std::vector<GrammarSymbol>{nt_Factor},
[](std::vector<StackItem> &&stackItems) -> std::unique_ptr<AstNode>
{
if (stackItems.size() != 1)
{
throw std::runtime_error("Term production rule error");
}
return std::move(std::get<std::unique_ptr<AstNode>>(stackItems[0].value));
});
rules.emplace_back(nt_Factor,
std::vector<GrammarSymbol>{PredefineSymbol::LEFT_PAREN, nt_Expr, PredefineSymbol::RIGHT_PAREN},
[](std::vector<StackItem> &&stackItems) -> std::unique_ptr<AstNode>
{
if (stackItems.size() != 3)
{
throw std::runtime_error("Factor production rule error");
}
return std::move(std::get<std::unique_ptr<AstNode>>(stackItems[1].value));
});
rules.emplace_back(nt_Factor,
std::vector<GrammarSymbol>{PredefineSymbol::SYMBOL_IDENTIFIER},
[](std::vector<StackItem> &&stackItems) -> std::unique_ptr<AstNode>
{
if (stackItems.size() != 1)
{
throw std::runtime_error("Factor production rule error");
}
return std::move(std::make_unique<IdentifierNode>(std::get<Token>(stackItems[0].value).lexeme));
});
rules.emplace_back(nt_Factor,
std::vector<GrammarSymbol>{PredefineSymbol::SYMBOL_NUMBER},
[](std::vector<StackItem> &&stackItems) -> std::unique_ptr<AstNode>
{
if (stackItems.size() != 1)
{
throw std::runtime_error("Factor production rule error");
}
return std::move(std::make_unique<IntegerLiteralNode>(std::stoi(std::get<Token>(stackItems[0].value).lexeme)));
});
Grammar grammar(std::move(rules), nt_Expr);
grammar.calculateFirstSets();
grammar.calculateFollowSets();
Parser parser(grammar);
parser.parse(tokens);
parser.getIRProgram().print();
std::cout << "--------------------------" << std::endl;
VmCodeGenerator codeGenerator;
std::vector<Instruction> instructions = codeGenerator.translate(parser.getIRProgram().getInstructions());
std::string src;
for (const auto& instruction : instructions)
{
src += instruction.toString() + "\n";
std::cout << instruction.toString() << std::endl;
}
vm_.loadProgram(src);
return vm_.getSourceCode();
}
std::vector<std::string> WorkShop::getVMSrc()
{
return vm_.getSourceCode();
}
size_t WorkShop::getPC() const
{
return vm_.getProgramCounter();
}
void WorkShop::step()
{
vm_.step();
}
void WorkShop::resetProgram()
{
vm_.resetProgram();
}
const std::vector<int> &WorkShop::getVMRegisters() const
{
return vm_.getRegisters();
}
const std::vector<int> &WorkShop::getVMMemory() const
{
return vm_.getMemory();
}
bool WorkShop::getVMZeroFlag() const
{
return vm_.getZeroFlag();
}
bool WorkShop::getVMSignFlag() const
{
return vm_.getSignFlag();
}