Further work on implementing functions

This commit is contained in:
2025-05-20 15:03:08 +10:00
parent 46e6d7f9da
commit a0b4caa6e8
7 changed files with 95 additions and 18 deletions

View File

@@ -57,8 +57,9 @@ valtype Interpreter::keywordToValtype(keywords in) {
return valtype::KEYWORD;
}
void Interpreter::interpret(vector<Token> tokenList) {
optional<Token> Interpreter::interpret(vector<Token> tokenList, bool isFunction) {
if (debugMode) log.toggleDebugPrint();
if (isFunction) log.debug("Currently executing code for a function");
tokens = tokenList;
log.debug("Alright we got " + to_string(tokens.size()) + " tokens");
@@ -194,6 +195,7 @@ void Interpreter::interpret(vector<Token> tokenList) {
i -= 1;
}
}
// Detect any comparisons and convert
// Start at 1 to avoid having to do more crappy memory safety stuff
for (int i = 1; i < currentInstruction.size(); i++) {
@@ -210,7 +212,7 @@ void Interpreter::interpret(vector<Token> tokenList) {
if (currentInstruction[i - 1].keyword != keywords::VALUE ||
currentInstruction[i + 1].keyword != keywords::VALUE) {
syntaxError.generalError("Can only compare values");
return;
return {};
}
// Ensure value types are set correctly
@@ -246,7 +248,7 @@ void Interpreter::interpret(vector<Token> tokenList) {
if (currentInstruction[i - 1].keyword != keywords::VALUE ||
currentInstruction[i + 1].keyword != keywords::VALUE) {
syntaxError.generalError("Can only compare values of");
return;
return {};
}
// Ensure value types are set correctly
@@ -298,14 +300,53 @@ void Interpreter::interpret(vector<Token> tokenList) {
currentInstruction.insert(currentInstruction.begin() + startIndex, newToken);
}
}
// Call functions and assign values
for (int i = 0; i < currentInstruction.size(); i++) {
log.debug("Looking for a function");
Function empty;
for (int j = 0; j < functionnames.size(); j++) {
log.debug("There is a function called " + functionnames[j]);
//if (tokens[i].type == valtype::STR && functionnames[j] == get<string>(tokens[i].value.value)) log.debug("in theory we should be fine?");
}
if (currentInstruction[i].type == valtype::STR && functions.find(get<string>(currentInstruction[i].value.value)) != functions.end()) {
log.debug("Found a function");
Function fnbuf = functions[get<string>(currentInstruction[i].value.value)];
i++;
if (currentInstruction[i].keyword != keywords::OPARE) syntaxError.generalError("Expecting a '(' when calling a function");
i++;
vector<Token> argsForFn;
int argCount = 0;
if (currentInstruction[i].keyword != keywords::CPARE) while (currentInstruction[i].keyword != keywords::CPARE) {
if (i >= currentInstruction.size()) syntaxError.generalError("Expecting a ')' after function arguments");
if (currentInstruction[i].value.type != fnbuf.arguments[fnbuf.argNames[argCount]]) syntaxError.fnTypeMismatch(get<string>(currentInstruction[i].value.value), {log.getTypeString(fnbuf.arguments[fnbuf.argNames[argCount]])}, currentInstruction[i].value.type);
variables[fnbuf.argNames[argCount]] = tokens[i].value;
i++;
}
resumePoint.push(tokenIndex);
vector<Token> fnTokens;
int l = 1;
int m = 0;
while (l > 0) {
if (tokens[m].keyword == keywords::OBRAC) l++;
if (tokens[m].keyword == keywords::CBRAC) l--;
if (l < 1) break;
fnTokens.push_back(tokens[m]);
m++;
}
interpret(fnTokens, true);
log.debug("This is a test call for a function. Function won't actually be run yet. If you see this everything is fine");
//tokenIndex = functions[get<string>(currentInstruction[i].value.value)].startToken;
}
}
// Execute the instruction
log.debug("Length of line is " + to_string(lengthOfLine));
executeCode(currentInstruction);
executeCode(currentInstruction, isFunction);
lengthOfLine = 0;
}
}
void Interpreter::executeCode(vector<Token> tokens) {
optional<Token> Interpreter::executeCode(vector<Token> tokens, bool isFunction) {
SyntaxError syntaxError;
log.debug("Token length for this expression is " + to_string(tokens.size()));
for (int i = 0; i < tokens.size(); i++) {
@@ -329,7 +370,7 @@ void Interpreter::executeCode(vector<Token> tokens) {
if (tokens[i].keyword == keywords::CBRAC) {
tokenIndex = loop;
repeatScope --;
return;
return {};
}
}
if (tokens[i].keyword == keywords::PRINTLN) {
@@ -504,7 +545,7 @@ void Interpreter::executeCode(vector<Token> tokens) {
if (tokens[i].type != valtype::BOOL) syntaxError.comparisonTypeError("in an if statement");
if (!get<bool>(tokens[i].value.value)) {
skipScope ++;
return;
return {};
}
} else if (tokens[i].keyword == keywords::WHILE) {
i++;
@@ -532,36 +573,58 @@ void Interpreter::executeCode(vector<Token> tokens) {
} else if (tokens[i].keyword == keywords::FUN) {
i++;
Function buf;
if (keywordToValtype(tokens[i].keyword) == valtype::KEYWORD) syntaxError.generalError("fun needs a defining value for arg 1, not a value. ");
if (keywordToValtype(tokens[i].keyword) == valtype::KEYWORD) syntaxError.generalError("fun needs a defining value for arg 1, not a non-value.");
buf.type = keywordToValtype(tokens[i].keyword);
i++;
tokenIndex ++;
string valName;
if (tokens[i].value.type == valtype::STR) valName = get<string>(tokens[i].value.value);
buf.startToken = tokenIndex;
functionnames.push_back(valName);
i++;
tokenIndex ++;
if (tokens[i].keyword != keywords::OPARE) syntaxError.generalError("This is a placeholder");
i++;
tokenIndex ++;
map<string, valtype> valuetypes;
while (tokens[i].keyword != keywords::CPARE) {
// Get the arguments which we will use in the function
if (tokens[i + 1].keyword != keywords::CPARE) 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++;
tokenIndex ++;
if (tokens[i].type != valtype::STR) syntaxError.generalError("A third placeholder");
log.debug("This function needs a " + log.getTypeString(valbuf) + " which is accessible as " + get<string>(tokens[i].value.value));
buf.argNames.push_back(get<string>(tokens[i].value.value));
valuetypes[get<string>(tokens[i].value.value)] = valbuf;
i++;
tokenIndex ++;
if (tokens[i].keyword == keywords::CPARE) break;
if (tokens[i].keyword != keywords::COMMA) syntaxError.generalError("Yet another placeholder");
i++;
tokenIndex ++;
}
else log.debug("Function does not require arguments");
tokenIndex ++;
buf.arguments = valuetypes;
buf.startToken = tokenIndex;
functions[valName] = buf;
log.debug("Init a function");
log.debug("Init a function with name " + valName + " success. Function starts at index " + to_string(buf.startToken));
} else if (tokens[i].keyword == keywords::RETURN) {
i++;
if (i >= tokens.size()) syntaxError.fnNotSufficientArgs("return", 1, 1, 0);
if (isFunction){
return tokens[i++];
} else {
if (tokens[i].value.type != valtype::INT) syntaxError.improperReturnType("");
exit(get<int>(tokens[i].value.value));
}
} 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...");
if (tokens.size() <= i + 2) { // Need at least 3 tokens: variable, operator, value
syntaxError.mathTooFewArgs();
return;
return {};
}
string varName = get<string>(tokens[i].value.value);
i++;
@@ -578,7 +641,7 @@ void Interpreter::executeCode(vector<Token> tokens) {
default: validTypes = {"unknown"}; break;
}
syntaxError.fnTypeMismatch("assignment", validTypes, valueToken.type);
return;
return {};
}
variables[varName].value = valueToken.value.value;
i++; // Skip the value token since we've processed it
@@ -613,4 +676,5 @@ void Interpreter::executeCode(vector<Token> tokens) {
}
}
}
return {};
}