aboutsummaryrefslogtreecommitdiff
path: root/src/rule.cpp
blob: dbcaad10146249607af811e16b0b8b3f7af8eb8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 2007-2012 Xagasoft, All rights reserved.
 *
 * This file is part of the Xagasoft Build and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "rule.h"
#include "target.h"
#include "astbranch.h"
#include "astleaf.h"
#include "runner.h"
#include "variable.h"
#include "context.h"
#include "condition.h"
#include "profile.h"

#include <bu/sio.h>
using namespace Bu;

Rule::Rule( const Bu::String &sName ) :
    sName( sName ),
    pInput( NULL )
{
}

Rule::~Rule()
{
}

const Bu::String &Rule::getName() const
{
    return sName;
}

void Rule::setInput( const AstBranch *pNewInput )
{
    pInput = pNewInput;
}

const AstBranch *Rule::getInput() const
{
    return pInput;
}

bool Rule::hasOutputs() const
{
    return !lOutput.isEmpty();
}

void Rule::addOutput( const AstBranch *pNewOutput )
{
    lOutput.append( pNewOutput );
}

void Rule::addProfile( const AstBranch *pProfRoot )
{
    Profile *pProf = new Profile( pProfRoot );
    hProfiles.insert( pProf->getName(), pProf );
    /*
    hProfiles.insert(
        dynamic_cast<const AstLeaf *>(
            (*pProfile->getBranchBegin()).first()
            )->getStrValue(),
        pProfile
        );
        */
}

void Rule::prepTarget( class Target *pTarget )
{
    pTarget->setDisplay( getDisplay() );
    for( ProfileHash::iterator i = hProfiles.begin(); i; i++ )
    {
        pTarget->addProfile( *i );
    }
    for( AstBranchList::iterator i = lRequires.begin(); i; i++ )
    {
        pTarget->addRequires( *i );
    }
}

Target *Rule::createTarget( class Runner &r, const Bu::String &sInput,
        Target *pParent )
{
    r.getContext().pushScope( pParent->getVars() );
    r.getContext().addVariable("INPUT", sInput );
    Target *pTrg = new Target( false );
    for( AstBranchList::iterator i = lOutput.begin(); i; i++ )
    {
        Variable vOut = r.execExpr(
            (*(*i)->getBranchBegin()).begin(),
            Variable( sInput )
            );
        if( vOut.getType() == Variable::typeString )
        {
            pTrg->addOutput( vOut.getString() );
        }
        else if( vOut.getType() == Variable::typeList )
        {
            for( VarList::iterator j = vOut.begin(); j; j++ )
            {
                pTrg->addOutput( (*j).getString() );
            }
        }
    }
    r.getContext().addVariable("OUTPUT", pTrg->getOutputList() );
    pTrg->addInput( sInput );
    pTrg->setDisplay( getDisplay() );
    for( ProfileHash::iterator i = hProfiles.begin(); i; i++ )
    {
        pTrg->addProfile( *i );
    }
    for( AstBranchList::iterator i = lRequires.begin(); i; i++ )
    {
        pTrg->addRequires( *i );
    }
    pTrg->setVars( r.getContext().getScope() );
    r.getContext().popScope();

    return pTrg;
}

bool Rule::ruleMatches( Runner &r, const Bu::String &sInput )
{
    r.getContext().pushScope();
    r.getContext().addVariable("INPUT", sInput );
    Variable vInput( sInput );
    Variable vOut = r.execExpr(
        (*pInput->getBranchBegin()).begin(),
        vInput
        );
    r.getContext().popScope();
    if( vOut.getType() == Variable::typeBool )
        return vOut.getBool();
    else if( vOut.getType() == Variable::typeList )
        return vOut.begin();
    return false;
}

void Rule::addTag( const Bu::String &sTag )
{
    lsTags.append( sTag );
}

const StrList &Rule::getTagList() const
{
    return lsTags;
}

void Rule::setDisplay( const Bu::String &sStr )
{
    sDisplay = sStr;
}

const Bu::String &Rule::getDisplay() const
{
    return (sDisplay.isSet())?(sDisplay):(sName);
}

void Rule::addRequires( const AstBranch *pBr )
{
    lRequires.append( pBr );
}

Bu::Formatter &operator<<( Bu::Formatter &f, const Rule &/*t*/ )
{
    return f << "rule";
}

template<> Bu::Formatter &Bu::operator<< <Rule>( Bu::Formatter &f, const Rule *t )
{
    return f << (*t);
}