Adding all the files for an integer version of Funky: Groovy
[Funky.git] / lib / Funky / Private / Evaluator.h
blob71491f3b2a312a8746d67f5785df3d6cdf09fd1b
1 /* Funky: a light-weight embeddable programming language
2 * Copyright (c) 2007, Ronald Landheer-Cieslak
3 * All rights reserved
4 *
5 * This is free software. You may distribute it and/or modify it and
6 * distribute modified forms provided that the following terms are met:
8 * * Redistributions of the source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution;
13 * * None of the names of the authors of this software may be used to endorse
14 * or promote this software, derived software or any distribution of this
15 * software or any distribution of which this software is part, without
16 * prior written permission from the authors involved;
17 * * Unless you have received a written statement from Ronald Landheer-Cieslak
18 * that says otherwise, the terms of the GNU General Public License, as
19 * published by the Free Software Foundation, version 2 or (at your option)
20 * any later version, also apply.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 #ifndef _funky_private_evaluator_h
35 #define _funky_private_evaluator_h
37 #include "StoredFunctions.h"
39 namespace Funky
41 namespace Private
43 template < typename Arguments >
44 struct Evaluator
46 typedef boost::spirit::tree_match< const char * >::tree_iterator TreeIterator; // This is the type of an iterator with which we can traverse the parse tree
47 typedef boost::spirit::tree_node< boost::spirit::node_val_data<> > Node; // This is the type of a node in the parse tree
48 typedef std::stack< std::pair< Node, Arguments * > > Stack;
50 Evaluator(const StoredFunctions< Arguments > & functions)
51 : functions_(functions),
52 value_(0)
53 { /* no-op */ }
55 Evaluator & operator()(const Node & node, Arguments * arguments)
57 stack_.push(std::make_pair(node, arguments));
58 Loki::ScopeGuard stack_guard(Loki::MakeObjGuard(stack_, &Stack::pop));
60 Arguments args; // these are the ones we will pass to the next function we call
62 if (stack_.top().first.value.id() == function_name_id__)
64 std::string function_to_call(stack_.top().first.value.begin(), stack_.top().first.value.end());
65 // look up the function
66 typename StoredFunctions< Arguments >::const_iterator which(functions_.find(function_to_call));
67 if (which == functions_.end() && function_to_call != "shift")
68 throw Exceptions::FunctionCallError(Exceptions::FunctionCallError::unknown_function__);
69 else if (function_to_call == "shift")
71 if (stack_.top().second->empty())
72 throw Exceptions::FunctionCallError(Exceptions::FunctionCallError::no_more_arguments__);
73 else
75 value_ = Arguments(1, stack_.top().second->at(0));
76 stack_.top().second->erase(stack_.top().second->begin());
79 else
81 Arguments no_args;
82 value_ = which->second->call(*this, &no_args);
85 else
86 { // function call with arguments
87 TreeIterator curr(stack_.top().first.children.begin());
89 /* normally, the first node we find is always the name of a
90 * function to call. After that, we will find any number of
91 * arguments to pass to it, but those arguments can be either
92 * literals, parameters passed to us, or statements. If they
93 * are statements, we will need to evaluate them. If they are
94 * literals, we have the value we want; if they are parameters,
95 * they are in the stack's current top. */
96 // get the name of the next function to call
97 assert(curr->value.id() == function_name_id__);
98 std::string function_name(curr->value.begin(), curr->value.end());
99 typename StoredFunctions< Arguments >::const_iterator which(functions_.find(function_name));
100 enum { custom__, if__, defined__ } function_type(custom__);
101 if (which == functions_.end())
103 /* three possibilities: either it's an "if", for which we
104 * have a built-in version right here; or it's a "defined",
105 * in which case there should be one argument, which should be
106 * a parameter, or it's an unknown function, in which case we
107 * throw an exception.
108 * Note that we only start checking whether it's an "if" or
109 * a "defined" *after" we haven't found the function in the
110 * function map: we leave the possibility to the user to
111 * override both built-in operators, just like we leave
112 * him the possibility to override any built-in function.
113 * There is no such thing as a reserved word in Funky. */
114 if (function_name == "if")
115 function_type = if__;
116 else if (function_name == "defined")
117 function_type = defined__;
118 else
119 throw Exceptions::FunctionCallError(Exceptions::FunctionCallError::unknown_function__);
121 else
122 { /* we know this function - now call it */ }
123 ++curr;
124 assert(curr->value.id() == comma_separated_list_id__);
125 TreeIterator end(curr->children.end());
126 TreeIterator curr_arg(curr->children.begin());
127 unsigned int argument_index(0);
128 bool evaluate_second(true);
129 bool evaluate_third(true);
130 if (function_type == if__ && std::distance(curr_arg, end) != 6 /* counting commas */)
131 throw Exceptions::FunctionCallError(Exceptions::FunctionCallError::argument_count_mismatch__, 3, std::distance(curr_arg, end) / 2);
132 else
133 { /* all is well so far */ }
134 if (function_type == defined__ && std::distance(curr_arg, end) != 2 /* counting commas */)
135 throw Exceptions::FunctionCallError(Exceptions::FunctionCallError::argument_count_mismatch__, 1, std::distance(curr_arg, end) / 2);
136 else
137 { /* all is well so far */ }
140 assert(curr_arg == end || *(curr_arg->value.begin()) == ',');
141 if (curr_arg != end)
143 ++curr_arg;
144 assert(curr_arg != end);
145 if ((argument_index == 1 && !evaluate_second) ||
146 (argument_index == 2 && !evaluate_third))
147 { /* skip this argument */ }
148 else
150 switch (curr_arg->value.id().to_long())
152 case literal_id__ :
153 if (function_type == defined__)
154 args.push_back(1); // literals are always defined
155 else
156 args.push_back(boost::lexical_cast< typename Arguments::value_type >(std::string(curr_arg->value.begin(), curr_arg->value.end())));
157 break;
158 case parameter_id__ :
160 std::string parameter_id(std::string(curr_arg->value.begin(), curr_arg->value.end()));
161 assert(parameter_id.size() > 1);
162 if (parameter_id != "@@")
164 unsigned int index(boost::lexical_cast<unsigned int>(parameter_id.substr(1)));
165 if (stack_.top().second->size() <= index && function_type != defined__)
166 throw Exceptions::FunctionCallError(Exceptions::FunctionCallError::argument_out_of_range__, stack_.top().second->size() - 1, index);
167 else if (function_type == defined__ && stack_.top().second->size() <= index)
168 args.push_back(0);
169 else if (function_type == defined__)
170 args.push_back(1);
171 else
172 args.push_back(stack_.top().second->at(index));
174 else
176 if (function_type == defined__)
177 args.push_back(!stack_.top().second->empty());
178 else // copy all arguments
179 std::copy(stack_.top().second->begin(), stack_.top().second->end(), std::back_inserter(args));
182 break;
183 case statement_id__ :
185 // can't pass a statement to defined
186 if (function_type == defined__)
187 throw Exceptions::FunctionCallError(Exceptions::FunctionCallError::unexpected_statement__);
188 else
189 { /* not a defined call */ }
190 Arguments temp_args((*this)(*curr_arg, stack_.top().second).value_);
191 std::copy(temp_args.begin(), temp_args.end(), std::back_inserter(args));
193 break;
194 case function_name_id__ : // a function call without arguments
195 // can't pass a function call to defined
196 if (function_type == defined__)
197 throw Exceptions::FunctionCallError(Exceptions::FunctionCallError::unexpected_statement__);
198 else
199 { /* not a defined call */ }
201 std::string function_to_call(curr_arg->value.begin(), curr_arg->value.end());
202 // look up the function
203 typename StoredFunctions< Arguments >::const_iterator which(functions_.find(function_to_call));
204 if (which == functions_.end() && function_to_call != "shift")
205 throw Exceptions::FunctionCallError(Exceptions::FunctionCallError::unknown_function__);
206 else if (function_to_call == "shift")
208 if (stack_.top().second->empty())
209 throw Exceptions::FunctionCallError(Exceptions::FunctionCallError::no_more_arguments__);
210 else
212 args.push_back(stack_.top().second->at(0));
213 stack_.top().second->erase(stack_.top().second->begin());
216 else
218 Arguments no_args;
219 Arguments returned_args(which->second->call(*this, &no_args));
220 std::copy(returned_args.begin(), returned_args.end(), std::back_inserter(args));
223 break;
224 default :
225 throw std::logic_error("Unexpected node type");
228 ++curr_arg;
229 ++argument_index;
230 if (function_type == if__ && argument_index == 1)
232 // this is where we make the choice whether or not to evaluate the next, second argument
233 if (!args.back())
234 evaluate_second = false;
235 else
236 evaluate_third = false;
239 else
240 { /* at the end */ }
241 } while (curr_arg != end);
242 if (function_type != if__ && function_type != defined__)
243 value_ = which->second->call(*this, &args);
244 else
245 value_ = Arguments(1, args.back());
247 return *this;
250 const StoredFunctions< Arguments > & functions_;
251 Stack stack_;
252 Arguments value_;
257 #endif