Exemple #1
0
void AST::build_for_loop(ASTNode *parent, Node *stmt_node)
{
    /* for loop node */
    ASTNode *for_node = new ASTNode;
    for_node->type = ASTNode::FOR_LOOP;
    parent->children.push_back(for_node);

    /* for in node */
    ASTNode *in_node = new ASTNode;
    in_node->type = ASTNode::FOR_IN;
    for_node->children.push_back(in_node);
    
    /* in : identifier node */
    ASTNode *id_node = new ASTNode;
    id_node->type = ASTNode::VAR_ID;
    id_node->value = stmt_node->children[1]->token.str;
	/* check symbol table */
	std::string e_str;
	switch (_symbol_table.find(id_node->value).type) {
		case Symbol::VARIABLE_INT:
			id_node->variable_type = ASTVariable::INTEGER;
			break;
		case Symbol::VARIABLE_STRING:
			id_node->variable_type = ASTVariable::STRING;
			break;
		case Symbol::VARIABLE_BOOL:
			id_node->variable_type = ASTVariable::BOOLEAN;
			break;
		case Symbol::UNDEFINED:
			id_node->variable_type = ASTVariable::UNKNOWN;
			e_str = "AST::build_for_loop - Identifier ";
			e_str.append(id_node->value);
			report_error(e_str.append(" is not initialized."));
			break;
		default:
			id_node->variable_type = ASTVariable::UNKNOWN;
			e_str = "AST::build_for_loop - Identifier ";
			e_str.append(id_node->value);
			report_error(e_str.append(" has wrong type information."));
	}
    in_node->children.push_back(id_node);

    /* in : left side expr */
    build_expr(in_node, stmt_node->children[3]);
    /* in : right side expr */
    build_expr(in_node, stmt_node->children[5]);

    /* for do node */
    ASTNode *do_node = new ASTNode;
    do_node->type = ASTNode::FOR_DO;
    for_node->children.push_back(do_node);

    /* do : stmts */
    build(do_node, stmt_node->children[7]);
}
Exemple #2
0
int fit_logic(void)
{
      unsigned idx;

      for (idx = 0 ;  idx < pins ;  idx += 1) {
	    ivl_nexus_t cell;

	    struct pal_bind_s*pin = bind_pin + idx;
	    if (pin->sop == 0)
		  continue;

	    cell = pin->nexus;
	    if (cell == 0)
		  continue;


	      /* If there is an enable, then this is a bufifX or a
		 notifX. Build the expression for the enable, and
		 guess that the input to the cell is actually the
		 input to the enable. */
	    if (pin->enable) {
		  ivl_nexus_t en_nex = ivl_logic_pin(pin->enable, 2);
		  assert(cell == ivl_logic_pin(pin->enable, 0));

		  cell = ivl_logic_pin(pin->enable, 1);
		  assert(cell);

		  pin->enable_ex = build_expr(en_nex);
		  dump_expr(pin->enable_ex, ivl_nexus_name(en_nex));
	    }

	      /* If there is a reg, then the input to the cell is
		 really the D input to the ff. */
	    if (pin->reg) {
		  assert(cell == ivl_lpm_q(pin->reg, pin->reg_q));
		  cell = ivl_lpm_data(pin->reg, pin->reg_q);
	    }

	    assert(cell);

	      /* Here we are. Generate the sum-of-products for the
		 input. */
	    pin->sop_ex = build_expr(cell);
	    dump_expr(pin->sop_ex, ivl_nexus_name(cell));
      }

      return 0;
}
Exemple #3
0
void AST::build_opnd(ASTNode *parent, Node *opnd_node)
{
    ASTNode *wat_node = NULL;
	std::string e_str;
    switch (opnd_node->children[0]->token.type) {
        case Token::INTEGER:
            wat_node = new ASTNode;
            wat_node->type = ASTNode::CONSTANT;
            wat_node->value = opnd_node->children[0]->token.str;
            wat_node->variable_type = ASTVariable::INTEGER;
            parent->children.push_back(wat_node);
            break;
        case Token::STRING:
            wat_node = new ASTNode;
            wat_node->type = ASTNode::CONSTANT;
            wat_node->value = opnd_node->children[0]->token.str;
            wat_node->variable_type = ASTVariable::STRING;
            parent->children.push_back(wat_node);
            break;
        case Token::IDENTIFIER:
            wat_node = new ASTNode;
            wat_node->type = ASTNode::VAR_ID;
            wat_node->value = opnd_node->children[0]->token.str;
            /* check symbol table */
			switch (_symbol_table.find(wat_node->value).type) {
				case Symbol::VARIABLE_INT:
					wat_node->variable_type = ASTVariable::INTEGER;
					break;
				case Symbol::VARIABLE_STRING:
					wat_node->variable_type = ASTVariable::STRING;
					break;
				case Symbol::VARIABLE_BOOL:
					wat_node->variable_type = ASTVariable::BOOLEAN;
					break;
				case Symbol::UNDEFINED:
					wat_node->variable_type = ASTVariable::UNKNOWN;
					e_str = "AST::build_opnd - Identifier ";
					e_str.append(wat_node->value);
					report_error(e_str.append(" is not initialized."));
					break;
				default:
					wat_node->variable_type = ASTVariable::UNKNOWN;
					e_str = "AST::build_opnd - Identifier ";
					e_str.append(wat_node->value);
					report_error(e_str.append(" has wrong type information."));
			}
			parent->children.push_back(wat_node);
			break;
        case Token::BRACKET_LEFT:
            build_expr(parent, opnd_node->children[1]);
            break;
        default:
			e_str = "AST::build_opnd - Token type ";
			e_str.append(opnd_node->children[0]->token.type_str());
			report_error(e_str.append(" is not allowed at this location."));
    }
}
Exemple #4
0
void AST::build_assert(ASTNode *parent, Node *stmt_node)
{
    /* assert node */
    ASTNode *assert_node = new ASTNode;
    assert_node->type = ASTNode::ASSERT;

    /* set assert node to be children of parent */
    parent->children.push_back(assert_node);

    /* add expr node tree for assert node */
    build_expr(assert_node, stmt_node->children[2]);
}
Exemple #5
0
void AST::build_print(ASTNode *parent, Node *stmt_node)
{
    /* print node */
    ASTNode *print_node = new ASTNode;
    print_node->type = ASTNode::PRINT;

    /* set print node to be children of parent */
    parent->children.push_back(print_node);

    /* add expr node tree for print node */
    build_expr(print_node, stmt_node->children[1]);
}
Exemple #6
0
void AST::build_insert(ASTNode *parent, Node *stmt_node)
{
    /* insert node */
    ASTNode *insert_node = new ASTNode;
    insert_node->type = ASTNode::INSERT;

    /* identifier node */
    ASTNode *id_node = new ASTNode;
    id_node->type = ASTNode::VAR_ID;
    id_node->value = stmt_node->children[0]->token.str;
    /* check symbol table */
	std::string e_str;
	switch (_symbol_table.find(id_node->value).type) {
		case Symbol::VARIABLE_INT:
			id_node->variable_type = ASTVariable::INTEGER;
			break;
		case Symbol::VARIABLE_STRING:
			id_node->variable_type = ASTVariable::STRING;
			break;
		case Symbol::VARIABLE_BOOL:
			id_node->variable_type = ASTVariable::BOOLEAN;
			break;
		case Symbol::UNDEFINED:
			id_node->variable_type = ASTVariable::UNKNOWN;
			e_str = "AST::build_insert - Identifier ";
			e_str.append(id_node->value);
			report_error(e_str.append(" is not initialized."));
			break;
		default:
			id_node->variable_type = ASTVariable::UNKNOWN;
			e_str = "AST::build_insert - Identifier ";
			e_str.append(id_node->value);
			report_error(e_str.append(" has wrong type information."));
	}
	insert_node->children.push_back(id_node);

    /* set insert node to be children of parent */ 
    parent->children.push_back(insert_node);

    /* add expr node tree for insert node */
    build_expr(insert_node, stmt_node->children[2]);
}
Exemple #7
0
void AST::build_var_init(ASTNode *parent, Node *stmt_node)
{
    /* initialization node */
    ASTNode *var_init_node = new ASTNode;
    var_init_node->type = ASTNode::VAR_INIT;
    
    ASTVariable::TYPE var_type;
	Symbol::TYPE s_type;
    switch (stmt_node->children[3]->token.type) {
        case Token::KW_INT:
            var_type = ASTVariable::INTEGER;
			s_type = Symbol::VARIABLE_INT;
			break;
        case Token::KW_STRING:
            var_type = ASTVariable::STRING;
			s_type = Symbol::VARIABLE_STRING;
        	break;
		case Token::KW_BOOL:
            var_type = ASTVariable::BOOLEAN;
			s_type = Symbol::VARIABLE_BOOL;
        	break;
		default:
			std::string e_str = "AST::build_var_init - Cannot resolve variable type for token type ";
            report_error(e_str.append(stmt_node->children[3]->token.type_str()));
            var_type = ASTVariable::UNKNOWN;
    }
    var_init_node->variable_type = var_type;

    /* identifier node */
    ASTNode *id_node = new ASTNode;
    id_node->type = ASTNode::VAR_ID;
    id_node->value = stmt_node->children[1]->token.str;
    id_node->variable_type = var_type;
    var_init_node->children.push_back(id_node);
	
	/* add id to symbol table */
	Symbol s;
	s.type = s_type;
	_symbol_table.push(id_node->value, s);

    /* set initialization node to be children of parent */
    parent->children.push_back(var_init_node);

    if (stmt_node->children.size() == 6) {
        /* insert node */
        ASTNode *insert_node = new ASTNode;
        insert_node->type = ASTNode::INSERT;

        /* identifier node 2 */
        ASTNode *id_node2 = new ASTNode;
        id_node2->type = id_node->type;
        id_node2->value = id_node->value;
        id_node2->variable_type = id_node->variable_type;
        insert_node->children.push_back(id_node2);
		
		parent->children.push_back(insert_node);

        /* add expr node tree for insert node */
        build_expr(insert_node, stmt_node->children[5]);
    }

}
void uri_template_parse(char *tpl, zval *return_value, zval *vars, zval *capture)
{
	smart_str result = {0};
	zval *expressions = NULL;
	zval  vars_ptr;
	unsigned char c;
	char *start;
	int state = URI_TEMPLATE_ERROR_NONE;

	if (capture != NULL) {
		MAKE_STD_ZVAL(expressions);
		array_init(expressions);
	}

	vars_ptr = *vars;
	zval_copy_ctor(&vars_ptr);

	while (*tpl) {
		if (*tpl == '{') {
			start = tpl + 1;

			while (*(tpl++) && *tpl != '}' && *tpl != '{');

			if (*tpl == '}') {
				if (tpl - start > 0) {
					uri_template_expr *expr = build_expr(start, tpl - start);

					if (expr->error) {
						append_malformed_expr(&result, start, tpl - start);

						if (state == URI_TEMPLATE_ERROR_NONE) {
							state = URI_TEMPLATE_ERROR_EXPRESSION;
						}
					} else {
						uri_template_process(expr, &vars_ptr, &result);
					}

					if (expressions != NULL) {
						add_expr_to_array(expressions, expr);
					}

					uri_template_expr_free(expr);
				} else {
					smart_str_appends(&result, "{}");
				}
			} else if (*tpl == '{') {
				smart_str_appendl(&result, start - 1, tpl - start + 1);
				state = URI_TEMPLATE_ERROR_SYNTAX;
				tpl--;
			} else {
				smart_str_appendc(&result, '{');
				smart_str_appendl(&result, start, tpl - start);
				state = URI_TEMPLATE_ERROR_SYNTAX;
			}
		} else {
			c = *tpl;

			if (c == '}') {
				smart_str_appendc(&result, '}');
				state = URI_TEMPLATE_ERROR_SYNTAX;
			} else if (c == '%' && isxdigit(*(tpl + 1)) && isxdigit(*(tpl + 2))) {
				smart_str_appendc(&result, '%');
				smart_str_appendc(&result, *(++tpl));
				smart_str_appendc(&result, *(++tpl));
			} else {
				int result_len = result.len;
				int distance = 0;

				uri_template_substr_copy(&result, tpl, 1, URI_TEMPLATE_ALLOW_RESERVED);
        
				distance = result.len - result_len;
				tpl += (distance % 3 ? 1 : distance / 3) - 1;
			}
		}

		tpl++;
	}

	smart_str_0(&result);
	ZVAL_STRING(return_value, result.c ? result.c : "", 1);

	if (capture != NULL) {
		add_assoc_string(capture, "result", result.c ? result.c : "", 1);
		add_assoc_long(capture, "state", state);
		add_assoc_zval(capture, "expressions", expressions);
	}
	
	zval_dtor(&vars_ptr);
	smart_str_free(&result);
}