aboutsummaryrefslogtreecommitdiff
path: root/src/rule.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/rule.cpp')
-rw-r--r--src/rule.cpp167
1 files changed, 167 insertions, 0 deletions
diff --git a/src/rule.cpp b/src/rule.cpp
new file mode 100644
index 0000000..4c42346
--- /dev/null
+++ b/src/rule.cpp
@@ -0,0 +1,167 @@
1#include "rule.h"
2#include "target.h"
3#include "astbranch.h"
4#include "astleaf.h"
5#include "runner.h"
6#include "variable.h"
7#include "context.h"
8#include "condition.h"
9#include "profile.h"
10
11#include <bu/sio.h>
12using namespace Bu;
13
14Rule::Rule( const Bu::FString &sName ) :
15 sName( sName ),
16 pInput( NULL )
17{
18}
19
20Rule::~Rule()
21{
22}
23
24const Bu::FString &Rule::getName() const
25{
26 return sName;
27}
28
29void Rule::setInput( const AstBranch *pNewInput )
30{
31 pInput = pNewInput;
32}
33
34const AstBranch *Rule::getInput() const
35{
36 return pInput;
37}
38
39bool Rule::hasOutputs() const
40{
41 return !lOutput.isEmpty();
42}
43
44void Rule::addOutput( const AstBranch *pNewOutput )
45{
46 lOutput.append( pNewOutput );
47}
48
49void Rule::addProfile( const AstBranch *pProfRoot )
50{
51 Profile *pProf = new Profile( pProfRoot );
52 hProfiles.insert( pProf->getName(), pProf );
53 /*
54 hProfiles.insert(
55 dynamic_cast<const AstLeaf *>(
56 (*pProfile->getBranchBegin()).first()
57 )->getStrValue(),
58 pProfile
59 );
60 */
61}
62
63void Rule::prepTarget( class Target *pTarget )
64{
65 pTarget->setDisplay( getDisplay() );
66 for( ProfileHash::iterator i = hProfiles.begin(); i; i++ )
67 {
68 pTarget->addProfile( *i );
69 }
70 for( AstBranchList::iterator i = lRequires.begin(); i; i++ )
71 {
72 pTarget->addRequires( *i );
73 }
74}
75
76Target *Rule::createTarget( class Runner &r, const Bu::FString &sInput )
77{
78 r.getContext().pushScope();
79 r.getContext().addVariable("INPUT", sInput );
80 Target *pTrg = new Target( false );
81 for( AstBranchList::iterator i = lOutput.begin(); i; i++ )
82 {
83 Variable vOut = r.execExpr(
84 (*(*i)->getBranchBegin()).begin(),
85 Variable( sInput )
86 );
87 if( vOut.getType() == Variable::typeString )
88 {
89 pTrg->addOutput( vOut.getString() );
90 }
91 else if( vOut.getType() == Variable::typeList )
92 {
93 for( VarList::iterator j = vOut.begin(); j; j++ )
94 {
95 pTrg->addOutput( (*j).getString() );
96 }
97 }
98 }
99 r.getContext().addVariable("OUTPUT", pTrg->getOutputList() );
100 pTrg->addInput( sInput );
101 pTrg->setDisplay( getDisplay() );
102 for( ProfileHash::iterator i = hProfiles.begin(); i; i++ )
103 {
104 pTrg->addProfile( *i );
105 }
106 for( AstBranchList::iterator i = lRequires.begin(); i; i++ )
107 {
108 pTrg->addRequires( *i );
109 }
110 pTrg->setVars( r.getContext().getScope() );
111 r.getContext().popScope();
112
113 return pTrg;
114}
115
116bool Rule::ruleMatches( Runner &r, const Bu::FString &sInput )
117{
118 r.getContext().pushScope();
119 r.getContext().addVariable("INPUT", sInput );
120 Variable vInput( sInput );
121 Variable vOut = r.execExpr(
122 (*pInput->getBranchBegin()).begin(),
123 vInput
124 );
125 r.getContext().popScope();
126 if( vOut.getType() == Variable::typeBool )
127 return vOut.getBool();
128 else if( vOut.getType() == Variable::typeList )
129 return vOut.begin();
130 return false;
131}
132
133void Rule::addTag( const Bu::FString &sTag )
134{
135 lsTags.append( sTag );
136}
137
138const StrList &Rule::getTagList() const
139{
140 return lsTags;
141}
142
143void Rule::setDisplay( const Bu::FString &sStr )
144{
145 sDisplay = sStr;
146}
147
148const Bu::FString &Rule::getDisplay() const
149{
150 return ((bool)sDisplay)?(sDisplay):(sName);
151}
152
153void Rule::addRequires( const AstBranch *pBr )
154{
155 lRequires.append( pBr );
156}
157
158Bu::Formatter &operator<<( Bu::Formatter &f, const Rule &/*t*/ )
159{
160 return f << "rule";
161}
162
163template<> Bu::Formatter &Bu::operator<< <Rule>( Bu::Formatter &f, const Rule *t )
164{
165 return f << (*t);
166}
167