From 5ec45f1e6da3c1eaa339a35371e83c551f37214d Mon Sep 17 00:00:00 2001 From: Sygmei Date: Tue, 31 Jan 2017 14:18:46 +0100 Subject: [PATCH] Update 1.1.1 --- Kitanai.cpp | 41 +++++++++++++++ Kitanai.hpp | 145 +++++++++++++++++++++++++++++++++------------------- 2 files changed, 134 insertions(+), 52 deletions(-) diff --git a/Kitanai.cpp b/Kitanai.cpp index 62491f2..9b90bbf 100644 --- a/Kitanai.cpp +++ b/Kitanai.cpp @@ -238,12 +238,36 @@ void Token::execute(Program* prg) if (_execDebug) std::cout << "[Exe:CurrentStackValue] : Current StackValue (at $" << prg->getStackPosition().first << "+" << prg->getStackPosition().second << "): " << valueAt.getSummary() << std::endl; } + else if (getType() == TokenType::StackSize) { + this->type = TokenType::Number; + this->value = std::to_string(prg->getStackSize()); + } + else if (getType() == TokenType::StackLeft) { + prg->setStackPosition(prg->getStackPosition().second - 1); + this->type = TokenType::Null; + this->value = ""; + } + else if (getType() == TokenType::StackRight) { + prg->setStackPosition(prg->getStackPosition().second + 1); + this->type = TokenType::Null; + this->value = ""; + } + else if (getType() == TokenType::CurrentStackPosition) { + this->type = TokenType::String; + this->value = prg->getStackPosition().first; + } + else if (getType() == TokenType::CurrentSubStackPosition) { + this->type = TokenType::Number; + this->value = std::to_string(prg->getStackPosition().second); + } else if (getType() == TokenType::StackAccess) { if (_execDebug) std::cout << "[Exe:StackAccess] : Store value : " << getInstructionContent(parametersExecution).getSummary() << " at $" << prg->getStackPosition().first << "+" << prg->getStackPosition().second << std::endl; prg->storeInStack(getInstructionContent(parametersExecution)); + this->type = TokenType::Null; + this->value = ""; } else if (getType() == TokenType::Instruction && getValue() != "Ignore") { this->type = getInstructionContent(parametersExecution).getType(); @@ -269,16 +293,24 @@ void Token::execute(Program* prg) else if (getType() == TokenType::GotoNoOrigin) { prg->setSeekedFlag(std::stoi(getInstructionContent(parametersExecution).getValue())); prg->stopExecution(TokenType::Goto); + this->type = TokenType::Null; + this->value = ""; } else if (getType() == TokenType::ToggleExecution) { prg->stopExecution(TokenType::ToggleExecution); + this->type = TokenType::Null; + this->value = ""; } else if (getType() == TokenType::End) { prg->stopProgram(); + this->type = TokenType::Null; + this->value = ""; } } else if (getType() == TokenType::ToggleExecution && prg->getPauseCause() == TokenType::ToggleExecution) { prg->startExecution(); + this->type = TokenType::Null; + this->value = ""; } else if (TokenType::Flag == this->getType() && prg->getSeekedFlag() == std::stoi(this->getValue()) && prg->getPauseCause() == TokenType::Goto) { if (_execDebug) std::cout << "[Exe:Flag] Flag found : Restarting execution" << std::endl; @@ -507,6 +539,15 @@ void Program::setStackPosition(int pos) { stackPosition[stackPosition.size() - 1] = std::pair(stackPosition[stackPosition.size() - 1].first, pos); } +int Program::getStackSize() +{ + if (stack.find(stackPosition[stackPosition.size() - 1].first) == stack.end()) { + std::cout << "[Error] : Unknown Stack Position : " << stackPosition[stackPosition.size() - 1].first << std::endl; + } + else { + return stack[stackPosition[stackPosition.size() - 1].first].size(); + } +} Token Program::getStackAt() { if (stack.find(stackPosition[stackPosition.size() - 1].first) == stack.end()) { diff --git a/Kitanai.hpp b/Kitanai.hpp index 5665375..3fefcdd 100644 --- a/Kitanai.hpp +++ b/Kitanai.hpp @@ -35,49 +35,61 @@ namespace TokenType Goto, GotoNoOrigin, Origin, - NewInstruction, End, ToggleExecution, + StackSize, + StackLeft, + StackRight, + CurrentStackPosition, + CurrentSubStackPosition, Null }; - static std::map> TypeDataK = { - {"(", {"OpenInstruction", OpenInstruction}}, - {")", {"CloseInstruction", CloseInstruction}}, - {"[", {"OpenStackAccess", OpenStackAccess}}, - {"]", {"CloseStackAccess", CloseStackAccess}}, - {"?", {"Condition", Condition}}, - {"$", {"StackAt", StackAt}}, - {"@", {"CurrentStackValue", CurrentStackValue}}, - {"#", {"Flag", Flag}}, - {"&", {"Goto", Goto}}, - {"*", {"GotoNoOrigin", GotoNoOrigin}}, - {"~", {"Origin", Origin}}, - {";", {"NewInstruction", NewInstruction}}, - {"!", {"ToggleExecution", ToggleExecution}}, + static std::map> TypeDataK = { + {"(", {"OpenInstruction", OpenInstruction}}, + {")", {"CloseInstruction", CloseInstruction}}, + {"[", {"OpenStackAccess", OpenStackAccess}}, + {"]", {"CloseStackAccess", CloseStackAccess}}, + {"?", {"Condition", Condition}}, + {"$", {"StackAt", StackAt}}, + {"@", {"CurrentStackValue", CurrentStackValue}}, + {"#", {"Flag", Flag}}, + {"&", {"Goto", Goto}}, + {"*", {"GotoNoOrigin", GotoNoOrigin}}, + {"~", {"Origin", Origin}}, + {"!", {"ToggleExecution", ToggleExecution}}, + {"^", {"StackSize", StackSize}}, + {"<", {"StackLeft", StackLeft}}, + {">", {"StackRight", StackRight}}, + {":", {"CurrentStackPosition", CurrentStackPosition}}, + {";", {"CurrentSubStackPosition", CurrentSubStackPosition}}, {"%", {"End", End}} }; - static std::map> TypeDataR = { - {OpenInstruction, {"OpenInstruction", "("}}, - {CloseInstruction, {"CloseInstruction", ")"}}, - {OpenStackAccess, {"OpenStackAccess", "["}}, - {CloseStackAccess, {"CloseStackAccess", "]"}}, - {Condition, {"Condition", "?"}}, - {StackAt, {"StackAt", "$"}}, - {CurrentStackValue, {"CurrentStackValue", "@"}}, - {Flag, {"Flag", "#"}}, - {DynamicFlag, {"DynamicFlag", ""}}, - {Goto, {"Goto", "&"}}, - {GotoNoOrigin, {"GotoNoOrigin", "*"}}, - {Origin, {"Origin", "~"}}, - {NewInstruction, {"NewInstruction", ";"}}, - {End, {"End", "%"}}, - {ToggleExecution, {"ToggleExecution", "!"}}, - {Function, {"Function", ""}}, - {String, {"String", ""}}, - {Number, {"Number", ""}}, - {Instruction, {"Instruction", ""}}, - {StackAccess, {"StackAccess", ""}}, + static std::map> TypeDataR = { + {OpenInstruction, {"OpenInstruction", "("}}, + {CloseInstruction, {"CloseInstruction", ")"}}, + {OpenStackAccess, {"OpenStackAccess", "["}}, + {CloseStackAccess, {"CloseStackAccess", "]"}}, + {Condition, {"Condition", "?"}}, + {StackAt, {"StackAt", "$"}}, + {CurrentStackValue, {"CurrentStackValue", "@"}}, + {Flag, {"Flag", "#"}}, + {DynamicFlag, {"DynamicFlag", ""}}, + {Goto, {"Goto", "&"}}, + {GotoNoOrigin, {"GotoNoOrigin", "*"}}, + {Origin, {"Origin", "~"}}, + {End, {"End", "%"}}, + {ToggleExecution, {"ToggleExecution", "!"}}, + {Function, {"Function", ""}}, + {String, {"String", ""}}, + {Number, {"Number", ""}}, + {Instruction, {"Instruction", ""}}, + {StackAccess, {"StackAccess", ""}}, + {StackSize, {"StackSize", "^"}}, + {StackLeft, {"StackLeft", "<"}}, + {StackRight, {"StackRight", ">"}}, + {CurrentStackPosition, {"CurrentStackPosition", ":"}}, + {CurrentSubStackPosition, {"CurrentSubStackPosition", ";"}}, {Null, {"Null", ""}} }; static std::vector> TypeParameters = { @@ -294,6 +306,32 @@ namespace StdLib } return storeInstruction; } + Token f_read(const std::vector tokens) { + Token filenameToken = tokens[0]; + std::string filename = filenameToken.getValue(); + Token stackPlace = tokens[1]; + Token storeInstruction(TokenType::Instruction); + Token accessMemory = Token(TokenType::StackAt); + accessMemory.addParameter(stackPlace); + Token zeroPos = Token(TokenType::Number, "0"); + Token accessSubMem = Token(TokenType::StackAt); + accessSubMem.addParameter(zeroPos); + storeInstruction.addParameter(accessMemory); + storeInstruction.addParameter(accessSubMem); + std::ifstream infile(filename); + std::string line; + while (std::getline(infile, line)) + { + Token stackAcc(TokenType::StackAccess); + stackAcc.addParameter(Token(TokenType::String, line)); + storeInstruction.addParameter(stackAcc); + storeInstruction.addParameter(Token(TokenType::StackRight)); + } + return storeInstruction; + } + Token f_write(const std::vector tokens) { + return Token(TokenType::Null); + } Token fBuild(std::string funcname, int amount, std::function)> func, std::vector parameters) { if (amount != parameters.size()) { std::cout << "[Error] Number of parameters not correct in function : " << funcname << std::endl; @@ -308,26 +346,28 @@ namespace StdLib std::pair)>, int> rpart = fRightPart(name, func, amount); return std::pair)>, int>>(name, rpart); } - static std::map)>, int>> FunctionsName = { - f("add", f_add, 2), - f("sub", f_sub, 2), - f("mul", f_mul, 2), - f("div", f_div, 2), + static std::map)>, int>> FunctionsName = { + f("add", f_add, 2), + f("sub", f_sub, 2), + f("mul", f_mul, 2), + f("div", f_div, 2), f("mod", f_mod, 2), f("not", f_not, 1), - f("eq", f_eq, 2), - f("neq", f_neq, 2), - f("gt", f_gt, 2), - f("ge", f_ge, 2), - f("lt", f_lt, 2), - f("le", f_le, 2), - f("print", f_print, 1), - f("input", f_input, 1), - f("string", f_string, 1), - f("int", f_int, 1), - f("random", f_random, 0), + f("eq", f_eq, 2), + f("neq", f_neq, 2), + f("gt", f_gt, 2), + f("ge", f_ge, 2), + f("lt", f_lt, 2), + f("le", f_le, 2), + f("print", f_print, 1), + f("input", f_input, 1), + f("string", f_string, 1), + f("int", f_int, 1), + f("random", f_random, 0), f("time", f_time, 0), f("split", f_split, 3), + f("read", f_read, 2), + f("write", f_write, 2) }; static std::map customFunctions = {}; @@ -398,6 +438,7 @@ class Program void setDepth(int depth); void setStackPosition(std::string pos); void setStackPosition(int pos); + int getStackSize(); Token getStackAt(); void storeInStack(Token token); void stopExecution(TokenType::Type cause);