diff --git a/src/main.cpp b/src/main.cpp index ed91d3f..2a2ac4a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -252,6 +252,7 @@ struct Function { Types returnType; vector args; vector instructions; + map localLabels; }; /* @@ -410,7 +411,15 @@ Literal exec(vector in); function below) and acts upon their properties. This is the main interpreter function for the program. */ -Literal exec(vector in) { +Literal exec(vector in, bool executingFunction) { + map* currentLabels = &labels; + + for (auto& [fnName, fn] : functions) { + if (&fn.instructions == &in) { + currentLabels = &fn.localLabels; + break; + } + } for (int i = 0; i < in.size(); i++) { Instruction l = in[i]; if (processingFunction) { @@ -434,9 +443,9 @@ Literal exec(vector in) { } else if (holds_alternative(l.args[j])) { Line ln = get(l.args[j]); if (ln.isLabel) { - if (labels.find(ln.label) != labels.end()) { + if (currentLabels->find(ln.label) != currentLabels->end()) { Line newLine; - newLine.lineNum = labels[ln.label]; + newLine.lineNum = (*currentLabels)[ln.label]; l.args[j] = newLine; } else { error("Could not find label " + ln.label); @@ -1565,7 +1574,7 @@ Literal exec(vector in) { Allows functions to be defined. */ case Instructions::Fun: - if (l.args.size() < 3) { + if (l.args.size() < 2) { error("Could not find all arguments required for Fun inbuilt"); } { @@ -1705,7 +1714,7 @@ Literal exec(vector in) { } // Call the function - Literal retVal = exec(functions[ref.fnName].instructions); + Literal retVal = exec(functions[ref.fnName].instructions, true); // Remove variables that were declared in this scope for (Direct delref : localArgStack.top()) { @@ -1822,12 +1831,14 @@ vector> lexer(string in) { */ vector parser(vector> in) { vector out; - int lineNum = 1; + int functionInstructionIndex = 0; + for (vector lineTokens : in) { lineNum ++; Instruction newInst; if (lineTokens.empty()) { + if (processingFunction) functionInstructionIndex ++; out.push_back(newInst); continue; }; @@ -1837,8 +1848,11 @@ vector parser(vector> in) { if (firstInst) { firstInst = false; if (isLabel(i)) { - labels[i.substr(1)] = lineNum; - //out.push_back(newInst); + if (processingFunction) { + functions[procFnName].localLabels[i.substr(1)] = functionInstructionIndex; + } else { + labels[i.substr(1)] = lineNum; + } continue; } else if (i == "stdin") newInst.inst = Instructions::Stdin; @@ -1867,7 +1881,10 @@ vector parser(vector> in) { else if (i == "stoi") newInst.inst = Instructions::Stoi; else if (i == "stod") newInst.inst = Instructions::Stod; else if (i == "tostring") newInst.inst = Instructions::Tostring; - else if (i == "fun") newInst.inst = Instructions::Fun; + else if (i == "fun") { + newInst.inst = Instructions::Fun; + functionInstructionIndex = 0; + } else if (i == "return") newInst.inst = Instructions::Return; else if (i == "endfun") newInst.inst = Instructions::Endfun; else if (i == "pusharg") newInst.inst = Instructions::Pusharg; @@ -1977,6 +1994,7 @@ vector parser(vector> in) { } } } + if (processingFunction) functionInstructionIndex++; out.push_back(newInst); } return out; @@ -1999,7 +2017,7 @@ int main(int argc, char** argv) { while (getline(file, lns)) { in += lns += "\n"; } - Literal ret = exec(parser(lexer(in))); + Literal ret = exec(parser(lexer(in)), false); if (holds_alternative(ret.val)) { return get(ret.val); } else { diff --git a/tests/functions.grnd b/tests/functions.grnd index 8419fa5..f97c379 100644 --- a/tests/functions.grnd +++ b/tests/functions.grnd @@ -1,14 +1,19 @@ -fun -int !dingle -string &silly -stdlnout "This is inside the function" -stdout "The function argument is " -stdlnout $silly -return 10 +fun -bool !jumpy + stdlnout "This is the jumpy function" + set &counter 0 + jump %inloop + @jumpback + stdlnout "Yay I jumped!" + @inloop + add 1 $counter &counter + inequal 10 $counter &out + stdout "Condition is" + stdlnout $out + if $out %jumpback + stdlnout "Finished" + return true endfun -stdlnout "This is outside the function" +call !jumpy &tmp -pusharg "heheheha" -call !dingle &var - -stdout "Function returned " -stdlnout $var +stdlnout "I called a function"