Changeset a66ea217 in mainline for uspace/app/bithenge/expression.c
- Timestamp:
- 2012-08-08T00:52:22Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- d8bd2ec
- Parents:
- ad5c8a48
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bithenge/expression.c
rad5c8a48 ra66ea217 63 63 } 64 64 65 66 67 /***************** binary_expression *****************/ 68 65 69 typedef struct { 66 70 bithenge_expression_t base; … … 185 189 } 186 190 191 192 193 /***************** in_node_expression *****************/ 194 195 static int in_node_evaluate(bithenge_expression_t *self, 196 bithenge_scope_t *scope, bithenge_node_t **out) 197 { 198 for (; scope && !bithenge_scope_is_barrier(scope); 199 scope = bithenge_scope_outer(scope)) { 200 *out = bithenge_scope_in_node(scope); 201 if (*out) 202 return EOK; 203 } 204 return EINVAL; 205 } 206 207 static const bithenge_expression_ops_t in_node_ops = { 208 .evaluate = in_node_evaluate, 209 .destroy = expression_indestructible, 210 }; 211 212 static bithenge_expression_t in_node_expression = { 213 &in_node_ops, 1 214 }; 215 216 /** Create an expression that gets the current input node. 217 * @param[out] out Holds the new expression. 218 * @return EOK on success or an error code from errno.h. */ 219 int bithenge_in_node_expression(bithenge_expression_t **out) 220 { 221 bithenge_expression_inc_ref(&in_node_expression); 222 *out = &in_node_expression; 223 return EOK; 224 } 225 226 227 228 /***************** current_node_expression *****************/ 229 187 230 static int current_node_evaluate(bithenge_expression_t *self, 188 231 bithenge_scope_t *scope, bithenge_node_t **out) … … 212 255 return EOK; 213 256 } 257 258 259 260 /***************** param_expression *****************/ 214 261 215 262 typedef struct { … … 270 317 return EOK; 271 318 } 319 320 321 322 /***************** const_expression *****************/ 272 323 273 324 typedef struct { … … 651 702 } 652 703 704 705 706 /***************** expression_transform *****************/ 707 708 /* Also used by inputless_transform. */ 653 709 typedef struct { 654 710 bithenge_transform_t base; … … 672 728 { 673 729 expression_transform_t *self = transform_as_expression(base); 674 if (bithenge_node_type(in) != BITHENGE_NODE_BLOB) 675 return EINVAL; 676 aoff64_t len; 677 int rc = bithenge_blob_size(bithenge_node_as_blob(in), &len); 678 if (rc != EOK) 679 return rc; 680 if (len != 0) 681 return EINVAL; 682 return bithenge_expression_evaluate(self->expr, scope, out); 683 } 684 685 static int expression_transform_prefix_length(bithenge_transform_t *base, 686 bithenge_scope_t *scope, bithenge_blob_t *in, aoff64_t *out) 687 { 688 *out = 0; 689 return EOK; 690 } 691 730 bithenge_scope_t *inner; 731 int rc = bithenge_scope_new(&inner, scope); 732 if (rc != EOK) 733 return rc; 734 bithenge_scope_set_in_node(inner, in); 735 rc = bithenge_expression_evaluate(self->expr, inner, out); 736 bithenge_scope_dec_ref(inner); 737 return rc; 738 } 739 740 /* Also used by inputless_transform. */ 692 741 static void expression_transform_destroy(bithenge_transform_t *base) 693 742 { … … 699 748 static const bithenge_transform_ops_t expression_transform_ops = { 700 749 .apply = expression_transform_apply, 701 .prefix_length = expression_transform_prefix_length, 750 .destroy = expression_transform_destroy, 751 }; 752 753 /** Create a transform that evaluates an expression on the input node. Takes a 754 * reference to the expression. 755 * @param[out] out Holds the new transform. 756 * @param expr The expression to evaluate. 757 * @return EOK on success or an error code from errno.h. */ 758 int bithenge_expression_transform(bithenge_transform_t ** out, 759 bithenge_expression_t *expr) 760 { 761 int rc; 762 expression_transform_t *self = malloc(sizeof(*self)); 763 if (!self) { 764 rc = ENOMEM; 765 goto error; 766 } 767 768 rc = bithenge_init_transform(expression_as_transform(self), 769 &expression_transform_ops, 0); 770 if (rc != EOK) 771 goto error; 772 773 self->expr = expr; 774 *out = expression_as_transform(self); 775 return EOK; 776 777 error: 778 free(self); 779 bithenge_expression_dec_ref(expr); 780 return rc; 781 } 782 783 784 785 /***************** inputless_transform *****************/ 786 787 static int inputless_transform_prefix_length(bithenge_transform_t *base, 788 bithenge_scope_t *scope, bithenge_blob_t *in, aoff64_t *out) 789 { 790 *out = 0; 791 return EOK; 792 } 793 794 static int inputless_transform_prefix_apply(bithenge_transform_t *base, 795 bithenge_scope_t *scope, bithenge_blob_t *in, bithenge_node_t **out_node, 796 aoff64_t *out_size) 797 { 798 expression_transform_t *self = transform_as_expression(base); 799 *out_size = 0; 800 return bithenge_expression_evaluate(self->expr, scope, out_node); 801 } 802 803 static const bithenge_transform_ops_t inputless_transform_ops = { 804 .prefix_length = inputless_transform_prefix_length, 805 .prefix_apply = inputless_transform_prefix_apply, 702 806 .destroy = expression_transform_destroy, 703 807 }; … … 708 812 * @param expr The expression to evaluate. 709 813 * @return EOK on success or an error code from errno.h. */ 710 int bithenge_ expression_transform(bithenge_transform_t ** out,814 int bithenge_inputless_transform(bithenge_transform_t ** out, 711 815 bithenge_expression_t *expr) 712 816 { … … 719 823 720 824 rc = bithenge_init_transform(expression_as_transform(self), 721 & expression_transform_ops, 0);825 &inputless_transform_ops, 0); 722 826 if (rc != EOK) 723 827 goto error; … … 732 836 return rc; 733 837 } 838 839 840 841 /***************** if_transform *****************/ 734 842 735 843 typedef struct {
Note:
See TracChangeset
for help on using the changeset viewer.