Changeset a66ea217 in mainline for uspace/app/bithenge/expression.c


Ignore:
Timestamp:
2012-08-08T00:52:22Z (12 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d8bd2ec
Parents:
ad5c8a48
Message:

Bithenge: use expressions to make transforms

File:
1 edited

Legend:

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

    rad5c8a48 ra66ea217  
    6363}
    6464
     65
     66
     67/***************** binary_expression                         *****************/
     68
    6569typedef struct {
    6670        bithenge_expression_t base;
     
    185189}
    186190
     191
     192
     193/***************** in_node_expression                        *****************/
     194
     195static int in_node_evaluate(bithenge_expression_t *self,
     196    bithenge_scope_t *scope, bithenge_node_t **out)
     197{
     198        for (; scope && !bithenge_scope_is_barrier(scope);
     199            scope = bithenge_scope_outer(scope)) {
     200                *out = bithenge_scope_in_node(scope);
     201                if (*out)
     202                        return EOK;
     203        }
     204        return EINVAL;
     205}
     206
     207static const bithenge_expression_ops_t in_node_ops = {
     208        .evaluate = in_node_evaluate,
     209        .destroy = expression_indestructible,
     210};
     211
     212static bithenge_expression_t in_node_expression = {
     213        &in_node_ops, 1
     214};
     215
     216/** Create an expression that gets the current input node.
     217 * @param[out] out Holds the new expression.
     218 * @return EOK on success or an error code from errno.h. */
     219int bithenge_in_node_expression(bithenge_expression_t **out)
     220{
     221        bithenge_expression_inc_ref(&in_node_expression);
     222        *out = &in_node_expression;
     223        return EOK;
     224}
     225
     226
     227
     228/***************** current_node_expression                   *****************/
     229
    187230static int current_node_evaluate(bithenge_expression_t *self,
    188231    bithenge_scope_t *scope, bithenge_node_t **out)
     
    212255        return EOK;
    213256}
     257
     258
     259
     260/***************** param_expression                          *****************/
    214261
    215262typedef struct {
     
    270317        return EOK;
    271318}
     319
     320
     321
     322/***************** const_expression                          *****************/
    272323
    273324typedef struct {
     
    651702}
    652703
     704
     705
     706/***************** expression_transform           *****************/
     707
     708/* Also used by inputless_transform. */
    653709typedef struct {
    654710        bithenge_transform_t base;
     
    672728{
    673729        expression_transform_t *self = transform_as_expression(base);
    674         if (bithenge_node_type(in) != BITHENGE_NODE_BLOB)
    675                 return EINVAL;
    676         aoff64_t len;
    677         int rc = bithenge_blob_size(bithenge_node_as_blob(in), &len);
    678         if (rc != EOK)
    679                 return rc;
    680         if (len != 0)
    681                 return EINVAL;
    682         return bithenge_expression_evaluate(self->expr, scope, out);
    683 }
    684 
    685 static int expression_transform_prefix_length(bithenge_transform_t *base,
    686     bithenge_scope_t *scope, bithenge_blob_t *in, aoff64_t *out)
    687 {
    688         *out = 0;
    689         return EOK;
    690 }
    691 
     730        bithenge_scope_t *inner;
     731        int rc = bithenge_scope_new(&inner, scope);
     732        if (rc != EOK)
     733                return rc;
     734        bithenge_scope_set_in_node(inner, in);
     735        rc = bithenge_expression_evaluate(self->expr, inner, out);
     736        bithenge_scope_dec_ref(inner);
     737        return rc;
     738}
     739
     740/* Also used by inputless_transform. */
    692741static void expression_transform_destroy(bithenge_transform_t *base)
    693742{
     
    699748static const bithenge_transform_ops_t expression_transform_ops = {
    700749        .apply = expression_transform_apply,
    701         .prefix_length = expression_transform_prefix_length,
     750        .destroy = expression_transform_destroy,
     751};
     752
     753/** Create a transform that evaluates an expression on the input node. Takes a
     754 * reference to the expression.
     755 * @param[out] out Holds the new transform.
     756 * @param expr The expression to evaluate.
     757 * @return EOK on success or an error code from errno.h. */
     758int bithenge_expression_transform(bithenge_transform_t ** out,
     759    bithenge_expression_t *expr)
     760{
     761        int rc;
     762        expression_transform_t *self = malloc(sizeof(*self));
     763        if (!self) {
     764                rc = ENOMEM;
     765                goto error;
     766        }
     767
     768        rc = bithenge_init_transform(expression_as_transform(self),
     769            &expression_transform_ops, 0);
     770        if (rc != EOK)
     771                goto error;
     772
     773        self->expr = expr;
     774        *out = expression_as_transform(self);
     775        return EOK;
     776
     777error:
     778        free(self);
     779        bithenge_expression_dec_ref(expr);
     780        return rc;
     781}
     782
     783
     784
     785/***************** inputless_transform            *****************/
     786
     787static int inputless_transform_prefix_length(bithenge_transform_t *base,
     788    bithenge_scope_t *scope, bithenge_blob_t *in, aoff64_t *out)
     789{
     790        *out = 0;
     791        return EOK;
     792}
     793
     794static int inputless_transform_prefix_apply(bithenge_transform_t *base,
     795    bithenge_scope_t *scope, bithenge_blob_t *in, bithenge_node_t **out_node,
     796    aoff64_t *out_size)
     797{
     798        expression_transform_t *self = transform_as_expression(base);
     799        *out_size = 0;
     800        return bithenge_expression_evaluate(self->expr, scope, out_node);
     801}
     802
     803static const bithenge_transform_ops_t inputless_transform_ops = {
     804        .prefix_length = inputless_transform_prefix_length,
     805        .prefix_apply = inputless_transform_prefix_apply,
    702806        .destroy = expression_transform_destroy,
    703807};
     
    708812 * @param expr The expression to evaluate.
    709813 * @return EOK on success or an error code from errno.h. */
    710 int bithenge_expression_transform(bithenge_transform_t ** out,
     814int bithenge_inputless_transform(bithenge_transform_t ** out,
    711815    bithenge_expression_t *expr)
    712816{
     
    719823
    720824        rc = bithenge_init_transform(expression_as_transform(self),
    721             &expression_transform_ops, 0);
     825            &inputless_transform_ops, 0);
    722826        if (rc != EOK)
    723827                goto error;
     
    732836        return rc;
    733837}
     838
     839
     840
     841/***************** if_transform                              *****************/
    734842
    735843typedef struct {
Note: See TracChangeset for help on using the changeset viewer.