FINALLLY!!!!!!!!! Working version !!

This commit is contained in:
sam-astro 2022-01-10 16:22:55 -05:00
parent bde9ed7222
commit 42bf1cccce
4 changed files with 39 additions and 36 deletions

View File

@ -58,7 +58,7 @@ vector<boost::any> VarValues(const vector<string>& varNames, const unordered_map
for (int varIndex = 0; varIndex < varNames.size(); varIndex++) for (int varIndex = 0; varIndex < varNames.size(); varIndex++)
{ {
string varName = trim(varNames[varIndex]); string varName = trim(varNames[varIndex]);
cout << varName << endl; //cout << varName << endl;
auto iA = variableValues.find(varName); auto iA = variableValues.find(varName);
if (iA != variableValues.end()) if (iA != variableValues.end())
@ -98,6 +98,8 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
string expression = trim(ex); string expression = trim(ex);
bool inQuotes = false; bool inQuotes = false;
CompilerLog("OLDEXPRESSION: |" + expression + "|");
// If no operations are applied, then return self // If no operations are applied, then return self
if ((count(expression, '+') == 0 && count(expression, '-') == 0 && count(expression, '*') == 0 && count(expression, '/') == 0 && count(expression, '(') == 0 && count(expression, '^') == 0) || split(expression, '.')[0] == "CPP") if ((count(expression, '+') == 0 && count(expression, '-') == 0 && count(expression, '*') == 0 && count(expression, '/') == 0 && count(expression, '(') == 0 && count(expression, '^') == 0) || split(expression, '.')[0] == "CPP")
{ {
@ -113,7 +115,7 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
y++; y++;
} }
//cout << split(expression, '(')[0] << " " << argContents << endl; CompilerLog(split(expression, '(')[0] + " " + AnyAsString(GetVariableValue(split(argContents, ',')[0], variableValues)));
return ExecuteFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableValues)); return ExecuteFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableValues));
} }
else if (split(expression, '.')[0] == "CPP" && !inQuotes) else if (split(expression, '.')[0] == "CPP" && !inQuotes)
@ -126,7 +128,7 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
y++; y++;
} }
//cout << split(expression, '(')[0] << " " << unWrapVec(VarValues(split(argContents, ','), variables, variableValues)) << endl; CompilerLog(split(expression, '(')[0] + " " + argContents);
return CPPFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableValues)); return CPPFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variableValues));
} }
else else
@ -163,6 +165,7 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
i++; i++;
} }
CompilerLog(split(expression, '(')[0] + " " + AnyAsString(GetVariableValue(split(argContents, ',')[0], variableValues)));
string returnVal = AnyAsString(ExecuteFunction(name, VarValues(split(argContents, ','), variableValues))); string returnVal = AnyAsString(ExecuteFunction(name, VarValues(split(argContents, ','), variableValues)));
newExpression += returnVal; newExpression += returnVal;
//cout << newExpression << endl; //cout << newExpression << endl;
@ -196,7 +199,7 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
newExpression += expression[i]; newExpression += expression[i];
} }
} }
//cout << "NEW EXPRESSION: " << newExpression << endl; CompilerLog("NEW EXPRESSION: |" + newExpression + "|");
bool addStrings = false; bool addStrings = false;
for (int i = 0; i < (int)newExpression.size(); i++) for (int i = 0; i < (int)newExpression.size(); i++)
@ -223,7 +226,7 @@ boost::any EvalExpression(const string& ex, unordered_map<string, boost::any>& v
} }
//cout << "NewSTRING = " << Quoted(withoutParenthesis) << endl; //cout << "NewSTRING = " << Quoted(withoutParenthesis) << endl;
return Quoted(withoutParenthesis); return withoutParenthesis;
} }
else else
return evaluate(newExpression); return evaluate(newExpression);
@ -248,7 +251,7 @@ bool BooleanLogic(const string& valA, const string& determinant, const string& v
return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue); return AnyAsFloat(valARealValue) < AnyAsFloat(valBRealValue);
else else
LogWarning("unrecognized determinant \'" + determinant + "\'"); LogWarning("unrecognized determinant \'" + determinant + "\'");
return false; return false;
} }
@ -294,6 +297,9 @@ 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) boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unordered_map<string, boost::any>& variableValues)
{ {
CompilerLog(unWrapVec(words[lineNum]));
CompilerLog(AnyAsString(GetVariableValue("out", variableValues)));
if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/') if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/')
return nullType; return nullType;
@ -321,10 +327,10 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
// Iterate through all types to see if line inits or // Iterate through all types to see if line inits or
// re-inits a variable then store it with it's value // re-inits a variable then store it with it's value
else if (countInVector(types, words[lineNum][0]) > 0) else if (countInVector(types, trim(words[lineNum][0])) > 0)
{ {
//cout << unWrapVec(words[lineNum]) << endl;
variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(slice(words[lineNum], 3, -1)), variableValues); variableValues[words[lineNum][1]] = EvalExpression(unWrapVec(slice(words[lineNum], 3, -1)), variableValues);
CompilerLog("new var :: " + words[lineNum][1] + " = " + AnyAsString(variableValues[words[lineNum][1]]));
return nullType; return nullType;
} }
@ -370,7 +376,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++) for (int lineNum = 0; lineNum < (int)whileContents.size(); lineNum++)
{ {
boost::any returnVal = ProcessLine(innerWords, lineNum, variableValues); boost::any returnVal = ProcessLine(innerWords, lineNum, variableValues);
if (!returnVal.empty() && returnVal.empty() == false) if (!returnVal.empty())
return returnVal; return returnVal;
} }
} }
@ -412,7 +418,7 @@ boost::any ProcessLine(const vector<vector<string>>& words, int lineNum, unorder
for (int l = 0; l < (int)ifContents.size(); l++) for (int l = 0; l < (int)ifContents.size(); l++)
{ {
boost::any returnVal = ProcessLine(innerWords, l, variableValues); boost::any returnVal = ProcessLine(innerWords, l, variableValues);
if (!returnVal.empty() && returnVal.empty() == false) if (!returnVal.empty())
return returnVal; return returnVal;
} }
} }
@ -470,7 +476,7 @@ boost::any ExecuteFunction(const string& functionName, const vector<boost::any>&
for (int i = 0; i < (int)inputVarVals.size(); i++) { for (int i = 0; i < (int)inputVarVals.size(); i++) {
variableValues[args[i]] = inputVarVals[i]; variableValues[args[i]] = inputVarVals[i];
cout << "\x1B[33m" << args[i] << " == " << AnyAsString(inputVarVals[i]) << "\033[0m\t\t" << endl; //cout << "\x1B[33m" << args[i] << " == " << AnyAsString(inputVarVals[i]) << "\033[0m\t\t" << endl;
} }
//Iterate through all lines in function //Iterate through all lines in function
@ -485,7 +491,7 @@ boost::any ExecuteFunction(const string& functionName, const vector<boost::any>&
{ {
LogCriticalError("\'" + unWrapVec(words[lineNum]) + "\'\nIn function: " + functionName + "\nLine: " + to_string(lineNum)); LogCriticalError("\'" + unWrapVec(words[lineNum]) + "\'\nIn function: " + functionName + "\nLine: " + to_string(lineNum));
} }
if (!returnVal.empty() && returnVal.empty() == false) if (!returnVal.empty())
return returnVal; return returnVal;
} }
return nullType; return nullType;
@ -512,22 +518,17 @@ int parseSlang(string script)
string functName = split(words[lineNum][1], '(')[0]; string functName = split(words[lineNum][1], '(')[0];
string args = ""; string args = "";
for (int w = 1; w < (int)words[lineNum].size(); w++) { for (int w = 1; w < (int)words[lineNum].size(); w++) // Get all words from the instantiation line: these are the args
if (w < (int)words[lineNum].size() - 1) {
{ args += replace(replace(words[lineNum][w], "(", " "), ")", "");
args += replace(replace(words[lineNum][w], "(", " "), ")", "") + ",";
}
else
{
args += replace(replace(words[lineNum][w], "(", " "), ")", "");
}
} }
args = replace(args, functName + " ", ""); args = replace(args, functName + " ", "");
CompilerLog(args);
functionContents.push_back(split(args, ',')); functionContents.push_back(split(args, ','));
int numOfBrackets = 1; int numOfBrackets = 1;
for (int p = lineNum + 3; p < (int)words.size(); p++) for (int p = lineNum + 2; p < (int)words.size(); p++)
{ {
numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}"); numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}");
if (numOfBrackets == 0) if (numOfBrackets == 0)

