diff --git a/examples/function.io b/examples/function.io new file mode 100644 index 0000000..70f8065 --- /dev/null +++ b/examples/function.io @@ -0,0 +1,3 @@ +println "Time to define a function"; + +fun str myFunction(int myNum); diff --git a/src/Interpreter.cpp b/src/Interpreter.cpp index f108d36..12200c1 100644 --- a/src/Interpreter.cpp +++ b/src/Interpreter.cpp @@ -444,20 +444,6 @@ void Interpreter::executeCode(vector tokens) { Token typeToken = tokens[i]; Token nameToken = tokens[i + 1]; Token valueToken = tokens[i + 2]; -/* - if (tokens[i + 3].keyword == keywords::OSQUA && tokens[i + 4].keyword == keywords::CSQUA) { - log.debug("Creating a list variable"); - valueToken = tokens[i + 5]; - if (valueToken.keyword != keywords::LISTOBJ) syntaxError.generalError("Trying to add a non-list to a list variable"); - else { - string varName = get(nameToken.value.value); - Value newValue; - newValue.type = valtype::LIST; - newValue.value = get(valueToken.value.value); - break; - } - } -*/ i += 2; @@ -543,17 +529,33 @@ void Interpreter::executeCode(vector tokens) { } else { syntaxError.generalError("Achievement get: How did we get here?"); } - } else if (tokens[i].keyword == keywords::FUNCTION) { + } else if (tokens[i].keyword == keywords::FUN) { i++; Function buf; - if (tokens[i].type != valtype::KEYWORD) syntaxError.generalError("fun needs a keyword, not a value"); + if (keywordToValtype(tokens[i].keyword) == valtype::KEYWORD) syntaxError.generalError("fun needs a defining value for arg 1, 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 != ) + if (tokens[i].keyword != keywords::OPARE) syntaxError.generalError("This is a placeholder"); + i++; + map valuetypes; + while (tokens[i].keyword != keywords::CPARE) { + // We should have a valtype, a name for the variable, and a comma if the thing hasn't ended already + if (keywordToValtype(tokens[i].keyword) == valtype::KEYWORD) syntaxError.generalError("Another placeholder"); + valtype valbuf = keywordToValtype(tokens[i].keyword); + i++; + if (tokens[i].type != valtype::STR) syntaxError.generalError("A third placeholder"); + valuetypes[get(tokens[i].value.value)] = valbuf; + i++; + if (tokens[i].keyword == keywords::CPARE) break; + if (tokens[i].keyword != keywords::COMMA) syntaxError.generalError("Yet another placeholder"); + i++; + } + functions[valName] = buf; + log.debug("Init a function"); } 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..."); @@ -573,6 +575,7 @@ void Interpreter::executeCode(vector tokens) { case valtype::DEC: validTypes = {"dec"}; break; case valtype::STR: validTypes = {"str"}; break; case valtype::BOOL: validTypes = {"bool"}; break; + default: validTypes = {"unknown"}; break; } syntaxError.fnTypeMismatch("assignment", validTypes, valueToken.type); return; diff --git a/src/Interpreter.h b/src/Interpreter.h index b46d0d3..44f3abb 100644 --- a/src/Interpreter.h +++ b/src/Interpreter.h @@ -26,6 +26,7 @@ class Interpreter { private: int skipScope = 0; int repeatScope = 0; + int resumePoint = 0; vector loops; SyntaxError syntaxError; vector tokens;