Changeset f85ca3f in mainline
- Timestamp:
- 2012-07-28T21:25:49Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 84e8a70
- Parents:
- 32eb01b
- Location:
- uspace/app/bithenge
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified uspace/app/bithenge/expression.c ¶
r32eb01b rf85ca3f 58 58 } 59 59 60 static void expression_indestructible(bithenge_expression_t *self) 61 { 62 assert(false); 63 } 64 65 static int current_node_evaluate(bithenge_expression_t *self, 66 bithenge_scope_t *scope, bithenge_node_t **out) 67 { 68 *out = bithenge_scope_get_current_node(scope); 69 if (!*out) 70 return EINVAL; 71 return EOK; 72 } 73 74 static const bithenge_expression_ops_t current_node_ops = { 75 .evaluate = current_node_evaluate, 76 .destroy = expression_indestructible, 77 }; 78 79 static bithenge_expression_t current_node_expression = { 80 ¤t_node_ops, 1 81 }; 82 83 /** Create an expression that gets the current node being created. 84 * @param[out] out Holds the new expression. 85 * @return EOK on success or an error code from errno.h. */ 86 int bithenge_current_node_expression(bithenge_expression_t **out) 87 { 88 bithenge_expression_inc_ref(¤t_node_expression); 89 *out = ¤t_node_expression; 90 return EOK; 91 } 92 60 93 typedef struct { 61 94 bithenge_expression_t base; … … 176 209 *out = const_as_expression(self); 177 210 return EOK; 211 } 212 213 typedef struct { 214 bithenge_expression_t base; 215 bithenge_expression_t *expr; 216 bithenge_node_t *key; 217 } member_expression_t; 218 219 static member_expression_t *expression_as_member(bithenge_expression_t *base) 220 { 221 return (member_expression_t *)base; 222 } 223 224 static bithenge_expression_t *member_as_expression(member_expression_t *expr) 225 { 226 return &expr->base; 227 } 228 229 static int member_expression_evaluate(bithenge_expression_t *base, 230 bithenge_scope_t *scope, bithenge_node_t **out) 231 { 232 member_expression_t *self = expression_as_member(base); 233 bithenge_node_t *node; 234 int rc = bithenge_expression_evaluate(self->expr, scope, &node); 235 if (rc != EOK) 236 return rc; 237 bithenge_node_inc_ref(self->key); 238 rc = bithenge_node_get(node, self->key, out); 239 bithenge_node_dec_ref(node); 240 return rc; 241 } 242 243 static void member_expression_destroy(bithenge_expression_t *base) 244 { 245 member_expression_t *self = expression_as_member(base); 246 bithenge_expression_dec_ref(self->expr); 247 bithenge_node_dec_ref(self->key); 248 free(self); 249 } 250 251 static const bithenge_expression_ops_t member_expression_ops = { 252 .evaluate = member_expression_evaluate, 253 .destroy = member_expression_destroy, 254 }; 255 256 /** Create an expression that gets a member from a node. Takes references to 257 * @a expr and @a key. 258 * @param[out] out Holds the new expression. 259 * @param expr Calculates the node to get the member of. 260 * @param key The member to get. 261 * @return EOK on success or an error code from errno.h. */ 262 int bithenge_member_expression(bithenge_expression_t **out, 263 bithenge_expression_t *expr, bithenge_node_t *key) 264 { 265 int rc; 266 member_expression_t *self = malloc(sizeof(*self)); 267 if (!self) { 268 rc = ENOMEM; 269 goto error; 270 } 271 272 rc = bithenge_init_expression(member_as_expression(self), 273 &member_expression_ops); 274 if (rc != EOK) 275 goto error; 276 277 self->expr = expr; 278 self->key = key; 279 *out = member_as_expression(self); 280 return EOK; 281 282 error: 283 bithenge_expression_dec_ref(expr); 284 bithenge_node_dec_ref(key); 285 free(self); 286 return rc; 178 287 } 179 288 -
TabularUnified uspace/app/bithenge/expression.h ¶
r32eb01b rf85ca3f 93 93 int bithenge_init_expression(bithenge_expression_t *, 94 94 const bithenge_expression_ops_t *); 95 int bithenge_current_node_expression(bithenge_expression_t **); 95 96 int bithenge_param_expression(bithenge_expression_t **, int); 96 97 int bithenge_const_expression(bithenge_expression_t **, bithenge_node_t *); 98 int bithenge_member_expression(bithenge_expression_t **, 99 bithenge_expression_t *, bithenge_node_t *); 97 100 int bithenge_param_wrapper(bithenge_transform_t **, bithenge_transform_t *, 98 101 bithenge_expression_t **); -
TabularUnified uspace/app/bithenge/script.c ¶
r32eb01b rf85ca3f 119 119 } 120 120 121 /** Note that an error has occurred . */121 /** Note that an error has occurred if error is not EOK. */ 122 122 static void error_errno(state_t *state, int error) 123 123 { 124 124 // Don't overwrite a previous error. 125 if (state->error == EOK ) {125 if (state->error == EOK && error != EOK) { 126 126 done_with_token(state); 127 127 state->token = TOKEN_ERROR; … … 220 220 int rc = bithenge_parse_int(state->buffer + 221 221 state->old_buffer_pos, &state->token_int); 222 if (rc != EOK) 223 error_errno(state, rc); 222 error_errno(state, rc); 224 223 } else if (ch == '<') { 225 224 state->token = ch; … … 368 367 369 368 next_token(state); 369 370 return expr; 371 } else if (state->token == '.') { 372 next_token(state); 373 374 const char *id = expect_identifier(state); 375 bithenge_node_t *key = NULL; 376 bithenge_expression_t *expr = NULL; 377 int rc = bithenge_current_node_expression(&expr); 378 error_errno(state, rc); 379 380 if (state->error == EOK) { 381 rc = bithenge_new_string_node(&key, id, true); 382 id = NULL; 383 error_errno(state, rc); 384 } 385 386 if (state->error == EOK) { 387 rc = bithenge_member_expression(&expr, expr, key); 388 key = NULL; 389 if (rc != EOK) 390 expr = NULL; 391 error_errno(state, rc); 392 } 393 394 if (state->error != EOK) { 395 free((char *)id); 396 bithenge_node_dec_ref(key); 397 bithenge_expression_dec_ref(expr); 398 return NULL; 399 } 370 400 371 401 return expr; … … 588 618 done_with_token(state); 589 619 state->token = TOKEN_ERROR; 590 fclose(state->file); 620 if (state->file) 621 fclose(state->file); 591 622 transform_list_t *entry = state->transform_list; 592 623 while (entry) { -
TabularUnified uspace/app/bithenge/transform.c ¶
r32eb01b rf85ca3f 552 552 node->blob = bithenge_node_as_blob(in); 553 553 *out = struct_as_node(node); 554 bithenge_node_inc_ref(*out); 555 bithenge_scope_set_current_node(&node->scope, *out); 554 556 return EOK; 555 557 } -
TabularUnified uspace/app/bithenge/transform.h ¶
r32eb01b rf85ca3f 52 52 typedef struct { 53 53 /** @privatesection */ 54 int num_params; 54 55 bithenge_node_t **params; 55 int num_params;56 bithenge_node_t *current_node; 56 57 } bithenge_scope_t; 57 58 … … 74 75 static inline void bithenge_scope_init(bithenge_scope_t *scope) 75 76 { 77 scope->num_params = 0; 76 78 scope->params = NULL; 77 scope-> num_params = 0;79 scope->current_node = NULL; 78 80 } 79 81 … … 83 85 static inline void bithenge_scope_destroy(bithenge_scope_t *scope) 84 86 { 87 bithenge_node_dec_ref(scope->current_node); 85 88 for (int i = 0; i < scope->num_params; i++) 86 89 bithenge_node_dec_ref(scope->params[i]); … … 104 107 for (int i = 0; i < out->num_params; i++) 105 108 bithenge_node_inc_ref(out->params[i]); 106 return EOK; 109 out->current_node = scope->current_node; 110 if (out->current_node) 111 bithenge_node_inc_ref(out->current_node); 112 return EOK; 113 } 114 115 /** Set the current node being created. Takes a reference to @a node. 116 * @param scope The scope to set the current node in. 117 * @param node The current node being created. 118 * @return EOK on success or an error code from errno.h. */ 119 static inline void bithenge_scope_set_current_node(bithenge_scope_t *scope, 120 bithenge_node_t *node) 121 { 122 bithenge_node_dec_ref(scope->current_node); 123 scope->current_node = node; 124 } 125 126 /** Get the current node being created, which may be NULL. 127 * @param scope The scope to get the current node from. 128 * @return The node being created, or NULL. */ 129 static inline bithenge_node_t *bithenge_scope_get_current_node( 130 bithenge_scope_t *scope) 131 { 132 if (scope->current_node) 133 bithenge_node_inc_ref(scope->current_node); 134 return scope->current_node; 107 135 } 108 136 109 137 /** Allocate parameters. The parameters must then be set with @a 110 * bithenge_scope_set_param. 138 * bithenge_scope_set_param. This must not be called on a scope that already 139 * has parameters. 111 140 * @param scope The scope in which to allocate parameters. 112 141 * @param num_params The number of parameters to allocate.
Note:
See TracChangeset
for help on using the changeset viewer.