0

I am making a shell, I understand the concepts of lexers, parsers, and ASTs, now I want to implement them. I am working with C++ and I have to define the keywords, for my shell. I am thinking of using macros for this. I want them to be accessible globally but I am struggling to get the macros right, I believe I am missing something.

class All_operators {
  std::string name; 
  std::string value;  
  int presedence;
  All_operators(std::string name, std::string value, int presedence);
};

Let us assume that this is the operator class and I make a macro

#define DECLARE_OPS(name, value, pres) All_operators name{name, value, presedence}

How do I make the objects of this class visible across the program?

What is the best way to approach this? If you have any good please refer them to me.

Thank you.

Supreet Singh
  • 87
  • 1
  • 8
  • 1
    Why use a macro for this anyway? You already have a simple enough constructor. Guideline for C++ avoid macros if you can (you may want to look at templates, they often can get the job done too, not needed in your case): https://stackoverflow.com/questions/17043090/why-should-i-avoid-macros-in-c# – Pepijn Kramer Dec 14 '21 at 15:07
  • Get your stuff working first, then see if macros would simplify anything. Aproaching it from the other end just because you're thinking of using macros will not end well. – molbdnilo Dec 14 '21 at 15:16
  • How about a `std::map` in the global namespace? `std::map my_operators;` and then you can do `my_operators[name] = All_operators{name, value, pres};` or pack it into a macro. – mch Dec 14 '21 at 15:17
  • It's very odd to name the representation of a *single* thing "all things", and keywords are usually not operators. Words have meaning, and it's easy to confuse yourself if you don't take care with naming. – molbdnilo Dec 14 '21 at 15:17

0 Answers0