diff options
-rw-r--r-- | include/printer/cfamily.h | 14 | ||||
-rw-r--r-- | include/printer/dump.h | 9 | ||||
-rw-r--r-- | include/printer/generic.h | 56 |
3 files changed, 38 insertions, 41 deletions
diff --git a/include/printer/cfamily.h b/include/printer/cfamily.h index 83e7765..0426ded 100644 --- a/include/printer/cfamily.h +++ b/include/printer/cfamily.h @@ -7,23 +7,19 @@ #include "printer/generic.h" -/*! - * \file - */ - namespace chill { namespace printer { /*! - * \brief Print the AST for C like syntax, This replace the old print function + * \brief Print the AST in a C-like syntax. + * + * This replace the old print function. * Custom multiplexer should not be needed. This version should calculate the correct precedence for expressions. * Expression should be encapsulated in {} or () or ended with ; with heuristics at the parent node * * All precedence calculation taken from http://en.cppreference.com/w/cpp/language/operator_precedence */ class CFamily : public GenericPrinter { - public: - CFamily() {} - + protected: virtual int getPrecS(chillAST_BinaryOperator *n); virtual int getPrecS(chillAST_CallExpr *n); @@ -109,6 +105,8 @@ namespace chill { virtual void printS(std::string ident, chillAST_UnaryOperator *n, std::ostream &o); virtual void printS(std::string ident, chillAST_VarDecl *n, std::ostream &o); + public: + CFamily() {} }; } } diff --git a/include/printer/dump.h b/include/printer/dump.h index fd382a3..894ea4a 100644 --- a/include/printer/dump.h +++ b/include/printer/dump.h @@ -10,14 +10,13 @@ namespace chill { namespace printer { /*! - * \brief this replace the old dump function in the chillAST + * \brief Dump the whole AST in Prefix format * + * This replace the old dump function in the chillAST. * Everthing is written in a Tree-like structure: (<NodeName> <Params>). No precedence calculation is needed. */ class Dump : public GenericPrinter { - public: - Dump() {} - + protected: virtual void printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o); virtual void printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o); @@ -86,6 +85,8 @@ namespace chill { virtual void printS(std::string ident, chillAST_VarDecl *n, std::ostream &o); + public: + Dump() {} /*! * Just prints everything. Indent is igored due to need to limit the number of output * @param ident diff --git a/include/printer/generic.h b/include/printer/generic.h index 6918136..a466e7f 100644 --- a/include/printer/generic.h +++ b/include/printer/generic.h @@ -9,33 +9,18 @@ #include <string> #include <sstream> -/*! - * \file - * \brief this is a generic AST printSer that prints the code out to a C-family like syntax - */ namespace chill { + /*! + * \brief this is a generic AST printSer that prints the code out to a C-family like syntax + */ namespace printer { class GenericPrinter { protected: std::string identSpace; - public: - GenericPrinter() { identSpace = " "; } - //! Set the indentation for print - /*! - * Some subclass has indentation unused, like Dump. Also, only spaces is supported, - * so it is a number of the spaces in the indentaion. - * @param numspaces number of spaces for the indentation - */ - void setIndentSpace(int numspaces) { - identSpace = ""; - for (int i = 0; i < numspaces; ++i) - identSpace += " "; - } - - /*! - * Default return value for get prec, can be used as a reference - * @return default precedence - */ + /*! + * Default return value for get prec, can be used as a reference + * @return default precedence + */ virtual int defGetPrecS() { return INT8_MAX; } virtual int getPrecS(chillAST_ArraySubscriptExpr *n) { return defGetPrecS(); } @@ -105,12 +90,6 @@ namespace chill { virtual int getPrecS(chillAST_UnaryOperator *n) { return defGetPrecS(); } virtual int getPrecS(chillAST_VarDecl *n) { return defGetPrecS(); } - //! return the Precedence of the corresponding AST node - /*! - * @param n the chillAST_Node - * @return a int representing the subnodes's precedence, 0 being the highest, INT8_MAX being the default - */ - virtual int getPrec(chillAST_Node *n); //! default error output when encountering not recognized error code /*! * @param ident @@ -190,6 +169,26 @@ namespace chill { virtual void printS(std::string ident, chillAST_UnaryOperator *n, std::ostream &o) { errorPrintS(ident, n, o); } virtual void printS(std::string ident, chillAST_VarDecl *n, std::ostream &o) { errorPrintS(ident, n, o); } + + public: + GenericPrinter() { identSpace = " "; } + //! Set the indentation for print + /*! + * Some subclass has indentation unused, like Dump. Also, only spaces is supported, + * so it is a number of the spaces in the indentaion. + * @param numspaces number of spaces for the indentation + */ + void setIndentSpace(int numspaces) { + identSpace = ""; + for (int i = 0; i < numspaces; ++i) + identSpace += " "; + } + //! return the Precedence of the corresponding AST node + /*! + * @param n the chillAST_Node + * @return a int representing the subnodes's precedence, 0 being the highest, INT8_MAX being the default + */ + virtual int getPrec(chillAST_Node *n); //! Print the AST to string stream, multiplexer /*! * @param ident indentation of the node @@ -224,7 +223,6 @@ namespace chill { virtual void printErr(std::string ident, chillAST_Node *n) { print(ident, n, std::cerr); } - //! Print the subexpression with precedence virtual void printPrec(std::string ident, chillAST_Node *n, std::ostream &o, int prec) { if (getPrec(n) > prec) o << "("; |