sorry, you are right. It also came up when writing std::map::map
so it is a C++ map.
sorry, you are right. It also came up when writing std::map::map
so it is a C++ map.
I have looked at "map" examples and have understand that there are different ones as these.
Ex: Word1 will represent a Number, in this case: 1.
So "Word1" is the key value and the mapped value is: 1.
So I have made a ´rule´ for Word1 and Word2
However if I stick to this example:Code:map<string, int> data1; data1["Word1"] = 1; map<string, string> data2; data2["Word2"] = Word3;
But instead would have a string like below.Code:int Number1 = 1; if(Number1 == 1) { int i = 0; }
I dont understand the logic of how these will be put to maps and then be executable code, recogniced etc... Because in the string there is different things as ´if´, a variable, an operator and an int and also parantheses.Code:std::string Criteria = "if(Number1 == 1)"
I am still stuck only knowing that different maps consists of, string,string string, int etc.
I beleive I have to have a hierarchy of rules in maps and then when parsing this example, this will be recogniced in the maps in some way.
Last edited by Coding; 03-13-2008 at 08:32 AM.
It's actually possible to put function pointers in the map. Observe:
Be warned: This is pseudo code that is not tested.Code:typedef bool (ParseFunction)(std::string&); typedef map<std::string, ParseFunction*> FunctionMap; FunctionMap ParseMap; // ... ParseMap["if"] = &ParseIf; // ... std::string strToParse = "if (Number1 == 1)"; std::string strTemp = strToParse; int index = strTemp.find(" "); std::string strKeyword = strTemp.substr(0, index - 1); FunctionMap::iterator i = ParseMap.find( strKeyword.c_str() ); i->second( strTemp.substr(index + 1) ); // Call ParseIf // ... bool ParseIf(std::string& strToParse) { // ... }
It all feels rather weird.
At first you wanted to redefine C++ for some people. Now you are willing to implement your own interpreted language for them (which is not a small task).
Who are these people? If they are supposed to know a thing or two about programming, may-be there already exist simpler languages that they could learn instead of C++. And if they are not supposed to know anything about programming, why should they be typing code as "complicated" as if(Number1==5)? Shouldn't they be pushing buttons, making selections from listboxes etc?
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
I know it might not be a small task. The redefinement and the own interpreted is exactly the same.
The things I will interpret is not to many, it consists of about 10 different variables only and "&&", "||" more or less.
But these can be used in thousands of different combinations then.
So it will be the same if I used a simplier language than C++. It will still only consist of if, && and variables.
I might just need to understand the basics of how this could be done.
ListBoxes and buttons is impossible to use for this purpose though.
Last edited by Coding; 03-13-2008 at 09:52 AM.
Thanks for that, it will help me further.
The code above is kind of difficult for me. I do understand it but not exactly as I haven´t practically done pointers.
Something might be wrong with the second line in that code as it dont compiles.
I will look at it carefully and analyze what you have done. I have to ask though what the effect of this is.
The last line is:
Is it ment to be the: ifCode:bool ParseIf(std::string& strToParse) { // ... }
Last edited by Coding; 03-13-2008 at 09:48 AM.
I actually made a mistake on the second line. It should be:
typedef map<std::string, ParseFunction*> FunctionMap;
Updated post with slightly more correct code.
You could use a debugger to step through the code and understand how it works.
As for the function pointers... well, all it does is store the address of a function and stores that address in a map. And when it looks up the keyword, it finds that address and calls the function at that address.
I agree. And with the Windows GUI - wizard (MFC? methinks?) you can make short work of having a nice GUI app where these people can point and click all this stuff into.
The other advantage besides having a pretty GUI interface for the user, is that you can control exactly what information is entered and how it's entered. This is a huge benefit, because think about it, even writing this scripting language is severely error prone (typos?). And if there is typos etc, these people don't know how to go in and fix the code, like you said, they're not programmers.
I think it best you hide all 'code' from them, and limit their chance to screw things up. If you're the only programmer there, then ultimately it's gonna be you going in fixing their mess ups.
Ubuntu Desktop
GCC/G++
Geany (for quick projects)
Anjuta (for larger things)
Here is a basic example of how an interpreter work:
Get yourself a debugger, step through the code, try changing the expression a little and the value in the variables map and watch the outcome.Code:bool ParseIf(const std::string& strToParse); bool OperationEqual(const std::string& strToParse, const std::string& strVariable); std::map<std::string, bool (*)(const std::string&)> ParseMap; std::map<std::string, bool (*)(const std::string&, const std::string&)> ParseMap2; std::map<std::string, long> VariablesMap; void Help() { ParseMap["if"] = &ParseIf; ParseMap2["=="] = &OperationEqual; VariablesMap["Number1"] = 1; std::string strToParse = "if (Number1 == 1)"; std::string::size_type index = strToParse.find(" "); std::string strKeyword = strToParse.substr(0, index); if ( ParseMap.find( strKeyword.c_str() )->second( strToParse.substr(index + 1) ) ) ; // The if evaluated to true; execute action underneath the if else ; // The if evaluated to false; skip everything underneath the if } bool ParseIf(const std::string& strToParse) { if (strToParse[0] != '(') throw "ERROR: IF MUST BEGIN WITH ("; std::string::size_type index = strToParse.find(" "); std::string strVariable = strToParse.substr(1, index - 1); std::string::size_type index2 = strToParse.find(" ", index + 1); std::string strOperation = strToParse.substr(index + 1, index2 - index - 1); return ParseMap2.find( strOperation.c_str() )->second(strToParse.substr(index2 + 1), strVariable); } bool OperationEqual(const std::string& strToParse, const std::string& strVariable) { std::string::size_type index = strToParse.find(")"); std::string strNumber = strToParse.substr(0, index); long nNumber = strtol(strNumber.c_str(), NULL, 10); return (VariablesMap.find( strVariable.c_str() )->second == nNumber); }
If this is the way you want to do, of course.
Great thanks Elysia, I will check this out and try to understand this as good as I can.
I have to feel the logic myself to further develop something. This is not very easy though
I have to to ask. The code does not compile entirely. The compiler says that, Help, ParseIf and OperationEqual are illegal definitions. Should I include something to make it compile.
Everything above void Help() do compile.
Code:void Help() bool ParseIf(const std::string& strToParse) bool OperationEqual(const std::string& strToParse, const std::string& strVariable)
okay, that was strange. I tried to only put this and the problem starts when writing:
The compiler says: error C2601: 'Help' : local function definitions are illegalCode:void Help() { }
Any idéa what this could meen ?
Code:bool ParseIf(const std::string& strToParse); bool OperationEqual(const std::string& strToParse, const std::string& strVariable); std::map<std::string, bool (*)(const std::string&)> ParseMap; std::map<std::string, bool (*)(const std::string&, const std::string&)> ParseMap2; std::map<std::string, long> VariablesMap; void Help() { }
Last edited by Coding; 03-13-2008 at 11:53 AM.
Sorry if I dont really understand this. I have put everything inside a buttoncontrol but perheps this is completely wrong :\
Peheps I have to put this somewhere else.