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, Setlist, Getlistat, Setlistat, Getlistsize, Listappend, Listprepend,
Getstrcharat, Getstrsize, Getstrcharat, Getstrsize,
Stoi, Stod, Tostring, Stoi, Stod, Tostring,
Fun, Return, Endfun, Pusharg, Call, Fun, Return, Endfun, Pusharg, Call, Local,
Use, Extern Use, Extern
}; };
@@ -76,7 +76,7 @@ enum class Instructions {
See also parser function See also parser function
*/ */
enum class Types { 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; string varName;
}; };
struct TypeRef {
Types type;
};
struct FunctionRef {
string fnName;
};
/* /*
Function struct Function struct
Contains information needed to run a Ground function. Contains information needed to run a Ground function.
@@ -180,6 +187,12 @@ map<string, List> lists;
*/ */
map<string, int> labels; map<string, int> labels;
/*
functions map
Contains the positions of functions and types of their values
*/
map<string, Function> functions;
/* /*
ValueRef struct ValueRef struct
If the program being executed makes a value reference, it is stored in a ValueRef If the program being executed makes a value reference, it is stored in a ValueRef
@@ -237,7 +250,7 @@ struct Line {
*/ */
struct Instruction { struct Instruction {
Instructions inst = Instructions::Empty; 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; 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 getType function
This function determines the type of a value inside a string based on the is* 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 (isLine(in)) return Types::Line;
if (isLabel(in)) return Types::Label; if (isLabel(in)) return Types::Label;
if (isListRef(in)) return Types::ListRef; 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 + "\""); error("Could not determine type of \"" + in + "\"");
return Types::Int; return Types::Int;
} }
@@ -487,7 +515,7 @@ void exec(vector<Instruction> in) {
} }
bool first = true; 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)) { if (holds_alternative<Literal>(k)) {
listContents.val.push_back(get<Literal>(k)); listContents.val.push_back(get<Literal>(k));
} else { } else {
@@ -1543,6 +1571,23 @@ vector<Instruction> parser(vector<vector<string>> in) {
case Types::Label: case Types::Label:
error("Label should be defined as first token"); error("Label should be defined as first token");
break; 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: case Types::Line:
{ {
Line newLine; Line newLine;