diff --git a/src/generator.h b/src/generator.h index a3f7620..d8d626d 100644 --- a/src/generator.h +++ b/src/generator.h @@ -180,10 +180,11 @@ private: Var * result; bool found; const std::wstring * previous_name; - morm::Model * previous_model; - morm::ModelContainerWrapper * previous_model_container_wrapper; - morm::SpaceWrapper * previous_space_wrapper; size_t nested_calls; + Var * current_var; + Var * previous_var; + Var * previous_result; + FindHelper( @@ -198,9 +199,9 @@ private: nested_calls = 0; input = nullptr; result = nullptr; - previous_model = nullptr; - previous_model_container_wrapper = nullptr; - previous_space_wrapper = nullptr; + current_var = nullptr; + previous_var = nullptr; + previous_result = nullptr; } const std::wstring & current_name() @@ -1813,15 +1814,16 @@ template void Generator::FindVariable(FindHelper & find_helper) { find_helper.field_index = 0; + find_helper.previous_result = nullptr; + find_helper.previous_var = nullptr; find_helper.previous_name = nullptr; - find_helper.previous_model = nullptr; - find_helper.previous_model_container_wrapper = nullptr; - find_helper.previous_space_wrapper = nullptr; find_helper.nested_calls = 0; Var * origin_result = find_helper.result; - Var local_result, local_result2; - bool use_first_local_result = true; + //Var local_result, local_result2; + //bool use_first_local_result = true; + std::vector> results; + results.resize(find_helper.fields.size() + 1); origin_result->clear(); @@ -1833,37 +1835,21 @@ void Generator::FindVaria } else { - find_helper.result = use_first_local_result ? &local_result : &local_result2; - find_helper.result->clear(); + //find_helper.result = use_first_local_result ? &local_result : &local_result2; + //find_helper.result->clear(); + find_helper.result = &results[find_helper.field_index]; } - find_helper.found = false; 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.result->type == Var::TYPE_MODEL ) - { - find_helper.previous_model = find_helper.result->model; - } - else - if( find_helper.result->type == Var::TYPE_MODEL_CONTAINER_WRAPPER ) - { - find_helper.previous_model_container_wrapper = find_helper.result->model_container_wrapper; - } - else - if( find_helper.result->type == Var::TYPE_SPACE_WRAPPER ) - { - find_helper.previous_space_wrapper = find_helper.result->space_wrapper; - } - else + if( find_helper.result->type != Var::TYPE_MODEL && + find_helper.result->type != Var::TYPE_MODEL_CONTAINER_WRAPPER && + find_helper.result->type != Var::TYPE_SPACE_WRAPPER ) { find_helper.input = find_helper.result; - use_first_local_result = !use_first_local_result; + //use_first_local_result = !use_first_local_result; } find_helper.previous_name = &find_helper.current_name(); @@ -1885,10 +1871,22 @@ void Generator::FindVaria template void Generator::EvaluateVariable(FindHelper & find_helper) { - Var * 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_model || find_helper.previous_model_container_wrapper || find_helper.previous_space_wrapper) ) + if( find_helper.previous_var ) + { + 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.found && find_helper.previous_name && find_helper.previous_result && + (find_helper.previous_result->type == Var::TYPE_MODEL || + find_helper.previous_result->type == Var::TYPE_MODEL_CONTAINER_WRAPPER || + find_helper.previous_result->type == Var::TYPE_SPACE_WRAPPER)) { if( !EvaluateModelField(find_helper) ) return; @@ -1896,33 +1894,34 @@ void Generator::EvaluateV 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( var->type == Var::TYPE_FUNCTION && var->user_function ) + if( find_helper.current_var->type == Var::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::TYPE_MODEL_CONTAINER_WRAPPER && var->model_container_wrapper ) + if( find_helper.current_var->type == Var::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::TYPE_SPACE_WRAPPER && var->space_wrapper ) + if( find_helper.current_var->type == Var::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::TYPE_DATE && var->date ) + if( find_helper.current_var->type == Var::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); } } } @@ -1933,6 +1932,26 @@ void Generator::EvaluateV { find_helper.result->type = Var::Type::TYPE_STREAM; } + + if( find_helper.previous_var && !was_child_found ) + { + // may only model_container_wrapper and space_wrapper here? + if( find_helper.result->type == Var::TYPE_MODEL || + find_helper.result->type == Var::TYPE_MODEL_CONTAINER_WRAPPER || + find_helper.result->type == Var::TYPE_SPACE_WRAPPER || + find_helper.result->type == Var::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; } } @@ -1940,48 +1959,41 @@ void Generator::EvaluateV template bool Generator::EvaluateModelField(FindHelper & find_helper) { - //var = find_helper.previous_result->find_child(name); - - //if( !var ) + if( find_helper.previous_result->type == Var::TYPE_MODEL ) { - if( find_helper.previous_model ) + //find_helper.result->clear(); + find_helper.found = CallModelField(*find_helper.previous_result->model, find_helper); + } + else + if( find_helper.previous_result->type == Var::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_model, find_helper); + find_helper.found = CallModelField(*model, find_helper); } else - if( find_helper.previous_model_container_wrapper ) { - morm::Model * model = find_helper.previous_model_container_wrapper->get_model(); - - if( model ) - { - find_helper.found = CallModelField(*model, find_helper); - } - else - { - // 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; - } - } - - if( find_helper.found ) - { - if( find_helper.result->type == Var::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 - } + // 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; + } + } + + if( find_helper.found ) + { + if( find_helper.result->type == Var::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 } } -// else -// { -// // child found -// -// } return true; } @@ -2675,8 +2687,18 @@ void Generator::MakeTextF Call(item.function, nullptr, result, empty_stream); } - if( !result.to_bool() ) - break; + + if( result.type == Var::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 diff --git a/src/var.h b/src/var.h index b25ed1a..26cad3c 100644 --- a/src/var.h +++ b/src/var.h @@ -393,9 +393,12 @@ bool Var::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;