Skip to content

Commit 037e388

Browse files
committed
Add InstructionList class and cleaning #5
1 parent 663c71e commit 037e388

File tree

5 files changed

+88
-61
lines changed

5 files changed

+88
-61
lines changed

include/C_compilerData.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct CompilerData
6363
{
6464
codeg::StringDecomposer _decomposer;
6565

66-
std::list<codeg::Instruction*> _instructions;
66+
codeg::InstructionList _instructions;
6767
codeg::ReservedList _reservedKeywords;
6868

6969
codeg::PoolList _pools;

include/C_instruction.hpp

+24-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#define C_INSTRUCTION_H_INCLUDED
1919

2020
#include "C_stringDecomposer.hpp"
21+
#include <forward_list>
22+
#include <memory>
2123

2224
namespace codeg
2325
{
@@ -66,21 +68,37 @@ extern const char* ReadableStringBinaryOpcodes[];
6668

6769
struct CompilerData;
6870

69-
/// --------- Instructions ---------
70-
7171
class Instruction
7272
{
73-
protected:
74-
Instruction();
75-
virtual ~Instruction();
76-
7773
public:
74+
Instruction() = default;
75+
virtual ~Instruction() = default;
76+
7877
virtual std::string getName() const = 0;
7978

8079
virtual void compile(const codeg::StringDecomposer& input, codeg::CompilerData& data) = 0;
8180
virtual void compileDefinition(const codeg::StringDecomposer& input, codeg::CompilerData& data);
8281
};
8382

83+
class InstructionList
84+
{
85+
public:
86+
using InstructionListType = std::forward_list<std::unique_ptr<codeg::Instruction> >;
87+
88+
InstructionList() = default;
89+
~InstructionList() = default;
90+
91+
void clear();
92+
93+
void push(codeg::Instruction* newInstruction);
94+
codeg::Instruction* get(const std::string& name) const;
95+
96+
private:
97+
codeg::InstructionList::InstructionListType g_data;
98+
};
99+
100+
/// --------- Instructions ---------
101+
84102
class Instruction_set : public Instruction
85103
{
86104
/**

src/C_instruction.cpp

+23-3
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,34 @@ const char* ReadableStringBinaryOpcodes[]=
6767

6868
///Instruction
6969

70-
Instruction::Instruction(){}
71-
Instruction::~Instruction(){}
72-
7370
void Instruction::compileDefinition(const codeg::StringDecomposer& input, codeg::CompilerData& data)
7471
{
7572
data._functions.back().addLine(input._cleaned);
7673
}
7774

75+
///InstructionList
76+
77+
void InstructionList::clear()
78+
{
79+
this->g_data.clear();
80+
}
81+
82+
void InstructionList::push(codeg::Instruction* newInstruction)
83+
{
84+
this->g_data.push_front( std::unique_ptr<codeg::Instruction>(newInstruction) );
85+
}
86+
codeg::Instruction* InstructionList::get(const std::string& name) const
87+
{
88+
for (auto& valPtr : this->g_data)
89+
{
90+
if (valPtr.get()->getName() == name)
91+
{
92+
return valPtr.get();
93+
}
94+
}
95+
return nullptr;
96+
}
97+
7898
///Instruction_set
7999
Instruction_set::Instruction_set(){}
80100
Instruction_set::~Instruction_set(){}

src/C_keyword.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,10 @@ bool Keyword::process(const std::string& str, const codeg::KeywordTypes& wantedT
5252
}
5353

5454
///Instruction
55-
for (auto&& value : data._instructions)
55+
if ( data._instructions.get(str) != nullptr )
5656
{
57-
if (value->getName() == str)
58-
{
59-
this->_type = codeg::KeywordTypes::KEYWORD_INSTRUCTION;
60-
return this->_type == wantedType;
61-
}
57+
this->_type = codeg::KeywordTypes::KEYWORD_INSTRUCTION;
58+
return this->_type == wantedType;
6259
}
6360

6461
///Replacing with an existing macro

src/main.cpp

+37-45
Original file line numberDiff line numberDiff line change
@@ -211,36 +211,35 @@ int main(int argc, char **argv)
211211
data._reservedKeywords.push("end_def");
212212

213213
///Instructions
214-
data._instructions.push_back(new codeg::Instruction_set());
215-
data._instructions.push_back(new codeg::Instruction_unset());
216-
data._instructions.push_back(new codeg::Instruction_var());
217-
data._instructions.push_back(new codeg::Instruction_label());
218-
data._instructions.push_back(new codeg::Instruction_jump());
219-
data._instructions.push_back(new codeg::Instruction_restart());
220-
data._instructions.push_back(new codeg::Instruction_affect());
221-
data._instructions.push_back(new codeg::Instruction_get());
222-
data._instructions.push_back(new codeg::Instruction_write());
223-
data._instructions.push_back(new codeg::Instruction_choose());
224-
data._instructions.push_back(new codeg::Instruction_do());
225-
data._instructions.push_back(new codeg::Instruction_tick());
226-
data._instructions.push_back(new codeg::Instruction_brut());
227-
data._instructions.push_back(new codeg::Instruction_function());
228-
data._instructions.push_back(new codeg::Instruction_if());
229-
data._instructions.push_back(new codeg::Instruction_else());
230-
data._instructions.push_back(new codeg::Instruction_ifnot());
231-
data._instructions.push_back(new codeg::Instruction_end());
232-
data._instructions.push_back(new codeg::Instruction_call());
233-
data._instructions.push_back(new codeg::Instruction_clock());
234-
data._instructions.push_back(new codeg::Instruction_pool());
235-
data._instructions.push_back(new codeg::Instruction_import());
236-
data._instructions.push_back(new codeg::Instruction_definition());
237-
data._instructions.push_back(new codeg::Instruction_enddef());
214+
data._instructions.push(new codeg::Instruction_set());
215+
data._instructions.push(new codeg::Instruction_unset());
216+
data._instructions.push(new codeg::Instruction_var());
217+
data._instructions.push(new codeg::Instruction_label());
218+
data._instructions.push(new codeg::Instruction_jump());
219+
data._instructions.push(new codeg::Instruction_restart());
220+
data._instructions.push(new codeg::Instruction_affect());
221+
data._instructions.push(new codeg::Instruction_get());
222+
data._instructions.push(new codeg::Instruction_write());
223+
data._instructions.push(new codeg::Instruction_choose());
224+
data._instructions.push(new codeg::Instruction_do());
225+
data._instructions.push(new codeg::Instruction_tick());
226+
data._instructions.push(new codeg::Instruction_brut());
227+
data._instructions.push(new codeg::Instruction_function());
228+
data._instructions.push(new codeg::Instruction_if());
229+
data._instructions.push(new codeg::Instruction_else());
230+
data._instructions.push(new codeg::Instruction_ifnot());
231+
data._instructions.push(new codeg::Instruction_end());
232+
data._instructions.push(new codeg::Instruction_call());
233+
data._instructions.push(new codeg::Instruction_clock());
234+
data._instructions.push(new codeg::Instruction_pool());
235+
data._instructions.push(new codeg::Instruction_import());
236+
data._instructions.push(new codeg::Instruction_definition());
237+
data._instructions.push(new codeg::Instruction_enddef());
238238

239239
///Code
240240
data._code.resize(65536);
241241

242242
std::string readedLine;
243-
bool validInstruction = false;
244243

245244
try
246245
{
@@ -253,28 +252,21 @@ int main(int argc, char **argv)
253252

254253
if (data._decomposer._keywords.size() > 0)
255254
{
256-
validInstruction = false;
257-
258-
for (auto&& instruction : data._instructions)
259-
{
260-
if (instruction->getName() == data._decomposer._keywords[0])
261-
{
262-
validInstruction = true;
263-
264-
if ( data._writeLinesIntoDefinition )
265-
{//Compile in a definition (detect the end_def keyword)
266-
instruction->compileDefinition(data._decomposer, data);
267-
}
268-
else
269-
{//Compile
270-
instruction->compile(data._decomposer, data);
271-
}
272-
break;
255+
codeg::Instruction* instruction = data._instructions.get( data._decomposer._keywords[0] );
256+
257+
if (instruction != nullptr)
258+
{//Instruction founded
259+
if ( data._writeLinesIntoDefinition )
260+
{//Compile in a definition (detect the end_def keyword)
261+
instruction->compileDefinition(data._decomposer, data);
262+
}
263+
else
264+
{//Compile
265+
instruction->compile(data._decomposer, data);
273266
}
274267
}
275-
276-
if (!validInstruction)
277-
{
268+
else
269+
{//Bad instruction
278270
throw codeg::FatalError("unknown instruction \""+data._decomposer._keywords[0]+"\"");
279271
}
280272
}

0 commit comments

Comments
 (0)