Compare commits

...

3 Commits

3 changed files with 190 additions and 113 deletions

View File

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

View File

@ -176,23 +176,32 @@ private:
std::vector<std::wstring> & fields;
size_t field_index;
std::vector<Var<StreamType>> & parameters;
Var<StreamType> & result;
Var<StreamType> * input;
Var<StreamType> * result;
bool found;
bool evaluate_last;
const std::wstring * previous_name;
Var<StreamType> * previous_result;
size_t nested_calls;
Var<StreamType> * current_var;
Var<StreamType> * previous_var;
Var<StreamType> * previous_result;
FindHelper(const std::wstring & name, std::vector<std::wstring> & fields, std::vector<Var<StreamType>> & parameters, Var<StreamType> & result):
name(name), fields(fields), parameters(parameters), result(result)
FindHelper(
const std::wstring & name,
std::vector<std::wstring> & fields,
std::vector<Var<StreamType>> & parameters
): name(name), fields(fields), parameters(parameters)
{
field_index = 0;
found = false;
evaluate_last = true;
previous_name = nullptr;
previous_result = nullptr;
nested_calls = 0;
input = nullptr;
result = nullptr;
current_var = nullptr;
previous_var = nullptr;
previous_result = nullptr;
}
const std::wstring & current_name()
@ -202,9 +211,6 @@ private:
bool is_last_field()
{
if( field_index == 0 )
return true;
return field_index == fields.size();
}
@ -262,7 +268,7 @@ private:
// as std::wostringstream are not copyable
std::vector<StreamType*> filter_tab;
std::vector<StreamType*> ezc_frame_stack_tab;
const StreamType empty_stream;
StreamType empty_stream;
// temporary streams used in [if..] [for...] or [def ...]
// or if output_stream is null and an ezc function should be called
@ -370,7 +376,7 @@ private:
void FindVariable(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);
@ -386,7 +392,7 @@ private:
bool Call(Item::Function & item_fun,
const std::wstring * fun_name,
Var<StreamType> & result,
const StreamType & in_stream);
StreamType & in_stream);
bool Call(Item::Function & item_fun, Var<StreamType> & result);
@ -1047,8 +1053,7 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallFunct
{
// if( find_helper.parameters.empty() )
{
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);
CallFunction(function, info);
}
// else
@ -1111,14 +1116,14 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::DumpSpace
if( dump_space )
{
space.serialize_to_space_stream(find_helper.result.stream, pretty);
find_helper.result.type = Var<StreamType>::TYPE_STREAM;
space.serialize_to_space_stream(find_helper.result->stream, pretty);
find_helper.result->type = Var<StreamType>::TYPE_STREAM;
}
else
if( dump_json )
{
space.serialize_to_json_stream(find_helper.result.stream, pretty);
find_helper.result.type = Var<StreamType>::TYPE_STREAM;
space.serialize_to_json_stream(find_helper.result->stream, pretty);
find_helper.result->type = Var<StreamType>::TYPE_STREAM;
}
}
}
@ -1167,23 +1172,23 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::PrintDate
if( only_date )
{
find_helper.result.type = Var<StreamType>::TYPE_STREAM;
date.SerializeYearMonthDay(find_helper.result.stream, is_roman);
find_helper.result->type = Var<StreamType>::TYPE_STREAM;
date.SerializeYearMonthDay(find_helper.result->stream, is_roman);
}
else
if( only_time )
{
find_helper.result.type = Var<StreamType>::TYPE_STREAM;
find_helper.result->type = Var<StreamType>::TYPE_STREAM;
if( is_no_sec )
date.SerializeHourMin(find_helper.result.stream);
date.SerializeHourMin(find_helper.result->stream);
else
date.SerializeHourMinSec(find_helper.result.stream);
date.SerializeHourMinSec(find_helper.result->stream);
}
else
{
find_helper.result.type = Var<StreamType>::TYPE_STREAM;
date.Serialize(find_helper.result.stream, is_roman, !is_no_sec);
find_helper.result->type = Var<StreamType>::TYPE_STREAM;
date.Serialize(find_helper.result->stream, is_roman, !is_no_sec);
}
}
@ -1358,14 +1363,14 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallSpace
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.type = Var<StreamType>::TYPE_STREAM;
find_helper.result->stream << space_wrapper.get_space_iterator_value(model_wrapper_space_table_index);
find_helper.result->type = Var<StreamType>::TYPE_STREAM;
}
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.type = Var<StreamType>::TYPE_STREAM;
find_helper.result->stream << (space_wrapper.get_space_iterator_value(model_wrapper_space_table_index) + 1);
find_helper.result->type = Var<StreamType>::TYPE_STREAM;
}
}
}
@ -1407,15 +1412,15 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::PrintLast
if( no_escape )
{
//pt::WTextStream str;
space.serialize_to_string(find_helper.result.stream);
find_helper.result.type = Var<StreamType>::TYPE_STREAM;
space.serialize_to_string(find_helper.result->stream);
find_helper.result->type = Var<StreamType>::TYPE_STREAM;
//CopyStream(str, out_stream, false);
}
else
{
// space->serialize_to_string(out_stream);
space.serialize_to_string(find_helper.result.stream);
find_helper.result.type = Var<StreamType>::TYPE_STREAM;
space.serialize_to_string(find_helper.result->stream);
find_helper.result->type = Var<StreamType>::TYPE_STREAM;
}
}
@ -1536,9 +1541,8 @@ bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::CallModel
*/
//pt::WTextStream str;
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);
// if( found && !str.empty())
@ -1810,24 +1814,55 @@ template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream>
void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::FindVariable(FindHelper & find_helper)
{
find_helper.field_index = 0;
find_helper.previous_name = nullptr;
find_helper.previous_result = nullptr;
find_helper.previous_var = nullptr;
find_helper.previous_name = nullptr;
find_helper.nested_calls = 0;
Var<StreamType> * origin_result = find_helper.result;
//Var<StreamType> local_result, local_result2;
//bool use_first_local_result = true;
std::vector<Var<StreamType>> results;
results.resize(find_helper.fields.size() + 1);
origin_result->clear();
do
{
find_helper.found = false;
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.result = &results[find_helper.field_index];
}
EvaluateVariable(find_helper);
if( find_helper.found )
{
if( find_helper.result->type != Var<StreamType>::TYPE_MODEL &&
find_helper.result->type != Var<StreamType>::TYPE_MODEL_CONTAINER_WRAPPER &&
find_helper.result->type != Var<StreamType>::TYPE_SPACE_WRAPPER )
{
find_helper.input = find_helper.result;
//use_first_local_result = !use_first_local_result;
}
find_helper.previous_name = &find_helper.current_name();
find_helper.field_index += 1;
}
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 );
}
@ -1836,115 +1871,131 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::FindVaria
template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream>
void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::EvaluateVariable(FindHelper & find_helper)
{
Var<StreamType> * var = nullptr;
bool was_child_found = false;
find_helper.found = false;
find_helper.current_var = nullptr;
const std::wstring & name = find_helper.current_name();
if( find_helper.previous_name && find_helper.previous_result )
if( find_helper.previous_var )
{
EvaluateModelField(find_helper);
find_helper.current_var = find_helper.previous_var->find_child(name);
find_helper.found = (find_helper.current_var != nullptr);
was_child_found = (find_helper.current_var != nullptr);
}
if( find_helper.nested_calls > max_nested_calls )
if( !find_helper.found && find_helper.previous_name && find_helper.previous_result &&
(find_helper.previous_result->type == Var<StreamType>::TYPE_MODEL ||
find_helper.previous_result->type == Var<StreamType>::TYPE_MODEL_CONTAINER_WRAPPER ||
find_helper.previous_result->type == Var<StreamType>::TYPE_SPACE_WRAPPER))
{
return;
if( !EvaluateModelField(find_helper) )
return;
}
if( !find_helper.found )
{
var = FindInScope(name);
find_helper.result->clear();
find_helper.current_var = FindInScope(name);
if( var )
if( find_helper.current_var )
{
// if a function returns another function we should make CallFunction() again?
if( var->type == Var<StreamType>::TYPE_FUNCTION && var->user_function )
if( find_helper.current_var->type == Var<StreamType>::TYPE_FUNCTION && find_helper.current_var->user_function )
{
EvaluateFunction(var->user_function, find_helper);
EvaluateFunction(find_helper.current_var->user_function, find_helper);
}
else
if( var->type == Var<StreamType>::TYPE_MODEL_CONTAINER_WRAPPER && var->model_container_wrapper )
if( find_helper.current_var->type == Var<StreamType>::TYPE_MODEL_CONTAINER_WRAPPER && find_helper.current_var->model_container_wrapper )
{
if( find_helper.is_last_field() && is_generating_for )
{
var->model_container_wrapper->increment_iterator();
var->clear_childs();
find_helper.current_var->model_container_wrapper->increment_iterator();
find_helper.current_var->clear_childs();
}
}
else
if( var->type == Var<StreamType>::TYPE_SPACE_WRAPPER && var->space_wrapper )
if( find_helper.current_var->type == Var<StreamType>::TYPE_SPACE_WRAPPER && find_helper.current_var->space_wrapper )
{
find_helper.found = CallSpace(*var->space_wrapper, find_helper);
find_helper.found = CallSpace(*find_helper.current_var->space_wrapper, find_helper);
}
else
if( var->type == Var<StreamType>::TYPE_DATE && var->date )
if( find_helper.current_var->type == Var<StreamType>::TYPE_DATE && find_helper.current_var->date )
{
if( find_helper.is_last_field() )
PrintDate(*var->date, find_helper);
PrintDate(*find_helper.current_var->date, find_helper);
}
}
find_helper.previous_result = var ? &find_helper.result : nullptr;
}
if( find_helper.found )
{
if( find_helper.result.type == Var<StreamType>::Type::TYPE_VOID && !find_helper.result.stream.empty() )
find_helper.result.type = Var<StreamType>::Type::TYPE_STREAM;
if( find_helper.result->type == Var<StreamType>::Type::TYPE_VOID && !find_helper.result->stream.empty() )
{
find_helper.result->type = Var<StreamType>::Type::TYPE_STREAM;
}
// if( !find_helper.is_last_field() || find_helper.evaluate_last )
// {
//
//
// }
if( find_helper.previous_var && !was_child_found )
{
// may only model_container_wrapper and space_wrapper here?
if( find_helper.result->type == Var<StreamType>::TYPE_MODEL ||
find_helper.result->type == Var<StreamType>::TYPE_MODEL_CONTAINER_WRAPPER ||
find_helper.result->type == Var<StreamType>::TYPE_SPACE_WRAPPER ||
find_helper.result->type == Var<StreamType>::TYPE_FUNCTION )
{
find_helper.previous_var->add_child(name, *find_helper.result);
}
}
find_helper.previous_var = find_helper.current_var;
find_helper.previous_result = find_helper.result;
}
else
{
find_helper.previous_var = nullptr;
find_helper.previous_result = nullptr;
}
}
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);
//if( !var )
if( find_helper.previous_result->type == Var<StreamType>::TYPE_MODEL )
{
if( find_helper.previous_result->type == Var<StreamType>::TYPE_MODEL )
//find_helper.result->clear();
find_helper.found = CallModelField(*find_helper.previous_result->model, find_helper);
}
else
if( find_helper.previous_result->type == Var<StreamType>::TYPE_MODEL_CONTAINER_WRAPPER )
{
//find_helper.result->clear();
morm::Model * model = find_helper.previous_result->model_container_wrapper->get_model();
if( model )
{
find_helper.found = CallModelField(*find_helper.previous_result->model, find_helper);
find_helper.previous_result = find_helper.found ? &find_helper.result : nullptr;
find_helper.found = CallModelField(*model, find_helper);
}
else
if( find_helper.previous_result->type == Var<StreamType>::TYPE_MODEL_CONTAINER_WRAPPER )
{
morm::Model * model = find_helper.previous_result->model_container_wrapper->get_model();
if( model )
{
find_helper.found = CallModelField(*model, find_helper); // parameters here?
find_helper.previous_result = find_helper.found ? &find_helper.result : nullptr;
}
else
{
// put some log (no [for] statement)
// CreateMsg(L"model ", name, fields, L" is not initialized, have you forgotten to use [for ...] statement?");
return;
}
}
if( find_helper.found )
{
if( find_helper.result.type == Var<StreamType>::TYPE_FUNCTION && find_helper.result.user_function )
{
EvaluateFunction(find_helper.result.user_function, find_helper);
if( !find_helper.found )
return; // maximum nested calls exceeded
}
// put some log (no [for] statement)
// CreateMsg(L"model ", name, fields, L" is not initialized, have you forgotten to use [for ...] statement?");
find_helper.found = false;
return false;
}
}
// else
// {
// // child found
//
// }
if( find_helper.found )
{
if( find_helper.result->type == Var<StreamType>::TYPE_FUNCTION && find_helper.result->user_function )
{
EvaluateFunction(find_helper.result->user_function, find_helper);
if( !find_helper.found )
return false; // maximum nested calls exceeded
}
}
return true;
}
@ -1960,10 +2011,10 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::EvaluateF
CallFunction(user_function, find_helper);
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;
find_helper.result.clear();
user_function = find_helper.result->user_function;
find_helper.result->clear();
find_helper.nested_calls += 1;
if( find_helper.nested_calls <= max_nested_calls )
@ -2044,7 +2095,7 @@ bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::Call(
Item::Function & item_fun,
const std::wstring * fun_name,
Var<StreamType> & result,
const StreamType & in_stream)
StreamType & in_stream)
{
//FindHelperOld find_helper;
std::vector<Var<StreamType>> parameters;
@ -2085,6 +2136,7 @@ std::vector<Var<StreamType>> parameters;
if( fun_child.is_function )
{
empty_stream.clear();
Call(fun_child, nullptr, parameters[i], empty_stream);
}
else
@ -2094,7 +2146,14 @@ std::vector<Var<StreamType>> parameters;
}
}
FindHelper find_helper(*name, item_fun.fields, parameters, result);
Var<StreamType> empty_input; // fix me for filters
empty_input.type = Var<StreamType>::TYPE_STREAM;
empty_input.stream = in_stream; // IMPROVEME don't copy here streams, let Var::stream be a pointer?
FindHelper find_helper(*name, item_fun.fields, parameters);
find_helper.result = &result;
find_helper.input = &empty_input;
FindVariable(find_helper);
return find_helper.found;
}
@ -2106,6 +2165,7 @@ template<class StreamType, bool is_pikotools_stream, bool is_autoescape_stream>
bool Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::Call(Item::Function & item_fun, Var<StreamType> & result)
{
//return Call(item_fun, nullptr, stream_temp1, true, result, empty_stream);
empty_stream.clear();
return Call(item_fun, nullptr, result, empty_stream);
}
@ -2489,6 +2549,7 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::MakeTextF
else
{
Var<StreamType> result;
empty_stream.clear();
if( Call(item.function, nullptr, result, empty_stream) )
{
@ -2622,11 +2683,22 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::MakeTextF
else
{
//Call(item.function, nullptr, stream_temp1, true, result, empty_stream);
empty_stream.clear();
Call(item.function, nullptr, result, empty_stream);
}
if( !result.to_bool() )
break;
if( result.type == Var<StreamType>::Type::TYPE_MODEL_CONTAINER_WRAPPER )
{
result.model_container_wrapper->increment_iterator();
// what about clear_childs() ?
if( !result.model_container_wrapper->is_iterator_correct() )
break;
}
// if( !result.to_bool() )
// break;
if( !item.item_tab.empty() )
MakeText( *item.item_tab[0] ); // should be only one item
@ -2659,6 +2731,7 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::MakeTextD
{
// call function
//if( Call(fun, nullptr, stream_temp_define, true, var, empty_stream) )
empty_stream.clear();
if( Call(fun, nullptr, var, empty_stream) )
{
//CopyStreamToString(stream_temp_define, var.str);
@ -2931,6 +3004,7 @@ void Generator<StreamType, is_pikotools_stream, is_autoescape_stream>::MakeTextR
// this Call() sets last_res which is used later when we return to CallBlock()
//Call(item.function, nullptr, stream_temp1, false, result, empty_stream);
//ClearStream(stream_temp1);
empty_stream.clear();
Call(item.function, nullptr, result, empty_stream);
}
}

View File

@ -393,9 +393,12 @@ bool Var<StreamType>::to_bool() const
case TYPE_FUNCTION:
case TYPE_DATE:
case TYPE_MODEL:
case TYPE_MODEL_CONTAINER_WRAPPER:
case TYPE_SPACE_WRAPPER:
break;
case TYPE_MODEL_CONTAINER_WRAPPER:
return !model_container_wrapper->is_container_empty();
break;
}
return false;