visitor

ANTLR 4 and AST visitors

五迷三道 提交于 2019-12-04 10:34:21
问题 I'm trying to use ASTs with ANTLR4, with this files: Builder.java import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.TokenStream; public class Builder { public static void main(String[] args) { CharStream input = new ANTLRInputStream("ON M1==2 && M3 == 5 && (M2 > 1 || M5 <= 5.0) " + "DO P5:42 P4:10"); ExprLexer lexer = new ExprLexer(input); TokenStream tokens = new CommonTokenStream

Visitor Pattern in Objective-C

五迷三道 提交于 2019-12-04 08:16:20
问题 I've been looking at the best way to implement the Visitor design pattern in Objective-C. Since the language doesn't support method overloading, a 'traditional' implementation such as one might find in Java seems impossible. In my current implementation, I have a Visitor protocol, a Visitor class, and several subclasses of that Visitor class, along with the various objects to visit. Once a visited object accepts the Visitor, they call the visit method of the Visitor, passing themselves as an

Iterating hierarchy of nodes - Visitor and Composite?

寵の児 提交于 2019-12-04 05:04:58
Let's imagine I have a collection of nodes that I use for my Renderer class later on. Then I have a Visitor class that can visit node or whole collection. It's simple because my collection of nodes it's simply a wrapper to the std::list with few extra methods. The problem is I'd like to have a tree like structure for nodes(instead of simple list) so a node can have a parent and n children. That would be handy as I'd like to be able to pass to my Renderer a node and render everything "below" that node. The answer probably is Composite. How can I use together Visitor and Composite? I've read

Visitor and templated virtual methods

时间秒杀一切 提交于 2019-12-04 02:20:15
In a typical implementation of the Visitor pattern, the class must account for all variations (descendants) of the base class. There are many instances where the same method content in the visitor is applied to the different methods. A templated virtual method would be ideal in this case, but for now, this is not allowed. So, can templated methods be used to resolve virtual methods of the parent class? Given (the foundation): struct Visitor_Base; // Forward declaration. struct Base { virtual accept_visitor(Visitor_Base& visitor) = 0; }; // More forward declarations struct Base_Int; struct Base

Visitor pattern in python

折月煮酒 提交于 2019-12-03 16:44:40
问题 here is a simplified implementation of the visitor pattern in C++. Ist it possible to implement something like this in Python? I need it, because I will pass Objects from C++ code to a function in Python. My idea was to implement a visitor in Python to find out the type of the Object. My C++ code: #include <iostream> #include <string> class t_element_base { public: virtual void accept( class t_visitor &v ) = 0; }; class t_element_deriv_one: public t_element_base { public: void accept( t

Translator Pattern

丶灬走出姿态 提交于 2019-12-03 15:36:01
In a previous job, my manager suggested use of a Translator pattern for converting data from a DataTable to objects. Basically, the Translator class had only static (i.e. class) methods so it was an aggregation of function calls. My initial approach was to implement constructors for each object that could take a DataTable row as an argument and create an instance that corresponded to the data. He said that the Translator class had been suggested by Microsoft, and provided better modularity of code. I can see this point, but at the same time it seems like a very non-OO approach (although the

Building own C# compiler using ANTLR: Compilation Unit

China☆狼群 提交于 2019-12-03 12:40:52
问题 // Create a scanner that reads from the input stream passed to us CSLexer lexer = new CSLexer(new ANTLRFileStream(f)); tokens.TokenSource = lexer; // Create a parser that reads from the scanner CSParser parser = new CSParser(tokens); // start parsing at the compilationUnit rule CSParser.compilation_unit_return x = parser.compilation_unit(); object ast = x.Tree; What can I do with the x which is of compilation_unit_return type, to extract its root, its classes, its methods etc? Do I have to

When should you really use the visitor pattern

為{幸葍}努か 提交于 2019-12-03 08:17:20
Ok before marking this as a duplicate let me clarify myself. I'm reading about the visitor pattern and its applicable uses. I've stumbled upon this post: When should I use the Visitor Design Pattern? and the user who wrote the first answer says as follow : Now we want to add a new operation to the hierarchy, namely we want each animal to make its sound. As far as the hierarchy is this simple, you can do it with straight polymorphism: ... But proceeding in this way, each time you want to add an operation you must modify the interface to every single class of the hierarchy. Now, I mostly see why

How to make a safer C++ variant visitor, similar to switch statements?

核能气质少年 提交于 2019-12-03 06:38:31
问题 The pattern that a lot of people use with C++17 / boost variants looks very similar to switch statements. For example: (snippet from cppreference.com) std::variant<int, long, double, std::string> v = ...; std::visit(overloaded { [](auto arg) { std::cout << arg << ' '; }, [](double arg) { std::cout << std::fixed << arg << ' '; }, [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; }, }, v); The problem is when you put the wrong type in the visitor or change the variant signature

Visitor pattern in python

落爺英雄遲暮 提交于 2019-12-03 06:35:42
here is a simplified implementation of the visitor pattern in C++. Ist it possible to implement something like this in Python? I need it, because I will pass Objects from C++ code to a function in Python. My idea was to implement a visitor in Python to find out the type of the Object. My C++ code: #include <iostream> #include <string> class t_element_base { public: virtual void accept( class t_visitor &v ) = 0; }; class t_element_deriv_one: public t_element_base { public: void accept( t_visitor &v ); std::string t_element_deriv_one_text() { return "t_element_deriv_one"; } }; class t_element