View File

@ -36,6 +36,12 @@ int LogWarning(const string& warningText)
return 1; return 1;
} }
int CompilerLog(const string& logText)
{
cerr << "\x1B[32mclog: " << logText << "\033[0m\t\t" << endl;
return 1;
}
int LogCriticalError(const string& errorText) int LogCriticalError(const string& errorText)
{ {
cerr << "\x1B[31mERROR: " << errorText << "\033[0m\t\t" << endl; cerr << "\x1B[31mERROR: " << errorText << "\033[0m\t\t" << endl;
@ -67,22 +73,17 @@ int GetBuiltins(const string& s)
string functName = split(words[lineNum][1], '(')[0]; string functName = split(words[lineNum][1], '(')[0];
string args = ""; string args = "";
for (int w = 1; w < (int)words[lineNum].size(); w++) { for (int w = 1; w < (int)words[lineNum].size(); w++) // Get all words from the instantiation line: these are the args
if (w < (int)words[lineNum].size() - 1) {
{ args += replace(replace(words[lineNum][w], "(", " "), ")", "");
args += replace(replace(words[lineNum][w], "(", " "), ")", "") + ",";
}
else
{
args += replace(replace(words[lineNum][w], "(", " "), ")", "");
}
} }
args = replace(args, functName + " ", ""); args = replace(args, functName + " ", "");
CompilerLog(args);
functionContents.push_back(split(args, ',')); functionContents.push_back(split(args, ','));
int numOfBrackets = 1; int numOfBrackets = 1;
for (int p = lineNum + 3; p < (int)words.size(); p++) for (int p = lineNum + 2; p < (int)words.size(); p++)
{ {
numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}"); numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}");
if (numOfBrackets == 0) if (numOfBrackets == 0)

View File

@ -1,12 +1,11 @@
// Default variables, can be overwritten // Default variables, can be overwritten
// if re-initialized or changed // if re-initialized or changed
float PI = 3.14159265358979 float PI = 3.14159265358979323846264338
float EulersNumber = 2.71828183 float EulersNumber = 2.71828183
// Trigonometric function Sin // Trigonometric function Sin
func Sin(input) func Sin(input)
{ {
//Print(input)
float out = CPP.Math.Sin(input) float out = CPP.Math.Sin(input)
return out return out
} }

View File

@ -2,6 +2,7 @@
func Main(input, in) func Main(input, in)
{ {
print "PI is: " + PI print "PI is: " + PI
int x = 1 int x = 1
print x print x
@ -12,13 +13,14 @@ func Main(input, in)
print x print x
float s = Sin(x) float s = Sin(x)
print s print s
int k = Sigmoid(s) k = Sigmoid(s)
print k print k
x += 1 x += 1
} }
CPP.Graphics.Init(64, 64, 4) //CPP.Graphics.Init(64, 64, 4)
return 0
} }
func Update(input) func Update(input)