source: mainline/uspace/lib/bithenge/expression.h@ 3a7356dc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3a7356dc was 3a7356dc, checked in by Sean Bartell <wingedtachikoma@…>, 13 years ago

Bithenge: make concatenation lazier

  • Property mode set to 100644
File size: 4.9 KB
Line 
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 "blob.h"
41#include "transform.h"
42#include "tree.h"
43
44/** An expression that calculates a value given a scope. */
45typedef struct {
46 /** @privatesection */
47 const struct bithenge_expression_ops *ops;
48 unsigned int refs;
49} bithenge_expression_t;
50
51/** Operations provided by an expression. */
52typedef struct bithenge_expression_ops {
53 /** @copydoc bithenge_expression_t::bithenge_expression_evaluate */
54 int (*evaluate)(bithenge_expression_t *self, bithenge_scope_t *scope,
55 bithenge_node_t **out);
56 /** Destroy the expression.
57 * @param self The expression. */
58 void (*destroy)(bithenge_expression_t *self);
59} bithenge_expression_ops_t;
60
61/** Increment an expression's reference count.
62 * @param self The expression to reference. */
63static inline void bithenge_expression_inc_ref(bithenge_expression_t *self)
64{
65 assert(self);
66 self->refs++;
67}
68
69/** Decrement an expression's reference count and free it if appropriate.
70 * @param self The expression to dereference, or NULL. */
71static inline void bithenge_expression_dec_ref(bithenge_expression_t *self)
72{
73 if (!self)
74 return;
75 assert(self->ops);
76 assert(self->refs > 0);
77 if (--self->refs == 0)
78 self->ops->destroy(self);
79}
80
81/** Evaluate an expression. Takes ownership of nothing.
82 * @memberof bithenge_expression_t
83 * @param self The expression.
84 * @param scope The scope.
85 * @param[out] out Where the output tree will be stored.
86 * @return EOK on success or an error code from errno.h. */
87static inline int bithenge_expression_evaluate(bithenge_expression_t *self,
88 bithenge_scope_t *scope, bithenge_node_t **out)
89{
90 assert(self);
91 assert(self->ops);
92 return self->ops->evaluate(self, scope, out);
93}
94
95typedef enum {
96 BITHENGE_EXPRESSION_INVALID_BINARY_OP,
97
98 BITHENGE_EXPRESSION_ADD,
99 BITHENGE_EXPRESSION_SUBTRACT,
100 BITHENGE_EXPRESSION_MULTIPLY,
101 BITHENGE_EXPRESSION_INTEGER_DIVIDE,
102 BITHENGE_EXPRESSION_MODULO,
103
104 BITHENGE_EXPRESSION_LESS_THAN,
105 BITHENGE_EXPRESSION_GREATER_THAN,
106 BITHENGE_EXPRESSION_LESS_THAN_OR_EQUAL,
107 BITHENGE_EXPRESSION_GREATER_THAN_OR_EQUAL,
108 BITHENGE_EXPRESSION_EQUALS,
109 BITHENGE_EXPRESSION_NOT_EQUALS,
110
111 BITHENGE_EXPRESSION_AND,
112 BITHENGE_EXPRESSION_OR,
113
114 BITHENGE_EXPRESSION_MEMBER,
115 BITHENGE_EXPRESSION_CONCAT,
116} bithenge_binary_op_t;
117
118int bithenge_init_expression(bithenge_expression_t *,
119 const bithenge_expression_ops_t *);
120int bithenge_binary_expression(bithenge_expression_t **, bithenge_binary_op_t,
121 bithenge_expression_t *, bithenge_expression_t *);
122int bithenge_in_node_expression(bithenge_expression_t **);
123int bithenge_current_node_expression(bithenge_expression_t **);
124int bithenge_param_expression(bithenge_expression_t **, int);
125int bithenge_const_expression(bithenge_expression_t **, bithenge_node_t *);
126int bithenge_scope_member_expression(bithenge_expression_t **,
127 bithenge_node_t *);
128int bithenge_subblob_expression(bithenge_expression_t **,
129 bithenge_expression_t *, bithenge_expression_t *, bithenge_expression_t *,
130 bool);
131int bithenge_param_wrapper(bithenge_transform_t **, bithenge_transform_t *,
132 bithenge_expression_t **);
133int bithenge_expression_transform(bithenge_transform_t **,
134 bithenge_expression_t *);
135int bithenge_inputless_transform(bithenge_transform_t **,
136 bithenge_expression_t *);
137
138int bithenge_concat_blob(bithenge_node_t **, bithenge_blob_t *,
139 bithenge_blob_t *);
140int bithenge_concat_blob_lazy(bithenge_node_t **, bithenge_blob_t *,
141 bithenge_expression_t *, bithenge_scope_t *);
142
143#endif
144
145/** @}
146 */
Note: See TracBrowser for help on using the repository browser.