diff --git a/src/Interpreter.cpp b/src/Interpreter.cpp index 7bee270..f108d36 100644 --- a/src/Interpreter.cpp +++ b/src/Interpreter.cpp @@ -46,6 +46,17 @@ void Interpreter::initInterpreter(vector args) { variables["args"] = buf; } +valtype Interpreter::keywordToValtype(keywords in) { + switch (in) { + case keywords::INT: return valtype::INT; + case keywords::DEC: return valtype::DEC; + case keywords::BOOL: return valtype::BOOL; + case keywords::STR: return valtype::STR; + default: return valtype::KEYWORD; + } + return valtype::KEYWORD; +} + void Interpreter::interpret(vector tokenList) { if (debugMode) log.toggleDebugPrint(); tokens = tokenList; @@ -532,6 +543,17 @@ void Interpreter::executeCode(vector tokens) { } else { syntaxError.generalError("Achievement get: How did we get here?"); } + } else if (tokens[i].keyword == keywords::FUNCTION) { + i++; + Function buf; + if (tokens[i].type != valtype::KEYWORD) syntaxError.generalError("fun needs a keyword, not a value"); + buf.type = keywordToValtype(tokens[i].keyword); + i++; + string valName; + if (tokens[i].value.type == valtype::STR) valName = get(tokens[i].value.value); + buf.startToken = tokenIndex; + i++; + if (tokens[i].keyword != ) } else { if (tokens[i].keyword == keywords::VALUE && tokens[i].value.type == valtype::STR && variables.find(get(tokens[i].value.value)) != variables.end()) { log.debug("Manipulating variable..."); diff --git a/src/Interpreter.h b/src/Interpreter.h index 37ca035..b46d0d3 100644 --- a/src/Interpreter.h +++ b/src/Interpreter.h @@ -30,6 +30,7 @@ private: SyntaxError syntaxError; vector tokens; map variables; + map functions; Logger log; int loop; int lengthOfLine; @@ -37,6 +38,7 @@ private: optional consume(); optional peek(int offset = 1); void executeCode(vector tokens); + valtype keywordToValtype(keywords in); public: void initInterpreter(vector args); diff --git a/src/Parser.cpp b/src/Parser.cpp index d1a496f..210b841 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -72,7 +72,7 @@ void Parser::parseLines(string in, Logger log) { } terms.push_back(termBuffer); termBuffer.clear(); - } else if ((c == ',' || c == '{' || c == '}' || c == '(' || c == ')' || c == '[' || c == ']') && !isString) { + } else if ((c == ',' || c == '{' || c == '}' || c == '(' || c == ')' || c == '[' || c == ']' || c == ',') && !isString) { if (!buffer.empty()) { termBuffer.push_back(buffer); buffer.clear(); @@ -130,6 +130,7 @@ void Parser::processLines() { else if (ct == "bool") token.keyword = keywords::BOOL; else if (ct == "if") token.keyword = keywords::IF; else if (ct == "while") token.keyword = keywords::WHILE; + else if (ct == "fun") token.keyword = keywords::FUNCTION; else if (ct == "{") token.keyword = keywords::OBRAC; else if (ct == "}") token.keyword = keywords::CBRAC; else if (ct == "(") token.keyword = keywords::OPARE; @@ -152,6 +153,7 @@ void Parser::processLines() { else if (ct == "//") token.keyword = keywords::COMMENT; else if (ct == "[") token.keyword = keywords::OSQUA; else if (ct == "]") token.keyword = keywords::CSQUA; + else if (ct == ",") token.keyword = keywords::COMMA; else { token.keyword = keywords::VALUE; // Convert the value based on its type diff --git a/src/common.h b/src/common.h index d690f31..5334979 100644 --- a/src/common.h +++ b/src/common.h @@ -38,7 +38,7 @@ enum class keywords { ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, LESS, GREATER, EQLESS, EQGREATER, INCREMENT, DECREMENT, - PRINT, PRINTLN, PRINTLIST, LET, INPUT, EXIT, + PRINT, PRINTLN, PRINTLIST, LET, FUNCTION, INPUT, EXIT, VALUE, LISTOBJ, SEMICOLON, VARIABLE, COMMENT }; @@ -55,6 +55,12 @@ struct Value { variant value; }; +struct Function { + valtype type; + map arguments; + int startToken; +}; + struct Token { keywords keyword; Value value;