#define question

This is a discussion on #define question within the C++ Programming forums, part of the General Programming Boards category; sorry, you are right. It also came up when writing std::map::map so it is a C++ map....

  1. #31
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    sorry, you are right. It also came up when writing std::map::map
    so it is a C++ map.

  2. #32
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    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
    Code:
    map<string, int> data1;
    data1["Word1"] = 1;
    
    map<string, string> data2;
    data2["Word2"] = Word3;
    However if I stick to this example:
    Code:
    int Number1 = 1;
    
    if(Number1 == 1)
    {
        int i = 0;
    }
    But instead would have a string like below.
    Code:
    std::string Criteria = "if(Number1 == 1)"
    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.
    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.

  3. #33
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    It's actually possible to put function pointers in the map. Observe:

    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)
    {
    // ...
    }
    Be warned: This is pseudo code that is not tested.
    Last edited by Elysia; 03-13-2008 at 10:04 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #34
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #35
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    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.

  6. #36
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    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&#180;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:

    Code:
    bool ParseIf(std::string& strToParse)
    {
    // ...
    }
    Is it ment to be the: if
    Last edited by Coding; 03-13-2008 at 09:48 AM.

  7. #37
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    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.
    Last edited by Elysia; 03-13-2008 at 10:05 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #38
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by anon View Post
    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 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)

  9. #39
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    Here is a basic example of how an interpreter work:

    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);
    }
    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.
    If this is the way you want to do, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #40
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    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

  11. #41
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    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)

  12. #42
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    The code is checked and compiles fine for me.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #43
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    okay, that was strange. I tried to only put this and the problem starts when writing:

    Code:
    void Help()
    
    {
    }
    The compiler says: error C2601: 'Help' : local function definitions are illegal
    Any id&#233;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.

  14. #44
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    Are you trying to put that code inside a function!?!
    Like
    Code:
    int main()
    {
    	void Help()
    	{
    		//...
    ?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #45
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    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.

Page 3 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  2. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  3. macro (#define) question
    By Darrok in forum C++ Programming
    Replies: 30
    Last Post: 12-20-2001, 04:01 PM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21