20#include "../../kernel.hpp"
80 static const string sOperators =
"+-*/^&|!?:{";
81 string sArgs = _sArgList;
86 while (sArgs.length())
93 if (vArg.size() == 1 && !vArg[0].length())
101 for (
unsigned int j = 0; j < vArg.size(); j++)
105 if (!vArg[j].length())
110 if (vArg[j].front() ==
'(' && vArg[j].back() ==
')')
114 if (vArg[j].find_first_of(sOperators) != string::npos)
116 vArg[j] =
"(" + vArg[j] +
")";
122 for (
size_t n = 0; n <
vArguments.size(); n++)
126 if (n >= vArg.size())
129 if (!vArg[n].length())
137 if (n+1 < vArg.size())
139 for (
unsigned int k = n+1; k < vArg.size(); k++)
140 vArg[n] +=
"," + vArg[k];
142 vArg.erase(vArg.begin()+n+1, vArg.end());
147 while (sParsedDefinition.find(
vArguments[n]) != string::npos)
149 sParsedDefinition.replace(sParsedDefinition.find(
vArguments[n]),
vArguments[n].length(), vArg[n]);
161 return sParsedDefinition;
178 for (
size_t i = 0; i <
vArguments.size(); i++)
197 string sImport = _sExportedString;
201 sName = sImport.substr(0, sImport.find(
';'));
203 sImport.erase(0, sImport.find(
';')+1);
208 sImport.erase(0, sImport.find(
';')+1);
213 sImport.erase(0, sImport.find(
';')+1);
229 while (sImport.length())
231 vArguments.push_back(sImport.substr(0, sImport.find(
';')));
233 sImport.erase(0, sImport.find(
';')+1);
283 if (sDefinition.find(
"-set") != string::npos)
284 sDefinition.erase(sDefinition.find(
"-set"));
285 else if (sDefinition.find(
"--") != string::npos)
286 sDefinition.erase(sDefinition.find(
"--"));
319 static std::string sDelim =
"+-*/^!=&| ><()?[]{}$%#:.,;";
321 if (
sName.find_first_of(sDelim) != string::npos)
375 string sDelim =
"+-*/^!=&| ><()?[]{}$%§~#:.,;";
380 sArguments.erase(0, 1);
385 while (sArguments.length())
391 for (
size_t i = 0; i <
vArguments.size(); i++)
403 if (
vArguments[i].find_first_of(sDelim) != string::npos)
428 if (sAsVal.front() ==
'{')
431 if (sAsVal.back() ==
'}')
441 for (
auto iter = mAsVal.begin(); iter != mAsVal.end(); ++iter)
469 for (
size_t i = 0; i <
vArguments.size(); i++)
510 sCommands =
",for,if,while,endfor,endwhile,endif,else,elseif,continue,break,explicit,procedure,endprocedure,throw,return,switch,case,endswitch,default,";
548 string sFunctionName = sDefinition.substr(0, sDefinition.find(
'(')+1);
549 string sFunction = sDefinition.substr(sDefinition.find(
":=")+2);
550 string sFuncOccurence;
558 for (
unsigned int i = 0; i < sFunction.length(); i++)
560 if (sFunction.substr(i, sFunctionName.length()) == sFunctionName
561 && (!i || !isalnum(sFunction[i-1])))
563 sFuncOccurence = sFunction.substr(i, sFunction.find(
')', i) + 1 - i);
565 if (!
call(sFuncOccurence))
571 sFunction.replace(i, sFunction.find(
')', i) + 1 - i, sFuncOccurence);
577 return sDefinition.substr(0, sDefinition.find(
":=")) +
" := " + sFunction;
595 map<string, FunctionDefinition>::const_iterator iter =
mFunctionsMap.begin();
628 string sToLocate = sFunc.substr(0,sFunc.find(
'('));
637 if (
sBuilt_In.find(
"," + sToLocate +
"(") != string::npos ||
sCommands.find(
"," + sToLocate +
",") != string::npos)
668 mFunctionsMap[sExpr.substr(0, sExpr.find(
"()"))].appendComment(sComment);
677 if (sExpr.find(
'(') == string::npos
678 || sExpr.find(
'(') > sExpr.find(
":=")
679 ||
sBuilt_In.find(
","+sExpr.substr(0,sExpr.find(
'(')+1)) != string::npos
680 || (
sTables.length() &&
sTables.find(
";"+sExpr.substr(0,sExpr.find(
'('))+
";") != string::npos)
681 ||
sCommands.find(
","+sExpr.substr(0,sExpr.find(
'('))+
",") != string::npos
682 || sExpr.find(
":=") == string::npos)
685 if (sExpr.find(
":=") == string::npos)
687 else if (sExpr.find(
'(') == string::npos || sExpr.find(
'(') > sExpr.find(
":="))
689 else if (
sBuilt_In.find(
","+sExpr.substr(0,sExpr.find(
'(')+1)) != string::npos)
691 else if (
sTables.length() &&
sTables.find(
";"+sExpr.substr(0,sExpr.find(
'('))+
";") != string::npos)
693 else if (
sCommands.find(
","+sExpr.substr(0,sExpr.find(
'('))+
",") != string::npos)
703 string sDefinition = sExpr;
719 if (!
call(sFunctionString))
737 if (!
call(sFunctionString))
807 string sImpFunc =
"";
808 bool bDoRecursion =
false;
828 if (sExpr[sExpr.length()-1] !=
' ')
838 if (sExpr.find(iter->second.sName +
"(") != string::npos)
846 if (!
checkDelimiter(sExpr.substr(sExpr.find(iter->second.sName +
"(", nPos)-1, iter->second.sName.length()+2))
847 ||
isInQuotes(sExpr, sExpr.find(iter->second.sName +
"(", nPos),
true))
849 nPos = sExpr.find(iter->second.sName +
"(", nPos) + iter->second.sName.length() + 1;
855 sTemp = sExpr.substr(0, sExpr.find(iter->second.sName +
"(", nPos));
859 nPos = sExpr.find(iter->second.sName +
"(",nPos) + iter->second.sName.length();
862 string sArgs = sExpr.substr(nPos);
867 if (nPos_2 == string::npos)
879 sImpFunc = iter->second.parse(sArgs);
883 while (sExpr[nPos] ==
')' && sExpr[nPos+1] ==
')' && sTemp.back() ==
'(' && sTemp[sTemp.length()-2] ==
'(')
890 if (sImpFunc.front() ==
'{' && sImpFunc.back() ==
'}')
892 if (sExpr[nPos] ==
')' && sTemp.back() ==
'(')
894 if (sTemp[sTemp.length()-2] !=
' ')
896 static string sDelim =
"+-*/^!?:,!&|#";
898 if (sDelim.find(sTemp[sTemp.length()-2]) != string::npos)
910 sExpr = sTemp + sImpFunc + sExpr.substr(nPos);
912 else if (sTemp.back() ==
'(' && sExpr[nPos] ==
')')
914 sExpr = sTemp + sImpFunc + sExpr.substr(nPos);
918 sExpr = sTemp +
"(" + sImpFunc +
")" + sExpr.substr(nPos);
928 while (sExpr.find(iter->second.sName +
"(", nPos) != string::npos);
935 if (!
call(sExpr, nRecursion+1))
969 return iter->second.sDefinitionString;
988 return iter->second.sSignature;
1007 return iter->second.getDefinition();
1026 return iter->second.sComment;
1059 ofstream ofDefineFile;
1072 if (ofDefineFile.good())
1074 ofDefineFile <<
"# This file saves the function definitions. Do not edit unless you know, what you're doing!" << endl;
1079 ofDefineFile << iter->second.exportFunction() << endl;
1107 ifstream ifDefinedFile;
1115 if (ifDefinedFile.good())
1121 while (!ifDefinedFile.eof())
1123 getline(ifDefinedFile, sInputLine);
1126 if (!sInputLine.length() || sInputLine.front() ==
'#')
1167 for (
size_t i = 0; i <
sBuilt_In.length(); i++)
This class implements the basic input/ output file system and provides functionalities to work with f...
std::string ValidFileName(std::string _sFileName, const std::string sExtension=".dat", bool checkExtension=true, bool doCleanPath=true) const
This member function evaluates, whether the passed filename is a valid filename. One may supply a pre...
This class implements a single function definition. It is managed by the Define class.
bool splitAndValidateArguments()
This private member function validates the arguments of the function definition.
std::vector< std::string > vArguments
FunctionDefinition(const std::string &_sDefinitionString="")
Constructor of the FunctionDefinition class. Creates a definition from the passed definition string.
bool convertToValues()
This private member function converts the selected of the passed variables into their values.
std::string getDefinition() const
This member function returns the definition of the function without the appended parameters.
bool appendComment(const std::string &_sComment)
This member function appends a comment, which might be set after the definition.
std::string sDefinitionString
FunctionDefinition & operator=(const FunctionDefinition &)
Assignment operator overload for the Function definition class.
bool importFunction(const std::string &_sExportedString)
This member function imports a previously exported definition string and distributes its contents alo...
std::string parse(const std::string &_sArgList)
This member function parses the call to the contained function definition and returns a function defi...
bool decodeDefinition()
This private member function decodes the definition in the private member variable "sDefinitionString...
bool replaceArgumentOccurences()
This private member function replaces all occurences of the passed arguments with the corresponding n...
std::string exportFunction() const
This member function creates the save string used for writing to the definition file.
std::string sParsedDefinitionString
This class implements the function definition managing instance.
std::string getFunctionSignature(size_t _i) const
Returns the function signature of the ith defined custom function.
bool reset()
This member function resets the FunctionDefinitionManager object to a state before any function was d...
std::string getDefinitionString(size_t _i) const
Returns the definition string of the ith defined custom function.
bool defineFunc(const std::string &sExpr, bool bRedefine=false, bool bFallback=false)
This function defines a custom function, by passing it to a new FunctionDefinition class instance.
void setPredefinedFuncs(const std::string &sPredefined)
This member function updates the internal list of predefined functions. If the list is whitespace-sep...
bool undefineFunc(const std::string &sFunc)
This function removes a previously defined function from the internal memory.
std::string getComment(size_t _i) const
Returns the comment of the ith defined function.
std::string resolveRecursiveDefinitions(std::string sDefinition)
This private member function resolves recursive definitions, which are handled by replacing the occur...
bool call(std::string &sExpr, int nRecursion=0)
This function searches for known custom definitions in the passed expression and replaces them with t...
std::map< std::string, FunctionDefinition >::const_iterator findItemById(size_t id) const
This private member function returns the iterator to the function pointed by the passed ID.
bool isDefined(const std::string &sFunc)
This method checks, whether the passed function is already defined.
FunctionDefinitionManager(bool _isLocal)
Default constructor of the FunctionDefinitionManager class. Prepares the list of protected command st...
bool load(const Settings &_option, bool bAutoLoad=false)
This function loads previously saved function definitions to memory.
std::map< std::string, FunctionDefinition > mFunctionsMap
bool save(const Settings &_option)
This function saves the function definitions to the definition file.
std::string getImplementation(size_t _i) const
Returns the implementation of the ith defined custom function.
size_t getDefinedFunctions() const
Returns the number of defined functions.
std::string get(const std::string &sMessage, const std::vector< std::string > &vTokens) const
This member function returns the language string for the passed language identifier and replaces all ...
static NumeReKernel * getInstance()
This static member function returns a a pointer to the singleton instance of the kernel.
static void printPreFmt(const std::string &__sLine, bool printingEnabled=true)
This member function appends the pre- formatted string to the buffer and informs the terminal that we...
static void print(const std::string &__sLine, bool printingEnabled=true)
This member function appends the passed string as a new output line to the buffer and informs the ter...
void refreshFunctionTree()
This member function informs the GUI to reload the contents of the function tree as soon as possible.
This class manages the setting values of the internal (kernel) settings of this application.
bool systemPrints() const
Returns, whether system messages shall be printed to the terminal.
size_t getPrecision() const
Returns the precision for displaying floating numbers in the terminal. This value determines the numb...
Common exception class for all exceptions thrown in NumeRe.
@ FUNCTION_ALREADY_EXISTS
@ TOO_MANY_FUNCTION_CALLS
@ ELLIPSIS_MUST_BE_LAST_ARG
@ TOO_MANY_ARGS_FOR_DEFINE
@ FUNCTION_NAMES_MUST_NOT_CONTAIN_SIGN
@ FUNCTION_STRING_IS_COMMAND
@ CANNOT_FIND_DEFINE_OPRT
@ FUNCTION_ARGS_MUST_NOT_CONTAIN_SIGN
@ CANNOT_FIND_FUNCTION_ARGS
static size_t invalid_position
void SetExpr(StringView a_sExpr)
Set the expression. Triggers first time calculation thus the creation of the bytecode and scanning of...
value_type Eval()
Single-value wrapper around the vectorized overload of this member function.
const varmap_type & GetUsedVar()
Return a map containing the used variables only.
std::string toSystemCodePage(std::string)
Converts an internal to an external string. Does nothing currently.
unsigned int getMatchingParenthesis(const StringView &)
Returns the position of the closing parenthesis.
CONSTCD11 std::enable_if<!std::chrono::treat_as_floating_point< T >::value, T >::type trunc(T t) NOEXCEPT
Namespace for mathematical applications.
std::map< string_type, value_type * > varmap_type
Type used for storing variables.
void StripSpaces(std::string &)
Removes leading and trailing white spaces and tabulator characters.
int findParameter(const std::string &sCmd, const std::string &sParam, const char cFollowing)
This function searches the passed parameter in the passed command string. If something is found,...
std::string toString(int)
Converts an integer to a string without the Settings bloat.