| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 Sean Bartell
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup bithenge
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * Expressions.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #ifndef BITHENGE_EXPRESSION_H_
|
|---|
| 38 | #define BITHENGE_EXPRESSION_H_
|
|---|
| 39 |
|
|---|
| 40 | #include "transform.h"
|
|---|
| 41 | #include "tree.h"
|
|---|
| 42 |
|
|---|
| 43 | /** An expression that calculates a value given a scope. */
|
|---|
| 44 | typedef struct {
|
|---|
| 45 | /** @privatesection */
|
|---|
| 46 | const struct bithenge_expression_ops *ops;
|
|---|
| 47 | unsigned int refs;
|
|---|
| 48 | } bithenge_expression_t;
|
|---|
| 49 |
|
|---|
| 50 | /** Operations provided by an expression. */
|
|---|
| 51 | typedef struct bithenge_expression_ops {
|
|---|
| 52 | /** @copydoc bithenge_expression_t::bithenge_expression_evaluate */
|
|---|
| 53 | int (*evaluate)(bithenge_expression_t *self, bithenge_scope_t *scope,
|
|---|
| 54 | bithenge_node_t **out);
|
|---|
| 55 | /** Destroy the expression.
|
|---|
| 56 | * @param self The expression. */
|
|---|
| 57 | void (*destroy)(bithenge_expression_t *self);
|
|---|
| 58 | } bithenge_expression_ops_t;
|
|---|
| 59 |
|
|---|
| 60 | /** Increment an expression's reference count.
|
|---|
| 61 | * @param self The expression to reference. */
|
|---|
| 62 | static inline void bithenge_expression_inc_ref(bithenge_expression_t *self)
|
|---|
| 63 | {
|
|---|
| 64 | assert(self);
|
|---|
| 65 | self->refs++;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /** Decrement an expression's reference count and free it if appropriate.
|
|---|
| 69 | * @param self The expression to dereference, or NULL. */
|
|---|
| 70 | static inline void bithenge_expression_dec_ref(bithenge_expression_t *self)
|
|---|
| 71 | {
|
|---|
| 72 | if (!self)
|
|---|
| 73 | return;
|
|---|
| 74 | assert(self->ops);
|
|---|
| 75 | if (--self->refs == 0)
|
|---|
| 76 | self->ops->destroy(self);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /** Evaluate an expression. Takes ownership of nothing.
|
|---|
| 80 | * @memberof bithenge_expression_t
|
|---|
| 81 | * @param self The expression.
|
|---|
| 82 | * @param scope The scope.
|
|---|
| 83 | * @param[out] out Where the output tree will be stored.
|
|---|
| 84 | * @return EOK on success or an error code from errno.h. */
|
|---|
| 85 | static inline int bithenge_expression_evaluate(bithenge_expression_t *self,
|
|---|
| 86 | bithenge_scope_t *scope, bithenge_node_t **out)
|
|---|
| 87 | {
|
|---|
| 88 | assert(self);
|
|---|
| 89 | assert(self->ops);
|
|---|
| 90 | return self->ops->evaluate(self, scope, out);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | typedef enum {
|
|---|
| 94 | BITHENGE_EXPRESSION_INVALID_BINARY_OP,
|
|---|
| 95 | BITHENGE_EXPRESSION_ADD,
|
|---|
| 96 | BITHENGE_EXPRESSION_SUBTRACT,
|
|---|
| 97 | BITHENGE_EXPRESSION_MULTIPLY,
|
|---|
| 98 | BITHENGE_EXPRESSION_INTEGER_DIVIDE,
|
|---|
| 99 | BITHENGE_EXPRESSION_MODULO,
|
|---|
| 100 | BITHENGE_EXPRESSION_LESS_THAN,
|
|---|
| 101 | BITHENGE_EXPRESSION_GREATER_THAN,
|
|---|
| 102 | BITHENGE_EXPRESSION_LESS_THAN_OR_EQUAL,
|
|---|
| 103 | BITHENGE_EXPRESSION_GREATER_THAN_OR_EQUAL,
|
|---|
| 104 | BITHENGE_EXPRESSION_EQUALS,
|
|---|
| 105 | BITHENGE_EXPRESSION_NOT_EQUALS,
|
|---|
| 106 | } bithenge_binary_op_t;
|
|---|
| 107 |
|
|---|
| 108 | int bithenge_init_expression(bithenge_expression_t *,
|
|---|
| 109 | const bithenge_expression_ops_t *);
|
|---|
| 110 | int bithenge_binary_expression(bithenge_expression_t **, bithenge_binary_op_t,
|
|---|
| 111 | bithenge_expression_t *, bithenge_expression_t *);
|
|---|
| 112 | int bithenge_in_node_expression(bithenge_expression_t **);
|
|---|
| 113 | int bithenge_current_node_expression(bithenge_expression_t **);
|
|---|
| 114 | int bithenge_param_expression(bithenge_expression_t **, int);
|
|---|
| 115 | int bithenge_const_expression(bithenge_expression_t **, bithenge_node_t *);
|
|---|
| 116 | int bithenge_member_expression(bithenge_expression_t **,
|
|---|
| 117 | bithenge_expression_t *, bithenge_node_t *);
|
|---|
| 118 | int bithenge_scope_member_expression(bithenge_expression_t **,
|
|---|
| 119 | bithenge_node_t *);
|
|---|
| 120 | int bithenge_subblob_expression(bithenge_expression_t **,
|
|---|
| 121 | bithenge_expression_t *, bithenge_expression_t *, bithenge_expression_t *,
|
|---|
| 122 | bool);
|
|---|
| 123 | int bithenge_param_wrapper(bithenge_transform_t **, bithenge_transform_t *,
|
|---|
| 124 | bithenge_expression_t **);
|
|---|
| 125 | int bithenge_expression_transform(bithenge_transform_t **,
|
|---|
| 126 | bithenge_expression_t *);
|
|---|
| 127 | int bithenge_inputless_transform(bithenge_transform_t **,
|
|---|
| 128 | bithenge_expression_t *);
|
|---|
| 129 |
|
|---|
| 130 | #endif
|
|---|
| 131 |
|
|---|
| 132 | /** @}
|
|---|
| 133 | */
|
|---|