Ejemplo n.º 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);
     }
 }
bool expression_parser::is_unary_expression(array_t& expression_stack) {
    if (is_postfix_expression(expression_stack))
        return true;

    name_t operator_l;

    if (is_unary_operator(operator_l)) {
        if (!is_unary_expression(expression_stack))
            throw_exception("Unary expression required.");

        if (operator_l != add_k)
            expression_stack.push_back(any_regular_t(operator_l));

        return true;
    }

    return false;
}
Ejemplo n.º 3
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{
  }
}
Ejemplo n.º 4
0
Archivo: parser.c Proyecto: fwum/fwum
static statement *parse_simple_expression(linked_list *tokens) {
	while(equals_string(((parse_token*)ll_get_first(tokens))->data, "(") && equals_string(((parse_token*)ll_get_last(tokens))->data, ")")) {
		ll_remove_first(tokens);
		ll_remove_last(tokens);
	}
	int size = ll_size(tokens);
	switch(size) {
	case 0:
		return NULL;
	case 1:
		return parse_single_token(tokens);
	default: {
		if(size == 2) {
			parse_token *token = ll_get_first(tokens);
			bool isStack;
	        if((isStack = equals_string(token->data, "new")) || equals_string(token->data, "newref")) {
	            statement *expression = new(expression);
	            expression->children = ll_new();
	            expression->type = isStack ? STACK_INIT : HEAP_INIT;
	            linked_list *name = ll_duplicate(tokens);
	            ll_remove_first(name);
	            ll_add_first(expression->children, parse_simple_expression(name));
	            return expression;
	        }
		}
		int paren_level = 1;
		linked_iter iterator = ll_iter_head(tokens);
		bool is_index = true, is_call = true;
		ll_iter_next(&iterator);
		parse_token *second = ll_iter_next(&iterator);
		if(equals_string(second->data, "(")) {
			is_index = false;
		} else if(equals_string(second->data, "[")) {
			is_call = false;
		} else {
			is_index = is_call = false;
		}
		while((is_index || is_call) && ll_iter_has_next(&iterator)) {
			parse_token *token = ll_iter_next(&iterator);
			if(equals_string(token->data, "(") || equals_string(token->data, "[")) {
				paren_level += 1;
			} else if(paren_level == 0) {
				is_index = false;
				is_call = false;
			} else if(equals_string(token->data, ")") || equals_string(token->data, "]")) {
				paren_level -= 1;
			}
		}
		if(is_index) {
			return parse_array_index(tokens);
		} else if(is_call) {
			return parse_func_call(tokens);
		}
		linked_list *operator = get_node();
		linked_iter level = ll_iter_head(operator);
		while(ll_iter_has_next(&level)) {
			int paren_level = 0;
			linked_list *currentLevel = ll_iter_next(&level);
			linked_iter iterator = ll_iter_head(tokens);
			for(parse_token *current = ll_iter_next(&iterator); ll_iter_has_next(&iterator); current = ll_iter_next(&iterator)) {
				char currentChar = current->data.data[0];
				if(currentChar == '(') {
					paren_level += 1;
				} else if(currentChar == ')') {
					paren_level -= 1;
				}
				if(paren_level != 0) continue;
				linked_iter innerMost = ll_iter_head(currentLevel);
				while(ll_iter_has_next(&innerMost)) {
					operator_node *currentOperator = ll_iter_next(&innerMost);
					if(equals_string(current->data, currentOperator->data)) {
						if(!is_unary_operator(new_slice(currentOperator->data))) {
							linked_list *op1 = ll_duplicate(tokens);
							while(ll_get_last(op1) != current)
								ll_remove_last(op1);
							ll_remove_last(op1);
							linked_list *op2 = tokens;
							while(ll_get_first(op2) != current)
								ll_remove_first(op2);
							ll_remove_first(op2);
							statement *expression = new(expression);
							expression->data = new_slice("");
							expression->children = ll_new();
							expression->type = currentOperator->operatorType;
							statement *op1_exp = parse_simple_expression(op1);
							statement *op2_exp = parse_simple_expression(op2);
							ll_add_last(expression->children, op1_exp);
							ll_add_last(expression->children, op2_exp);
							return expression;
						} else {
							statement *expression = new(expression);
							expression->data = new_slice(currentOperator->data);
							expression->type = currentOperator->operatorType;
							linked_list *rest = ll_duplicate(tokens);
							ll_remove_first(rest);
							expression->children = ll_new();
							ll_add_first(expression->children, parse_simple_expression(rest));
							return expression;
						}
					}
				}
			}
		}
		return NULL;
		}
	}
}