source: icGREP/icgrep-devel/icgrep/pbix_compiler.cpp @ 3935

Last change on this file since 3935 was 3914, checked in by cameron, 5 years ago

Updates for icgrep-0.9: re simplifications, re names, replimit mods, debugged while loops

File size: 7.2 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 "pbix_compiler.h"
8
9Pbix_Compiler::Pbix_Compiler(std::string lf_ccname)
10{
11    m_lf_ccname = lf_ccname;
12    symgen = SymbolGenerator();
13}
14
15CodeGenState Pbix_Compiler::compile(RE *re)
16{   
17    std::string gs_retVal;
18    gs_retVal = symgen.gensym("start_marker");
19
20    CodeGenState cg_state;
21    cg_state.stmtsl.push_back(new Assign(gs_retVal, new All(1)));
22    cg_state.newsym = gs_retVal;
23
24    cg_state = re2pablo_helper(re, cg_state);
25
26    //These three lines are specifically for grep.
27    gs_retVal = symgen.gensym("marker");
28    cg_state.stmtsl.push_back(new Assign(gs_retVal, new And(new MatchStar(new Var(cg_state.newsym), new Not(new Var(m_lf_ccname))), new Var(m_lf_ccname))));
29    cg_state.newsym = gs_retVal;
30
31    return cg_state;
32}
33
34CodeGenState Pbix_Compiler::re2pablo_helper(RE *re, CodeGenState cg_state)
35{
36    if (Name* name = dynamic_cast<Name*>(re))
37    {
38        std::string gs_retVal = symgen.gensym("marker");
39        cg_state.stmtsl.push_back(new Assign(gs_retVal, new Advance(new And(new Var(cg_state.newsym), new CharClass(name->getName())))));
40        cg_state.newsym = gs_retVal;
41
42        //cout << "\n" << "(" << StatementPrinter::PrintStmts(cg_state) << ")" << "\n" << endl;
43    }
44    else if (Start* start = dynamic_cast<Start*>(re))
45    {
46        std::string gs_retVal = symgen.gensym("start_of_line_marker");
47        cg_state.stmtsl.push_back(new Assign(gs_retVal, new And(new Var(cg_state.newsym), new Not(new Advance(new Not(new CharClass(m_lf_ccname)))))));
48        cg_state.newsym = gs_retVal;
49    }
50    else if (End* end = dynamic_cast<End*>(re))
51    {
52        std::string gs_retVal = symgen.gensym("end_of_line_marker");
53        cg_state.stmtsl.push_back(new Assign(gs_retVal, new And(new Var(cg_state.newsym), new CharClass(m_lf_ccname))));
54        cg_state.newsym = gs_retVal;
55    }
56    else if (Seq* seq = dynamic_cast<Seq*>(re))
57    {
58        std::list<RE*>::iterator it = seq->GetREList()->begin();
59        if (it != seq->GetREList()->end())
60        {
61            cg_state = Seq_helper(seq->GetREList(), it, cg_state);
62        }
63    //cout << "\n" << "Seq => (" << StatementPrinter::PrintStmts(cg_state) << ")" << "\n" << endl;
64    }
65    else if (Alt* alt = dynamic_cast<Alt*>(re))
66    {
67        if (alt->GetREList() == 0)
68        {
69
70            std::string gs_retVal = symgen.gensym("always_fail_marker");
71            cg_state.stmtsl.push_back(new Assign(gs_retVal, new All(0)));
72            cg_state.newsym = gs_retVal;
73        }
74        else
75        {
76            if (alt->GetREList()->size() == 1)
77            {
78                cg_state = re2pablo_helper(alt->GetREList()->front(), cg_state);
79            }
80            else
81            {
82                std::list<RE*>::iterator it = alt->GetREList()->begin();
83                cg_state = Alt_helper(alt->GetREList(), it, cg_state);
84            }
85        }
86    //cout << "\n" << "Alt => (" << StatementPrinter::PrintStmts(cg_state) << ")" << "\n" << endl;
87    }
88    else if (Rep* rep = dynamic_cast<Rep*>(re))
89    {
90        if ((dynamic_cast<Name*>(rep->getRE()) != 0) && (rep->getLB() == 0) && (rep->getUB()== unboundedRep))
91        {
92            //std::cout << "Matchstar!" << std::endl;
93
94            Name* rep_name = dynamic_cast<Name*>(rep->getRE());
95            std::string gs_retVal = symgen.gensym("marker");
96            cg_state.stmtsl.push_back(new Assign(gs_retVal, new MatchStar(new Var(cg_state.newsym), new CharClass(rep_name->getName()))));
97            cg_state.newsym = gs_retVal;
98        }
99        else if (rep->getUB() == unboundedRep)
100        {
101            if (rep->getLB() == 0)
102            {
103                //std::cout << "While, no lb." << std::endl;
104
105                std::string while_test_gs_retVal = symgen.gensym("while_test");
106                std::string while_accum_gs_retVal = symgen.gensym("while_accum");
107                CodeGenState while_test_state;
108                while_test_state.newsym = while_test_gs_retVal;
109                CodeGenState t1_cg_state = re2pablo_helper(rep->getRE(), while_test_state);
110                cg_state.stmtsl.push_back(new Assign(while_test_gs_retVal, new Var(cg_state.newsym)));
111                cg_state.stmtsl.push_back(new Assign(while_accum_gs_retVal, new Var(cg_state.newsym)));
112                std::list<PabloS*> stmtList;
113                stmtList = t1_cg_state.stmtsl;
114                stmtList.push_back(new Assign(while_test_gs_retVal, new And(new Var(t1_cg_state.newsym), new Not(new Var(while_accum_gs_retVal)))));
115                stmtList.push_back(new Assign(while_accum_gs_retVal, new Or(new Var(while_accum_gs_retVal), new Var(t1_cg_state.newsym))));
116                cg_state.stmtsl.push_back( new While(new Var(while_test_gs_retVal), stmtList));
117                cg_state.newsym = while_accum_gs_retVal;
118            }
119            else //if (rep->getLB() > 1)
120            {
121                CodeGenState t1_cg_state = re2pablo_helper(rep->getRE(), cg_state);
122                rep->setLB(rep->getLB() - 1);
123                cg_state = re2pablo_helper(rep, t1_cg_state);
124            }
125        }
126        else if (rep->getUB() != unboundedRep)
127        {
128            if ((rep->getLB() == 0) && (rep->getUB() == 0))
129            {
130                //Just fall through...do nothing.
131            }
132            else if ((rep->getLB() == 0) && (rep->getUB() > 0))
133            {
134                CodeGenState t1_cg_state = re2pablo_helper(rep->getRE(), cg_state);
135                rep->setUB(rep->getUB() - 1);
136                CodeGenState t2_cg_state = re2pablo_helper(re, t1_cg_state);
137                std::string gs_retVal = symgen.gensym("alt_marker");
138                cg_state.stmtsl = t2_cg_state.stmtsl;
139                cg_state.stmtsl.push_back(new Assign(gs_retVal, new Or(new Var(cg_state.newsym), new Var(t2_cg_state.newsym))));
140                cg_state.newsym = gs_retVal;
141            }
142            else //if ((rep->getLB() > 0) && (rep->getUB() > 0))
143            {
144                CodeGenState t1_cg_state = re2pablo_helper(rep->getRE(), cg_state);
145                rep->setLB(rep->getLB() - 1);
146                rep->setUB(rep->getUB() - 1);
147                cg_state = re2pablo_helper(rep, t1_cg_state);
148            }
149        }
150    }
151
152    return cg_state;
153}
154
155CodeGenState Pbix_Compiler::Seq_helper(std::list<RE*>* lst, std::list<RE*>::const_iterator it, CodeGenState cg_state)
156{
157    if (it != lst->end())
158    {
159        cg_state = re2pablo_helper(*it, cg_state);
160        cg_state = Seq_helper(lst, ++it, cg_state);
161    }
162
163    return cg_state;
164}
165
166CodeGenState Pbix_Compiler::Alt_helper(std::list<RE*>* lst, std::list<RE*>::const_iterator it, CodeGenState cg_state)
167{
168    CodeGenState t1_cg_state = re2pablo_helper(*it, cg_state);
169    cg_state.stmtsl = t1_cg_state.stmtsl;
170    ++it;
171    if (it != lst->end())
172    {
173        CodeGenState t2_cg_state = Alt_helper(lst, it, cg_state);
174        cg_state.stmtsl = t2_cg_state.stmtsl;
175        std::string gs_retVal = symgen.gensym("alt_marker");
176        cg_state.stmtsl.push_back(new Assign(gs_retVal, new Or(new Var(t1_cg_state.newsym), new Var(t2_cg_state.newsym))));
177        cg_state.newsym = gs_retVal;
178    }
179    else
180    {
181        cg_state.newsym = t1_cg_state.newsym;
182    }
183
184    return cg_state;
185}
186
Note: See TracBrowser for help on using the repository browser.