utree expression::eval_function(utree const& node) { typedef utree::const_iterator iter; iter it = node.begin(), end = node.end(); std::string func_name = as<std::string>(*it); ++it; if (func_name == "test") { utree color = eval_node(*it); ++it; return test(color); } else if (func_name == "hue") { utree color = eval_node(*it); ++it; return hue(color); } else if (func_name == "saturation") { utree color = eval_node(*it); ++it; return saturation(color); } else if (func_name == "lightness") { utree color = eval_node(*it); ++it; return lightness(color); } else if (func_name == "alpha") { utree color = eval_node(*it); ++it; return alpha(color); } else if (func_name == "saturate") { utree color = eval_node(*it); ++it; utree value = eval_node(*it); ++it; return saturate(color,value); } else if (func_name == "desaturate") { utree color = eval_node(*it); ++it; utree value = eval_node(*it); ++it; return desaturate(color,value); } else if (func_name == "lighten") { utree color = eval_node(*it); ++it; utree value = eval_node(*it); ++it; return lighten(color,value); } else if (func_name == "darken") { utree color = eval_node(*it); ++it; utree value = eval_node(*it); ++it; return darken(color,value); } else if (func_name == "fadein") { utree color = eval_node(*it); ++it; utree value = eval_node(*it); ++it; return fadein(color,value); } else if (func_name == "fadeout") { utree color = eval_node(*it); ++it; utree value = eval_node(*it); ++it; return fadeout(color,value); } else if (func_name == "spin") { utree color = eval_node(*it); ++it; utree value = eval_node(*it); ++it; return spin(color,value); } else if (func_name == "mix") { utree color1 = eval_node(*it); ++it; utree color2 = eval_node(*it); ++it; utree weight = eval_node(*it); ++it; return mix(color1, color2, weight); } else if (func_name == "greyscale") { utree color = eval_node(*it); ++it; return greyscale(color); } else if (func_name == "url") { return eval_node(*it); } else { throw carto_error("Unknown function: " + func_name, get_location(node)); } }
void operator() (utree const& ut){ using spirit::utree_type; typedef utree::const_iterator iterator; int id = cur_id; n_id++; switch (ut.which()) { case utree_type::reference_type: out << prefix << id << " [label=\"[reference]\"];\n"; out << prefix << id << " -> " << prefix << cur_id << ";\n"; (*this)(ut.deref()); return; case utree_type::range_type: case utree_type::list_type: { iterator it, end; int start_id = n_id; if (annotations[ut.tag()].second == json_object) { out << prefix << id << " [label=\"[object]\"];\n"; it = ut.front().begin(); end = ut.front().end(); n_id += ut.front().size(); } else { if (annotations[ut.tag()].second == json_array) { out << prefix << id << " [label=\"[array]\"];\n"; } else if (annotations[ut.tag()].second == json_pair) { out << prefix << id << " [label=\"[pair]\"];\n"; } else { BOOST_ASSERT(false); return; } it = ut.begin(); end = ut.end(); n_id += ut.size(); } for (int i=0; it != end; ++it, ++i) { cur_id = start_id+i; out << prefix << id << " -> " << prefix << cur_id << ";\n"; (*this)(*it); } return; } default: break; } utree::visit(ut, *this); }
utree expression::eval_node(utree const& node) { //std::cout << node << " " << node.which() << " " << get_node_type(node) << "\n"; if (node.which() == spirit::utree_type::list_type) { switch(get_node_type(node)) { case CARTO_EXP_PLUS: BOOST_ASSERT(node.size()==2); return eval_add( eval_node(node.front()), eval_node(node.back()) ); case CARTO_EXP_MINUS: BOOST_ASSERT(node.size()==2); return eval_sub( eval_node(node.front()), eval_node(node.back()) ); case CARTO_EXP_TIMES: BOOST_ASSERT(node.size()==2); return eval_mult( eval_node(node.front()), eval_node(node.back()) ); case CARTO_EXP_DIVIDE: BOOST_ASSERT(node.size()==2); return eval_div( eval_node(node.front()), eval_node(node.back()) ); case CARTO_EXP_NEG: BOOST_ASSERT(node.size()==1); return eval_mult( utree(-1.0), eval_node(node.back()) ); case CARTO_EXP_FUNCTION: return eval_function(node); case CARTO_EXP_COLOR: BOOST_ASSERT(node.size()==4); return node; //case exp_var: // return eval_var(node); default: { std::stringstream out; out << "Invalid expression node type: " << get_node_type(node); throw carto_error(out.str(), get_location(node)); } } } else if (get_node_type(node) == CARTO_EXP_VAR) { return eval_var(node); } //} else { // std::cout << "Shouldn't be here!\n"; // std::cout << "Node: " << node << "\n"; // std::cout << "Node which: " << node.which() << "\n"; // std::cout << "Node type: " << get_node_type(node) << "\n"; // if (node.which() == spirit::utree_type::list_type) // std::cout << "Node size: " << node.size() << "\n"; //} return node; }
utree eval(scope const& env) const { return f.eval(env); }
bool empty() const { return f.which() != utree_type::function_type; }
void evaluateInstance(utree const &ast) { for ( utree::const_iterator it = ast.begin(), ie = ast.end(); it != ie; ++it ) { evaluateCommand(*it); } }
void print_member_pair (utree const& ut) const { utree::const_iterator it = ut.begin(); print_key(*it); ++it; out << ':'; (*this)(*it); }