Ejemplo n.º 1
0
ExprNode Parser::parse_binary_rhs_(int expr_prec, ExprNode lhs, std::string const* fn_name, Function* fn)
{
	while (1)
	{
		auto cur_prec = operator_precedence(cur_tok_);
		if (cur_prec < expr_prec)
			return lhs;
		auto bin_op = cur_tok_;
		cur_tok_ = lex_->next();

		auto rhs = parse_unary_(fn_name, fn);
		auto next_op = cur_tok_;
		auto next_prec = operator_precedence(next_op);
		while (cur_prec < next_prec ||
			   (operator_associativity(next_op) == Associativity::right && cur_prec == next_prec))
		{
			rhs = parse_binary_rhs_(next_prec, std::move(rhs), fn_name, fn);
			next_op = cur_tok_;
			next_prec = operator_precedence(next_op);
		}

		if (bin_op == '=')
			lhs = std::make_unique<AssignmentTree>(std::move(lhs), std::move(rhs));
		else
			lhs = std::make_unique<BinaryExprTree>(bin_op, std::move(lhs), std::move(rhs));
	}
}
Ejemplo n.º 2
0
// Parses an expression, stopping when we reach a binary
// operator of lower precedence than the given precedence.
Operand expr_precedence(Parser *parser, Precedence precedence) {
    Lexer *lexer = lexer->next;

    // Expect a left hand side operand
    Operand left = expr_left(parser);

    // Parse a binary operator
    Token operator = lexer->token;
    while (operator_precedence(operator) > precedence) {

    }
}
Ejemplo n.º 3
0
static bool execute(char *buffer, bool *data)
{
    char *token, **ops, *o1, *o2;
    bool *output, res;
    int n_o, n_q, sz_o, sz_q;;

    sz_o = 8192;
    sz_q = 8192;
    ops = malloc(sz_q * sizeof(char *));
    output = malloc(sz_o * sizeof(bool));
    n_o = n_q = 0;


    for (token = strtok(buffer, " "); token; token = strtok(NULL, " ")) {
        if (strncmp(token, "b", 1) == 0) {
            push_bit(data[atoi(token + 1)], &output, &n_o, &sz_o);
        } else if (is_function(token)) {
            push_op(token, &ops, &n_q, &sz_q);
        } else if (strncmp(token, ",", 1) == 0) {
            while (strncmp(peek_op(ops, n_q), "(", 1) != 0) {
                token = pop_op(ops, &n_q);
                if (is_operator(token)) {
                    push_bit(operator(token, pop_bit(output, &n_o), pop_bit(output, &n_o)), &output, &n_o, &sz_o);
                } else {
                    push_bit(function(token, output, &n_o), &output, &n_o, &sz_o);
                }
            }
        } else if (is_operator(token)) {
            o1 = token;
            while (peek_op(ops, n_q) && is_operator(peek_op(ops, n_q))) {
                o2 = peek_op(ops, n_q);
                if (operator_precedence(o1) <= operator_precedence(o2)) {
                    push_bit(operator(pop_op(ops, &n_q), pop_bit(output, &n_o), pop_bit(output, &n_o)), &output, &n_o, &sz_o);
                } else {
                    break;
                }
            }
            push_op(o1, &ops, &n_q, &sz_q);
        } else if (strncmp(token, "(", 1) == 0) {
            push_op(token, &ops, &n_q, &sz_q);
        } else if (strncmp(token, ")", 1) == 0) {
            while (strncmp(peek_op(ops, n_q), "(", 1) != 0) {
                token = pop_op(ops, &n_q);
                if (is_operator(token)) {
                    push_bit(operator(token, pop_bit(output, &n_o), pop_bit(output, &n_o)), &output, &n_o, &sz_o);
                } else {
                    push_bit(function(token, output, &n_o), &output, &n_o, &sz_o);
                }
            }

            token = pop_op(ops, &n_q); /* pop the left bracket */

            if (peek_op(ops, n_q) && is_function(peek_op(ops, n_q))) {
                push_bit(function(pop_op(ops, &n_q), output, &n_o), &output, &n_o, &sz_o);
            }
        } else {
            fprintf(stderr, "ERROR: unknown symbol: %s\n", token);
            exit(EXIT_FAILURE);
        }
    }

    while ((token = pop_op(ops, &n_q))) {
        if (is_operator(token)) {
            push_bit(operator(token, pop_bit(output, &n_o), pop_bit(output, &n_o)), &output, &n_o, &sz_o);
        } else {
            push_bit(function(token, output, &n_o), &output, &n_o, &sz_o);
        }
    }

    res = output[0];

    free(output);
    free(ops);

    return res;
}