Continue work on functions

This commit is contained in:
2025-05-18 14:27:04 +10:00
parent 7fb60587e7
commit 46e6d7f9da
3 changed files with 24 additions and 17 deletions

3
examples/function.io Normal file
View File

@@ -0,0 +1,3 @@
println "Time to define a function";
fun str myFunction(int myNum);

View File

@@ -444,20 +444,6 @@ void Interpreter::executeCode(vector<Token> tokens) {
Token typeToken = tokens[i]; Token typeToken = tokens[i];
Token nameToken = tokens[i + 1]; Token nameToken = tokens[i + 1];
Token valueToken = tokens[i + 2]; 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<string>(nameToken.value.value);
Value newValue;
newValue.type = valtype::LIST;
newValue.value = get<List>(valueToken.value.value);
break;
}
}
*/
i += 2; i += 2;
@@ -543,17 +529,33 @@ void Interpreter::executeCode(vector<Token> tokens) {
} else { } else {
syntaxError.generalError("Achievement get: How did we get here?"); syntaxError.generalError("Achievement get: How did we get here?");
} }
} else if (tokens[i].keyword == keywords::FUNCTION) { } else if (tokens[i].keyword == keywords::FUN) {
i++; i++;
Function buf; 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); buf.type = keywordToValtype(tokens[i].keyword);
i++; i++;
string valName; string valName;
if (tokens[i].value.type == valtype::STR) valName = get<string>(tokens[i].value.value); if (tokens[i].value.type == valtype::STR) valName = get<string>(tokens[i].value.value);
buf.startToken = tokenIndex; buf.startToken = tokenIndex;
i++; i++;
if (tokens[i].keyword != ) if (tokens[i].keyword != keywords::OPARE) syntaxError.generalError("This is a placeholder");
i++;
map<string, valtype> 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<string>(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 { } else {
if (tokens[i].keyword == keywords::VALUE && tokens[i].value.type == valtype::STR && variables.find(get<string>(tokens[i].value.value)) != variables.end()) { if (tokens[i].keyword == keywords::VALUE && tokens[i].value.type == valtype::STR && variables.find(get<string>(tokens[i].value.value)) != variables.end()) {
log.debug("Manipulating variable..."); log.debug("Manipulating variable...");
@@ -573,6 +575,7 @@ void Interpreter::executeCode(vector<Token> tokens) {
case valtype::DEC: validTypes = {"dec"}; break; case valtype::DEC: validTypes = {"dec"}; break;
case valtype::STR: validTypes = {"str"}; break; case valtype::STR: validTypes = {"str"}; break;
case valtype::BOOL: validTypes = {"bool"}; break; case valtype::BOOL: validTypes = {"bool"}; break;
default: validTypes = {"unknown"}; break;
} }
syntaxError.fnTypeMismatch("assignment", validTypes, valueToken.type); syntaxError.fnTypeMismatch("assignment", validTypes, valueToken.type);
return; return;

View File

@@ -26,6 +26,7 @@ class Interpreter {
private: private:
int skipScope = 0; int skipScope = 0;
int repeatScope = 0; int repeatScope = 0;
int resumePoint = 0;
vector<int> loops; vector<int> loops;
SyntaxError syntaxError; SyntaxError syntaxError;
vector<Token> tokens; vector<Token> tokens;