This commit is contained in:
sam-astro 2022-01-06 18:41:41 -05:00
parent 41b90c3b4b
commit 7b737d7799
5 changed files with 50 additions and 30 deletions

View File

@ -286,14 +286,17 @@ int varOperation(const vector<string>& str, unordered_map<string, boost::any>& v
boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<string, boost::any>& variableValues)
{
if (words[lineNum].size() == 0)
return nullType;
if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/')
return boost::any{};
return nullType;
// If print statement (deprecated, now use CPP.System.Print() function)
else if (words[lineNum][0] == "print")
{
cout << AnyAsString(EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl;
return boost::any{};
return nullType;
}
// Check if function return
@ -308,7 +311,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
else if (IsFunction(trim(split(words[lineNum][0], '(')[0])))
{
ExecuteFunction(trim(split(words[lineNum][0], '(')[0]), VarValues(split(RMParenthesis(replace(unWrapVec(words[lineNum]), trim(split(words[lineNum][0], '(')[0]), "")), ','), variableValues));
return boost::any{};
return nullType;
}
// Iterate through all types to see if line inits or
@ -316,7 +319,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
else if (countInVector(types, words[lineNum][0]) > 0)
{
variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 3, words[lineNum].end())), variableValues);
return boost::any{};
return nullType;
}
// Check existing variables: If matches, then it means
@ -325,7 +328,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
{
// Evaluates what the operator (ex. '=', '+=') does to the value on the left by the value on the right
varOperation(vector<string>(words[lineNum].begin(), words[lineNum].end()), variableValues);
return boost::any{};
return nullType;
}
// Gathers while loop contents
@ -361,11 +364,11 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++)
{
boost::any returnVal = ProcessLine(innerWords, lineNum, variableValues);
if (AnyAsString(returnVal) != "")
if (!returnVal.empty() && returnVal.empty() == false)
return returnVal;
}
}
return boost::any{};
return nullType;
}
// Gathers if statement contents
@ -403,7 +406,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
for (int l = 0; l < (int)ifContents.size(); l++)
{
boost::any returnVal = ProcessLine(innerWords, l, variableValues);
if (!returnVal.empty())
if (!returnVal.empty() && returnVal.empty() == false)
return returnVal;
}
}
@ -438,9 +441,9 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
{
ProcessLine(innerWords, lineNum, variableValues);
}
return boost::any{};
return nullType;
}
return boost::any{};
return nullType;
}
//// Gathers else statement contents
//if (words[lineNum][0] == "else")
@ -448,7 +451,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
//
//}
return boost::any{};
return nullType;
}
boost::any ExecuteFunction(const string& functionName, const vector<boost::any>& inputVarVals)
@ -457,11 +460,9 @@ boost::any ExecuteFunction(const string& functionName, const vector<boost::any>&
vector<vector<string>> words = functionValues[functionName];
unordered_map<string, boost::any> variableValues;
vector<string> args = split(functionValues[functionName][0][0], ',');
vector<string> args = split(words[0][0], ',');
for (int i = 0; i < (int)inputVarVals.size(); i++)
{
variableValues[trim(args[i])] = EvalExpression(AnyAsString(inputVarVals[i]), variableValues);
}
variableValues[args[i]] = inputVarVals[i];
//Iterate through all lines in function
for (int lineNum = 0; lineNum < (int)words.size(); lineNum++)
@ -475,10 +476,10 @@ boost::any ExecuteFunction(const string& functionName, const vector<boost::any>&
{
LogCriticalError("\'" + unWrapVec(words[lineNum]) + "\'\nIn function: " + functionName + "\nLine: " + to_string(lineNum));
}
if (!returnVal.empty())
if (!returnVal.empty() && returnVal.empty() == false)
return returnVal;
}
return boost::any{};
return nullType;
}
int parseSlang(string script)
@ -537,8 +538,8 @@ int parseSlang(string script)
globalVariableValues[words[lineNum][1]] = stof(words[lineNum][3]);
else if (words[lineNum][0] == "bool")
globalVariableValues[words[lineNum][1]] = stob(words[lineNum][3]);
else
LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum));
//else
// LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum));
}
}

View File

@ -75,7 +75,7 @@ string AnyAsString(const boost::any& val)
catch (boost::bad_any_cast) // Does not convert, return
{
LogWarning("invalid conversion to type \'string\'");
return "";
return "ERR";
}
}
}
@ -196,4 +196,21 @@ int any_type(const boost::any& val)
}
}
// Gets if any is NullType
bool any_null(const boost::any& val)
{
/*if (val.type() == typeid(NullType))
return true;*/
return false;
//try // Try converting to Null
//{
// NullType n = any_cast<NullType>(val);
// return true;
//}
//catch (boost::bad_any_cast)
//{
// return false;
//}
}
#endif

View File

@ -23,6 +23,12 @@ unordered_map<string, boost::any> builtinVarVals;
Parser mainWindow;
class NullType {
public:
string type = "NULL";
};
boost::any nullType;
int LogWarning(const string& warningText)
{
@ -96,8 +102,8 @@ int GetBuiltins(const string& s)
builtinVarVals[words[lineNum][1]] = stof(words[lineNum][3]);
else if (words[lineNum][0] == "bool")
builtinVarVals[words[lineNum][1]] = stob(words[lineNum][3]);
else
LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum));
//else
// LogWarning("unrecognized type \'" + words[lineNum][0] + "\' on line: " + to_string(lineNum));
}
}

View File

@ -19,14 +19,10 @@ bool isNumber(const string& str)
return true;
}
bool stob(string str)
bool stob(const string& str)
{
//transform(str.begin(), str.end(), str.begin(), ::tolower);
//istringstream is(str);
//bool b;
//is >> boolalpha >> b;
//return b;
return false;
bool b = trim(str) == "true";
return b;
}
string StringRaw(const string& s)

View File

@ -7,7 +7,7 @@ using namespace std;
bool isNumber(const string& str);
bool stob(string str);
bool stob(const string& str);
string StringRaw(const string& s);