eolian: expr eval APIs now take units

This commit is contained in:
Daniel Kolesa 2017-05-25 16:18:10 +02:00
parent 4125e3f34b
commit c234b5ee69
8 changed files with 52 additions and 46 deletions

View File

@ -69,12 +69,12 @@ _type_exists(const char *tname, Eina_Strbuf *buf)
}
static void
_append_defval(Eina_Strbuf *buf, const Eolian_Expression *exp,
const Eolian_Type *tp)
_append_defval(const Eolian_Unit *src, Eina_Strbuf *buf,
const Eolian_Expression *exp, const Eolian_Type *tp)
{
if (exp)
{
Eolian_Value val = eolian_expression_eval_type(exp, tp);
Eolian_Value val = eolian_expression_eval_type(src, exp, tp);
Eina_Stringshare *lit = eolian_expression_value_to_literal(&val);
if (lit)
{
@ -115,9 +115,9 @@ _append_defval(Eina_Strbuf *buf, const Eolian_Expression *exp,
}
static void
_gen_func(const Eolian_Class *cl, const Eolian_Function *fid,
Eolian_Function_Type ftype, Eina_Strbuf *buf,
const Eolian_Implement *impl, Eina_Strbuf *lbuf)
_gen_func(const Eolian_Unit *src, const Eolian_Class *cl,
const Eolian_Function *fid, Eolian_Function_Type ftype,
Eina_Strbuf *buf, const Eolian_Implement *impl, Eina_Strbuf *lbuf)
{
Eina_Bool is_empty = eolian_implement_is_empty(impl, ftype);
Eina_Bool is_auto = eolian_implement_is_auto(impl, ftype);
@ -248,7 +248,7 @@ _gen_func(const Eolian_Class *cl, const Eolian_Function *fid,
}
else if ((ftype != EOLIAN_PROP_SET) && dfv)
{
Eolian_Value val = eolian_expression_eval(dfv, EOLIAN_MASK_ALL);
Eolian_Value val = eolian_expression_eval(src, dfv, EOLIAN_MASK_ALL);
if (val.type)
{
Eina_Stringshare *vals = eolian_expression_value_to_literal(&val);
@ -359,7 +359,7 @@ _gen_func(const Eolian_Class *cl, const Eolian_Function *fid,
if (rtp)
{
eina_strbuf_append(buf, " return ");
_append_defval(buf, def_ret, rtp);
_append_defval(src, buf, def_ret, rtp);
eina_strbuf_append(buf, ";\n");
}
eina_strbuf_append(buf, "}\n\n");
@ -420,7 +420,7 @@ _gen_func(const Eolian_Class *cl, const Eolian_Function *fid,
if (strcmp(rtpn, "void"))
{
eina_strbuf_append_printf(buf, ", %s, ", rtpn);
_append_defval(buf, def_ret, rtp);
_append_defval(src, buf, def_ret, rtp);
}
if (has_params)
{
@ -682,14 +682,14 @@ eo_gen_source_gen(const Eolian_Unit *src,
{
case EOLIAN_PROP_GET:
case EOLIAN_PROP_SET:
_gen_func(cl, fid, ftype, buf, imp, lbuf);
_gen_func(src, cl, fid, ftype, buf, imp, lbuf);
break;
case EOLIAN_PROPERTY:
_gen_func(cl, fid, EOLIAN_PROP_SET, buf, imp, lbuf);
_gen_func(cl, fid, EOLIAN_PROP_GET, buf, imp, lbuf);
_gen_func(src, cl, fid, EOLIAN_PROP_SET, buf, imp, lbuf);
_gen_func(src, cl, fid, EOLIAN_PROP_GET, buf, imp, lbuf);
break;
default:
_gen_func(cl, fid, EOLIAN_METHOD, buf, imp, lbuf);
_gen_func(src, cl, fid, EOLIAN_METHOD, buf, imp, lbuf);
}
}
eina_iterator_free(itr);

View File

@ -97,7 +97,7 @@ _type_generate(const Eolian_Unit *src, const Eolian_Typedecl *tp,
else
{
Eolian_Value val =
eolian_expression_eval(vale, EOLIAN_MASK_INT);
eolian_expression_eval(src, vale, EOLIAN_MASK_INT);
const char *lit = eolian_expression_value_to_literal(&val);
eina_strbuf_append_printf(buf, " %s = %s", membn, lit);
const char *exp = eolian_expression_serialize(vale);
@ -164,7 +164,7 @@ _var_generate(const Eolian_Unit *src, const Eolian_Variable *vr, Eina_Bool legac
eina_strbuf_prepend_printf(buf, "#ifndef %s\n", fn);
eina_strbuf_append_printf(buf, "#define %s ", fn);
const Eolian_Expression *vv = eolian_variable_value_get(vr);
Eolian_Value val = eolian_expression_eval_type(vv, vt);
Eolian_Value val = eolian_expression_eval_type(src, vv, vt);
Eina_Stringshare *lit = eolian_expression_value_to_literal(&val);
eina_strbuf_append(buf, lit);
Eina_Stringshare *exp = eolian_expression_serialize(vv);
@ -239,7 +239,7 @@ void eo_gen_types_header_gen(const Eolian_Unit *src,
}
}
void eo_gen_types_source_gen(const Eolian_Unit *src EINA_UNUSED,
void eo_gen_types_source_gen(const Eolian_Unit *src,
const char *eof, Eina_Strbuf *buf)
{
const Eolian_Declaration *decl;
@ -274,7 +274,7 @@ void eo_gen_types_source_gen(const Eolian_Unit *src EINA_UNUSED,
eina_stringshare_del(ct);
free(fn);
Eolian_Value val = eolian_expression_eval_type(vv, vt);
Eolian_Value val = eolian_expression_eval_type(src, vv, vt);
Eina_Stringshare *lit = eolian_expression_value_to_literal(&val);
eina_strbuf_append(buf, lit);
eina_strbuf_append_char(buf, ';');

View File

@ -356,8 +356,8 @@ ffi.cdef [[
const char *eolian_type_free_func_get(const Eolian_Type *tp);
const char *eolian_typedecl_free_func_get(const Eolian_Typedecl *tp);
Eolian_Value_t eolian_expression_eval(const Eolian_Expression *expr, Eolian_Expression_Mask m);
Eolian_Value_t eolian_expression_eval_type(const Eolian_Expression *expr, const Eolian_Type *type);
Eolian_Value_t eolian_expression_eval(const Eolian_Unit *unit, const Eolian_Expression *expr, Eolian_Expression_Mask m);
Eolian_Value_t eolian_expression_eval_type(const Eolian_Unit *unit, const Eolian_Expression *expr, const Eolian_Type *type);
const char *eolian_expression_value_to_literal(const Eolian_Value *v);
const char *eolian_expression_serialize(const Eolian_Expression *expr);
Eolian_Expression_Type eolian_expression_type_get(const Eolian_Expression *expr);
@ -1309,15 +1309,15 @@ M.unary_operator = {
M.Expression = ffi.metatype("Eolian_Expression", {
__index = {
eval = function(self, mask)
eval = function(self, unit, mask)
mask = mask or emask.ALL
local v = eolian.eolian_expression_eval(self, mask)
local v = eolian.eolian_expression_eval(unit, self, mask)
if v == nil then return nil end
return ffi.cast("Eolian_Value*", v)
end,
eval_type = function(self, tp)
local v = eolian.eolian_expression_eval_type(self, tp)
eval_type = function(self, unit, tp)
local v = eolian.eolian_expression_eval_type(unit, self, tp)
if v == nil then return nil end
return ffi.cast("Eolian_Value*", v)
end,

View File

@ -1917,6 +1917,7 @@ EAPI Eina_Stringshare *eolian_type_free_func_get(const Eolian_Type *tp);
/*
* @brief Evaluate an Eolian expression.
*
* @param[in] unit the unit to look in
* @param[in] expr the expression.
* @param[in] mask the mask of allowed values (can combine with bitwise OR).
* @return the value, its type is set to EOLIAN_EXPR_UNKNOWN on error.
@ -1926,11 +1927,12 @@ EAPI Eina_Stringshare *eolian_type_free_func_get(const Eolian_Type *tp);
*
* @ingroup Eolian
*/
EAPI Eolian_Value eolian_expression_eval(const Eolian_Expression *expr, Eolian_Expression_Mask m);
EAPI Eolian_Value eolian_expression_eval(const Eolian_Unit *unit, const Eolian_Expression *expr, Eolian_Expression_Mask m);
/*
* @brief Evaluate an Eolian expression given a type instead of a mask.
*
* @param[in] unit the unit to look in
* @param[in] expr the expression.
* @param[in] type the type the expression is assigned to.
* @return the value, its type is set to EOLIAN_EXPR_UNKNOWN on error.
@ -1940,7 +1942,7 @@ EAPI Eolian_Value eolian_expression_eval(const Eolian_Expression *expr, Eolian_E
*
* @ingroup Eolian
*/
EAPI Eolian_Value eolian_expression_eval_type(const Eolian_Expression *expr, const Eolian_Type *type);
EAPI Eolian_Value eolian_expression_eval_type(const Eolian_Unit *unit, const Eolian_Expression *expr, const Eolian_Type *type);
/*
* @brief Convert the result of expression evaluation to a literal as in how

View File

@ -6,12 +6,13 @@
#include "eo_lexer.h"
EAPI Eolian_Value
eolian_expression_eval(const Eolian_Expression *expr, Eolian_Expression_Mask m)
eolian_expression_eval(const Eolian_Unit *unit, const Eolian_Expression *expr,
Eolian_Expression_Mask m)
{
Eolian_Value err;
err.type = EOLIAN_EXPR_UNKNOWN;
EINA_SAFETY_ON_NULL_RETURN_VAL(expr, err);
return database_expr_eval(NULL, expr, m);
return database_expr_eval(unit, expr, m);
}
static Eolian_Value
@ -90,13 +91,14 @@ _eval_type(const Eolian_Unit *unit, const Eolian_Expression *expr,
}
EAPI Eolian_Value
eolian_expression_eval_type(const Eolian_Expression *expr,
eolian_expression_eval_type(const Eolian_Unit *unit,
const Eolian_Expression *expr,
const Eolian_Type *type)
{
Eolian_Value err;
err.type = EOLIAN_EXPR_UNKNOWN;
EINA_SAFETY_ON_NULL_RETURN_VAL(expr, err);
return _eval_type(NULL, expr, type);
return _eval_type(unit, expr, type);
}
static void

View File

@ -165,7 +165,8 @@ _etype_to_str(const Eolian_Typedecl *tp, Eina_Strbuf *buf)
eina_strbuf_append(buf, ef->name);
if (ef->value)
{
Eolian_Value val = eolian_expression_eval(ef->value,
/* FIXME: pass unit properly */
Eolian_Value val = eolian_expression_eval(NULL, ef->value,
EOLIAN_MASK_INT);
const char *ret;
eina_strbuf_append(buf, " = ");

View File

@ -174,10 +174,11 @@ _validate_expr(const Eolian_Expression *expr,
const Eolian_Type *tp, Eolian_Expression_Mask msk)
{
Eolian_Value val;
/* FIXME: pass unit properly */
if (tp)
val = eolian_expression_eval_type(expr, tp);
val = eolian_expression_eval_type(NULL, expr, tp);
else
val = eolian_expression_eval(expr, msk);
val = eolian_expression_eval(NULL, expr, msk);
return (val.type != EOLIAN_EXPR_UNKNOWN);
}

View File

@ -557,7 +557,7 @@ START_TEST(eolian_simple_parsing)
fail_if(strcmp(eolian_type_name_get(tp), "bool"));
expr = eolian_function_return_default_value_get(fid, EOLIAN_PROP_SET);
fail_if(!expr);
v = eolian_expression_eval(expr, EOLIAN_MASK_BOOL);
v = eolian_expression_eval(unit, expr, EOLIAN_MASK_BOOL);
fail_if(v.type != EOLIAN_EXPR_BOOL);
/* Get return */
tp = eolian_function_return_type_get(fid, EOLIAN_PROP_GET);
@ -573,7 +573,7 @@ START_TEST(eolian_simple_parsing)
fail_if(strcmp(eolian_parameter_name_get(param), "value"));
expr = eolian_parameter_default_value_get(param);
fail_if(!expr);
v = eolian_expression_eval(expr, EOLIAN_MASK_INT);
v = eolian_expression_eval(unit, expr, EOLIAN_MASK_INT);
fail_if(v.type != EOLIAN_EXPR_INT);
fail_if(v.value.i != 100);
@ -598,7 +598,7 @@ START_TEST(eolian_simple_parsing)
eina_stringshare_del(string);
expr = eolian_function_return_default_value_get(fid, EOLIAN_METHOD);
fail_if(!expr);
v = eolian_expression_eval(expr, EOLIAN_MASK_NULL);
v = eolian_expression_eval(unit, expr, EOLIAN_MASK_NULL);
fail_if(v.type != EOLIAN_EXPR_NULL);
fail_if(eolian_function_is_legacy_only(fid, EOLIAN_METHOD));
@ -619,7 +619,7 @@ START_TEST(eolian_simple_parsing)
fail_if(strcmp(eolian_parameter_name_get(param), "c"));
expr = eolian_parameter_default_value_get(param);
fail_if(!expr);
v = eolian_expression_eval(expr, EOLIAN_MASK_FLOAT);
v = eolian_expression_eval(unit, expr, EOLIAN_MASK_FLOAT);
fail_if(v.type != EOLIAN_EXPR_DOUBLE);
fail_if(v.value.d != 1337.6);
fail_if(!(eina_iterator_next(iter, (void**)&param)));
@ -788,7 +788,7 @@ START_TEST(eolian_var)
fail_if(!(name = eolian_type_name_get(type)));
fail_if(strcmp(name, "int"));
fail_if(!(exp = eolian_variable_value_get(var)));
v = eolian_expression_eval_type(exp, type);
v = eolian_expression_eval_type(unit, exp, type);
fail_if(v.type != EOLIAN_EXPR_INT);
fail_if(v.value.i != 5);
@ -800,7 +800,7 @@ START_TEST(eolian_var)
fail_if(!(name = eolian_type_name_get(type)));
fail_if(strcmp(name, "float"));
fail_if(!(exp = eolian_variable_value_get(var)));
v = eolian_expression_eval_type(exp, type);
v = eolian_expression_eval_type(unit, exp, type);
fail_if(v.type != EOLIAN_EXPR_FLOAT);
fail_if(((int)v.value.f) != 10);
@ -852,7 +852,7 @@ START_TEST(eolian_enum)
fail_if(!(field = eolian_typedecl_enum_field_get(tdl, "first")));
fail_if(!(exp = eolian_typedecl_enum_field_value_get(field, EINA_FALSE)));
v = eolian_expression_eval(exp, EOLIAN_MASK_ALL);
v = eolian_expression_eval(unit, exp, EOLIAN_MASK_ALL);
fail_if(v.type != EOLIAN_EXPR_INT);
fail_if(v.value.i != 0);
@ -861,7 +861,7 @@ START_TEST(eolian_enum)
fail_if(!(field = eolian_typedecl_enum_field_get(tdl, "baz")));
fail_if(!(exp = eolian_typedecl_enum_field_value_get(field, EINA_FALSE)));
v = eolian_expression_eval(exp, EOLIAN_MASK_ALL);
v = eolian_expression_eval(unit, exp, EOLIAN_MASK_ALL);
fail_if(v.type != EOLIAN_EXPR_INT);
fail_if(v.value.i != 15);
@ -870,7 +870,7 @@ START_TEST(eolian_enum)
fail_if(!(field = eolian_typedecl_enum_field_get(tdl, "foo")));
fail_if(!(exp = eolian_typedecl_enum_field_value_get(field, EINA_FALSE)));
v = eolian_expression_eval(exp, EOLIAN_MASK_ALL);
v = eolian_expression_eval(unit, exp, EOLIAN_MASK_ALL);
fail_if(v.type != EOLIAN_EXPR_INT);
fail_if(v.value.i != 15);
@ -882,19 +882,19 @@ START_TEST(eolian_enum)
fail_if(!(field = eolian_typedecl_enum_field_get(tdl, "flag1")));
fail_if(!(exp = eolian_typedecl_enum_field_value_get(field, EINA_FALSE)));
v = eolian_expression_eval(exp, EOLIAN_MASK_ALL);
v = eolian_expression_eval(unit, exp, EOLIAN_MASK_ALL);
fail_if(v.type != EOLIAN_EXPR_INT);
fail_if(v.value.i != (1 << 0));
fail_if(!(field = eolian_typedecl_enum_field_get(tdl, "flag2")));
fail_if(!(exp = eolian_typedecl_enum_field_value_get(field, EINA_FALSE)));
v = eolian_expression_eval(exp, EOLIAN_MASK_ALL);
v = eolian_expression_eval(unit, exp, EOLIAN_MASK_ALL);
fail_if(v.type != EOLIAN_EXPR_INT);
fail_if(v.value.i != (1 << 1));
fail_if(!(field = eolian_typedecl_enum_field_get(tdl, "flag3")));
fail_if(!(exp = eolian_typedecl_enum_field_value_get(field, EINA_FALSE)));
v = eolian_expression_eval(exp, EOLIAN_MASK_ALL);
v = eolian_expression_eval(unit, exp, EOLIAN_MASK_ALL);
fail_if(v.type != EOLIAN_EXPR_INT);
fail_if(v.value.i != (1 << 2));
@ -912,14 +912,14 @@ START_TEST(eolian_enum)
fail_if(!(name = eolian_type_name_get(type)));
fail_if(strcmp(name, "Baz"));
fail_if(!(exp = eolian_variable_value_get(var)));
v = eolian_expression_eval(exp, EOLIAN_MASK_ALL);
v = eolian_expression_eval(unit, exp, EOLIAN_MASK_ALL);
fail_if(v.type != EOLIAN_EXPR_INT);
fail_if(v.value.i != (1 << 0));
fail_if(!(var = eolian_variable_constant_get_by_name(unit, "Pants")));
fail_if(eolian_variable_type_get(var) != EOLIAN_VAR_CONSTANT);
fail_if(!(exp = eolian_variable_value_get(var)));
v = eolian_expression_eval(exp, EOLIAN_MASK_ALL);
v = eolian_expression_eval(unit, exp, EOLIAN_MASK_ALL);
fail_if(v.type != EOLIAN_EXPR_INT);
fail_if(v.value.i != 5);