mirror of
https://github.com/sam-astro/Z-Sharp.git
synced 2025-12-13 09:02:10 +00:00
Added strig differentiation, and am making cross-referencable functions that can be made in c++. Also made a "builtin.slg" file to load built-in functions and variables written in slang
This commit is contained in:
parent
29f4ad704f
commit
435d2a2270
264
Slang/Main.cpp
264
Slang/Main.cpp
@ -8,11 +8,10 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include "eval.h"
|
#include "eval.h"
|
||||||
#include "strops.h"
|
#include "strops.h"
|
||||||
|
#include "builtin.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
vector<string> types = { "int", "float", "string", "bool", "void", "null" };
|
|
||||||
|
|
||||||
vector<string> globalVariables;
|
vector<string> globalVariables;
|
||||||
vector<string> globalVariableValues;
|
vector<string> globalVariableValues;
|
||||||
vector<string> functions;
|
vector<string> functions;
|
||||||
@ -43,7 +42,7 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int ExecuteFunction(string functionName, vector<string> inputVarVals);
|
string ExecuteFunction(string functionName, vector<string> inputVarVals);
|
||||||
|
|
||||||
bool isNumber(const string& str)
|
bool isNumber(const string& str)
|
||||||
{
|
{
|
||||||
@ -65,17 +64,53 @@ string AddItem(string varName, string variableContent, string addItem)
|
|||||||
|
|
||||||
string StringRaw(string str)
|
string StringRaw(string str)
|
||||||
{
|
{
|
||||||
|
if (str.size() < 3)
|
||||||
|
return str;
|
||||||
|
|
||||||
string withoutQuotes;
|
string withoutQuotes;
|
||||||
|
|
||||||
|
if (str[0] != '\"')
|
||||||
|
withoutQuotes += str[0];
|
||||||
|
|
||||||
for (int ch = 1; ch < (int)str.size() - 1; ch++)
|
for (int ch = 1; ch < (int)str.size() - 1; ch++)
|
||||||
withoutQuotes += str[ch];
|
withoutQuotes += str[ch];
|
||||||
|
|
||||||
|
if (str[str.size() - 1] != '\"')
|
||||||
|
withoutQuotes += str[str.size() - 1];
|
||||||
|
|
||||||
return withoutQuotes;
|
return withoutQuotes;
|
||||||
}
|
}
|
||||||
|
|
||||||
string Quoted(string str)
|
string Quoted(string str)
|
||||||
{
|
{
|
||||||
return "\"" + str + "\"";
|
string withQuotes;
|
||||||
|
|
||||||
|
if (str[0] != '\"')
|
||||||
|
withQuotes += '\"';
|
||||||
|
|
||||||
|
withQuotes += str;
|
||||||
|
|
||||||
|
if (str[str.size() - 1] != '\"')
|
||||||
|
withQuotes += '\"';
|
||||||
|
|
||||||
|
return withQuotes;
|
||||||
|
}
|
||||||
|
|
||||||
|
string RMParenthesis(string str)
|
||||||
|
{
|
||||||
|
str = trim(str);
|
||||||
|
string withoutParenthesis;
|
||||||
|
|
||||||
|
if (str[0] != '(')
|
||||||
|
withoutParenthesis += str[0];
|
||||||
|
|
||||||
|
for (int ch = 1; ch < (int)str.size() - 1; ch++)
|
||||||
|
withoutParenthesis += str[ch];
|
||||||
|
|
||||||
|
if (str[str.size() - 1] != ')')
|
||||||
|
withoutParenthesis += str[str.size() - 1];
|
||||||
|
|
||||||
|
return withoutParenthesis;
|
||||||
}
|
}
|
||||||
|
|
||||||
string GetVariableValue(string varName, vector<string>& variables, vector<string>& variableVals)
|
string GetVariableValue(string varName, vector<string>& variables, vector<string>& variableVals)
|
||||||
@ -114,27 +149,107 @@ string GetVariableValue(string varName, vector<string>& variables, vector<string
|
|||||||
return variableVals[v];
|
return variableVals[v];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//else if (!isVar && count(varName, '\"') > 0)
|
|
||||||
//{
|
|
||||||
// string withoutQuotes;
|
|
||||||
|
|
||||||
// for (int ch = 1; ch < (int)varName.size() - 1; ch++)
|
|
||||||
// withoutQuotes += varName[ch];
|
|
||||||
|
|
||||||
// return withoutQuotes;
|
|
||||||
//}
|
|
||||||
|
|
||||||
return varName;
|
return varName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<string> VarValues(vector<string> varNames, vector<string>& variables, vector<string>& variableVals)
|
||||||
|
{
|
||||||
|
vector<string> realValues;
|
||||||
|
|
||||||
|
for (int varIndex = 0; varIndex < varNames.size(); varIndex++)
|
||||||
|
{
|
||||||
|
string typ = "string";
|
||||||
|
bool isVar = false;
|
||||||
|
|
||||||
|
// Checks against global vars
|
||||||
|
for (int v = 0; v < (int)globalVariables.size(); v++)
|
||||||
|
if (varNames[varIndex] == split(globalVariables[v], ' ')[1])
|
||||||
|
{
|
||||||
|
typ = split(globalVariables[v], ' ')[0];
|
||||||
|
isVar = true;
|
||||||
|
}
|
||||||
|
// Checks against local vars
|
||||||
|
for (int v = 0; v < (int)variables.size(); v++)
|
||||||
|
if (varNames[varIndex] == split(variables[v], ' ')[1])
|
||||||
|
{
|
||||||
|
typ = split(variables[v], ' ')[0];
|
||||||
|
isVar = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it is a var
|
||||||
|
if (isVar)
|
||||||
|
{
|
||||||
|
// Checks against global vars
|
||||||
|
for (int v = 0; v < (int)globalVariables.size(); v++)
|
||||||
|
if (varNames[varIndex] == split(globalVariables[v], ' ')[1])
|
||||||
|
{
|
||||||
|
realValues.push_back(globalVariableValues[v]);
|
||||||
|
}
|
||||||
|
// Checks against local vars
|
||||||
|
for (int v = 0; v < (int)variables.size(); v++)
|
||||||
|
if (varNames[varIndex] == split(variables[v], ' ')[1])
|
||||||
|
{
|
||||||
|
realValues.push_back(variableVals[v]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
realValues.push_back(varNames[varIndex]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return realValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsFunction(string funcName)
|
||||||
|
{
|
||||||
|
// Iterate through all functions
|
||||||
|
for (int t = 0; t < (int)functions.size(); t++)
|
||||||
|
if (trim(funcName) == split(functions[t], ' ')[0])
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
string EvalExpression(string expression, vector<string>& variables, vector<string>& variableVals)
|
string EvalExpression(string expression, vector<string>& variables, vector<string>& variableVals)
|
||||||
{
|
{
|
||||||
expression = trim(expression);
|
expression = trim(expression);
|
||||||
cout << "EXPRESSION: " << expression << endl;
|
//cout << "EXPRESSION: " << expression << endl;
|
||||||
|
|
||||||
// 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)
|
if ((count(expression, '+') == 0 && count(expression, '-') == 0 && count(expression, '*') == 0 && count(expression, '/') == 0 && count(expression, '(') == 0 && count(expression, '^') == 0) || split(expression, '.')[0] == "CPP")
|
||||||
return GetVariableValue(expression, variables, variableVals);
|
{
|
||||||
|
if (IsFunction(split(expression, '(')[0]))
|
||||||
|
{
|
||||||
|
//cout << split(expression, '(')[0] << endl;
|
||||||
|
string argContents = "";
|
||||||
|
int y = indexInStr(expression, '(') + 1;
|
||||||
|
while (y < expression.size() && expression[y] != ')')
|
||||||
|
{
|
||||||
|
argContents += expression[y];
|
||||||
|
|
||||||
|
y++;
|
||||||
|
}
|
||||||
|
//cout << split(expression, '(')[0] << " " << argContents << endl;
|
||||||
|
string returnVal = ExecuteFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variables, variableVals));
|
||||||
|
return returnVal;
|
||||||
|
}
|
||||||
|
else if (split(expression, '.')[0] == "CPP")
|
||||||
|
{
|
||||||
|
string argContents = "";
|
||||||
|
int y = indexInStr(expression, '(') + 1;
|
||||||
|
while (y < expression.size() && expression[y] != ')')
|
||||||
|
{
|
||||||
|
argContents += expression[y];
|
||||||
|
|
||||||
|
y++;
|
||||||
|
}
|
||||||
|
cout << split(expression, '(')[0] << " " << argContents << endl;
|
||||||
|
string returnVal = CPPFunction(split(expression, '(')[0], VarValues(split(argContents, ','), variables, variableVals));
|
||||||
|
return returnVal;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return GetVariableValue(expression, variables, variableVals);
|
||||||
|
}
|
||||||
|
|
||||||
string newExpression = "";
|
string newExpression = "";
|
||||||
bool inQuotes = false;
|
bool inQuotes = false;
|
||||||
@ -145,14 +260,44 @@ string EvalExpression(string expression, vector<string>& variables, vector<strin
|
|||||||
{
|
{
|
||||||
string name = "";
|
string name = "";
|
||||||
|
|
||||||
while (i < expression.size() && isalpha(expression[i]))
|
while (i < expression.size() && (isalpha(expression[i]) || expression[i] == '.'))
|
||||||
{
|
{
|
||||||
name += expression[i];
|
name += expression[i];
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
newExpression += GetVariableValue(name, variables, variableVals);
|
//string varVal = GetVariableValue(name, variables, variableVals);
|
||||||
|
if (IsFunction(name))
|
||||||
|
{
|
||||||
|
string argContents = "";
|
||||||
|
i++;
|
||||||
|
while (i < expression.size() && expression[i] != ')')
|
||||||
|
{
|
||||||
|
argContents += expression[i];
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
string returnVal = ExecuteFunction(name, VarValues(split(argContents, ','), variables, variableVals));
|
||||||
|
newExpression += returnVal;
|
||||||
|
//cout << newExpression << endl;
|
||||||
|
}
|
||||||
|
else if (split(name, '.')[0] == "CPP")
|
||||||
|
{
|
||||||
|
string argContents = "";
|
||||||
|
int y = indexInStr(expression, '(') + 1;
|
||||||
|
while (y < expression.size() && expression[y] != ')')
|
||||||
|
{
|
||||||
|
argContents += expression[y];
|
||||||
|
|
||||||
|
y++;
|
||||||
|
}
|
||||||
|
cout << split(expression, '(')[0] << " " << argContents << endl;
|
||||||
|
string returnVal = CPPFunction(split(name, '(')[0], VarValues(split(argContents, ','), variables, variableVals));
|
||||||
|
return returnVal;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
newExpression += GetVariableValue(name, variables, variableVals);
|
||||||
|
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
@ -161,7 +306,7 @@ string EvalExpression(string expression, vector<string>& variables, vector<strin
|
|||||||
newExpression += expression[i];
|
newExpression += expression[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cout << "NEW EXPRESSION: " << newExpression << endl;
|
//cout << "NEW EXPRESSION: " << newExpression << endl;
|
||||||
|
|
||||||
bool addStrings = false;
|
bool addStrings = false;
|
||||||
for (int i = 0; i < (int)newExpression.size(); i++)
|
for (int i = 0; i < (int)newExpression.size(); i++)
|
||||||
@ -187,7 +332,7 @@ string EvalExpression(string expression, vector<string>& variables, vector<strin
|
|||||||
withoutParenthesis += newExpression[i];
|
withoutParenthesis += newExpression[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "NewSTRING = " << Quoted(withoutParenthesis) << endl;
|
//cout << "NewSTRING = " << Quoted(withoutParenthesis) << endl;
|
||||||
return Quoted(withoutParenthesis);
|
return Quoted(withoutParenthesis);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -233,7 +378,7 @@ int evalEqu(vector<string> str, vector<string>& variables, vector<string>& varia
|
|||||||
else if (str[1] == "/=")
|
else if (str[1] == "/=")
|
||||||
variableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
variableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||||
|
|
||||||
cout << variables[v] << " is " << variableValues[v] << endl;
|
//cout << variables[v] << " is " << variableValues[v] << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -253,29 +398,35 @@ int evalEqu(vector<string> str, vector<string>& variables, vector<string>& varia
|
|||||||
else if (str[1] == "/=")
|
else if (str[1] == "/=")
|
||||||
globalVariableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
globalVariableValues[v] = EvalExpression(variableValues[v] + "/(" + unWrapVec(vector<string>(str.begin() + 2, str.end())) + ")", variables, variableValues);
|
||||||
|
|
||||||
cout << variables[v] << " is " << variableValues[v] << endl;
|
//cout << variables[v] << " is " << variableValues[v] << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int ProcessLine(vector<vector<string>> words, int lineNum, vector<string>& variables, vector<string>& variableValues)
|
string ProcessLine(vector<vector<string>> words, int lineNum, vector<string>& variables, vector<string>& variableValues)
|
||||||
{
|
{
|
||||||
if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/')
|
if (words[lineNum][0][0] == '/' && words[lineNum][0][1] == '/')
|
||||||
return 0;
|
return "";
|
||||||
|
|
||||||
if (words[lineNum][0] == "print") {
|
if (words[lineNum][0] == "print")
|
||||||
cout << EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 1, words[lineNum].end())), variables, variableValues) << endl;
|
{
|
||||||
return 0;
|
cout << StringRaw(EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 1, words[lineNum].end())), variables, variableValues)) << endl;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (words[lineNum][0] == "return") {
|
||||||
|
//cout << StringRaw(EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 1, words[lineNum].end())), variables, variableValues)) << endl;
|
||||||
|
return EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 1, words[lineNum].end())), variables, variableValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterate through all functions
|
// Iterate through all functions
|
||||||
for (int t = 0; t < (int)functions.size(); t++)
|
for (int t = 0; t < (int)functions.size(); t++)
|
||||||
{
|
{
|
||||||
if (words[lineNum][0] == split(functions[t], ' ')[0])
|
if (split(words[lineNum][0], '(')[0] == split(functions[t], ' ')[0])
|
||||||
{
|
{
|
||||||
ExecuteFunction(words[lineNum][0], split(unWrapVec(rangeInVec(words[lineNum], 1, -1)), ','));
|
ExecuteFunction(split(functions[t], ' ')[0], VarValues(split(RMParenthesis(replace(unWrapVec(words[lineNum]), split(functions[t], ' ')[0], "")), ','), variables, variableValues));
|
||||||
return 0;
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -292,15 +443,15 @@ int ProcessLine(vector<vector<string>> words, int lineNum, vector<string>& varia
|
|||||||
{
|
{
|
||||||
evalEqu(vector<string>(words[lineNum].begin() + 1, words[lineNum].end()), variables, variableValues);
|
evalEqu(vector<string>(words[lineNum].begin() + 1, words[lineNum].end()), variables, variableValues);
|
||||||
|
|
||||||
return 0;
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Checks if it is variable
|
//Checks if it is variable
|
||||||
variables.push_back(words[lineNum][0] + " " + words[lineNum][1]);
|
variables.push_back(words[lineNum][0] + " " + words[lineNum][1]);
|
||||||
variableValues.push_back(EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 3, words[lineNum].end())), variables, variableValues));
|
variableValues.push_back(EvalExpression(unWrapVec(vector<string>(words[lineNum].begin() + 3, words[lineNum].end())), variables, variableValues));
|
||||||
cout << variables[(int)variables.size() - 1] << " is " << variableValues[(int)variableValues.size() - 1] << endl;
|
//cout << variables[(int)variables.size() - 1] << " is " << variableValues[(int)variableValues.size() - 1] << endl;
|
||||||
return 0;
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Second, iterate all existing variable names
|
// Second, iterate all existing variable names
|
||||||
@ -310,7 +461,7 @@ int ProcessLine(vector<vector<string>> words, int lineNum, vector<string>& varia
|
|||||||
{
|
{
|
||||||
evalEqu(vector<string>(words[lineNum].begin(), words[lineNum].end()), variables, variableValues);
|
evalEqu(vector<string>(words[lineNum].begin(), words[lineNum].end()), variables, variableValues);
|
||||||
|
|
||||||
return 0;
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Gathers while loop contents
|
// Gathers while loop contents
|
||||||
@ -348,7 +499,7 @@ int ProcessLine(vector<vector<string>> words, int lineNum, vector<string>& varia
|
|||||||
ProcessLine(words, lineNum, variables, variableValues);
|
ProcessLine(words, lineNum, variables, variableValues);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return "";
|
||||||
}
|
}
|
||||||
// Gathers if statement contents
|
// Gathers if statement contents
|
||||||
if (words[lineNum][0] == "if")
|
if (words[lineNum][0] == "if")
|
||||||
@ -385,13 +536,13 @@ int ProcessLine(vector<vector<string>> words, int lineNum, vector<string>& varia
|
|||||||
ProcessLine(words, lineNum, variables, variableValues);
|
ProcessLine(words, lineNum, variables, variableValues);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
int ExecuteFunction(string functionName, vector<string> inputVarVals)
|
string ExecuteFunction(string functionName, vector<string> inputVarVals)
|
||||||
{
|
{
|
||||||
//vector<string> inputVarVals = split(replace(inVals, " ", ""), ',');
|
//vector<string> inputVarVals = split(replace(inVals, " ", ""), ',');
|
||||||
|
|
||||||
@ -413,7 +564,7 @@ int ExecuteFunction(string functionName, vector<string> inputVarVals)
|
|||||||
{
|
{
|
||||||
variables.push_back(trim(functionNameParts[i]));
|
variables.push_back(trim(functionNameParts[i]));
|
||||||
variableValues.push_back(EvalExpression(inputVarVals[i], variables, variableValues));
|
variableValues.push_back(EvalExpression(inputVarVals[i], variables, variableValues));
|
||||||
cout << variables[(int)variables.size() - 1] << " is " << variableValues[(int)variableValues.size() - 1] << endl;
|
//cout << variables[(int)variables.size() - 1] << " is " << variableValues[(int)variableValues.size() - 1] << endl;
|
||||||
}
|
}
|
||||||
vector<vector<string>> words;
|
vector<vector<string>> words;
|
||||||
for (int i = 0; i < (int)functionLines.size(); i++)
|
for (int i = 0; i < (int)functionLines.size(); i++)
|
||||||
@ -422,9 +573,20 @@ int ExecuteFunction(string functionName, vector<string> inputVarVals)
|
|||||||
//Iterate through all lines in function
|
//Iterate through all lines in function
|
||||||
for (int lineNum = 0; lineNum < (int)functionLines.size(); lineNum++)
|
for (int lineNum = 0; lineNum < (int)functionLines.size(); lineNum++)
|
||||||
{
|
{
|
||||||
ProcessLine(words, lineNum, variables, variableValues);
|
string returnVal = "";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string returnVal = ProcessLine(words, lineNum, variables, variableValues);
|
||||||
|
}
|
||||||
|
catch (const std::exception&)
|
||||||
|
{
|
||||||
|
cout << "\x1B[31mERROR: \'" << unWrapVec(words[lineNum]) << "\'\nIn function: " << functionName << "\nLine: " << lineNum << "\033[0m\t\t" << endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (returnVal != "")
|
||||||
|
return returnVal;
|
||||||
}
|
}
|
||||||
return 0;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
int parseSlang(string script)
|
int parseSlang(string script)
|
||||||
@ -476,28 +638,38 @@ int parseSlang(string script)
|
|||||||
functionContents = removeTabs(functionContents, 1);
|
functionContents = removeTabs(functionContents, 1);
|
||||||
functions.push_back(functName);
|
functions.push_back(functName);
|
||||||
functionValues.push_back(functionContents);
|
functionValues.push_back(functionContents);
|
||||||
cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl;
|
//cout << functName << " is \n" << Vec2Str(functionContents) << endl << endl;
|
||||||
}
|
}
|
||||||
//Checks if it is variable
|
//Checks if it is variable
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
globalVariables.push_back(words[lineNum][0] + " " + words[lineNum][1]);
|
globalVariables.push_back(words[lineNum][0] + " " + words[lineNum][1]);
|
||||||
globalVariableValues.push_back((string)words[lineNum][3]);
|
globalVariableValues.push_back((string)words[lineNum][3]);
|
||||||
cout << words[lineNum][1] << " is " << words[lineNum][3] << endl;
|
//cout << words[lineNum][1] << " is " << words[lineNum][3] << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Executes main, which is the starting function
|
// Executes main, which is the starting function
|
||||||
ExecuteFunction("Main", vector<string> {"\"hi\"", "7"});
|
ExecuteFunction("Main", vector<string> {"\"hi\"", "0"});
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
ifstream t("../Slang/script.slg");
|
ifstream builtin("../Slang/builtin.slg");
|
||||||
|
stringstream builtinString;
|
||||||
|
builtinString << builtin.rdbuf();
|
||||||
|
|
||||||
|
GetBuiltins(builtinString.str());
|
||||||
|
functions = builtinFunctions;
|
||||||
|
functionValues = builtinFunctionValues;
|
||||||
|
globalVariables = builtinVars;
|
||||||
|
globalVariableValues = builtinVarVals;
|
||||||
|
|
||||||
|
ifstream script("../Slang/script.slg");
|
||||||
stringstream scriptString;
|
stringstream scriptString;
|
||||||
scriptString << t.rdbuf();
|
scriptString << script.rdbuf();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
system("pause");
|
system("pause");
|
||||||
|
|||||||
@ -145,11 +145,13 @@
|
|||||||
<ClCompile Include="strops.cpp" />
|
<ClCompile Include="strops.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="builtin.h" />
|
||||||
<ClInclude Include="eval.h" />
|
<ClInclude Include="eval.h" />
|
||||||
<ClInclude Include="olcPixelGameEngine.h" />
|
<ClInclude Include="olcPixelGameEngine.h" />
|
||||||
<ClInclude Include="strops.h" />
|
<ClInclude Include="strops.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<None Include="builtin.slg" />
|
||||||
<None Include="script.slg" />
|
<None Include="script.slg" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
|||||||
@ -35,8 +35,12 @@
|
|||||||
<ClInclude Include="strops.h">
|
<ClInclude Include="strops.h">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="builtin.h">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="script.slg" />
|
<None Include="script.slg" />
|
||||||
|
<None Include="builtin.slg" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
92
Slang/builtin.h
Normal file
92
Slang/builtin.h
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
#ifndef BUILTIN_H
|
||||||
|
#define BUILTIN_H
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <regex>
|
||||||
|
#include <limits>
|
||||||
|
#include <algorithm>
|
||||||
|
#include "strops.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
vector<string> types = { "int", "float", "string", "bool", "void", "null" };
|
||||||
|
|
||||||
|
vector<string> builtinFunctions;
|
||||||
|
vector<vector<string>> builtinFunctionValues;
|
||||||
|
vector<string> builtinVars;
|
||||||
|
vector<string> builtinVarVals;
|
||||||
|
|
||||||
|
int GetBuiltins(string script)
|
||||||
|
{
|
||||||
|
script = replace(script, " ", "\t");
|
||||||
|
|
||||||
|
vector<string> lines = split(script, '\n');
|
||||||
|
vector<vector<string>> words;
|
||||||
|
for (int i = 0; i < (int)lines.size(); i++)
|
||||||
|
{
|
||||||
|
words.push_back(split(lines[i], ' '));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Go through entire script and iterate through all types to see if line is a
|
||||||
|
// function declaration, then store it with it's value
|
||||||
|
for (int lineNum = 0; lineNum < (int)words.size(); lineNum++)
|
||||||
|
for (int t = 0; t < (int)types.size(); t++)
|
||||||
|
if (words[lineNum][0] == types[t])
|
||||||
|
{
|
||||||
|
//Checks if it is function
|
||||||
|
if (words[lineNum][(int)words[lineNum].size() - 1][(int)words[lineNum][(int)words[lineNum].size() - 1].size() - 1] == ')')
|
||||||
|
{
|
||||||
|
vector<string> functionContents;
|
||||||
|
|
||||||
|
string functName;
|
||||||
|
for (int w = 1; w < (int)words[lineNum].size(); w++) {
|
||||||
|
if (w < (int)words[lineNum].size() - 1)
|
||||||
|
{
|
||||||
|
functName += replace(replace(words[lineNum][w], "(", " "), ")", "") + " ";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
functName += replace(replace(words[lineNum][w], "(", " "), ")", "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int numOfBrackets = 1;
|
||||||
|
for (int p = lineNum + 2; p < (int)words.size(); p++)
|
||||||
|
{
|
||||||
|
numOfBrackets += countInVector(words[p], "{") - countInVector(words[p], "}");
|
||||||
|
if (numOfBrackets == 0)
|
||||||
|
break;
|
||||||
|
functionContents.push_back("");
|
||||||
|
for (int w = 0; w < (int)words[p].size(); w++)
|
||||||
|
{
|
||||||
|
functionContents[(int)functionContents.size() - 1] += words[p][w] + " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
functionContents = removeTabs(functionContents, 1);
|
||||||
|
builtinFunctions.push_back(functName);
|
||||||
|
builtinFunctionValues.push_back(functionContents);
|
||||||
|
}
|
||||||
|
//Checks if it is variable
|
||||||
|
else
|
||||||
|
{
|
||||||
|
builtinVars.push_back(words[lineNum][0] + " " + words[lineNum][1]);
|
||||||
|
builtinVarVals.push_back((string)words[lineNum][3]);
|
||||||
|
//cout << words[lineNum][1] << " is " << words[lineNum][3] << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
string CPPFunction(string name, vector<string> args)
|
||||||
|
{
|
||||||
|
cout << name << " -- " << args[0] << endl;
|
||||||
|
if (name == "CPP.Math.Sin")
|
||||||
|
return to_string(sin(stof(args[0])));
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
7
Slang/builtin.slg
Normal file
7
Slang/builtin.slg
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
float pi = 3.14159265358979
|
||||||
|
|
||||||
|
float Sin(float input)
|
||||||
|
{
|
||||||
|
float out = CPP.Math.Sin(input)
|
||||||
|
return out
|
||||||
|
}
|
||||||
@ -8,17 +8,19 @@ void Next(string in, string sin)
|
|||||||
print in
|
print in
|
||||||
print sin
|
print sin
|
||||||
print green
|
print green
|
||||||
|
return "awe"
|
||||||
}
|
}
|
||||||
|
|
||||||
void Main(string input, int in2)
|
void Main(string input, int in)
|
||||||
{
|
{
|
||||||
|
print pi
|
||||||
int x = 1
|
int x = 1
|
||||||
|
|
||||||
int a = 0
|
int a = 0
|
||||||
int b = 1
|
int b = 1
|
||||||
|
|
||||||
print input
|
print input
|
||||||
print in2
|
print in
|
||||||
|
|
||||||
int k = 0
|
int k = 0
|
||||||
|
|
||||||
@ -26,15 +28,10 @@ void Main(string input, int in2)
|
|||||||
{
|
{
|
||||||
int k = 1 / (1+2.71828^-x)
|
int k = 1 / (1+2.71828^-x)
|
||||||
print k
|
print k
|
||||||
print "X is " + x
|
float s = Sin(x)
|
||||||
string str = x
|
print "Sin is " + s
|
||||||
|
|
||||||
// str= 0+(" :: ")
|
|
||||||
str += " :: "
|
|
||||||
str += k
|
|
||||||
|
|
||||||
x += 10
|
x += 10
|
||||||
print str
|
|
||||||
}
|
}
|
||||||
Next "seen", "bop"
|
print Next("seen", "bop")
|
||||||
}
|
}
|
||||||
@ -25,6 +25,9 @@ string trim(const string& s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
vector<string> split(string str, char del) {
|
vector<string> split(string str, char del) {
|
||||||
|
if (count(str, del) == 0)
|
||||||
|
return vector<string>{str};
|
||||||
|
|
||||||
// declaring temp string to store the curr "word" upto del
|
// declaring temp string to store the curr "word" upto del
|
||||||
string temp = "";
|
string temp = "";
|
||||||
vector<string> splitWords;
|
vector<string> splitWords;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user