Fork us on GitHub Follow us on Facebook Follow us on Twitter

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


Ignore:
Timestamp:
2012-08-07T05:00:41Z (9 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master
Children:
ad5c8a48
Parents:
a8be91a
Message:

Bithenge: search for current node members in outer scopes

File:
1 edited

Legend:

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

    ra8be91a rc12b2ae  
    305305}
    306306
     307
     308
     309/***************** member_expression                         *****************/
     310
    307311typedef struct {
    308312        bithenge_expression_t base;
     
    380384        return rc;
    381385}
     386
     387
     388
     389/***************** scope_member_expression                   *****************/
     390
     391typedef struct {
     392        bithenge_expression_t base;
     393        bithenge_node_t *key;
     394} scope_member_expression_t;
     395
     396static scope_member_expression_t *expression_as_scope_member(
     397    bithenge_expression_t *base)
     398{
     399        return (scope_member_expression_t *)base;
     400}
     401
     402static bithenge_expression_t *scope_member_as_expression(
     403    scope_member_expression_t *expr)
     404{
     405        return &expr->base;
     406}
     407
     408static int scope_member_expression_evaluate(bithenge_expression_t *base,
     409    bithenge_scope_t *scope, bithenge_node_t **out)
     410{
     411        scope_member_expression_t *self = expression_as_scope_member(base);
     412        for (; scope && !bithenge_scope_is_barrier(scope);
     413            scope = bithenge_scope_outer(scope)) {
     414                bithenge_node_inc_ref(self->key);
     415                bithenge_node_t *cur = bithenge_scope_get_current_node(scope);
     416                int rc = bithenge_node_get(cur, self->key, out);
     417                bithenge_node_dec_ref(cur);
     418                if (rc != ENOENT) /* EOK or error */
     419                        return rc;
     420        }
     421        return ENOENT;
     422}
     423
     424static void scope_member_expression_destroy(bithenge_expression_t *base)
     425{
     426        scope_member_expression_t *self = expression_as_scope_member(base);
     427        bithenge_node_dec_ref(self->key);
     428        free(self);
     429}
     430
     431static const bithenge_expression_ops_t scope_member_expression_ops = {
     432        .evaluate = scope_member_expression_evaluate,
     433        .destroy = scope_member_expression_destroy,
     434};
     435
     436int bithenge_scope_member_expression(bithenge_expression_t **out,
     437    bithenge_node_t *key)
     438{
     439        int rc;
     440        scope_member_expression_t *self = malloc(sizeof(*self));
     441        if (!self) {
     442                rc = ENOMEM;
     443                goto error;
     444        }
     445
     446        rc = bithenge_init_expression(scope_member_as_expression(self),
     447            &scope_member_expression_ops);
     448        if (rc != EOK)
     449                goto error;
     450
     451        self->key = key;
     452        *out = scope_member_as_expression(self);
     453        return EOK;
     454
     455error:
     456        bithenge_node_dec_ref(key);
     457        free(self);
     458        return rc;
     459}
     460
     461
     462
     463/***************** param_wrapper                             *****************/
    382464
    383465typedef struct {
Note: See TracChangeset for help on using the changeset viewer.