Start work on functions

This commit is contained in:
2025-08-11 14:57:45 +10:00
parent 3a8600b481
commit db0c362efb
2 changed files with 66 additions and 16 deletions

View File

@@ -232,7 +232,21 @@ Some symbols specific to this category:
Defines a function. All code between `fun` and `endfun` will be included in the function.
Usage: `fun !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
#### local
Defines a variable. The variable will be destroyed when the function is finished.
Usage: `local &var $value`
#### return
Returns back to the main program (or other function that called this function). Also returns a value, which must be of the type defined when defining the function.
Usage: `return $value`
#### endfun

View File

@@ -57,7 +57,8 @@ enum class Instructions {
Setlist, Getlistat, Setlistat, Getlistsize, Listappend, Listprepend,
Getstrcharat, Getstrsize,
Stoi, Stod, Tostring,
Fun, Endfun, Pusharg, Call
Fun, Return, Endfun, Pusharg, Call,
Use, Extern
};
/*
@@ -122,6 +123,30 @@ struct List {
vector<Literal> val;
};
/*
Direct struct
If the program being executed makes a direct reference, it is stored in a Direct
struct. For example, if the following line was written:
stdin &myVar
The Direct struct in the instruction should look like this:
{
varName = "myVar";
}
*/
struct Direct {
string varName;
};
/*
Function struct
Contains information needed to run a Ground function.
*/
struct Function {
int startPos;
Types returnType;
vector<pair<Direct, Types>> args;
};
/*
ListRef struct
Contains the name of a list referenced by the program. For example, if the
@@ -169,20 +194,6 @@ struct ValueRef {
string varName;
};
/*
Direct struct
If the program being executed makes a direct reference, it is stored in a Direct
struct. For example, if the following line was written:
stdin &myVar
The Direct struct in the instruction should look like this:
{
varName = "myVar";
}
*/
struct Direct {
string varName;
};
/*
Line struct
If the program being executed makes a line reference, it is stored in a Line
@@ -1346,6 +1357,10 @@ void exec(vector<Instruction> in) {
}
}
break;
/*
end instructoin
Ends execution of the code, eith the status code provided.
*/
case Instructions::End:
if (l.args.size() < 1) {
error("Could not find all arguments required for End inbuilt");
@@ -1360,6 +1375,20 @@ void exec(vector<Instruction> in) {
error("First argument of end must be an int value");
}
break;
case Instructions::Fun:
break;
case Instructions::Return:
break;
case Instructions::Endfun:
break;
case Instructions::Pusharg:
break;
case Instructions::Call:
break;
case Instructions::Use:
break;
case Instructions::Extern:
break;
default:
cout << "Still to be implemented" << endl;
break;
@@ -1486,6 +1515,13 @@ vector<Instruction> parser(vector<vector<string>> 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 == "return") newInst.inst = Instructions::Return;
else if (i == "endfun") newInst.inst = Instructions::Endfun;
else if (i == "pusharg") newInst.inst = Instructions::Pusharg;
else if (i == "call") newInst.inst = Instructions::Call;
else if (i == "use") newInst.inst = Instructions::Use;
else if (i == "extern") newInst.inst = Instructions::Extern;
else error("Unexpected token: " + i);
} else {
Types type = getType(i);