From bb74ee3d51b439620d10dc051842fe0b9cae001d Mon Sep 17 00:00:00 2001 From: Sygmei Date: Wed, 18 Jan 2017 18:12:17 +0100 Subject: [PATCH] Add files via upload --- Kitanai.cpp | 37 +++++++++++++++++---- Kitanai.hpp | 95 ++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 102 insertions(+), 30 deletions(-) diff --git a/Kitanai.cpp b/Kitanai.cpp index 7339b54..fb24bee 100644 --- a/Kitanai.cpp +++ b/Kitanai.cpp @@ -2,6 +2,7 @@ int main(int argc, char** argv) { + StdLib::FunctionsName["func"] = StdLib::fRightPart("func", StdLib::f_func, 3); RunArgParser runParser(argv, argc); Program prog; if (argc == 2 || runParser.argumentExists("-f")) { @@ -18,6 +19,9 @@ int main(int argc, char** argv) if (runParser.argumentExists("-a")) { Token::_dispMemAd = true; } + if (runParser.argumentExists("-i")) { + prog.import(runParser.getArgumentValue("-i")); + } prog.parseFile((argc == 2) ? argv[1] : runParser.getArgumentValue("-f")); prog.exec(); @@ -173,13 +177,16 @@ void Token::execute(Program* prg) { std::vector parametersExecution = parameters; doChainAdoption(); - if (getType() != TokenType::Condition) { + if (getType() == TokenType::Condition) { + parametersExecution[0].execute(prg); + } + else if (getType() == TokenType::Function && getValue() == "func") { + } + else + { for (Token& tok : parametersExecution) { tok.execute(prg); } - } - else { - parametersExecution[0].execute(prg); } if (getType() != TokenType::Null && parent != nullptr) { prg->setDepth(getDepth()); @@ -196,7 +203,7 @@ void Token::execute(Program* prg) returnValue.doChainAdoption(); returnValue.setParent(this); returnValue.execute(prg); - if (_execDebug) std::cout << "[Exe::Function] " << getValue() << " returned : " << returnValue.getSummary() << std::endl; + if (_execDebug) std::cout << "[Exe::Function] " << getValue() << " returned : " << returnValue.getValue() << std::endl; this->type = returnValue.getType(); this->value = returnValue.getValue(); } @@ -238,7 +245,7 @@ void Token::execute(Program* prg) prg->storeInStack(getInstructionContent(parametersExecution)); } - else if (getType() == TokenType::Instruction) { + else if (getType() == TokenType::Instruction && getValue() != "Ignore") { this->type = getInstructionContent(parametersExecution).getType(); this->value = getInstructionContent(parametersExecution).getValue(); } @@ -289,7 +296,9 @@ Token Token::getInstructionContent(std::vector& tokens, int index) { int cIndex = 0; for (Token token : tokens) { - if (token.getType() != TokenType::Null && cIndex == index) { + if (token.getType() == TokenType::Instruction && token.getValue() == "Ignore") { + } + else if (token.getType() != TokenType::Null && cIndex == index) { return token; } else if (token.getType() != TokenType::Null) { @@ -642,6 +651,20 @@ void Program::exec() instructions.execute(this); } } +void Program::import(std::string path) +{ + for (int i = 0; i < 2; i++) { + this->parseFile(path); + this->exec(); + this->instructions = Token(TokenType::Instruction); + isProgramOver = false; + execution = true; + } +} +Token& Program::getInstructions() +{ + return instructions; +} std::pair Program::getStackPosition() { return stackPosition[stackPosition.size() - 1]; diff --git a/Kitanai.hpp b/Kitanai.hpp index e411814..55a3a35 100644 --- a/Kitanai.hpp +++ b/Kitanai.hpp @@ -68,7 +68,7 @@ namespace TokenType {Flag, {"Flag", "#"}}, {DynamicFlag, {"DynamicFlag", ""}}, {Goto, {"Goto", "&"}}, - {GotoNoOrigin, {"GotoNoOrigin", "-&"}}, + {GotoNoOrigin, {"GotoNoOrigin", "*"}}, {Origin, {"Origin", "~"}}, {NewInstruction, {"NewInstruction", ";"}}, {End, {"End", "%"}}, @@ -290,38 +290,85 @@ namespace StdLib } return storeInstruction; } - Token B_(std::string funcname, int amount, std::function)> func, std::vector parameters) { + 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; return Token(TokenType::Null); } return func(parameters); } - std::pair)>, int>> B__(std::string name, std::function)> func, int amount) { - std::pair)>, int> rpart = - std::pair)>, int>(std::bind(B_, name, amount, func, std::placeholders::_1), amount); + std::pair)>, int> fRightPart (std::string name, std::function)> func, int amount) { + return std::pair)>, int>(std::bind(fBuild, name, amount, func, std::placeholders::_1), amount); + } + std::pair)>, int>> f(std::string name, std::function)> func, int amount) { + std::pair)>, int> rpart = fRightPart(name, func, amount); return std::pair)>, int>>(name, rpart); } static std::map)>, int>> FunctionsName = { - B__("add", f_add, 2), - B__("sub", f_sub, 2), - B__("mul", f_mul, 2), - B__("div", f_div, 2), - B__("mod", f_mod, 2), - B__("not", f_not, 1), - B__("eq", f_eq, 2), - B__("neq", f_neq, 2), - B__("gt", f_gt, 2), - B__("ge", f_ge, 2), - B__("lt", f_lt, 2), - B__("le", f_le, 2), - B__("print", f_print, 1), - B__("input", f_input, 1), - B__("string", f_string, 1), - B__("int", f_int, 1), - B__("random", f_random, 0), - B__("split", f_split, 3) + 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("split", f_split, 3), }; + + static std::map customFunctions = {}; + Token fCall(std::string funcName, int expectedArgs, const std::vector tokens) { + if (expectedArgs == tokens.size()) { + Token callInstruction(TokenType::Instruction); + Token argDef(TokenType::Instruction, "Ignore"); + Token toArgs(TokenType::StackAt); + toArgs.addParameter(Token(TokenType::String, "args")); + argDef.addParameter(toArgs); + for (int i = 0; i < tokens.size(); i++) { + Token incMemory(TokenType::StackAt); + incMemory.addParameter(Token(TokenType::Number, std::to_string(i))); + Token writeMemory(TokenType::StackAccess); + writeMemory.addParameter(tokens[i]); + argDef.addParameter(incMemory); + argDef.addParameter(writeMemory); + } + Token funcCode(TokenType::Instruction, "Code"); + funcCode.addParameter(toArgs); + for (Token& tok : *customFunctions[funcName]->getParameters()) { + funcCode.addParameter(tok); + } + callInstruction.addParameter(argDef); + callInstruction.addParameter(funcCode); + return callInstruction; + } + else { + std::cout << "[Error] Number of parameters not correct in custom function : " << funcName << " (expected "<< expectedArgs << " got " << tokens.size() << ")" << std::endl; + for (int i = 0; i < tokens.size(); i++) { + Token cTok = tokens[i]; + std::cout << "Arg#" << i << " : " << cTok.getSummary() << std::endl; + } + return Token(TokenType::Null); + } + } + Token f_func(const std::vector tokens) { + Token tFuncName = tokens[0]; + Token tFuncArgs = tokens[1]; + std::string funcName = tFuncName.getValue(); + int funcArgs = std::stoi(tFuncArgs.getValue()); + Token funcBody = tokens[2]; + customFunctions[funcName] = new Token(funcBody); + FunctionsName[funcName] = std::pair)>, int>(std::bind(fCall, funcName, funcArgs, std::placeholders::_1), funcArgs); + return Token(TokenType::Null); + } } class Program @@ -358,5 +405,7 @@ class Program void addOrigin(int flagNumber); int getNextOrigin(); void removeOriginFlag(Token& flag); + void import(std::string path); + Token& getInstructions(); std::pair getStackPosition(); }; \ No newline at end of file