mark a new function FindVariable()

This commit is contained in:
Tomasz Sowa 2021-10-25 21:33:21 +02:00
parent 03a76d35ad
commit a61fd2dcd0
1 changed files with 166 additions and 51 deletions

View File

@ -309,7 +309,7 @@ private:
void PrintLastSpaceField(pt::Space * space, Var<StreamType> & result, std::vector<Var<StreamType>> & parameters); void PrintLastSpaceField(pt::Space * space, Var<StreamType> & result, std::vector<Var<StreamType>> & parameters);
void CallSpaceObjectForLastField(Var<StreamType> & result, std::vector<Var<StreamType>> & parameters, pt::Space * space); void CallSpaceObjectForLastField(Var<StreamType> & result, std::vector<Var<StreamType>> & parameters, pt::Space * space);
pt::Space * CallSpaceObjectForMiddleField(const std::wstring & root_space_name, std::vector<std::wstring> & fields, size_t field_index, pt::Space * space); //pt::Space * CallSpaceObjectForMiddleField(const std::wstring & root_space_name, std::vector<std::wstring> & fields, size_t field_index, pt::Space * space);
void CallSpaceTableForLastField(morm::SpaceWrapper & space_wrapper, Var<StreamType> & result, std::vector<Var<StreamType>> & parameters, void CallSpaceTableForLastField(morm::SpaceWrapper & space_wrapper, Var<StreamType> & result, std::vector<Var<StreamType>> & parameters,
pt::Space * space, size_t model_wrapper_space_table_index); pt::Space * space, size_t model_wrapper_space_table_index);
@ -317,7 +317,7 @@ private:
pt::Space * CallSpaceTableForMiddleField(morm::SpaceWrapper & space_wrapper, const std::wstring & root_space_name, std::vector<std::wstring> & fields, pt::Space * CallSpaceTableForMiddleField(morm::SpaceWrapper & space_wrapper, const std::wstring & root_space_name, std::vector<std::wstring> & fields,
size_t field_index, pt::Space * space, size_t model_wrapper_space_table_index); size_t field_index, pt::Space * space, size_t model_wrapper_space_table_index);
bool CallSpace(FindHelper & find_helper, std::vector<std::wstring> & fields, Var<StreamType> & result, std::vector<Var<StreamType>> & parameters); bool CallSpace(morm::SpaceWrapper * space_wrapper, const std::wstring & name, size_t & field_index, std::vector<std::wstring> & fields, Var<StreamType> & result, std::vector<Var<StreamType>> & parameters);
#ifdef EZC_HAS_MORM_LIBRARY #ifdef EZC_HAS_MORM_LIBRARY
bool CallModelField(morm::Model & model, const std::wstring & field, Var<StreamType> & result, std::vector<Var<StreamType>> & parameters, const StreamType & in_stream); bool CallModelField(morm::Model & model, const std::wstring & field, Var<StreamType> & result, std::vector<Var<StreamType>> & parameters, const StreamType & in_stream);
@ -329,6 +329,14 @@ private:
#endif #endif
Var<StreamType> * FindInScope(const std::wstring & name);
Var<StreamType> * FindVariable(const std::wstring & name, std::vector<std::wstring> & fields, size_t & field_index, std::vector<Var<StreamType>> & parameters);
bool EvaluateVariable(const std::wstring & name, Var<StreamType> & var, std::vector<std::wstring> & fields, size_t & field_index, std::vector<Var<StreamType>> & parameters, Var<StreamType> & result);
bool CallVariable(std::wstring & name, bool CallVariable(std::wstring & name,
std::vector<std::wstring> & fields, std::vector<std::wstring> & fields,
size_t & field_index, size_t & field_index,
@ -1267,19 +1275,19 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallSpace
} }
template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream> //template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream>
pt::Space * Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallSpaceObjectForMiddleField(const std::wstring & root_space_name, std::vector<std::wstring> & fields, size_t field_index, pt::Space * space) //pt::Space * Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallSpaceObjectForMiddleField(const std::wstring & root_space_name, std::vector<std::wstring> & fields, size_t field_index, pt::Space * space)
{ //{
std::wstring & next_field = fields[field_index]; // std::wstring & next_field = fields[field_index];
space = space->get_space(next_field); // space = space->get_space(next_field);
//
if( !space && !IsTestingFunctionExistence() ) // if( !space && !IsTestingFunctionExistence() )
{ // {
CreateMsg(L"cannot find space field: ", root_space_name, fields, field_index + 1); // CreateMsg(L"cannot find space field: ", root_space_name, fields, field_index + 1);
} // }
//
return space; // return space;
} //}
@ -1375,13 +1383,11 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::PrintLast
template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream> template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream>
bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallSpace( bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallSpace(
FindHelper & find_helper, std::vector<std::wstring> & fields, Var<StreamType> & result, std::vector<Var<StreamType>> & parameters) morm::SpaceWrapper * space_wrapper, const std::wstring & name, size_t & field_index, std::vector<std::wstring> & fields, Var<StreamType> & result, std::vector<Var<StreamType>> & parameters)
{ {
morm::SpaceWrapper * space_wrapper = find_helper.wrapper->space_wrapper;
pt::Space * space = space_wrapper->get_space(); pt::Space * space = space_wrapper->get_space();
//last_res = false;
size_t field_index = find_helper.field_index;
size_t model_wrapper_space_table_index = 0; size_t model_wrapper_space_table_index = 0;
bool status = false;
/* /*
* *
@ -1392,34 +1398,38 @@ bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallSpace
*/ */
while( (field_index < fields.size() + 1) && space ) while( (field_index < fields.size() + 1) && space )
{ {
bool is_last_field = (field_index == fields.size());
if( space->is_object() ) if( space->is_object() )
{ {
if( is_last_field ) std::wstring & next_field = fields[field_index];
pt::Space * next_space = space->get_space(next_field);
if( !next_space )
{ {
CallSpaceObjectForLastField(result, parameters, space); CallSpaceObjectForLastField(result, parameters, space);
} status = true;
else
{
space = CallSpaceObjectForMiddleField(*find_helper.fun_name, fields, field_index, space);
} }
field_index += 1; space = next_space;
if( space )
field_index += 1;
} }
else else
if( space->is_table() ) if( space->is_table() )
{ {
bool is_last_field = (field_index == fields.size());
if( model_wrapper_space_table_index < space_wrapper->space_indices_table_size() ) if( model_wrapper_space_table_index < space_wrapper->space_indices_table_size() )
{ {
if( is_last_field ) if( is_last_field )
{ {
CallSpaceTableForLastField(*space_wrapper, result, parameters, space, model_wrapper_space_table_index); CallSpaceTableForLastField(*space_wrapper, result, parameters, space, model_wrapper_space_table_index);
field_index += 1; field_index += 1;
status = true;
} }
else else
{ {
space = CallSpaceTableForMiddleField(*space_wrapper, *find_helper.fun_name, fields, field_index, space, model_wrapper_space_table_index); space = CallSpaceTableForMiddleField(*space_wrapper, name, fields, field_index, space, model_wrapper_space_table_index);
// increment field_index only if next_field is 'this' and space is not an object // increment field_index only if next_field is 'this' and space is not an object
if( space && !space->is_object() ) if( space && !space->is_object() )
@ -1435,36 +1445,41 @@ bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallSpace
} }
else else
{ {
CreateMsg(L"", *find_helper.fun_name, fields, field_index, L" exceeded the maximum number of fields for a space object"); CreateMsg(L"", name, fields, field_index, L" exceeded the maximum number of fields for a space object");
space = nullptr; space = nullptr;
} }
} }
else else
{ {
if( is_last_field ) PrintLastSpaceField(space, result, parameters);
{ status = true;
if( !IsTestingFunctionExistence() ) // increment field_index here?
{ break;
PrintLastSpaceField(space, result, parameters);
}
field_index += 1; // if( is_last_field )
} // {
else // if( !IsTestingFunctionExistence() )
{ // {
if( !IsTestingFunctionExistence() ) // PrintLastSpaceField(space, result, parameters);
{ // }
CreateMsg(L"", *find_helper.fun_name, fields, field_index, L" of a space is not an object nor a table"); //
} // field_index += 1;
// }
space = nullptr; // else
} // {
// if( !IsTestingFunctionExistence() )
// {
// CreateMsg(L"", name, fields, field_index, L" of a space is not an object nor a table");
// }
//
// space = nullptr;
// }
} }
model_wrapper_space_table_index += 1; model_wrapper_space_table_index += 1;
} }
return space != nullptr; return status;
} }
@ -1603,10 +1618,10 @@ bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallWrapp
bool all_fields_known = (find_helper.field_index == fields.size()); bool all_fields_known = (find_helper.field_index == fields.size());
bool last_field_not_known = (find_helper.field_index + 1 == fields.size()); bool last_field_not_known = (find_helper.field_index + 1 == fields.size());
if( find_helper.wrapper->space_wrapper ) // if( find_helper.wrapper->space_wrapper )
{ // {
found = CallSpace(find_helper, fields, result, parameters); // found = CallSpace(find_helper, fields, result, parameters);
} // }
if( find_helper.wrapper->date ) if( find_helper.wrapper->date )
{ {
@ -1825,6 +1840,106 @@ bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallVaria
} }
template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream>
Var<StreamType> * Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::FindInScope(const std::wstring & name)
{
// here we should look in scopes (not implemented yet)
if( pvars )
{
return pvars->find(name);
}
return nullptr;
}
template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream>
Var<StreamType> * Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::FindVariable(
const std::wstring & name, std::vector<std::wstring> & fields, size_t & field_index, std::vector<Var<StreamType>> & parameters)
{
Var<StreamType> * var = nullptr;
std::vector<Var<StreamType>> res;
res.resize(1);
bool was_evaluated = false;
do
{
const std::wstring * pname = (field_index == 0)? &name : &fields[field_index - 1];
Var<StreamType> * var = FindInScope(*pname);
if( var )
{
if( was_evaluated )
{
Var<StreamType> improve_me;
EvaluateVariable(*var, res, improve_me);
res[0] = improve_me;
}
else
{
if( EvaluateVariable(*var, parameters, res[0]) )
was_evaluated = true;
}
}
field_index += 1;
}
while( field_index < fields.size() + 1 && var )
return var;
}
template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream>
bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::EvaluateVariable(
const std::wstring & name, Var<StreamType> & var, std::vector<std::wstring> & fields, size_t & field_index, std::vector<Var<StreamType>> & parameters, Var<StreamType> & result)
{
StreamType fake_stream;
bool status = false;
if( var.type == Var<StreamType>::TYPE_FUNCTION )
{
if( var.user_function )
{
CallFunction(var.user_function, result, parameters, fake_stream);
status = true;
}
else
{
// put some log that variable.user_function is null?
}
}
if( var.type == Var<StreamType>::TYPE_MODEL )
{
if( var.model )
{
//status = CallModel(*var.model, name, fields, field_index, parameters, result);
}
else
{
// put some log that variable.user_function is null?
}
}
if( var.type == Var<StreamType>::TYPE_SPACE_WRAPPER )
{
if( var.space_wrapper )
{
status = CallSpace(*var.space_wrapper, name, field_index, fields, result, parameters);
}
else
{
// put some log that variable.user_function is null?
}
}
return status;
}
/* /*
template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream> template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream>
bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallVariable( bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallVariable(