Fix critical bug, further functions

This commit is contained in:
2025-08-15 11:35:58 +10:00
parent bb753e97d4
commit e4cc6b2f14
2 changed files with 21 additions and 12 deletions

View File

@@ -234,13 +234,7 @@ Defines a function. All code between `fun` and `endfun` will be included in the
Usage: `fun -type !functionname -type &var -type &var -type &var # and so on...` Usage: `fun -type !functionname -type &var -type &var -type &var # and so on...`
Usage note: The first type specified before the function name must be the return type. The type displayed before all vars Usage note: The first type specified before the function name must be the return type. The type displayed before all vars shows what type that variable must be.
#### local
Defines a variable. The variable will be destroyed when the function is finished.
Usage: `local &var $value`
#### return #### return

View File

@@ -39,6 +39,7 @@
#include <variant> #include <variant>
#include <vector> #include <vector>
#include <map> #include <map>
#include <stack>
#include <fstream> #include <fstream>
using namespace std; using namespace std;
@@ -254,12 +255,17 @@ struct Function {
*/ */
map<string, Function> functions; map<string, Function> functions;
/*
fnArgs vector
Containst the arguments to be passed to a function
*/
vector<Literal> fnArgs;
/* /*
funargs vector localArgStack stack
Contains the arguments to be passed to a function Contains the variables in a scope
*/ */
vector<Literal> args; stack<vector<Literal>> localArgStack;
/* /*
error function error function
@@ -380,6 +386,8 @@ Types getType(string in) {
bool processingFunction = false; bool processingFunction = false;
string procFnName = ""; string procFnName = "";
bool inFunction = false;
Literal exec(vector<Instruction> in) { Literal exec(vector<Instruction> in) {
for (int i = 0; i < in.size(); i++) { for (int i = 0; i < in.size(); i++) {
Instruction l = in[i]; Instruction l = in[i];
@@ -513,7 +521,6 @@ Literal exec(vector<Instruction> in) {
} else { } else {
error("Second argument of set must be a value (literal or value reference)"); error("Second argument of set must be a value (literal or value reference)");
} }
variables[varName] = varContents; variables[varName] = varContents;
} }
break; break;
@@ -1502,6 +1509,14 @@ Literal exec(vector<Instruction> in) {
error("No function is being defined. Cannot end function declaration here"); error("No function is being defined. Cannot end function declaration here");
break; break;
case Instructions::Pusharg: case Instructions::Pusharg:
if (l.args.size() < 1) {
error("Could not find all arguments required for Endfun inbuilt");
}
if (holds_alternative<Literal>(l.args[0])) {
fnArgs.push_back(get<Literal>(l.args[0]));
} else {
error("First argument of pusharg must be a literal");
}
break; break;
case Instructions::Local: case Instructions::Local:
break; break;
@@ -1611,7 +1626,7 @@ vector<Instruction> parser(vector<vector<string>> in) {
firstInst = false; firstInst = false;
if (isLabel(i)) { if (isLabel(i)) {
labels[i.substr(1)] = lineNum; labels[i.substr(1)] = lineNum;
out.push_back(newInst); //out.push_back(newInst);
continue; continue;
} }
else if (i == "stdin") newInst.inst = Instructions::Stdin; else if (i == "stdin") newInst.inst = Instructions::Stdin;