예제 #1
0
 void operator()(ast_expr *&ae)
 {
     if( is_unary_operator(ae->op_name))
     {
         ast_unary_expr_standardize()((ast_unary_expr *&)ae);
     }
     else if( is_binary_operator(ae->op_name))
     {
         ast_binary_expr_standardize()((ast_binary_expr *&)ae);
     }
     else if( is_event(ae->op_name))
     {
         ast_event_standardize()((ast_event *)ae);
     }
 }
예제 #2
0
/* Infers the type of operations with return types. For unary operators, such as
! and unary minus, the operator is assigned the return type of its child.
For binary operators, the operator is assigned the return type of its
children, only if both children have the same return type.
 */
void infer_type(ast_node root){

  /* Note: post-order traversal, unlike most other traversals */
  ast_node child;
  for (child = root->left_child; child != NULL; child = child->right_sibling){
    infer_type(child);
  }

  /* Only assign a return type to nodes withouth one already */
  if(root->return_type == 0){

    if(is_unary_operator(root)){
      root->return_type = root->left_child->return_type;

    } else if(is_binary_operator(root)){
      /* Check that the types of both children are equal */
      /* Assignment operator is also checked here, since it is a binary operator */
      if(root->left_child->return_type == root->left_child->right_sibling->return_type){
        root->return_type = root->left_child->return_type;
      }
    }
  } else{
  }
}
예제 #3
0
// assign token atrributes
Token Token::token_attributes(const string expresstion)
{
	Token item;

	string result = "";
	// if token is digit
	if (isdigit(expresstion[indx]))
	{
		int t = indx;
		while (isdigit(expresstion[t]))
		{
			result += expresstion[t];
			t++;
		}

		// convert strind int int
		int_val = atoi(result.c_str());
		item.int_val = atoi(result.c_str());
		item.get_str_val() = "";
		str_val = "";
		item.type = "operand";
		int step = result.length();
		indx = indx + step;
		//return type;
		return item;
	}
	//if token is operator
	while (is_operators(expresstion[indx]))
	{
	
		item.int_val = 0;
		int_val = 0;
		item.type = "operator";
		int tmpidx = indx;

		if (indx < expresstion.length() - 1)
		{
			if (expresstion[indx] == '+' && expresstion[tmpidx + 1] == '+' && isdigit(expresstion[tmpidx + 2]))
			{
				item.str_val = "++";
				str_val = "++";
				item.operator_type = "unary";
				item.operator_precedence = assign_precedece(item.str_val, item.operator_type);
				indx = indx + 2;
				return item;
			}
			if (expresstion[indx] == '-' && expresstion[tmpidx + 1] == '-' && isdigit(expresstion[tmpidx + 2]))
			{
				item.str_val = "--";
				str_val = "--";
				item.operator_type = "unary";
				item.operator_precedence = assign_precedece(item.str_val, item.operator_type);
				indx++;
				return item;
			}
		}
		if (indx < expresstion.length())
		{
			if (expresstion[indx] == '!' && isdigit(expresstion[tmpidx + 1])
				|| expresstion[indx] == '!' && expresstion[tmpidx + 1] == '('
				|| expresstion[indx] == '!' && expresstion[tmpidx + 1] == '-')
			{
				item.str_val = expresstion[indx];
				item.operator_type = "unary";
				item.operator_precedence = assign_precedece(item.str_val, item.operator_type);
				indx++;
				return item;
			}
			if (expresstion[indx] == '-' && isdigit(expresstion[tmpidx + 1]) && indx == 0)

			{

				item.str_val = "-";
				item.operator_type = "unary";
				item.operator_precedence = assign_precedece(item.str_val, item.operator_type);
				indx++;
				return item;
			}

			if (indx != 0)
			{
				string th(1, expresstion[tmpidx - 1]);

				if (expresstion[indx] == '-' && !isdigit(expresstion[tmpidx - 1]) && isdigit(expresstion[tmpidx + 1]) 
					&& !is_binary_operator(th) || expresstion[tmpidx + 1]=='(')
				{
					item.str_val = "-";
					item.operator_type = "unary";
					item.operator_precedence = assign_precedece(item.str_val, item.operator_type);
					indx++;
					return item;
				}
			}
		}
		if (indx < expresstion.length() - 1 && indx != 0)
		{
			if (isdigit(expresstion[tmpidx - 1]) && expresstion[indx] == '&' && expresstion[tmpidx + 1] == '&' && isdigit(expresstion[tmpidx + 2]))
			{
				item.str_val = "&&";
				str_val = "&&";
				item.operator_type = "binary";
				item.operator_precedence = assign_precedece(item.str_val, item.operator_type);
				indx = indx + 2;
				return item;
			}
			if (isdigit(expresstion[tmpidx - 1]) && expresstion[indx] == '|' && expresstion[tmpidx + 1] == '|' && isdigit(expresstion[tmpidx + 2]))
			{
				item.str_val = "||";
				str_val = "||";
				item.operator_type = "binary";
				item.operator_precedence = assign_precedece(item.str_val, item.operator_type);
				indx = indx + 2;
				return item;
			}
		}
		if (indx < expresstion.length())
		{
			if (expresstion[indx] == '>' && expresstion[tmpidx + 1] == '=')
			{
				item.str_val = ">=";
				str_val = ">=";
				item.operator_type = "binary";
				item.operator_precedence = assign_precedece(item.str_val, item.operator_type);
				indx = indx + 2;
				return item;
			}
			if (expresstion[indx] == '<' && expresstion[tmpidx + 1] == '=')
			{
				item.str_val = "<=";
				str_val = "<=";
				item.operator_type = "binary";
				item.operator_precedence = assign_precedece(item.str_val, item.operator_type);
				indx = indx + 2;
				return item;
			}
			if (expresstion[indx] == '=' && expresstion[tmpidx + 1] == '=')
			{
				item.str_val = "==";
				str_val = "==";
				item.operator_type = "binary";
				item.operator_precedence = assign_precedece(item.str_val, item.operator_type);
				indx = indx + 2;
				return item;
			}
			if (expresstion[indx] == '!' && expresstion[tmpidx + 1] == '=')
			{
				item.str_val = "!=";
				str_val = "!=";
				item.operator_type = "binary";
				item.operator_precedence = assign_precedece(item.str_val, item.operator_type);
				indx = indx + 2;
				return item;
			}
		}

		item.str_val = expresstion[indx];
		item.operator_type = "binary";
		item.operator_precedence = assign_precedece(item.str_val, item.operator_type);
		indx++;
		return item;
	}
}
void function::add_to_namespace(
    object const& name_space, char const* name_, object const& attribute, char const* doc)
{
    str const name(name_);
    PyObject* const ns = name_space.ptr();
    
