Continue work on functions
This commit is contained in:
3
examples/function.io
Normal file
3
examples/function.io
Normal file
@@ -0,0 +1,3 @@
|
||||
println "Time to define a function";
|
||||
|
||||
fun str myFunction(int myNum);
|
@@ -444,20 +444,6 @@ void Interpreter::executeCode(vector<Token> 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<string>(nameToken.value.value);
|
||||
Value newValue;
|
||||
newValue.type = valtype::LIST;
|
||||
newValue.value = get<List>(valueToken.value.value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
i += 2;
|
||||
|
||||
@@ -543,17 +529,33 @@ void Interpreter::executeCode(vector<Token> 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<string>(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<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 {
|
||||
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...");
|
||||
@@ -573,6 +575,7 @@ void Interpreter::executeCode(vector<Token> 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;
|
||||
|
@@ -26,6 +26,7 @@ class Interpreter {
|
||||
private:
|
||||
int skipScope = 0;
|
||||
int repeatScope = 0;
|
||||
int resumePoint = 0;
|
||||
vector<int> loops;
|
||||
SyntaxError syntaxError;
|
||||
vector<Token> tokens;
|
||||
|
Reference in New Issue
Block a user