Typerefs and functionrefs

This commit is contained in:
2025-08-12 09:45:00 +10:00
parent db0c362efb
commit 52eadaa9c3

View File

@@ -57,7 +57,7 @@ enum class Instructions {
Setlist, Getlistat, Setlistat, Getlistsize, Listappend, Listprepend,
Getstrcharat, Getstrsize,
Stoi, Stod, Tostring,
Fun, Return, Endfun, Pusharg, Call,
Fun, Return, Endfun, Pusharg, Call, Local,
Use, Extern
};
@@ -76,7 +76,7 @@ enum class Instructions {
See also parser function
*/
enum class Types {
Int, Double, String, Char, Bool, Value, Direct, Line, ListRef, Label
Int, Double, String, Char, Bool, Value, Direct, Line, ListRef, Label, Type, Function
};
/*
@@ -137,6 +137,13 @@ struct Direct {
string varName;
};
struct TypeRef {
Types type;
};
struct FunctionRef {
string fnName;
};
/*
Function struct
Contains information needed to run a Ground function.
@@ -180,6 +187,12 @@ map<string, List> lists;
*/
map<string, int> labels;
/*
functions map
Contains the positions of functions and types of their values
*/
map<string, Function> functions;
/*
ValueRef struct
If the program being executed makes a value reference, it is stored in a ValueRef
@@ -237,7 +250,7 @@ struct Line {
*/
struct Instruction {
Instructions inst = Instructions::Empty;
vector<variant<Literal, ValueRef, ListRef, Direct, Line>> args;
vector<variant<Literal, ValueRef, ListRef, FunctionRef, TypeRef, Direct, Line>> args;
};
/*
@@ -314,6 +327,19 @@ bool isListRef(string in) {
else return false;
}
bool isType(string in) {
if (in.size() >= 1 && in[0] == '-') {
string type = in.substr(1);
if (type == "string" || type == "char" || type == "bool" || type == "double" || type == "int") return true;
else return false;
} else return false;
}
bool isFunction(string in) {
if (in.size() >= 1 && in[0] == '!') return true;
else return false;
}
/*
getType function
This function determines the type of a value inside a string based on the is*
@@ -330,6 +356,8 @@ Types getType(string in) {
if (isLine(in)) return Types::Line;
if (isLabel(in)) return Types::Label;
if (isListRef(in)) return Types::ListRef;
if (isType(in)) return Types::Type;
if (isFunction(in)) return Types::Function;
error("Could not determine type of \"" + in + "\"");
return Types::Int;
}
@@ -487,7 +515,7 @@ void exec(vector<Instruction> in) {
}
bool first = true;
for (variant<Literal, ValueRef, ListRef, Direct, Line> k : l.args) {
for (variant<Literal, ValueRef, ListRef, FunctionRef, TypeRef, Direct, Line> k : l.args) {
if (holds_alternative<Literal>(k)) {
listContents.val.push_back(get<Literal>(k));
} else {
@@ -1543,6 +1571,23 @@ vector<Instruction> parser(vector<vector<string>> in) {
case Types::Label:
error("Label should be defined as first token");
break;
case Types::Type:
{
TypeRef newType;
string type = i.substr(1);
if (type == "string") newType.type = Types::String;
else if (type == "char") newType.type = Types::Char;
else if (type == "double") newType.type = Types::Double;
else if (type == "int") newType.type = Types::Int;
else if (type == "bool") newType.type = Types::Bool;
}
break;
case Types::Function:
{
FunctionRef newFunction;
newFunction.fnName = i.substr(1);
newInst.args.push_back(newFunction);
}
case Types::Line:
{
Line newLine;