Changeset 3c70376 in mainline


Ignore:
Timestamp:
2012-08-09T04:57:22Z (12 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

Location:
uspace/app/bithenge
Files:
2 added
7 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bithenge/Makefile

    r0b60d2d r3c70376  
    3535        helenos/block.c \
    3636        blob.c \
     37        compound.c \
    3738        expression.c \
    3839        file.c \
  • uspace/app/bithenge/Makefile.linux

    r0b60d2d r3c70376  
    3636SOURCES = \
    3737        blob.c \
     38        compound.c \
    3839        expression.c \
    3940        file.c \
  • 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 */
  • uspace/app/bithenge/expression.h

    r0b60d2d r3c70376  
    120120int bithenge_inputless_transform(bithenge_transform_t **,
    121121    bithenge_expression_t *);
    122 int bithenge_if_transform(bithenge_transform_t **, bithenge_expression_t *,
    123     bithenge_transform_t *, bithenge_transform_t *);
    124122
    125123#endif
  • uspace/app/bithenge/script.c

    r0b60d2d r3c70376  
    3838#include <stdio.h>
    3939#include <stdlib.h>
     40#include "compound.h"
    4041#include "expression.h"
    4142#include "os.h"
     
    537538                        next_token(state);
    538539                        bithenge_expression_t *start = parse_expression(state);
    539                         bool absolute_limit;
     540                        bool absolute_limit = false;
    540541                        if (state->token == ',') {
    541542                                absolute_limit = false;
  • uspace/app/bithenge/transform.c

    r0b60d2d r3c70376  
    948948bithenge_named_transform_t *bithenge_primitive_transforms = primitive_transforms;
    949949
    950 typedef struct {
    951         bithenge_transform_t base;
    952         bithenge_transform_t **xforms;
    953         size_t num;
    954 } compose_transform_t;
    955 
    956 static bithenge_transform_t *compose_as_transform(compose_transform_t *xform)
    957 {
    958         return &xform->base;
    959 }
    960 
    961 static compose_transform_t *transform_as_compose(bithenge_transform_t *xform)
    962 {
    963         return (compose_transform_t *)xform;
    964 }
    965 
    966 static int compose_apply(bithenge_transform_t *base, bithenge_scope_t *scope,
    967     bithenge_node_t *in, bithenge_node_t **out)
    968 {
    969         int rc;
    970         compose_transform_t *self = transform_as_compose(base);
    971         bithenge_node_inc_ref(in);
    972 
    973         /* i ranges from (self->num - 1) to 0 inside the loop. */
    974         for (size_t i = self->num; i--; ) {
    975                 bithenge_node_t *tmp;
    976                 rc = bithenge_transform_apply(self->xforms[i], scope, in,
    977                     &tmp);
    978                 bithenge_node_dec_ref(in);
    979                 if (rc != EOK)
    980                         return rc;
    981                 in = tmp;
    982         }
    983 
    984         *out = in;
    985         return rc;
    986 }
    987 
    988 static int compose_prefix_length(bithenge_transform_t *base,
    989     bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out)
    990 {
    991         compose_transform_t *self = transform_as_compose(base);
    992         return bithenge_transform_prefix_length(self->xforms[self->num - 1],
    993             scope, blob, out);
    994 }
    995 
    996 static void compose_destroy(bithenge_transform_t *base)
    997 {
    998         compose_transform_t *self = transform_as_compose(base);
    999         for (size_t i = 0; i < self->num; i++)
    1000                 bithenge_transform_dec_ref(self->xforms[i]);
    1001         free(self->xforms);
    1002         free(self);
    1003 }
    1004 
    1005 static const bithenge_transform_ops_t compose_transform_ops = {
    1006         .apply = compose_apply,
    1007         .prefix_length = compose_prefix_length,
    1008         .destroy = compose_destroy,
    1009 };
    1010 
    1011 /** Create a composition of multiple transforms. When the result is applied to a
    1012  * node, each transform is applied in turn, with the last transform applied
    1013  * first. @a xforms may contain any number of transforms or no transforms at
    1014  * all. This function takes ownership of @a xforms and the references therein.
    1015  * @param[out] out Holds the result.
    1016  * @param[in] xforms The transforms to apply.
    1017  * @param num The number of transforms.
    1018  * @return EOK on success or an error code from errno.h. */
    1019 int bithenge_new_composed_transform(bithenge_transform_t **out,
    1020     bithenge_transform_t **xforms, size_t num)
    1021 {
    1022         if (num == 0) {
    1023                 /* TODO: optimize */
    1024         } else if (num == 1) {
    1025                 *out = xforms[0];
    1026                 free(xforms);
    1027                 return EOK;
    1028         }
    1029 
    1030         int rc;
    1031         compose_transform_t *self = malloc(sizeof(*self));
    1032         if (!self) {
    1033                 rc = ENOMEM;
    1034                 goto error;
    1035         }
    1036         rc = bithenge_init_transform(compose_as_transform(self),
    1037             &compose_transform_ops, 0);
    1038         if (rc != EOK)
    1039                 goto error;
    1040         self->xforms = xforms;
    1041         self->num = num;
    1042         *out = compose_as_transform(self);
    1043         return EOK;
    1044 error:
    1045         for (size_t i = 0; i < num; i++)
    1046                 bithenge_transform_dec_ref(xforms[i]);
    1047         free(xforms);
    1048         free(self);
    1049         return rc;
    1050 }
    1051 
    1052950/** @}
    1053951 */
  • uspace/app/bithenge/transform.h

    r0b60d2d r3c70376  
    148148int bithenge_new_barrier_transform(bithenge_transform_t **,
    149149    bithenge_transform_t *, int);
    150 int bithenge_new_composed_transform(bithenge_transform_t **,
    151     bithenge_transform_t **, size_t);
    152150
    153151int bithenge_scope_new(bithenge_scope_t **, bithenge_scope_t *);
Note: See TracChangeset for help on using the changeset viewer.