1 /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 2 * Use of this source code is governed by a BSD-style license that can be 3 * found in the LICENSE file. 4 */ 5 6 #ifndef CRAS_EXPR_H_ 7 #define CRAS_EXPR_H_ 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 #include "array.h" 14 #include "dumper.h" 15 16 /* Value */ 17 18 enum cras_expr_value_type { 19 CRAS_EXPR_VALUE_TYPE_NONE, 20 CRAS_EXPR_VALUE_TYPE_BOOLEAN, 21 CRAS_EXPR_VALUE_TYPE_INT, 22 CRAS_EXPR_VALUE_TYPE_STRING, 23 CRAS_EXPR_VALUE_TYPE_FUNCTION, 24 }; 25 26 DECLARE_ARRAY_TYPE(struct cras_expr_value, cras_expr_value_array); 27 typedef void (*cras_expr_function_type)(cras_expr_value_array *operands, 28 struct cras_expr_value *result); 29 30 struct cras_expr_value { 31 enum cras_expr_value_type type; 32 union { 33 char boolean; 34 int integer; 35 const char *string; 36 cras_expr_function_type function; 37 } u; 38 }; 39 40 /* initial value for the value type is zero */ 41 // clang-format off 42 #define CRAS_EXPR_VALUE_INIT {} 43 // clang-format on 44 45 /* Expression */ 46 47 enum expr_type { 48 EXPR_TYPE_NONE, 49 EXPR_TYPE_LITERAL, 50 EXPR_TYPE_VARIABLE, 51 EXPR_TYPE_COMPOUND, 52 }; 53 54 DECLARE_ARRAY_TYPE(struct cras_expr_expression *, expr_array); 55 56 struct cras_expr_expression { 57 enum expr_type type; 58 union { 59 struct cras_expr_value literal; 60 const char *variable; 61 expr_array children; 62 } u; 63 }; 64 65 /* Environment */ 66 67 DECLARE_ARRAY_TYPE(const char *, string_array); 68 69 struct cras_expr_env { 70 string_array keys; 71 cras_expr_value_array values; 72 }; 73 74 /* initial value for the environment type is zero */ 75 // clang-format off 76 #define CRAS_EXPR_ENV_INIT {} 77 // clang-format on 78 79 void cras_expr_env_install_builtins(struct cras_expr_env *env); 80 void cras_expr_env_set_variable_boolean(struct cras_expr_env *env, 81 const char *name, char boolean); 82 void cras_expr_env_set_variable_integer(struct cras_expr_env *env, 83 const char *name, int integer); 84 void cras_expr_env_set_variable_string(struct cras_expr_env *env, 85 const char *name, const char *str); 86 void cras_expr_env_free(struct cras_expr_env *env); 87 void cras_expr_env_dump(struct dumper *d, const struct cras_expr_env *env); 88 89 struct cras_expr_expression *cras_expr_expression_parse(const char *str); 90 void cras_expr_expression_eval(struct cras_expr_expression *expr, 91 struct cras_expr_env *env, 92 struct cras_expr_value *value); 93 int cras_expr_expression_eval_boolean(struct cras_expr_expression *expr, 94 struct cras_expr_env *env, char *boolean); 95 int cras_expr_expression_eval_int(struct cras_expr_expression *expr, 96 struct cras_expr_env *env, int *integer); 97 void cras_expr_expression_free(struct cras_expr_expression *expr); 98 void cras_expr_expression_dump(struct dumper *d, 99 const struct cras_expr_expression *expr); 100 void cras_expr_value_free(struct cras_expr_value *value); 101 void cras_expr_value_dump(struct dumper *d, 102 const struct cras_expr_value *value); 103 104 #ifdef __cplusplus 105 } /* extern "C" */ 106 #endif 107 108 #endif /* CRAS_EXPR_H_ */ 109