diff options
Diffstat (limited to 'src/rule.cpp')
| -rw-r--r-- | src/rule.cpp | 167 | 
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> | ||
| 12 | using namespace Bu; | ||
| 13 | |||
| 14 | Rule::Rule( const Bu::FString &sName ) : | ||
| 15 | sName( sName ), | ||
| 16 | pInput( NULL ) | ||
| 17 | { | ||
| 18 | } | ||
| 19 | |||
| 20 | Rule::~Rule() | ||
| 21 | { | ||
| 22 | } | ||
| 23 | |||
| 24 | const Bu::FString &Rule::getName() const | ||
| 25 | { | ||
| 26 | return sName; | ||
| 27 | } | ||
| 28 | |||
| 29 | void Rule::setInput( const AstBranch *pNewInput ) | ||
| 30 | { | ||
| 31 | pInput = pNewInput; | ||
| 32 | } | ||
| 33 | |||
| 34 | const AstBranch *Rule::getInput() const | ||
| 35 | { | ||
| 36 | return pInput; | ||
| 37 | } | ||
| 38 | |||
| 39 | bool Rule::hasOutputs() const | ||
| 40 | { | ||
| 41 | return !lOutput.isEmpty(); | ||
| 42 | } | ||
| 43 | |||
| 44 | void Rule::addOutput( const AstBranch *pNewOutput ) | ||
| 45 | { | ||
| 46 | lOutput.append( pNewOutput ); | ||
| 47 | } | ||
| 48 | |||
| 49 | void 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 | |||
| 63 | void 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 | |||
| 76 | Target *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 | |||
| 116 | bool 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 | |||
| 133 | void Rule::addTag( const Bu::FString &sTag ) | ||
| 134 | { | ||
| 135 | lsTags.append( sTag ); | ||
| 136 | } | ||
| 137 | |||
| 138 | const StrList &Rule::getTagList() const | ||
| 139 | { | ||
| 140 | return lsTags; | ||
| 141 | } | ||
| 142 | |||
| 143 | void Rule::setDisplay( const Bu::FString &sStr ) | ||
| 144 | { | ||
| 145 | sDisplay = sStr; | ||
| 146 | } | ||
| 147 | |||
| 148 | const Bu::FString &Rule::getDisplay() const | ||
| 149 | { | ||
| 150 | return ((bool)sDisplay)?(sDisplay):(sName); | ||
| 151 | } | ||
| 152 | |||
| 153 | void Rule::addRequires( const AstBranch *pBr ) | ||
| 154 | { | ||
| 155 | lRequires.append( pBr ); | ||
| 156 | } | ||
| 157 | |||
| 158 | Bu::Formatter &operator<<( Bu::Formatter &f, const Rule &/*t*/ ) | ||
| 159 | { | ||
| 160 | return f << "rule"; | ||
| 161 | } | ||
| 162 | |||
| 163 | template<> Bu::Formatter &Bu::operator<< <Rule>( Bu::Formatter &f, const Rule *t ) | ||
| 164 | { | ||
| 165 | return f << (*t); | ||
| 166 | } | ||
| 167 | |||
