Changeset 78d3a00 in mainline for uspace/app/bithenge/expression.c


Ignore:
Timestamp:
2012-07-31T21:07:26Z (13 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
3f2ea63
Parents:
20ac1a4
Message:

Bithenge: add switch transforms and sugar

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bithenge/expression.c

    r20ac1a4 r78d3a00  
    6363}
    6464
     65typedef struct {
     66        bithenge_expression_t base;
     67        bithenge_binary_op_t op;
     68        bithenge_expression_t *a, *b;
     69} binary_expression_t;
     70
     71static inline binary_expression_t *expression_as_binary(
     72    bithenge_expression_t *base)
     73{
     74        return (binary_expression_t *)base;
     75}
     76
     77static inline bithenge_expression_t *binary_as_expression(
     78    binary_expression_t *self)
     79{
     80        return &self->base;
     81}
     82
     83static int binary_expression_evaluate(bithenge_expression_t *base,
     84    bithenge_scope_t *scope, bithenge_node_t **out)
     85{
     86        binary_expression_t *self = expression_as_binary(base);
     87        bithenge_node_t *a, *b;
     88        int rc = bithenge_expression_evaluate(self->a, scope, &a);
     89        if (rc != EOK)
     90                return rc;
     91        rc = bithenge_expression_evaluate(self->b, scope, &b);
     92        if (rc != EOK) {
     93                bithenge_node_dec_ref(a);
     94                return rc;
     95        }
     96        switch (self->op) {
     97        case BITHENGE_EXPRESSION_EQUALS:
     98                rc = bithenge_new_boolean_node(out, bithenge_node_equal(a, b));
     99                break;
     100        }
     101        bithenge_node_dec_ref(a);
     102        bithenge_node_dec_ref(b);
     103        return rc;
     104}
     105
     106static void binary_expression_destroy(bithenge_expression_t *base)
     107{
     108        binary_expression_t *self = expression_as_binary(base);
     109        bithenge_expression_dec_ref(self->a);
     110        bithenge_expression_dec_ref(self->b);
     111        free(self);
     112}
     113
     114static const bithenge_expression_ops_t binary_expression_ops = {
     115        .evaluate = binary_expression_evaluate,
     116        .destroy = binary_expression_destroy,
     117};
     118
     119/** Create a binary expression. Takes ownership of @a a and @a b.
     120 * @param[out] out Holds the new expression.
     121 * @param op The operator to apply.
     122 * @param a The first operand.
     123 * @param b The second operand.
     124 * @return EOK on success or an error code from errno.h. */
     125int bithenge_binary_expression(bithenge_expression_t **out,
     126    bithenge_binary_op_t op, bithenge_expression_t *a,
     127    bithenge_expression_t *b)
     128{
     129        int rc;
     130        binary_expression_t *self = malloc(sizeof(*self));
     131        if (!self) {
     132                rc = ENOMEM;
     133                goto error;
     134        }
     135
     136        rc = bithenge_init_expression(binary_as_expression(self),
     137            &binary_expression_ops);
     138        if (rc != EOK)
     139                goto error;
     140
     141        self->op = op;
     142        self->a = a;
     143        self->b = b;
     144        *out = binary_as_expression(self);
     145        return EOK;
     146
     147error:
     148        bithenge_expression_dec_ref(a);
     149        bithenge_expression_dec_ref(b);
     150        free(self);
     151        return rc;
     152}
     153
    65154static int current_node_evaluate(bithenge_expression_t *self,
    66155    bithenge_scope_t *scope, bithenge_node_t **out)
Note: See TracChangeset for help on using the changeset viewer.