    if (attribute.ptr()->ob_type == &function_type)
    {
        function* new_func = downcast<function>(attribute.ptr());
        PyObject* dict = 0;
        
#if PY_VERSION_HEX < 0x03000000
        // Old-style class gone in Python 3
        if (PyClass_Check(ns))
            dict = ((PyClassObject*)ns)->cl_dict;
        else
#endif        
        if (PyType_Check(ns))
            dict = ((PyTypeObject*)ns)->tp_dict;
        else    
            dict = PyObject_GetAttrString(ns, const_cast<char*>("__dict__"));

        if (dict == 0)
            throw_error_already_set();

        handle<> existing(allow_null(::PyObject_GetItem(dict, name.ptr())));
        
        if (existing)
        {
            if (existing->ob_type == &function_type)
            {
                new_func->add_overload(
                    handle<function>(
                        borrowed(
                            downcast<function>(existing.get())
                        )
                    )
                );
            }
            else if (existing->ob_type == &PyStaticMethod_Type)
            {
                char const* name_space_name = extract<char const*>(name_space.attr("__name__"));
                
                ::PyErr_Format(
                    PyExc_RuntimeError
                    , "Boost.Python - All overloads must be exported "
                      "before calling \'class_<...>(\"%s\").staticmethod(\"%s\")\'"
                    , name_space_name
                    , name_
                    );
                throw_error_already_set();
            }
        }
        else if (is_binary_operator(name_))
        {
            // Binary operators need an additional overload which
            // returns NotImplemented, so that Python will try the
            // __rxxx__ functions on the other operand. We add this
            // when no overloads for the operator already exist.
            new_func->add_overload(not_implemented_function());
        }

        // A function is named the first time it is added to a namespace.
        if (new_func->name().is_none())
            new_func->m_name = name;

        handle<> name_space_name(
            allow_null(::PyObject_GetAttrString(name_space.ptr(), const_cast<char*>("__name__"))));
        
        if (name_space_name)
            new_func->m_namespace = object(name_space_name);
    }

    // The PyObject_GetAttrString() or PyObject_GetItem calls above may
    // have left an active error
    PyErr_Clear();
    if (PyObject_SetAttr(ns, name.ptr(), attribute.ptr()) < 0)
        throw_error_already_set();

    object mutable_attribute(attribute);
/*
    if (doc != 0 && docstring_options::show_user_defined_)
    {
        // Accumulate documentation
        
        if (
            PyObject_HasAttrString(mutable_attribute.ptr(), "__doc__")
            && mutable_attribute.attr("__doc__"))
        {
            mutable_attribute.attr("__doc__") += "\n\n";
            mutable_attribute.attr("__doc__") += doc;
        }
        else {
            mutable_attribute.attr("__doc__") = doc;
        }
    }

    if (docstring_options::show_signatures_)
    {
        if (   PyObject_HasAttrString(mutable_attribute.ptr(), "__doc__")
            && mutable_attribute.attr("__doc__")) {
            mutable_attribute.attr("__doc__") += (
              mutable_attribute.attr("__doc__")[-1] != "\n" ? "\n\n" : "\n");
        }
        else {
            mutable_attribute.attr("__doc__") = "";
        }
        function* f = downcast<function>(attribute.ptr());
        mutable_attribute.attr("__doc__") += str("\n    ").join(make_tuple(
          "C++ signature:", f->signature(true)));
    }
    */
    str _doc;

    if (docstring_options::show_py_signatures_)
    {
        _doc += str(const_cast<const char*>(detail::py_signature_tag));
    }
    if (doc != 0 && docstring_options::show_user_defined_)
        _doc += doc;

    if (docstring_options::show_cpp_signatures_)
    {
        _doc += str(const_cast<const char*>(detail::cpp_signature_tag));
    }
    if(_doc)
    {    
        object mutable_attribute(attribute);
        mutable_attribute.attr("__doc__")= _doc;
    }
}