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

Last change on this file since 3940 was 3940, checked in by daled, 5 years ago

The framework for the unicode categories is in place. The static c++ code for each of the categories just needs to be placed into the stub unicode categories class.

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