Commit c1b77455 authored by Brice Videau's avatar Brice Videau
Browse files

Refactored expressions.

parent 83ae78ea
......@@ -72,11 +72,11 @@ typedef enum ccs_object_type_e ccs_object_type_t;
enum ccs_data_type_e {
CCS_NONE,
CCS_INACTIVE,
CCS_INTEGER,
CCS_FLOAT,
CCS_BOOLEAN,
CCS_STRING,
CCS_INACTIVE,
CCS_OBJECT,
CCS_DATA_TYPE_MAX,
CCS_DATA_TYPE_FORCE_64BIT = INT64_MAX
......
......@@ -24,6 +24,8 @@ enum ccs_expression_type_e {
CCS_NEGATIVE,
CCS_NOT,
CCS_LIST,
CCS_LITERAL,
CCS_VARIABLE,
CCS_EXPRESSION_TYPE_MAX,
CCS_EXPRESSION_FORCE_32BIT = INT_MAX
};
......@@ -41,7 +43,7 @@ typedef enum ccs_expression_type_e ccs_expression_type_t;
// 6 : MULTIPLY, DIVIDE, MODULO
// 7 : POSITIVE, NEGATIVE, NOT
// max - 1: LIST
// max : LITERAL, VARIABLE, HYPERPARAMETER
// max : LITERAL, VARIABLE
// One for each expression type:
extern const int ccs_expression_precedence[];
......@@ -68,6 +70,14 @@ ccs_create_expression(ccs_expression_type_t type,
ccs_datum_t *nodes,
ccs_expression_t *expression_ret);
extern ccs_error_t
ccs_create_literal(ccs_datum_t value,
ccs_expression_t *expression_ret);
extern ccs_error_t
ccs_create_variable(ccs_hyperparameter_t hyperparameter,
ccs_expression_t *expression_ret);
extern ccs_error_t
ccs_expression_get_type(ccs_expression_t expression,
ccs_expression_type_t *type_ret);
......@@ -79,9 +89,17 @@ ccs_expression_get_num_nodes(ccs_expression_t expression,
extern ccs_error_t
ccs_expression_get_nodes(ccs_expression_t expression,
size_t num_nodes,
ccs_datum_t *nodes,
ccs_expression_t *nodes,
size_t *num_nodes_ret);
extern ccs_error_t
ccs_literal_get_value(ccs_expression_t expression,
ccs_datum_t *value_ret);
extern ccs_error_t
ccs_variable_get_hyperparameter(ccs_expression_t expression,
ccs_hyperparameter_t *hyperparameter_ret);
extern ccs_error_t
ccs_expression_eval(ccs_expression_t expression,
ccs_context_t context,
......
......@@ -33,9 +33,14 @@ ccs_objective_space_add_variable(ccs_objective_space_t objective_space,
extern ccs_error_t
ccs_objective_space_add_objective(ccs_objective_space_t objective_space,
ccs_object_t objective,
ccs_expression_t objective,
ccs_objective_type_t type);
extern ccs_error_t
ccs_objective_space_get_objective(ccs_objective_space_t objective_space,
ccs_expression_t *objective_ret,
ccs_objective_type_t *type_ret);
#ifdef __cplusplus
}
#endif
......
This diff is collapsed.
......@@ -23,7 +23,18 @@ struct _ccs_expression_s {
struct _ccs_expression_data_s {
ccs_expression_type_t type;
size_t num_nodes;
ccs_datum_t *nodes;
ccs_expression_t *nodes;
};
struct _ccs_expression_literal_data_s {
_ccs_expression_data_t expr;
ccs_datum_t value;
};
typedef struct _ccs_expression_literal_data_s _ccs_expression_literal_data_t;
struct _ccs_expression_variable_data_s {
_ccs_expression_data_t expr;
ccs_hyperparameter_t hyperparameter;
};
typedef struct _ccs_expression_variable_data_s _ccs_expression_variable_data_t;
#endif //_EXPRESSION_INTERNAL_H
......@@ -675,11 +675,12 @@ void test_in() {
void
test_compound() {
ccs_expression_t expression1, expression2;
ccs_datum_t result;
ccs_error_t err;
ccs_datum_t nodes[3];
size_t num_nodes_ret;
ccs_expression_t expression1, expression2;
ccs_datum_t result;
ccs_error_t err;
ccs_expression_t nodes[3];
size_t num_nodes_ret;
ccs_expression_type_t type;
err = ccs_create_binary_expression(CCS_ADD, ccs_float(3.0), ccs_int(1),
&expression1);
......@@ -687,10 +688,20 @@ test_compound() {
err = ccs_expression_get_nodes(expression1, 3, nodes, &num_nodes_ret);
assert( err == CCS_SUCCESS );
assert( num_nodes_ret == 2 );
assert( nodes[0].type == CCS_FLOAT );
assert( nodes[0].value.f == 3.0 );
assert( nodes[1].type == CCS_INTEGER );
assert( nodes[1].value.i == 1 );
err = ccs_expression_get_type(nodes[0], &type);
assert( err == CCS_SUCCESS );
assert( type == CCS_LITERAL );
err = ccs_literal_get_value(nodes[0], &result);
assert( err == CCS_SUCCESS );
assert( result.type == CCS_FLOAT );
assert( result.value.f == 3.0 );
err = ccs_expression_get_type(nodes[1], &type);
assert( err == CCS_SUCCESS );
assert( type == CCS_LITERAL );
err = ccs_literal_get_value(nodes[1], &result);
assert( err == CCS_SUCCESS );
assert( result.type == CCS_INTEGER );
assert( result.value.i == 1 );
err = ccs_create_binary_expression(CCS_MULTIPLY, ccs_float(2.0),
ccs_object(expression1), &expression2);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment