Changeset 3c70376 in mainline for uspace/app/bithenge/expression.c


Ignore:
Timestamp:
2012-08-09T04:57:22Z (13 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c54f5d0
Parents:
0b60d2d
Message:

Bithenge: move compound transforms to separate file; fix warnings

File:
1 edited

Legend:

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

    r0b60d2d r3c70376  
    100100
    101101        /* Check types and get values. */
    102         bithenge_int_t a_int, b_int;
     102        bithenge_int_t a_int = 0, b_int = 0;
    103103        switch (self->op) {
    104104        case BITHENGE_EXPRESSION_ADD: /* fallthrough */
     
    961961}
    962962
    963 
    964 
    965 /***************** if_transform                              *****************/
    966 
    967 typedef struct {
    968         bithenge_transform_t base;
    969         bithenge_expression_t *expr;
    970         bithenge_transform_t *true_xform, *false_xform;
    971 } if_transform_t;
    972 
    973 static inline bithenge_transform_t *if_as_transform(if_transform_t *self)
    974 {
    975         return &self->base;
    976 }
    977 
    978 static inline if_transform_t *transform_as_if(bithenge_transform_t *base)
    979 {
    980         return (if_transform_t *)base;
    981 }
    982 
    983 static int if_transform_choose(if_transform_t *self, bithenge_scope_t *scope,
    984     bool *out)
    985 {
    986         bithenge_node_t *cond_node;
    987         int rc = bithenge_expression_evaluate(self->expr, scope, &cond_node);
    988         if (rc != EOK)
    989                 return rc;
    990         if (bithenge_node_type(cond_node) != BITHENGE_NODE_BOOLEAN) {
    991                 bithenge_node_dec_ref(cond_node);
    992                 return EINVAL;
    993         }
    994         *out = bithenge_boolean_node_value(cond_node);
    995         bithenge_node_dec_ref(cond_node);
    996         return EOK;
    997 }
    998 
    999 static int if_transform_apply(bithenge_transform_t *base,
    1000     bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out)
    1001 {
    1002         if_transform_t *self = transform_as_if(base);
    1003         bool cond;
    1004         int rc = if_transform_choose(self, scope, &cond);
    1005         if (rc != EOK)
    1006                 return rc;
    1007         return bithenge_transform_apply(
    1008             cond ? self->true_xform : self->false_xform, scope, in, out);
    1009 }
    1010 
    1011 static int if_transform_prefix_length(bithenge_transform_t *base,
    1012     bithenge_scope_t *scope, bithenge_blob_t *in, aoff64_t *out)
    1013 {
    1014         if_transform_t *self = transform_as_if(base);
    1015         bool cond;
    1016         int rc = if_transform_choose(self, scope, &cond);
    1017         if (rc != EOK)
    1018                 return rc;
    1019         return bithenge_transform_prefix_length(
    1020             cond ? self->true_xform : self->false_xform, scope, in, out);
    1021 }
    1022 
    1023 static void if_transform_destroy(bithenge_transform_t *base)
    1024 {
    1025         if_transform_t *self = transform_as_if(base);
    1026         bithenge_expression_dec_ref(self->expr);
    1027         bithenge_transform_dec_ref(self->true_xform);
    1028         bithenge_transform_dec_ref(self->false_xform);
    1029         free(self);
    1030 }
    1031 
    1032 static const bithenge_transform_ops_t if_transform_ops = {
    1033         .apply = if_transform_apply,
    1034         .prefix_length = if_transform_prefix_length,
    1035         .destroy = if_transform_destroy,
    1036 };
    1037 
    1038 /** Create a transform that applies either of two transforms depending on a
    1039  * boolean expression. Takes references to @a expr, @a true_xform, and
    1040  * @a false_xform.
    1041  * @param[out] out Holds the new transform.
    1042  * @param expr The boolean expression to evaluate.
    1043  * @param true_xform The transform to apply if the expression is true.
    1044  * @param false_xform The transform to apply if the expression is false.
    1045  * @return EOK on success or an error code from errno.h. */
    1046 int bithenge_if_transform(bithenge_transform_t **out,
    1047     bithenge_expression_t *expr, bithenge_transform_t *true_xform,
    1048     bithenge_transform_t *false_xform)
    1049 {
    1050         int rc;
    1051         if_transform_t *self = malloc(sizeof(*self));
    1052         if (!self) {
    1053                 rc = ENOMEM;
    1054                 goto error;
    1055         }
    1056 
    1057         rc = bithenge_init_transform(if_as_transform(self), &if_transform_ops,
    1058             0);
    1059         if (rc != EOK)
    1060                 goto error;
    1061 
    1062         self->expr = expr;
    1063         self->true_xform = true_xform;
    1064         self->false_xform = false_xform;
    1065         *out = if_as_transform(self);
    1066         return EOK;
    1067 
    1068 error:
    1069         free(self);
    1070         bithenge_expression_dec_ref(expr);
    1071         bithenge_transform_dec_ref(true_xform);
    1072         bithenge_transform_dec_ref(false_xform);
    1073         return rc;
    1074 }
    1075 
    1076963/** @}
    1077964 */
Note: See TracChangeset for help on using the changeset viewer.