Skip to content

Commit

Permalink
Update 1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Sygmei committed Jan 31, 2017
1 parent 171de79 commit 5ec45f1
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 52 deletions.
41 changes: 41 additions & 0 deletions Kitanai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -507,6 +539,15 @@ void Program::setStackPosition(int pos)
{
stackPosition[stackPosition.size() - 1] = std::pair<std::string, int>(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()) {
Expand Down
145 changes: 93 additions & 52 deletions Kitanai.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,61 @@ namespace TokenType
Goto,
GotoNoOrigin,
Origin,
NewInstruction,
End,
ToggleExecution,
StackSize,
StackLeft,
StackRight,
CurrentStackPosition,
CurrentSubStackPosition,
Null
};

static std::map<std::string, std::pair<std::string, Type>> 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<std::string, std::pair<std::string, Type>> 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<Type, std::pair<std::string, std::string>> 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<Type, std::pair<std::string, std::string>> 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<std::vector<Type>> TypeParameters = {
Expand Down Expand Up @@ -294,6 +306,32 @@ namespace StdLib
}
return storeInstruction;
}
Token f_read(const std::vector<Token> 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<Token> tokens) {
return Token(TokenType::Null);
}
Token fBuild(std::string funcname, int amount, std::function<Token(const std::vector<Token>)> func, std::vector<Token> parameters) {
if (amount != parameters.size()) {
std::cout << "[Error] Number of parameters not correct in function : " << funcname << std::endl;
Expand All @@ -308,26 +346,28 @@ namespace StdLib
std::pair<std::function<Token(const std::vector<Token>)>, int> rpart = fRightPart(name, func, amount);
return std::pair<std::string, std::pair<std::function<Token(const std::vector<Token>)>, int>>(name, rpart);
}
static std::map<std::string, std::pair<std::function<Token(const std::vector<Token>)>, 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<std::string, std::pair<std::function<Token(const std::vector<Token>)>, 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<std::string, Token*> customFunctions = {};
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5ec45f1

Please sign in to comment.