forked from ground/ground
Access variables in structs
This commit is contained in:
60
src/main.cpp
60
src/main.cpp
@@ -34,7 +34,6 @@
|
|||||||
Happy coding!
|
Happy coding!
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <cinttypes>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
@@ -470,6 +469,10 @@ bool isFunction(string in) {
|
|||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isReferencingStruct(string in) {
|
||||||
|
return in.find('.') != string::npos;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
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*
|
||||||
@@ -569,6 +572,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
|
|||||||
if (l.inst == Instructions::Endstruct) {
|
if (l.inst == Instructions::Endstruct) {
|
||||||
processingStruct = false;
|
processingStruct = false;
|
||||||
procStructName = "";
|
procStructName = "";
|
||||||
|
continue;
|
||||||
} else if (l.inst == Instructions::Fun) {
|
} else if (l.inst == Instructions::Fun) {
|
||||||
if (l.args.size() < 2) {
|
if (l.args.size() < 2) {
|
||||||
error("Could not find all arguments required for Fun inbuilt");
|
error("Could not find all arguments required for Fun inbuilt");
|
||||||
@@ -618,14 +622,14 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
|
|||||||
newFunction.args.push_back(newArg);
|
newFunction.args.push_back(newArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
functions[fnName] = newFunction;
|
structs[procStructName].functions[fnName] = newFunction;
|
||||||
processingFunction = true;
|
processingFunction = true;
|
||||||
procFnName = fnName;
|
procFnName = fnName;
|
||||||
} else if (l.inst == Instructions::Init) {
|
} else if (l.inst == Instructions::Init) {
|
||||||
if (l.args.size() < 2) {
|
if (l.args.size() < 2) {
|
||||||
error("Could not find all arguments required for Init inbuilt");
|
error("Could not find all arguments required for Init inbuilt");
|
||||||
}
|
}
|
||||||
{
|
|
||||||
Direct varRef;
|
Direct varRef;
|
||||||
TypeRef type;
|
TypeRef type;
|
||||||
if (holds_alternative<Direct>(l.args[0])) {
|
if (holds_alternative<Direct>(l.args[0])) {
|
||||||
@@ -643,7 +647,6 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
|
|||||||
if (type.isCustomType) {
|
if (type.isCustomType) {
|
||||||
newVal.val = structs[type.customType];
|
newVal.val = structs[type.customType];
|
||||||
} else {
|
} else {
|
||||||
Literal newVal;
|
|
||||||
switch (type.type) {
|
switch (type.type) {
|
||||||
case Types::Int:
|
case Types::Int:
|
||||||
newVal.val = 0;
|
newVal.val = 0;
|
||||||
@@ -664,19 +667,47 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
|
|||||||
error("You dingus you werent supposed to get here");
|
error("You dingus you werent supposed to get here");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
variables[varRef.varName] = newVal;
|
structs[procStructName].values[varRef.varName] = newVal;
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pre process value references and labels
|
// Pre process value references and labels
|
||||||
for (int j = 0; j < l.args.size(); j++) {
|
for (int j = 0; j < l.args.size(); j++) {
|
||||||
|
|
||||||
if (holds_alternative<ValueRef>(l.args[j])) {
|
if (holds_alternative<ValueRef>(l.args[j])) {
|
||||||
if (variables.find(get<ValueRef>(l.args[j]).varName) != variables.end()) {
|
string varName = get<ValueRef>(l.args[j]).varName;
|
||||||
l.args[j] = variables[get<ValueRef>(l.args[j]).varName];
|
if (variables.find(varName) != variables.end()) {
|
||||||
|
l.args[j] = variables[varName];
|
||||||
} else {
|
} else {
|
||||||
error("Could not find variable " + get<ValueRef>(l.args[j]).varName);
|
if (isReferencingStruct(varName)) {
|
||||||
|
string structName;
|
||||||
|
string varInStruct;
|
||||||
|
|
||||||
|
int dotPos = varName.find('.');
|
||||||
|
if (dotPos != string::npos) {
|
||||||
|
structName = varName.substr(0, dotPos);
|
||||||
|
varInStruct = varName.substr(dotPos + 1);
|
||||||
|
if (variables.find(structName) != variables.end()) {
|
||||||
|
if (holds_alternative<Struct>(variables[structName].val)) {
|
||||||
|
Struct structVal = get<Struct>(variables[structName].val);
|
||||||
|
if (structVal.values.find(varInStruct) != structVal.values.end()) {
|
||||||
|
l.args[j] = structVal.values[varInStruct];
|
||||||
|
} else {
|
||||||
|
error("Could not find property '" + varInStruct + "' in struct '" + structName + "'");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
error("Variable '" + structName + "' is not a struct");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
error("Could not find struct '" + structName + "'");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
error("Invalid struct member access syntax");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
error("Could not find variable " + varName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (holds_alternative<Line>(l.args[j])) {
|
} else if (holds_alternative<Line>(l.args[j])) {
|
||||||
Line ln = get<Line>(l.args[j]);
|
Line ln = get<Line>(l.args[j]);
|
||||||
@@ -916,7 +947,12 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
|
|||||||
|
|
||||||
Literal newVal;
|
Literal newVal;
|
||||||
if (type.isCustomType) {
|
if (type.isCustomType) {
|
||||||
newVal.val = structs[type.customType];
|
if (structs.find(type.customType) == structs.end()) {
|
||||||
|
error("Unknown struct type: " + type.customType);
|
||||||
|
}
|
||||||
|
|
||||||
|
Struct newStructInstance = structs[type.customType];
|
||||||
|
newVal.val = newStructInstance;
|
||||||
} else {
|
} else {
|
||||||
switch (type.type) {
|
switch (type.type) {
|
||||||
case Types::Int:
|
case Types::Int:
|
||||||
@@ -2063,7 +2099,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
|
|||||||
string structName;
|
string structName;
|
||||||
|
|
||||||
if (holds_alternative<TypeRef>(l.args[0])) {
|
if (holds_alternative<TypeRef>(l.args[0])) {
|
||||||
|
structName = get<TypeRef>(l.args[0]).customType;
|
||||||
} else {
|
} else {
|
||||||
error("First argument of struct must be a type reference");
|
error("First argument of struct must be a type reference");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,11 +18,9 @@ struct -point
|
|||||||
endfun
|
endfun
|
||||||
endstruct
|
endstruct
|
||||||
|
|
||||||
pusharg 43 32
|
|
||||||
init &myPoint -point
|
init &myPoint -point
|
||||||
|
|
||||||
set &myPoint.xpos 17
|
println $myPoint.xpos
|
||||||
set &myPoint.ypos 23
|
|
||||||
|
|
||||||
!myPoint.dingle &out
|
!myPoint.dingle &out
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user