Env<>::in is Var<> now and we pass last result as in into next field

This commit is contained in:
Tomasz Sowa 2021-11-06 19:07:39 +01:00
parent bb0cc59eca
commit e2a8729a2f
2 changed files with 117 additions and 73 deletions

View File

@ -143,8 +143,8 @@ struct Env
//const std::wstring & par; //const std::wstring & par;
// an input stream used in [filter] statement // an input stream used in [filter] statement
// if there is other statement than [filter] then this is an empty stream // and when methods/functions are chained
const StreamType & in; Var<StreamType> & in;
// indicates that this function is from [for ...] statement // indicates that this function is from [for ...] statement
bool is_for; bool is_for;
@ -189,7 +189,7 @@ struct Env
Env(Var<StreamType> & result, Env(Var<StreamType> & result,
std::vector<Var<StreamType>> & pars, std::vector<Var<StreamType>> & pars,
const StreamType & input_stream, Var<StreamType> & input_stream,
Stack & s, Stack & s,
const Item & item_) : out(result.stream), res(result), params(pars), in(input_stream), stack(s), item(item_) const Item & item_) : out(result.stream), res(result), params(pars), in(input_stream), stack(s), item(item_)
{ {

View File

@ -176,23 +176,31 @@ private:
std::vector<std::wstring> & fields; std::vector<std::wstring> & fields;
size_t field_index; size_t field_index;
std::vector<Var<StreamType>> & parameters; std::vector<Var<StreamType>> & parameters;
Var<StreamType> & result; Var<StreamType> * input;
Var<StreamType> * result;
bool found; bool found;
bool evaluate_last;
const std::wstring * previous_name; const std::wstring * previous_name;
Var<StreamType> * previous_result; morm::Model * previous_model;
morm::ModelContainerWrapper * previous_model_container_wrapper;
morm::SpaceWrapper * previous_space_wrapper;
size_t nested_calls; size_t nested_calls;
FindHelper(const std::wstring & name, std::vector<std::wstring> & fields, std::vector<Var<StreamType>> & parameters, Var<StreamType> & result): FindHelper(
name(name), fields(fields), parameters(parameters), result(result) const std::wstring & name,
std::vector<std::wstring> & fields,
std::vector<Var<StreamType>> & parameters
): name(name), fields(fields), parameters(parameters)
{ {
field_index = 0; field_index = 0;
found = false; found = false;
evaluate_last = true;
previous_name = nullptr; previous_name = nullptr;
previous_result = nullptr;
nested_calls = 0; nested_calls = 0;
input = nullptr;
result = nullptr;
previous_model = nullptr;
previous_model_container_wrapper = nullptr;
previous_space_wrapper = nullptr;
} }
const std::wstring & current_name() const std::wstring & current_name()
@ -202,9 +210,6 @@ private:
bool is_last_field() bool is_last_field()
{ {
if( field_index == 0 )
return true;
return field_index == fields.size(); return field_index == fields.size();
} }
@ -370,7 +375,7 @@ private:
void FindVariable(FindHelper & find_helper); void FindVariable(FindHelper & find_helper);
void EvaluateVariable(FindHelper & find_helper); void EvaluateVariable(FindHelper & find_helper);
void EvaluateModelField(FindHelper & find_helper); bool EvaluateModelField(FindHelper & find_helper);
void EvaluateFunction(typename Var<StreamType>::UserFunction user_function, FindHelper & find_helper); void EvaluateFunction(typename Var<StreamType>::UserFunction user_function, FindHelper & find_helper);
@ -1047,8 +1052,7 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallFunct
{ {
// if( find_helper.parameters.empty() ) // if( find_helper.parameters.empty() )
{ {
StreamType fake_stream; Env<StreamType> info(*find_helper.result, find_helper.parameters, *find_helper.input, stack_tab[stack_index-1], *stack_tab[stack_index-1].item);
Env<StreamType> info(find_helper.result, find_helper.parameters, fake_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item);
CallFunction(function, info); CallFunction(function, info);
} }
// else // else
@ -1111,14 +1115,14 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::DumpSpace
if( dump_space ) if( dump_space )
{ {
space.serialize_to_space_stream(find_helper.result.stream, pretty); space.serialize_to_space_stream(find_helper.result->stream, pretty);
find_helper.result.type = Var<StreamType>::TYPE_STREAM; find_helper.result->type = Var<StreamType>::TYPE_STREAM;
} }
else else
if( dump_json ) if( dump_json )
{ {
space.serialize_to_json_stream(find_helper.result.stream, pretty); space.serialize_to_json_stream(find_helper.result->stream, pretty);
find_helper.result.type = Var<StreamType>::TYPE_STREAM; find_helper.result->type = Var<StreamType>::TYPE_STREAM;
} }
} }
} }
@ -1167,23 +1171,23 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::PrintDate
if( only_date ) if( only_date )
{ {
find_helper.result.type = Var<StreamType>::TYPE_STREAM; find_helper.result->type = Var<StreamType>::TYPE_STREAM;
date.SerializeYearMonthDay(find_helper.result.stream, is_roman); date.SerializeYearMonthDay(find_helper.result->stream, is_roman);
} }
else else
if( only_time ) if( only_time )
{ {
find_helper.result.type = Var<StreamType>::TYPE_STREAM; find_helper.result->type = Var<StreamType>::TYPE_STREAM;
if( is_no_sec ) if( is_no_sec )
date.SerializeHourMin(find_helper.result.stream); date.SerializeHourMin(find_helper.result->stream);
else else
date.SerializeHourMinSec(find_helper.result.stream); date.SerializeHourMinSec(find_helper.result->stream);
} }
else else
{ {
find_helper.result.type = Var<StreamType>::TYPE_STREAM; find_helper.result->type = Var<StreamType>::TYPE_STREAM;
date.Serialize(find_helper.result.stream, is_roman, !is_no_sec); date.Serialize(find_helper.result->stream, is_roman, !is_no_sec);
} }
} }
@ -1358,14 +1362,14 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallSpace
if( HasParam(find_helper.parameters, L"index") ) if( HasParam(find_helper.parameters, L"index") )
{ {
find_helper.result.stream << space_wrapper.get_space_iterator_value(model_wrapper_space_table_index); find_helper.result->stream << space_wrapper.get_space_iterator_value(model_wrapper_space_table_index);
find_helper.result.type = Var<StreamType>::TYPE_STREAM; find_helper.result->type = Var<StreamType>::TYPE_STREAM;
} }
if( HasParam(find_helper.parameters, L"index-one") ) if( HasParam(find_helper.parameters, L"index-one") )
{ {
find_helper.result.stream << (space_wrapper.get_space_iterator_value(model_wrapper_space_table_index) + 1); find_helper.result->stream << (space_wrapper.get_space_iterator_value(model_wrapper_space_table_index) + 1);
find_helper.result.type = Var<StreamType>::TYPE_STREAM; find_helper.result->type = Var<StreamType>::TYPE_STREAM;
} }
} }
} }
@ -1407,15 +1411,15 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::PrintLast
if( no_escape ) if( no_escape )
{ {
//pt::WTextStream str; //pt::WTextStream str;
space.serialize_to_string(find_helper.result.stream); space.serialize_to_string(find_helper.result->stream);
find_helper.result.type = Var<StreamType>::TYPE_STREAM; find_helper.result->type = Var<StreamType>::TYPE_STREAM;
//CopyStream(str, out_stream, false); //CopyStream(str, out_stream, false);
} }
else else
{ {
// space->serialize_to_string(out_stream); // space->serialize_to_string(out_stream);
space.serialize_to_string(find_helper.result.stream); space.serialize_to_string(find_helper.result->stream);
find_helper.result.type = Var<StreamType>::TYPE_STREAM; find_helper.result->type = Var<StreamType>::TYPE_STREAM;
} }
} }
@ -1536,9 +1540,8 @@ bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallModel
*/ */
//pt::WTextStream str; //pt::WTextStream str;
bool found = false; bool found = false;
StreamType fake_stream;
Env<StreamType> info(find_helper.result, find_helper.parameters, fake_stream, stack_tab[stack_index-1], *stack_tab[stack_index-1].item); Env<StreamType> info(*find_helper.result, find_helper.parameters, *find_helper.input, stack_tab[stack_index-1], *stack_tab[stack_index-1].item);
found = model.get_raw_value(nullptr, find_helper.current_name().c_str(), nullptr, info); found = model.get_raw_value(nullptr, find_helper.current_name().c_str(), nullptr, info);
// if( found && !str.empty()) // if( found && !str.empty())
@ -1811,23 +1814,69 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::FindVaria
{ {
find_helper.field_index = 0; find_helper.field_index = 0;
find_helper.previous_name = nullptr; find_helper.previous_name = nullptr;
find_helper.previous_result = nullptr; find_helper.previous_model = nullptr;
find_helper.previous_model_container_wrapper = nullptr;
find_helper.previous_space_wrapper = nullptr;
find_helper.nested_calls = 0; find_helper.nested_calls = 0;
Var<StreamType> * origin_result = find_helper.result;
Var<StreamType> local_result, local_result2;
bool use_first_local_result = true;
origin_result->clear();
do do
{ {
if( find_helper.is_last_field() )
{
find_helper.result = origin_result;
}
else
{
find_helper.result = use_first_local_result ? &local_result : &local_result2;
find_helper.result->clear();
}
find_helper.found = false; find_helper.found = false;
EvaluateVariable(find_helper); EvaluateVariable(find_helper);
find_helper.previous_model = nullptr;
find_helper.previous_model_container_wrapper = nullptr;
find_helper.previous_space_wrapper = nullptr;
if( find_helper.found ) if( find_helper.found )
{ {
if( find_helper.result->type == Var<StreamType>::TYPE_MODEL )
{
find_helper.previous_model = find_helper.result->model;
}
else
if( find_helper.result->type == Var<StreamType>::TYPE_MODEL_CONTAINER_WRAPPER )
{
find_helper.previous_model_container_wrapper = find_helper.result->model_container_wrapper;
}
else
if( find_helper.result->type == Var<StreamType>::TYPE_SPACE_WRAPPER )
{
find_helper.previous_space_wrapper = find_helper.result->space_wrapper;
}
else
{
find_helper.input = find_helper.result;
use_first_local_result = !use_first_local_result;
}
find_helper.previous_name = &find_helper.current_name(); find_helper.previous_name = &find_helper.current_name();
find_helper.field_index += 1; find_helper.field_index += 1;
} }
else else
{ {
find_helper.result.clear(); if( find_helper.is_last_field() )
{
find_helper.result->clear();
}
} }
} }
while( find_helper.found && find_helper.field_index < find_helper.fields.size() + 1 ); while( find_helper.found && find_helper.field_index < find_helper.fields.size() + 1 );
} }
@ -1839,14 +1888,10 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::EvaluateV
Var<StreamType> * var = nullptr; Var<StreamType> * var = nullptr;
const std::wstring & name = find_helper.current_name(); const std::wstring & name = find_helper.current_name();
if( find_helper.previous_name && find_helper.previous_result ) if( find_helper.previous_name && (find_helper.previous_model || find_helper.previous_model_container_wrapper || find_helper.previous_space_wrapper) )
{ {
EvaluateModelField(find_helper); if( !EvaluateModelField(find_helper) )
} return;
if( find_helper.nested_calls > max_nested_calls )
{
return;
} }
if( !find_helper.found ) if( !find_helper.found )
@ -1855,7 +1900,6 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::EvaluateV
if( var ) if( var )
{ {
// if a function returns another function we should make CallFunction() again?
if( var->type == Var<StreamType>::TYPE_FUNCTION && var->user_function ) if( var->type == Var<StreamType>::TYPE_FUNCTION && var->user_function )
{ {
EvaluateFunction(var->user_function, find_helper); EvaluateFunction(var->user_function, find_helper);
@ -1881,62 +1925,55 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::EvaluateV
PrintDate(*var->date, find_helper); PrintDate(*var->date, find_helper);
} }
} }
find_helper.previous_result = var ? &find_helper.result : nullptr;
} }
if( find_helper.found ) if( find_helper.found )
{ {
if( find_helper.result.type == Var<StreamType>::Type::TYPE_VOID && !find_helper.result.stream.empty() ) if( find_helper.result->type == Var<StreamType>::Type::TYPE_VOID && !find_helper.result->stream.empty() )
find_helper.result.type = Var<StreamType>::Type::TYPE_STREAM; {
find_helper.result->type = Var<StreamType>::Type::TYPE_STREAM;
// if( !find_helper.is_last_field() || find_helper.evaluate_last ) }
// {
//
//
// }
} }
} }
template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream> template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream>
void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::EvaluateModelField(FindHelper & find_helper) bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::EvaluateModelField(FindHelper & find_helper)
{ {
//var = find_helper.previous_result->find_child(name); //var = find_helper.previous_result->find_child(name);
//if( !var ) //if( !var )
{ {
if( find_helper.previous_result->type == Var<StreamType>::TYPE_MODEL ) if( find_helper.previous_model )
{ {
find_helper.found = CallModelField(*find_helper.previous_result->model, find_helper); find_helper.found = CallModelField(*find_helper.previous_model, find_helper);
find_helper.previous_result = find_helper.found ? &find_helper.result : nullptr;
} }
else else
if( find_helper.previous_result->type == Var<StreamType>::TYPE_MODEL_CONTAINER_WRAPPER ) if( find_helper.previous_model_container_wrapper )
{ {
morm::Model * model = find_helper.previous_result->model_container_wrapper->get_model(); morm::Model * model = find_helper.previous_model_container_wrapper->get_model();
if( model ) if( model )
{ {
find_helper.found = CallModelField(*model, find_helper); // parameters here? find_helper.found = CallModelField(*model, find_helper);
find_helper.previous_result = find_helper.found ? &find_helper.result : nullptr;
} }
else else
{ {
// put some log (no [for] statement) // put some log (no [for] statement)
// CreateMsg(L"model ", name, fields, L" is not initialized, have you forgotten to use [for ...] statement?"); // CreateMsg(L"model ", name, fields, L" is not initialized, have you forgotten to use [for ...] statement?");
return; find_helper.found = false;
return false;
} }
} }
if( find_helper.found ) if( find_helper.found )
{ {
if( find_helper.result.type == Var<StreamType>::TYPE_FUNCTION && find_helper.result.user_function ) if( find_helper.result->type == Var<StreamType>::TYPE_FUNCTION && find_helper.result->user_function )
{ {
EvaluateFunction(find_helper.result.user_function, find_helper); EvaluateFunction(find_helper.result->user_function, find_helper);
if( !find_helper.found ) if( !find_helper.found )
return; // maximum nested calls exceeded return false; // maximum nested calls exceeded
} }
} }
} }
@ -1945,6 +1982,8 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::EvaluateM
// // child found // // child found
// //
// } // }
return true;
} }
@ -1960,10 +1999,10 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::EvaluateF
CallFunction(user_function, find_helper); CallFunction(user_function, find_helper);
find_helper.found = true; find_helper.found = true;
if( find_helper.result.type == Var<StreamType>::TYPE_FUNCTION && find_helper.result.user_function ) if( find_helper.result->type == Var<StreamType>::TYPE_FUNCTION && find_helper.result->user_function )
{ {
user_function = find_helper.result.user_function; user_function = find_helper.result->user_function;
find_helper.result.clear(); find_helper.result->clear();
find_helper.nested_calls += 1; find_helper.nested_calls += 1;
if( find_helper.nested_calls <= max_nested_calls ) if( find_helper.nested_calls <= max_nested_calls )
@ -2094,7 +2133,12 @@ std::vector<Var<StreamType>> parameters;
} }
} }
FindHelper find_helper(*name, item_fun.fields, parameters, result); Var<StreamType> empty_input; // fix me for filters
FindHelper find_helper(*name, item_fun.fields, parameters);
find_helper.result = &result;
find_helper.input = &empty_input;
FindVariable(find_helper); FindVariable(find_helper);
return find_helper.found; return find_helper.found;
} }