Changeset c12b2ae in mainline
- Timestamp:
- 2012-08-07T05:00:41Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ad5c8a48
- Parents:
- a8be91a
- Location:
- uspace
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bithenge/expression.c
ra8be91a rc12b2ae 305 305 } 306 306 307 308 309 /***************** member_expression *****************/ 310 307 311 typedef struct { 308 312 bithenge_expression_t base; … … 380 384 return rc; 381 385 } 386 387 388 389 /***************** scope_member_expression *****************/ 390 391 typedef struct { 392 bithenge_expression_t base; 393 bithenge_node_t *key; 394 } scope_member_expression_t; 395 396 static scope_member_expression_t *expression_as_scope_member( 397 bithenge_expression_t *base) 398 { 399 return (scope_member_expression_t *)base; 400 } 401 402 static bithenge_expression_t *scope_member_as_expression( 403 scope_member_expression_t *expr) 404 { 405 return &expr->base; 406 } 407 408 static 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 424 static 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 431 static const bithenge_expression_ops_t scope_member_expression_ops = { 432 .evaluate = scope_member_expression_evaluate, 433 .destroy = scope_member_expression_destroy, 434 }; 435 436 int 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 455 error: 456 bithenge_node_dec_ref(key); 457 free(self); 458 return rc; 459 } 460 461 462 463 /***************** param_wrapper *****************/ 382 464 383 465 typedef struct { -
uspace/app/bithenge/expression.h
ra8be91a rc12b2ae 104 104 int bithenge_member_expression(bithenge_expression_t **, 105 105 bithenge_expression_t *, bithenge_node_t *); 106 int bithenge_scope_member_expression(bithenge_expression_t **, 107 bithenge_node_t *); 106 108 int bithenge_param_wrapper(bithenge_transform_t **, bithenge_transform_t *, 107 109 bithenge_expression_t **); -
uspace/app/bithenge/script.c
ra8be91a rc12b2ae 363 363 static bithenge_expression_t *parse_expression(state_t *state) 364 364 { 365 int rc; 365 366 if (state->token == TOKEN_TRUE || state->token == TOKEN_FALSE) { 366 367 bool val = state->token == TOKEN_TRUE; 367 368 next_token(state); 368 369 bithenge_node_t *node; 369 intrc = bithenge_new_boolean_node(&node, val);370 rc = bithenge_new_boolean_node(&node, val); 370 371 if (rc != EOK) { 371 372 error_errno(state, rc); … … 385 386 next_token(state); 386 387 bithenge_node_t *node; 387 intrc = bithenge_new_integer_node(&node, val);388 rc = bithenge_new_integer_node(&node, val); 388 389 if (rc != EOK) { 389 390 error_errno(state, rc); … … 412 413 413 414 bithenge_expression_t *expr; 414 intrc = bithenge_param_expression(&expr, i);415 rc = bithenge_param_expression(&expr, i); 415 416 if (rc != EOK) { 416 417 error_errno(state, rc); … … 427 428 bithenge_node_t *key = NULL; 428 429 bithenge_expression_t *expr = NULL; 429 int rc = bithenge_current_node_expression(&expr);430 error_errno(state, rc);431 430 432 431 if (state->error == EOK) { … … 437 436 438 437 if (state->error == EOK) { 439 rc = bithenge_ member_expression(&expr,expr, key);438 rc = bithenge_scope_member_expression(&expr, key); 440 439 key = NULL; 441 440 if (rc != EOK) … … 857 856 858 857 if (state->error == EOK) { 859 int rc = bithenge_new_ scope_transform(&xform, xform,858 int rc = bithenge_new_barrier_transform(&xform, xform, 860 859 state->num_params); 861 860 if (rc != EOK) { -
uspace/app/bithenge/transform.c
ra8be91a rc12b2ae 179 179 bithenge_scope_inc_ref(outer); 180 180 self->outer = outer; 181 self->barrier = false; 181 182 self->num_params = 0; 182 183 self->params = NULL; … … 202 203 } 203 204 205 /** Get the outer scope of a scope, which may be NULL. 206 * @param self The scope to examine. 207 * @return The outer scope, which may be NULL. */ 208 bithenge_scope_t *bithenge_scope_outer(bithenge_scope_t *self) 209 { 210 return self->outer; 211 } 212 204 213 /** Set the current node being created. Takes a reference to @a node. 205 214 * @param scope The scope to set the current node in. … … 211 220 bithenge_node_dec_ref(scope->current_node); 212 221 scope->current_node = node; 222 } 223 224 /** Set a scope as a barrier. 225 * @param self The scope to change. */ 226 void bithenge_scope_set_barrier(bithenge_scope_t *self) 227 { 228 self->barrier = true; 229 } 230 231 /** Check whether a scope is a barrier, meaning that variable lookup stops at 232 * it. 233 * @param self The scope to check. 234 * @return Whether the scope is a barrier. */ 235 bool bithenge_scope_is_barrier(bithenge_scope_t *self) 236 { 237 return self->barrier; 213 238 } 214 239 … … 277 302 bithenge_transform_t base; 278 303 bithenge_transform_t *transform; 279 } scope_transform_t;280 281 static inline scope_transform_t *transform_as_param(304 } barrier_transform_t; 305 306 static inline barrier_transform_t *transform_as_barrier( 282 307 bithenge_transform_t *base) 283 308 { 284 return ( scope_transform_t *)base;285 } 286 287 static inline bithenge_transform_t * param_as_transform(288 scope_transform_t *self)309 return (barrier_transform_t *)base; 310 } 311 312 static inline bithenge_transform_t *barrier_as_transform( 313 barrier_transform_t *self) 289 314 { 290 315 return &self->base; 291 316 } 292 317 293 static int scope_transform_apply(bithenge_transform_t *base,318 static int barrier_transform_apply(bithenge_transform_t *base, 294 319 bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out) 295 320 { 296 scope_transform_t *self = transform_as_param(base);321 barrier_transform_t *self = transform_as_barrier(base); 297 322 bithenge_scope_t *inner_scope; 298 323 int rc = bithenge_scope_new(&inner_scope, scope); 299 324 if (rc != EOK) 300 325 return rc; 326 bithenge_scope_set_barrier(inner_scope); 301 327 rc = bithenge_transform_apply(self->transform, scope, in, out); 302 328 bithenge_scope_dec_ref(inner_scope); … … 304 330 } 305 331 306 static int scope_transform_prefix_length(bithenge_transform_t *base,332 static int barrier_transform_prefix_length(bithenge_transform_t *base, 307 333 bithenge_scope_t *scope, bithenge_blob_t *in, aoff64_t *out) 308 334 { 309 scope_transform_t *self = transform_as_param(base); 310 return bithenge_transform_prefix_length(self->transform, scope, in, 311 out); 312 } 313 314 static void scope_transform_destroy(bithenge_transform_t *base) 315 { 316 scope_transform_t *self = transform_as_param(base); 335 barrier_transform_t *self = transform_as_barrier(base); 336 bithenge_scope_t *inner_scope; 337 int rc = bithenge_scope_new(&inner_scope, scope); 338 if (rc != EOK) 339 return rc; 340 bithenge_scope_set_barrier(inner_scope); 341 rc = bithenge_transform_prefix_length(self->transform, scope, in, out); 342 bithenge_scope_dec_ref(inner_scope); 343 return rc; 344 } 345 346 static int barrier_transform_prefix_apply(bithenge_transform_t *base, 347 bithenge_scope_t *scope, bithenge_blob_t *in, bithenge_node_t **out_node, 348 aoff64_t *out_length) 349 { 350 barrier_transform_t *self = transform_as_barrier(base); 351 bithenge_scope_t *inner_scope; 352 int rc = bithenge_scope_new(&inner_scope, scope); 353 if (rc != EOK) 354 return rc; 355 bithenge_scope_set_barrier(inner_scope); 356 rc = bithenge_transform_prefix_apply(self->transform, scope, in, 357 out_node, out_length); 358 bithenge_scope_dec_ref(inner_scope); 359 return rc; 360 } 361 362 static void barrier_transform_destroy(bithenge_transform_t *base) 363 { 364 barrier_transform_t *self = transform_as_barrier(base); 317 365 bithenge_transform_dec_ref(self->transform); 318 366 free(self); 319 367 } 320 368 321 static const bithenge_transform_ops_t scope_transform_ops = { 322 .apply = scope_transform_apply, 323 .prefix_length = scope_transform_prefix_length, 324 .destroy = scope_transform_destroy, 325 }; 326 327 /** Create a wrapper transform that creates a new outer scope. This ensures 328 * nothing from the transform's users is passed in, other than parameters. The 329 * wrapper may have a different value for num_params. Takes a reference to 330 * @a transform, which it will use for all operations. 369 static const bithenge_transform_ops_t barrier_transform_ops = { 370 .apply = barrier_transform_apply, 371 .prefix_length = barrier_transform_prefix_length, 372 .prefix_apply = barrier_transform_prefix_apply, 373 .destroy = barrier_transform_destroy, 374 }; 375 376 /** Create a wrapper transform that creates a new scope. This ensures nothing 377 * from the outer scope is passed in, other than parameters. The wrapper may 378 * have a different value for num_params. Takes a reference to @a transform, 379 * which it will use for all operations. 331 380 * @param[out] out Holds the created transform. 332 381 * @param transform The transform to wrap. 333 382 * @param num_params The number of parameters to require, which may be 0. 334 383 * @return EOK on success or an error code from errno.h. */ 335 int bithenge_new_ scope_transform(bithenge_transform_t **out,384 int bithenge_new_barrier_transform(bithenge_transform_t **out, 336 385 bithenge_transform_t *transform, int num_params) 337 386 { … … 340 389 341 390 int rc; 342 scope_transform_t *self = malloc(sizeof(*self));391 barrier_transform_t *self = malloc(sizeof(*self)); 343 392 if (!self) { 344 393 rc = ENOMEM; 345 394 goto error; 346 395 } 347 rc = bithenge_init_transform( param_as_transform(self),348 & scope_transform_ops, num_params);396 rc = bithenge_init_transform(barrier_as_transform(self), 397 &barrier_transform_ops, num_params); 349 398 if (rc != EOK) 350 399 goto error; 351 400 self->transform = transform; 352 *out = param_as_transform(self);401 *out = barrier_as_transform(self); 353 402 return EOK; 354 403 error: -
uspace/app/bithenge/transform.h
ra8be91a rc12b2ae 54 54 unsigned int refs; 55 55 struct bithenge_scope *outer; 56 bool barrier; 56 57 int num_params; 57 58 bithenge_node_t **params; … … 144 145 int bithenge_transform_prefix_apply(bithenge_transform_t *, bithenge_scope_t *, 145 146 bithenge_blob_t *, bithenge_node_t **, aoff64_t *); 146 int bithenge_new_ scope_transform(bithenge_transform_t **,147 int bithenge_new_barrier_transform(bithenge_transform_t **, 147 148 bithenge_transform_t *, int); 148 149 int bithenge_new_composed_transform(bithenge_transform_t **, … … 151 152 int bithenge_scope_new(bithenge_scope_t **, bithenge_scope_t *); 152 153 void bithenge_scope_dec_ref(bithenge_scope_t *); 154 bithenge_scope_t *bithenge_scope_outer(bithenge_scope_t *); 153 155 void bithenge_scope_set_current_node(bithenge_scope_t *, bithenge_node_t *); 156 void bithenge_scope_set_barrier(bithenge_scope_t *); 157 bool bithenge_scope_is_barrier(bithenge_scope_t *); 154 158 bithenge_node_t *bithenge_scope_get_current_node(bithenge_scope_t *); 155 159 int bithenge_scope_alloc_params(bithenge_scope_t *, int); -
uspace/dist/src/bithenge/test-repeat.bh
ra8be91a rc12b2ae 3 3 .one <- repeat(1) { uint8 }; 4 4 .count <- uint8; 5 .many <- repeat(.count) { uint8 }; 5 if (true) { # test whether .count is still accessible 6 .many <- repeat(.count) { uint8 }; 7 } 6 8 }; 7 9
Note:
See TracChangeset
for help on using the changeset viewer.