Home | History | Annotate | Download | only in hal
      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 
     15 /* Value */
     16 
     17 enum cras_expr_value_type {
     18 	CRAS_EXPR_VALUE_TYPE_NONE,
     19 	CRAS_EXPR_VALUE_TYPE_BOOLEAN,
     20 	CRAS_EXPR_VALUE_TYPE_INT,
     21 	CRAS_EXPR_VALUE_TYPE_STRING,
     22 	CRAS_EXPR_VALUE_TYPE_FUNCTION,
     23 };
     24 
     25 DECLARE_ARRAY_TYPE(struct cras_expr_value, cras_expr_value_array);
     26 typedef void (*cras_expr_function_type)(cras_expr_value_array *operands,
     27 					struct cras_expr_value *result);
     28 
     29 struct cras_expr_value {
     30 	enum cras_expr_value_type type;
     31 	union {
     32 		char boolean;
     33 		int integer;
     34 		const char *string;
     35 		cras_expr_function_type function;
     36 	} u;
     37 };
     38 
     39 /* initial value for the value type is zero */
     40 #define CRAS_EXPR_VALUE_INIT {}
     41 
     42 /* Expression */
     43 
     44 enum expr_type {
     45 	EXPR_TYPE_NONE,
     46 	EXPR_TYPE_LITERAL,
     47 	EXPR_TYPE_VARIABLE,
     48 	EXPR_TYPE_COMPOUND,
     49 };
     50 
     51 DECLARE_ARRAY_TYPE(struct cras_expr_expression *, expr_array);
     52 
     53 struct cras_expr_expression {
     54 	enum expr_type type;
     55 	union {
     56 		struct cras_expr_value literal;
     57 		const char *variable;
     58 		expr_array children;
     59 	} u;
     60 };
     61 
     62 /* Environment */
     63 
     64 DECLARE_ARRAY_TYPE(const char *, string_array);
     65 
     66 struct cras_expr_env {
     67 	string_array keys;
     68 	cras_expr_value_array values;
     69 };
     70 
     71 /* initial value for the environment type is zero */
     72 #define CRAS_EXPR_ENV_INIT {}
     73 
     74 void cras_expr_env_install_builtins(struct cras_expr_env *env);
     75 void cras_expr_env_set_variable_boolean(struct cras_expr_env *env,
     76 					const char *name, char boolean);
     77 void cras_expr_env_set_variable_integer(struct cras_expr_env *env,
     78 					const char *name, int integer);
     79 void cras_expr_env_set_variable_string(struct cras_expr_env *env,
     80 				       const char *name, const char *str);
     81 void cras_expr_env_free(struct cras_expr_env *env);
     82 
     83 struct cras_expr_expression *cras_expr_expression_parse(const char *str);
     84 void cras_expr_expression_eval(struct cras_expr_expression *expr,
     85 			       struct cras_expr_env *env,
     86 			       struct cras_expr_value *value);
     87 int cras_expr_expression_eval_boolean(struct cras_expr_expression *expr,
     88 				      struct cras_expr_env *env, char *boolean);
     89 int cras_expr_expression_eval_int(struct cras_expr_expression *expr,
     90 				  struct cras_expr_env *env, int *integer);
     91 void cras_expr_expression_free(struct cras_expr_expression *expr);
     92 void cras_expr_value_free(struct cras_expr_value *value);
     93 
     94 #ifdef __cplusplus
     95 } /* extern "C" */
     96 #endif
     97 
     98 #endif /* CRAS_EXPR_H_ */
     99