Skip to content

Commit 5ec45f1

Browse files
committed
Update 1.1.1
1 parent 171de79 commit 5ec45f1

2 files changed

Lines changed: 134 additions & 52 deletions

File tree

Kitanai.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,36 @@ void Token::execute(Program* prg)
238238
if (_execDebug) std::cout << "[Exe:CurrentStackValue] : Current StackValue (at $" << prg->getStackPosition().first
239239
<< "+" << prg->getStackPosition().second << "): " << valueAt.getSummary() << std::endl;
240240
}
241+
else if (getType() == TokenType::StackSize) {
242+
this->type = TokenType::Number;
243+
this->value = std::to_string(prg->getStackSize());
244+
}
245+
else if (getType() == TokenType::StackLeft) {
246+
prg->setStackPosition(prg->getStackPosition().second - 1);
247+
this->type = TokenType::Null;
248+
this->value = "";
249+
}
250+
else if (getType() == TokenType::StackRight) {
251+
prg->setStackPosition(prg->getStackPosition().second + 1);
252+
this->type = TokenType::Null;
253+
this->value = "";
254+
}
255+
else if (getType() == TokenType::CurrentStackPosition) {
256+
this->type = TokenType::String;
257+
this->value = prg->getStackPosition().first;
258+
}
259+
else if (getType() == TokenType::CurrentSubStackPosition) {
260+
this->type = TokenType::Number;
261+
this->value = std::to_string(prg->getStackPosition().second);
262+
}
241263
else if (getType() == TokenType::StackAccess) {
242264
if (_execDebug) std::cout << "[Exe:StackAccess] : Store value : "
243265
<< getInstructionContent(parametersExecution).getSummary() << " at $"
244266
<< prg->getStackPosition().first << "+" << prg->getStackPosition().second << std::endl;
245267

246268
prg->storeInStack(getInstructionContent(parametersExecution));
269+
this->type = TokenType::Null;
270+
this->value = "";
247271
}
248272
else if (getType() == TokenType::Instruction && getValue() != "Ignore") {
249273
this->type = getInstructionContent(parametersExecution).getType();
@@ -269,16 +293,24 @@ void Token::execute(Program* prg)
269293
else if (getType() == TokenType::GotoNoOrigin) {
270294
prg->setSeekedFlag(std::stoi(getInstructionContent(parametersExecution).getValue()));
271295
prg->stopExecution(TokenType::Goto);
296+
this->type = TokenType::Null;
297+
this->value = "";
272298
}
273299
else if (getType() == TokenType::ToggleExecution) {
274300
prg->stopExecution(TokenType::ToggleExecution);
301+
this->type = TokenType::Null;
302+
this->value = "";
275303
}
276304
else if (getType() == TokenType::End) {
277305
prg->stopProgram();
306+
this->type = TokenType::Null;
307+
this->value = "";
278308
}
279309
}
280310
else if (getType() == TokenType::ToggleExecution && prg->getPauseCause() == TokenType::ToggleExecution) {
281311
prg->startExecution();
312+
this->type = TokenType::Null;
313+
this->value = "";
282314
}
283315
else if (TokenType::Flag == this->getType() && prg->getSeekedFlag() == std::stoi(this->getValue()) && prg->getPauseCause() == TokenType::Goto) {
284316
if (_execDebug) std::cout << "[Exe:Flag] Flag found : Restarting execution" << std::endl;
@@ -507,6 +539,15 @@ void Program::setStackPosition(int pos)
507539
{
508540
stackPosition[stackPosition.size() - 1] = std::pair<std::string, int>(stackPosition[stackPosition.size() - 1].first, pos);
509541
}
542+
int Program::getStackSize()
543+
{
544+
if (stack.find(stackPosition[stackPosition.size() - 1].first) == stack.end()) {
545+
std::cout << "[Error] : Unknown Stack Position : " << stackPosition[stackPosition.size() - 1].first << std::endl;
546+
}
547+
else {
548+
return stack[stackPosition[stackPosition.size() - 1].first].size();
549+
}
550+
}
510551
Token Program::getStackAt()
511552
{
512553
if (stack.find(stackPosition[stackPosition.size() - 1].first) == stack.end()) {

Kitanai.hpp

Lines changed: 93 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,49 +35,61 @@ namespace TokenType
3535
Goto,
3636
GotoNoOrigin,
3737
Origin,
38-
NewInstruction,
3938
End,
4039
ToggleExecution,
40+
StackSize,
41+
StackLeft,
42+
StackRight,
43+
CurrentStackPosition,
44+
CurrentSubStackPosition,
4145
Null
4246
};
4347

44-
static std::map<std::string, std::pair<std::string, Type>> TypeDataK = {
45-
{"(", {"OpenInstruction", OpenInstruction}},
46-
{")", {"CloseInstruction", CloseInstruction}},
47-
{"[", {"OpenStackAccess", OpenStackAccess}},
48-
{"]", {"CloseStackAccess", CloseStackAccess}},
49-
{"?", {"Condition", Condition}},
50-
{"$", {"StackAt", StackAt}},
51-
{"@", {"CurrentStackValue", CurrentStackValue}},
52-
{"#", {"Flag", Flag}},
53-
{"&", {"Goto", Goto}},
54-
{"*", {"GotoNoOrigin", GotoNoOrigin}},
55-
{"~", {"Origin", Origin}},
56-
{";", {"NewInstruction", NewInstruction}},
57-
{"!", {"ToggleExecution", ToggleExecution}},
48+
static std::map<std::string, std::pair<std::string, Type>> TypeDataK = {
49+
{"(", {"OpenInstruction", OpenInstruction}},
50+
{")", {"CloseInstruction", CloseInstruction}},
51+
{"[", {"OpenStackAccess", OpenStackAccess}},
52+
{"]", {"CloseStackAccess", CloseStackAccess}},
53+
{"?", {"Condition", Condition}},
54+
{"$", {"StackAt", StackAt}},
55+
{"@", {"CurrentStackValue", CurrentStackValue}},
56+
{"#", {"Flag", Flag}},
57+
{"&", {"Goto", Goto}},
58+
{"*", {"GotoNoOrigin", GotoNoOrigin}},
59+
{"~", {"Origin", Origin}},
60+
{"!", {"ToggleExecution", ToggleExecution}},
61+
{"^", {"StackSize", StackSize}},
62+
{"<", {"StackLeft", StackLeft}},
63+
{">", {"StackRight", StackRight}},
64+
{":", {"CurrentStackPosition", CurrentStackPosition}},
65+
{";", {"CurrentSubStackPosition", CurrentSubStackPosition}},
5866
{"%", {"End", End}}
5967
};
60-
static std::map<Type, std::pair<std::string, std::string>> TypeDataR = {
61-
{OpenInstruction, {"OpenInstruction", "("}},
62-
{CloseInstruction, {"CloseInstruction", ")"}},
63-
{OpenStackAccess, {"OpenStackAccess", "["}},
64-
{CloseStackAccess, {"CloseStackAccess", "]"}},
65-
{Condition, {"Condition", "?"}},
66-
{StackAt, {"StackAt", "$"}},
67-
{CurrentStackValue, {"CurrentStackValue", "@"}},
68-
{Flag, {"Flag", "#"}},
69-
{DynamicFlag, {"DynamicFlag", ""}},
70-
{Goto, {"Goto", "&"}},
71-
{GotoNoOrigin, {"GotoNoOrigin", "*"}},
72-
{Origin, {"Origin", "~"}},
73-
{NewInstruction, {"NewInstruction", ";"}},
74-
{End, {"End", "%"}},
75-
{ToggleExecution, {"ToggleExecution", "!"}},
76-
{Function, {"Function", ""}},
77-
{String, {"String", ""}},
78-
{Number, {"Number", ""}},
79-
{Instruction, {"Instruction", ""}},
80-
{StackAccess, {"StackAccess", ""}},
68+
static std::map<Type, std::pair<std::string, std::string>> TypeDataR = {
69+
{OpenInstruction, {"OpenInstruction", "("}},
70+
{CloseInstruction, {"CloseInstruction", ")"}},
71+
{OpenStackAccess, {"OpenStackAccess", "["}},
72+
{CloseStackAccess, {"CloseStackAccess", "]"}},
73+
{Condition, {"Condition", "?"}},
74+
{StackAt, {"StackAt", "$"}},
75+
{CurrentStackValue, {"CurrentStackValue", "@"}},
76+
{Flag, {"Flag", "#"}},
77+
{DynamicFlag, {"DynamicFlag", ""}},
78+
{Goto, {"Goto", "&"}},
79+
{GotoNoOrigin, {"GotoNoOrigin", "*"}},
80+
{Origin, {"Origin", "~"}},
81+
{End, {"End", "%"}},
82+
{ToggleExecution, {"ToggleExecution", "!"}},
83+
{Function, {"Function", ""}},
84+
{String, {"String", ""}},
85+
{Number, {"Number", ""}},
86+
{Instruction, {"Instruction", ""}},
87+
{StackAccess, {"StackAccess", ""}},
88+
{StackSize, {"StackSize", "^"}},
89+
{StackLeft, {"StackLeft", "<"}},
90+
{StackRight, {"StackRight", ">"}},
91+
{CurrentStackPosition, {"CurrentStackPosition", ":"}},
92+
{CurrentSubStackPosition, {"CurrentSubStackPosition", ";"}},
8193
{Null, {"Null", ""}}
8294
};
8395
static std::vector<std::vector<Type>> TypeParameters = {
@@ -294,6 +306,32 @@ namespace StdLib
294306
}
295307
return storeInstruction;
296308
}
309+
Token f_read(const std::vector<Token> tokens) {
310+
Token filenameToken = tokens[0];
311+
std::string filename = filenameToken.getValue();
312+
Token stackPlace = tokens[1];
313+
Token storeInstruction(TokenType::Instruction);
314+
Token accessMemory = Token(TokenType::StackAt);
315+
accessMemory.addParameter(stackPlace);
316+
Token zeroPos = Token(TokenType::Number, "0");
317+
Token accessSubMem = Token(TokenType::StackAt);
318+
accessSubMem.addParameter(zeroPos);
319+
storeInstruction.addParameter(accessMemory);
320+
storeInstruction.addParameter(accessSubMem);
321+
std::ifstream infile(filename);
322+
std::string line;
323+
while (std::getline(infile, line))
324+
{
325+
Token stackAcc(TokenType::StackAccess);
326+
stackAcc.addParameter(Token(TokenType::String, line));
327+
storeInstruction.addParameter(stackAcc);
328+
storeInstruction.addParameter(Token(TokenType::StackRight));
329+
}
330+
return storeInstruction;
331+
}
332+
Token f_write(const std::vector<Token> tokens) {
333+
return Token(TokenType::Null);
334+
}
297335
Token fBuild(std::string funcname, int amount, std::function<Token(const std::vector<Token>)> func, std::vector<Token> parameters) {
298336
if (amount != parameters.size()) {
299337
std::cout << "[Error] Number of parameters not correct in function : " << funcname << std::endl;
@@ -308,26 +346,28 @@ namespace StdLib
308346
std::pair<std::function<Token(const std::vector<Token>)>, int> rpart = fRightPart(name, func, amount);
309347
return std::pair<std::string, std::pair<std::function<Token(const std::vector<Token>)>, int>>(name, rpart);
310348
}
311-
static std::map<std::string, std::pair<std::function<Token(const std::vector<Token>)>, int>> FunctionsName = {
312-
f("add", f_add, 2),
313-
f("sub", f_sub, 2),
314-
f("mul", f_mul, 2),
315-
f("div", f_div, 2),
349+
static std::map<std::string, std::pair<std::function<Token(const std::vector<Token>)>, int>> FunctionsName = {
350+
f("add", f_add, 2),
351+
f("sub", f_sub, 2),
352+
f("mul", f_mul, 2),
353+
f("div", f_div, 2),
316354
f("mod", f_mod, 2),
317355
f("not", f_not, 1),
318-
f("eq", f_eq, 2),
319-
f("neq", f_neq, 2),
320-
f("gt", f_gt, 2),
321-
f("ge", f_ge, 2),
322-
f("lt", f_lt, 2),
323-
f("le", f_le, 2),
324-
f("print", f_print, 1),
325-
f("input", f_input, 1),
326-
f("string", f_string, 1),
327-
f("int", f_int, 1),
328-
f("random", f_random, 0),
356+
f("eq", f_eq, 2),
357+
f("neq", f_neq, 2),
358+
f("gt", f_gt, 2),
359+
f("ge", f_ge, 2),
360+
f("lt", f_lt, 2),
361+
f("le", f_le, 2),
362+
f("print", f_print, 1),
363+
f("input", f_input, 1),
364+
f("string", f_string, 1),
365+
f("int", f_int, 1),
366+
f("random", f_random, 0),
329367
f("time", f_time, 0),
330368
f("split", f_split, 3),
369+
f("read", f_read, 2),
370+
f("write", f_write, 2)
331371
};
332372

333373
static std::map<std::string, Token*> customFunctions = {};
@@ -398,6 +438,7 @@ class Program
398438
void setDepth(int depth);
399439
void setStackPosition(std::string pos);
400440
void setStackPosition(int pos);
441+
int getStackSize();
401442
Token getStackAt();
402443
void storeInStack(Token token);
403444
void stopExecution(TokenType::Type cause);

0 commit comments

Comments
 (0)