mirror of
https://github.com/sam-astro/Z-Sharp.git
synced 2025-12-11 16:22:12 +00:00
Continued conversion into 'any' variable and function storage
This commit is contained in:
parent
a25df38803
commit
c360238d78
@ -7,11 +7,14 @@
|
||||
#include <limits>
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <any>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "eval.h"
|
||||
#include "strops.h"
|
||||
#include "builtin.h"
|
||||
#include "main.h"
|
||||
#include <boost/any.hpp>
|
||||
#include "anyops.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
@ -19,6 +22,7 @@ using namespace boost;
|
||||
unordered_map<string, any> globalVariableValues;
|
||||
unordered_map<string, vector<string>> functionValues;
|
||||
|
||||
|
||||
bool isNumber(const string& str)
|
||||
{
|
||||
for (char const& c : str) {
|
||||
@ -221,8 +225,8 @@ any EvalExpression(string expression, unordered_map<string, any>& variableVals)
|
||||
}
|
||||
|
||||
//string varVal = GetVariableValue(name, variables, variableVals);
|
||||
any funcPointer = IsFunction(name);
|
||||
if (funcIndex != -1 && !inQuotes)
|
||||
bool isFunc = IsFunction(name);
|
||||
if (isFunc && !inQuotes)
|
||||
{
|
||||
string argContents = "";
|
||||
i++;
|
||||
@ -232,7 +236,7 @@ any EvalExpression(string expression, unordered_map<string, any>& variableVals)
|
||||
|
||||
i++;
|
||||
}
|
||||
string returnVal = ExecuteFunction(name, VarValues(split(argContents, ','), variableVals));
|
||||
string returnVal = AnyAsString(ExecuteFunction(name, VarValues(split(argContents, ','), variableVals)));
|
||||
newExpression += returnVal;
|
||||
//cout << newExpression << endl;
|
||||
}
|
||||
@ -247,7 +251,7 @@ any EvalExpression(string expression, unordered_map<string, any>& variableVals)
|
||||
y++;
|
||||
}
|
||||
//cout << split(expression, '(')[0] << " " << argContents << endl;
|
||||
string returnVal = CPPFunction(split(name, '(')[0], VarValues(split(argContents, ',') variableVals));
|
||||
string returnVal = AnyAsString(CPPFunction(split(name, '(')[0], VarValues(split(argContents, ','), variableVals)));
|
||||
newExpression += returnVal;
|
||||
}
|
||||
else
|
||||
@ -255,7 +259,7 @@ any EvalExpression(string expression, unordered_map<string, any>& variableVals)
|
||||
if (inQuotes)
|
||||
newExpression += name;
|
||||
else
|
||||
newExpression += GetVariableValue(name, variableVals);
|
||||
newExpression += AnyAsString(GetVariableValue(name, variableVals));
|
||||
}
|
||||
|
||||
i--;
|
||||
@ -295,26 +299,26 @@ any EvalExpression(string expression, unordered_map<string, any>& variableVals)
|
||||
return Quoted(withoutParenthesis);
|
||||
}
|
||||
else
|
||||
return stof(evaluate(newExpression));
|
||||
return evaluate(newExpression);
|
||||
}
|
||||
|
||||
bool BooleanLogic(string valA, string determinant, string valB, unordered_map<string, any>& variableVals)
|
||||
{
|
||||
string valARealValue = EvalExpression(valA, variableVals);
|
||||
string valBRealValue = EvalExpression(valB, variableVals);
|
||||
any valARealValue = EvalExpression(valA, variableVals);
|
||||
any valBRealValue = EvalExpression(valB, variableVals);
|
||||
|
||||
if (determinant == "==")
|
||||
return valARealValue == valBRealValue;
|
||||
return AnyAsString(valARealValue) == AnyAsString(valBRealValue);
|
||||
if (determinant == "!=")
|
||||
return valARealValue != valBRealValue;
|
||||
return AnyAsString(valARealValue) != AnyAsString(valBRealValue);
|
||||
if (determinant == ">=")
|
||||
return floatval(valARealValue) >= floatval(valBRealValue);
|
||||
return AnyAsFloat(valARealValue) >= AnyAsFloat(valBRealValue);
|
||||
if (determinant == "<=")
|
||||
return floatval(valARealValue) <= floatval(valBRealValue);
|
||||
return AnyAsFloat(valARealValue) <= AnyAsFloat(valBRealValue);
|
||||
if (determinant == ">")
|
||||
return floatval(valARealValue) > floatval(valBRealValue);
|
||||
return AnyAsFloat(valARealValue) > AnyAsFloat(valBRealValue);
|
||||
if (determinant == "<")
|
||||
return floatval(valARealValue) < floatval(valBRealValue);
|
||||
return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue);
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -326,13 +330,13 @@ int evalEqu(vector<string> str, unordered_map<string, any>& variableValues)
|
||||
if (str[1] == "=")
|
||||
variableValues[str[0]] = EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
|
||||
else if (str[1] == "+=")
|
||||
variableValues[str[0]] += EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
|
||||
variableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
|
||||
else if (str[1] == "-=")
|
||||
variableValues[str[0]] -= EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
|
||||
variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
|
||||
else if (str[1] == "*=")
|
||||
variableValues[str[0]] *= EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
|
||||
variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
|
||||
else if (str[1] == "/=")
|
||||
variableValues[str[0]] /= EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
|
||||
variableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
|
||||
|
||||
//cout << variables[v] << " is " << variableValues[v] << endl;
|
||||
return 0;
|
||||
@ -342,13 +346,13 @@ int evalEqu(vector<string> str, unordered_map<string, any>& variableValues)
|
||||
if (str[1] == "=")
|
||||
globalVariableValues[str[0]] = EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
|
||||
else if (str[1] == "+=")
|
||||
globalVariableValues[str[0]] += EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
|
||||
globalVariableValues[str[0]] = EvalExpression(str[0] + "+(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variableValues);
|
||||
else if (str[1] == "-=")
|
||||
globalVariableValues[str[0]] -= EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
|
||||
globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) - AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
|
||||
else if (str[1] == "*=")
|
||||
globalVariableValues[str[0]] *= EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
|
||||
globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) * AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
|
||||
else if (str[1] == "/=")
|
||||
globalVariableValues[str[0]] /= EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues);
|
||||
globalVariableValues[str[0]] = AnyAsFloat(variableValues[str[0]]) / AnyAsFloat(EvalExpression(unWrapVec(vector<string>(str.begin() + 2, str.end())), variableValues));
|
||||
|
||||
//cout << variables[v] << " is " << variableValues[v] << endl;
|
||||
return 0;
|
||||
@ -359,13 +363,13 @@ int evalEqu(vector<string> str, unordered_map<string, any>& variableValues)
|
||||
any ProcessLine(vector<vector<string>> words, int lineNum, unordered_map<string, any>& variableValues)
|
||||
{
|
||||
if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/')
|
||||
return 0;
|
||||
return "";
|
||||
|
||||
// If print statement (deprecated, now use CPP.System.Print() function)
|
||||
if (words[lineNum][0] == "print")
|
||||
{
|
||||
cout << StringRaw(EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl;
|
||||
return 0;
|
||||
cout << AnyAsString(EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 1, words[lineNum].end())), variableValues)) << endl;
|
||||
return "";
|
||||
}
|
||||
|
||||
// Check if function return
|
||||
@ -377,11 +381,11 @@ any ProcessLine(vector<vector<string>> words, int lineNum, unordered_map<string,
|
||||
return EvalExpression(unWrapVec(words[lineNum]), variableValues);
|
||||
|
||||
// Check if it is function
|
||||
auto iA = functionValues.find(split(functions[t], ' ')[0]);
|
||||
auto iA = functionValues.find(trim(split(words[lineNum][0], '(')[0]));
|
||||
if (iA != functionValues.end())
|
||||
{
|
||||
ExecuteFunction(split(functions[t], ' ')[0], VarValues(split(RMParenthesis(replace(unWrapVec(words[lineNum]), split(functions[t], ' ')[0], "")), ','), variableValues));
|
||||
return 0;
|
||||
ExecuteFunction(trim(split(words[lineNum][0], '(')[0]), VarValues(split(RMParenthesis(replace(unWrapVec(words[lineNum]), trim(split(words[lineNum][0], '(')[0]), "")), ','), variableValues));
|
||||
return "";
|
||||
}
|
||||
|
||||
// Iterate through all types to see if line inits or
|
||||
@ -391,17 +395,17 @@ any ProcessLine(vector<vector<string>> words, int lineNum, unordered_map<string,
|
||||
if (words[lineNum][0] == types[t])
|
||||
{
|
||||
variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 3, words[lineNum].end())), variableValues);
|
||||
return 0;
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// Check existing variables
|
||||
auto iB = variableValues.find(words[lineNum][0]);
|
||||
if (iB != functionValues.end())
|
||||
if (iB != variableValues.end())
|
||||
{
|
||||
// Evaluates what the sign (ex. '=', '+=') does to the value on the left by the value on the right
|
||||
evalEqu(vector<string>(words[lineNum].begin(), words[lineNum].end()), variableValues);
|
||||
return 0;
|
||||
return "";
|
||||
}
|
||||
|
||||
// Gathers while loop contents
|
||||
@ -436,12 +440,12 @@ any ProcessLine(vector<vector<string>> words, int lineNum, unordered_map<string,
|
||||
//Iterate through all lines in while loop
|
||||
for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++)
|
||||
{
|
||||
string returnVal = ProcessLine(innerWords, lineNum, variableValues);
|
||||
if (returnVal != 0)
|
||||
any returnVal = ProcessLine(innerWords, lineNum, variableValues);
|
||||
if (AnyAsString(returnVal) != "")
|
||||
return returnVal;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
return "";
|
||||
}
|
||||
// Gathers if statement contents
|
||||
if (words[lineNum][0] == "if")
|
||||
@ -513,9 +517,9 @@ any ProcessLine(vector<vector<string>> words, int lineNum, unordered_map<string,
|
||||
{
|
||||
ProcessLine(innerWords, lineNum, variableValues);
|
||||
}
|
||||
return 0;
|
||||
return "";
|
||||
}
|
||||
return 0;
|
||||
return "";
|
||||
}
|
||||
//// Gathers else statement contents
|
||||
//if (words[lineNum][0] == "else")
|
||||
@ -523,7 +527,7 @@ any ProcessLine(vector<vector<string>> words, int lineNum, unordered_map<string,
|
||||
//
|
||||
//}
|
||||
|
||||
return 0;
|
||||
return "";
|
||||
}
|
||||
|
||||
any ExecuteFunction(string functionName, vector<any> inputVarVals)
|
||||
@ -535,11 +539,10 @@ any ExecuteFunction(string functionName, vector<any> inputVarVals)
|
||||
functionLines = functionValues[functionName];
|
||||
|
||||
unordered_map<string, any> variableValues;
|
||||
vector<string> functionNameParts = split(replace(functions[functionIndex], functionName + " ", ""), ',');
|
||||
vector<string> functionNameParts = split(replace(functionValues[functionName], functionName + " ", ""), ',');
|
||||
for (int i = 0; i < (int)inputVarVals.size(); i++)
|
||||
{
|
||||
variables.push_back(trim(functionNameParts[i]));
|
||||
variableValues.push_back(EvalExpression(inputVarVals[i], variables, variableValues));
|
||||
variableValues[trim(functionNameParts[i])] = EvalExpression(inputVarVals[i], variables, variableValues);
|
||||
//cout << variables[(int)variables.size() - 1] << " is " << variableValues[(int)variableValues.size() - 1] << endl;
|
||||
}
|
||||
vector<vector<string>> words;
|
||||
|
||||
@ -89,10 +89,12 @@
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<DebugInformationFormat>OldStyle</DebugInformationFormat>
|
||||
<AdditionalIncludeDirectories>D:\Code\boost</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>D:\Code\boost</AdditionalLibraryDirectories>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
|
||||
193
Slang/anyops.h
Normal file
193
Slang/anyops.h
Normal file
@ -0,0 +1,193 @@
|
||||
|
||||
#ifndef ANYOPS_H
|
||||
#define ANYOPS_H
|
||||
|
||||
using namespace boost;
|
||||
|
||||
// Will convert type 'any' val to a bool
|
||||
bool AnyAsBool(any val)
|
||||
{
|
||||
try // Try converting to bool
|
||||
{
|
||||
return any_cast<bool>(val);
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
try // Try converting to string
|
||||
{
|
||||
return any_cast<string>(val) == "true";
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
try // Try converting to float
|
||||
{
|
||||
return any_cast<float>(val) == 1.0f;
|
||||
}
|
||||
catch (const std::exception&) // Try converting to int
|
||||
{
|
||||
try
|
||||
{
|
||||
return any_cast<int>(val) == 1;
|
||||
}
|
||||
catch (const std::exception&) // Does not convert, return
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Will convert type 'any' val to a string
|
||||
string AnyAsString(any val)
|
||||
{
|
||||
try // Try converting to string
|
||||
{
|
||||
return any_cast<string>(val);
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
try // Try converting to int
|
||||
{
|
||||
return to_string(any_cast<int>(val));
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
try // Try converting to float
|
||||
{
|
||||
return to_string(any_cast<float>(val));
|
||||
}
|
||||
catch (const std::exception&) // Try converting to bool
|
||||
{
|
||||
try
|
||||
{
|
||||
string i = "false";
|
||||
if (any_cast<bool>(val) == true) i = "true";
|
||||
return i;
|
||||
}
|
||||
catch (const std::exception&) // Does not convert, return
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// Will convert type 'any' val to a float
|
||||
float AnyAsFloat(any val)
|
||||
{
|
||||
try // Try converting to float
|
||||
{
|
||||
return any_cast<float>(val);
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
try // Try converting to int
|
||||
{
|
||||
return (float)any_cast<int>(val);
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
try // Try converting to string, then converting it to float
|
||||
{
|
||||
return stof(any_cast<string>(val));
|
||||
}
|
||||
catch (const std::exception&) // Try converting to bool
|
||||
{
|
||||
try
|
||||
{
|
||||
float i = 0;
|
||||
if (any_cast<bool>(val) == true) i = 1;
|
||||
return i;
|
||||
}
|
||||
catch (const std::exception&) // Does not convert, return
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Will convert type 'any' val to an integer
|
||||
int AnyAsInt(any val)
|
||||
{
|
||||
try // Try converting to int
|
||||
{
|
||||
return any_cast<int>(val);
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
try // Try converting to float
|
||||
{
|
||||
return (int)any_cast<float>(val);
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
try // Try converting to string, then converting it to int
|
||||
{
|
||||
return stoi(any_cast<string>(val));
|
||||
}
|
||||
catch (const std::exception&) // Try converting to bool
|
||||
{
|
||||
try
|
||||
{
|
||||
int i = 0;
|
||||
if (any_cast<bool>(val) == true) i = 1;
|
||||
return i;
|
||||
}
|
||||
catch (const std::exception&) // Does not convert, return
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Gets type of 'any' val
|
||||
// 0 -> int; 1 -> float; 2 -> bool; 3 -> string;
|
||||
int any_type(any val)
|
||||
{
|
||||
try // Try converting to int
|
||||
{
|
||||
int i = any_cast<int>(val);
|
||||
return 0;
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
try // Try converting to float
|
||||
{
|
||||
float f = any_cast<float>(val);
|
||||
return 1;
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
try // Try converting to bool
|
||||
{
|
||||
bool b = any_cast<bool>(val);
|
||||
return 2;
|
||||
}
|
||||
catch (const std::exception&) // Try converting to string
|
||||
{
|
||||
try
|
||||
{
|
||||
string s = any_cast<string>(val);
|
||||
return 3;
|
||||
}
|
||||
catch (const std::exception&) // Does not convert, return
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -10,6 +10,7 @@
|
||||
#include <boost/any.hpp>
|
||||
#include "strops.h"
|
||||
#include "graphics.h"
|
||||
#include "anyops.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -89,28 +90,28 @@ int GetBuiltins(string script)
|
||||
}
|
||||
|
||||
// Executes
|
||||
any CPPFunction(string name, vector<string> args)
|
||||
any CPPFunction(string name, vector<any> args)
|
||||
{
|
||||
if (name == "CPP.Math.Sin")
|
||||
return sin(stof(args[0]));
|
||||
return sin(AnyAsFloat(args[0]));
|
||||
else if (name == "CPP.Math.Cos")
|
||||
return cos(stof(args[0]));
|
||||
return cos(AnyAsFloat(args[0]));
|
||||
else if (name == "CPP.Math.Tan")
|
||||
return tan(stof(args[0]));
|
||||
return tan(AnyAsFloat(args[0]));
|
||||
else if (name == "CPP.Math.Round")
|
||||
return round(stof(args[0]));
|
||||
return AnyAsInt(args[0]);
|
||||
else if (name == "CPP.Graphics.Init")
|
||||
{
|
||||
cout << "\x1B[32mInit graphics\033[0m\t\t" << endl;
|
||||
if (mainWindow.Construct(stoi(args[0]), stoi(args[1]), stoi(args[2]), stoi(args[2])))
|
||||
if (mainWindow.Construct(AnyAsInt(args[0]), AnyAsInt(args[1]), AnyAsInt(args[2]), AnyAsInt(args[2])))
|
||||
mainWindow.Start();
|
||||
}
|
||||
else if (name == "CPP.Graphics.SetPixel")
|
||||
mainWindow.Draw(stoi(args[0]), stoi(args[1]), olc::Pixel(stoi(args[2]), stoi(args[3]), stoi(args[4])));
|
||||
mainWindow.Draw(AnyAsInt(args[0]), AnyAsInt(args[1]), olc::Pixel(AnyAsInt(args[2]), AnyAsInt(args[3]), AnyAsInt(args[4])));
|
||||
else if (name == "CPP.System.Print")
|
||||
cout << stoi(args[0]);
|
||||
cout << AnyAsString(args[0]);
|
||||
else if (name == "CPP.System.PrintLine")
|
||||
cout << stoi(args[0]) << endl;
|
||||
cout << AnyAsString(args[0]) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user