~dominik-burgdoerfer/webplodder/trunk

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
// Copyright (C) 2010  Dominik 'domachine' Burgdörfer <dominik.burgdoerfer@googlemail.com>
//  This software is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  This software is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  You should have received a copy of the GNU General Public License
//  along with this software.  If not, see <http://www.gnu.org/licenses/>.


#ifndef  ELEMENTSTACK_INC
#define  ELEMENTSTACK_INC
#include "elements/element.hpp"
#include "elements/printable.hpp"
#include "elements/resolvable.hpp"
#include "utils/citerator.hpp"
#include <stx/ExpressionParser.h>
#include <boost/filesystem/path.hpp>
#include <boost/shared_ptr.hpp>
#include <list>
#include <fstream>
#include <map>

namespace wesl {
    namespace symbols {
        class Function;
        class Macro;
    }

    namespace types {
        class Object;
    }

    class ElementStack : public std::list<elements::ElementPtr>,
                         public elements::Element,
                         public elements::Printable,
                         public elements::Resolvable
    {
    public:
        // Types

        /**
         * @brief This class represents an interface to
         *        the stx-exparser library
         */
        class StxSymbolTableDriver : public stx::SymbolTable
        {
        public:
            /**
             * @brief Constructs a driver upon an ElementStack
             */
            StxSymbolTableDriver(ElementStack* stack);

            stx::AnyScalar lookupVariable(const std::string& varname) const;
            stx::AnyScalar processFunction(const std::string& funcname, 
                                  const paramlist_type& paramlist) const;

            /**
             * @brief Setter for the m_element member
             * @param nm_element the new value for m_element
             */
            void setElement(Element& element);

            /**
             * @brief Getter for the m_element member
             * @return the value of m_element
             */
            Element& element() const;

        private:
            ElementStack* m_stack;
            Element* m_element;
        };

        typedef std::map<std::string,boost::shared_ptr<symbols::Function> > FunctionMap;
        typedef std::map<std::string,boost::shared_ptr<symbols::Macro> > MacroMap;
        typedef std::map<std::string,stx::AnyScalar> VariableMap;

    public:
        // Constructors
        ElementStack(ElementStack* parent = 0);

        ElementStack(const boost::filesystem::path& sourcePath,
                     ElementStack* parent = 0);

        /**
         * @brief Opens the underlaying file so that it
         *        can be read through the CIterator
         */
        void open();

        /**
         * @brief Closes the underlaying file
         */
        void close();

        utils::CIterator& iterator();

        void setSourcePath(const boost::filesystem::path& sourcePath);

        boost::filesystem::path sourcePath() const;

        void setProcessed(bool state = true);

        bool processed() const;

        ElementStack& head();

        VariableMap::iterator lookupVariable(const std::string& name);

        VariableMap& dynamicVariableMap();
        const VariableMap& dynamicVariableMap() const;

        FunctionMap::iterator lookupFunction(const std::string& name);

        FunctionMap& dynamicFunctionMap();
        const FunctionMap& dynamicFunctionMap() const;

        MacroMap::iterator lookupMacro(const std::string& name);

        MacroMap& dynamicMacroMap();
        const MacroMap& dynamicMacroMap() const;

        void print(std::ostream& stream);

        stx::AnyScalar resolve();

        StxSymbolTableDriver& stxSymbolTableDriver();

    public:
        static boost::shared_ptr<ElementStack>
                  load(const boost::filesystem::path& path,
                       ElementStack& parent);

        static FunctionMap STATIC_FUNCTION_MAP;
        static MacroMap STATIC_MACRO_MAP;
        static VariableMap STATIC_VARIABLE_MAP;

    private:
        boost::filesystem::path m_sourcePath;
        int m_lineOffset;
        bool m_processed;
        utils::CIterator m_iterator;
        std::ifstream m_input;
        bool m_firstRead;
        FunctionMap m_dynamicFunctionMap;
        MacroMap m_dynamicMacroMap;
        VariableMap m_dynamicVariableMap;
        StxSymbolTableDriver m_stxSymbolTableDriver;
    };

    typedef boost::shared_ptr<ElementStack> ElementStackPtr;
    
}
#endif   // ----- #ifndef ELEMENTSTACK_INC  -----