source: icGREP/icgrep-devel/icgrep/printer_pablos.cpp @ 4194

Last change on this file since 4194 was 4194, checked in by nmedfort, 5 years ago

Modified RE module to use a LLVM-like dyn_cast system; added 'make' functions to hide RE constructors.

File size: 4.6 KB
Line 
1/*
2 *  Copyright (c) 2014 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include "printer_pablos.h"
8
9//Regular Expressions
10#include "re/re_re.h"
11#include "re/re_cc.h"
12#include "re/re_start.h"
13#include "re/re_end.h"
14#include "re/re_seq.h"
15#include "re/re_name.h"
16
17//Pablo Expressions
18#include "pe_advance.h"
19#include "pe_all.h"
20#include "pe_and.h"
21#include "pe_call.h"
22#include "pe_charclass.h"
23#include "pe_matchstar.h"
24#include "pe_not.h"
25#include "pe_or.h"
26#include "pe_pabloe.h"
27#include "pe_scanthru.h"
28#include "pe_sel.h"
29#include "pe_var.h"
30#include "pe_xor.h"
31
32//Pablo Statements
33#include "ps_pablos.h"
34#include "ps_assign.h"
35#include "ps_if.h"
36#include "ps_while.h"
37
38
39std::string StatementPrinter::PrintStmts(CodeGenState cg_state)
40{
41    std::string strOut = "[";
42
43    strOut = strOut.substr(0, strOut.length() - 1);
44    strOut += "],[";
45
46    strOut = Print_PB_PabloStmts(cg_state.stmtsl, strOut);
47
48    strOut = strOut.substr(0, strOut.length() - 1);
49    strOut += "],";
50
51    //Print the name of the variable that holds the match result for the overall expression so far.
52    strOut += "'" + cg_state.newsym + "'";
53
54    return strOut;
55}
56
57std::string StatementPrinter::Print_PB_PabloStmts(std::list<PabloS*> stmts, std::string strOut)
58{
59    std::list<PabloS*>::iterator it;
60    //std::cout << "Total Statements: " + std::to_string(stmts.size()) << std::endl;
61    for (it = stmts.begin(); it != stmts.end(); ++it)
62    {
63        strOut += ShowPabloS(*it);
64    }
65
66    return strOut;
67}
68
69std::string StatementPrinter::Print_CC_PabloStmts(std::list<PabloS*> stmts)
70{
71    std::string strOut;
72
73    std::cout << "Total Statements: " + std::to_string(stmts.size()) << std::endl;
74    std::list<PabloS*>::iterator it;
75    for (it = stmts.begin(); it != stmts.end(); ++it)
76    {
77        strOut += ShowPabloS(*it) + "\n";
78    }
79
80    return strOut;
81}
82
83std::string StatementPrinter::ShowPabloS(PabloS* stmt)
84{
85    std::string retVal = "";
86
87    if (Assign* an = dynamic_cast<Assign*>(stmt))
88    {
89        retVal = "Assign('" + an->getM() + "', " + ShowPabloE(an->getExpr()) + "),";
90    }
91    else if (If* ifstmt = dynamic_cast<If*>(stmt))
92    {
93        retVal = "If(" + ShowPabloE(ifstmt->getExpr()) + ", " + Print_PB_PabloStmts(ifstmt->getPSList(), retVal) + ")";
94    }
95    else if (While* whl = dynamic_cast<While*>(stmt))
96    {
97        retVal = "While(" + ShowPabloE(whl->getExpr()) + ", " + Print_PB_PabloStmts(whl->getPSList(), retVal) + ")";
98    }
99    else retVal = "UNKNOWN_STATEMENT_TYPE!!!";
100    return retVal;
101}
102
103std::string StatementPrinter::ShowPabloE(PabloE* expr)
104{
105    std::string retVal = "";
106
107    if (All* all = dynamic_cast<All*>(expr))
108    {
109        retVal = "All " + std::to_string(all->getNum()) + " ";
110    }
111    else if (Call* pablo_call = dynamic_cast<Call*>(expr))
112    {
113        retVal = "Call '" + pablo_call->getCallee() + "'";
114    }
115    else if (Var* pablo_var = dynamic_cast<Var*>(expr))
116    {
117        retVal = "Var '" + pablo_var->getVar() + "' ";
118    }
119    else if (And* pablo_and = dynamic_cast<And*>(expr))
120    {
121        retVal = "And(" + ShowPabloE(pablo_and->getExpr1()) +", " + ShowPabloE(pablo_and->getExpr2()) + ")";
122    }
123    else if (Or* pablo_or = dynamic_cast<Or*>(expr))
124    {
125        retVal = "Or(" + ShowPabloE(pablo_or->getExpr1()) + ", " + ShowPabloE(pablo_or->getExpr2()) + ")";
126    }
127    else if (Sel* pablo_sel = dynamic_cast<Sel*>(expr))
128    {
129        retVal = "((" + ShowPabloE(pablo_sel->getIf_expr()) + "And " + ShowPabloE(pablo_sel->getT_expr()) +
130                ")|(Not(" + ShowPabloE(pablo_sel->getIf_expr()) + ") And " + ShowPabloE(pablo_sel->getF_expr()) + ")";
131    }
132    else if (Not* pablo_not = dynamic_cast<Not*>(expr))
133    {
134        retVal = "Not (" + ShowPabloE(pablo_not->getExpr()) + ")";
135    }
136    else if (CharClass* cc = dynamic_cast<CharClass*>(expr))
137    {
138        retVal = "CharClass '" + cc->getCharClass() + "'";
139    }
140    else if (re::Name * name = dynamic_cast<re::Name *>(expr))
141    {
142        retVal = "Name '" + name->getName() + "'";
143    }
144    else if (Advance* adv = dynamic_cast<Advance*>(expr))
145    {
146        retVal = "Advance(" + ShowPabloE(adv->getExpr()) + ")";
147    }
148    else if (MatchStar* mstar = dynamic_cast<MatchStar*>(expr))
149    {
150        retVal = "MatchStar (" + ShowPabloE(mstar->getExpr1()) + ", " + ShowPabloE(mstar->getExpr2()) + ")";
151    }
152    else if (ScanThru* sthru = dynamic_cast<ScanThru*>(expr))
153    {
154        retVal = "ScanThru (" + ShowPabloE(sthru->getScanFrom()) + ", " + ShowPabloE(sthru->getScanThru()) + ")";
155    }
156    else retVal = "UNKNOWN_Pablo_EXPRESSION_TYPE!!!";
157
158    return retVal;
159}
160
161
Note: See TracBrowser for help on using the repository browser.