source: icGREP/icgrep-0.8/icgrep/pbix_compiler.cpp @ 3850

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

icgrep-0.8 distribution

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