Skip to content

Commit

Permalink
APREPRO: Fix loop with zero iteration count
Browse files Browse the repository at this point in the history
  • Loading branch information
gsjaardema committed Oct 12, 2023
1 parent d618bb7 commit 1ff5373
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 77 deletions.
2 changes: 1 addition & 1 deletion packages/seacas/libraries/aprepro_lib/apr_aprepro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#endif

namespace {
const std::string version_short{"6.24"};
const std::string version_short{"6.25"};
const std::string version_date{"(2023/10/12)"};
const std::string version_string = version_short + " " + version_date;

Expand Down
89 changes: 49 additions & 40 deletions packages/seacas/libraries/aprepro_lib/apr_scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1396,51 +1396,60 @@ YY_DECL
}
}

temp_f = get_temp_filename();
SEAMS::file_rec new_file(temp_f, 0, true, loop_iterations);
outer_file = &aprepro.ap_file_list.top();
new_file.loop_level = outer_file->loop_level + 1;

// Get optional loop index...
std::string sym_name;
if (tokens.size() == 1) {
// Default loop index variable name if not specified in loop command.
sym_name = fmt::format("__loop_{}", new_file.loop_level);
if (loop_iterations <= 0) {
BEGIN(LOOP_SKIP);
if (aprepro.ap_options.debugging) {
fmt::print(stderr, "DEBUG LOOP: iteration count = {}, Skipping loop...\n",
loop_iterations);
}
}
else {
sym_name = tokens[1];
}
SEAMS::symrec *li = aprepro.getsym(sym_name);
if (li == nullptr) {
li = aprepro.putsym(sym_name, SEAMS::Aprepro::SYMBOL_TYPE::VARIABLE, true);
}

// Get optional loop index initial value. Default to 0 if not specified.
double init = 0.0;
if (tokens.size() >= 3) {
init = std::stod(tokens[2]);
}
li->value.var = init;
temp_f = get_temp_filename();
SEAMS::file_rec new_file(temp_f, 0, true, loop_iterations);
outer_file = &aprepro.ap_file_list.top();
new_file.loop_level = outer_file->loop_level + 1;

// Get optional loop index...
std::string sym_name;
if (tokens.size() == 1) {
// Default loop index variable name if not specified in loop command.
sym_name = fmt::format("__loop_{}", new_file.loop_level);
}
else {
sym_name = tokens[1];
}
SEAMS::symrec *li = aprepro.getsym(sym_name);
if (li == nullptr) {
li = aprepro.putsym(sym_name, SEAMS::Aprepro::SYMBOL_TYPE::VARIABLE, true);
}

// Get optional loop index increment value. Default to 1 if not specified.
if (tokens.size() >= 4) {
double increment = std::stod(tokens[3]);
new_file.loop_increment = increment;
}
// Get optional loop index initial value. Default to 0 if not specified.
double init = 0.0;
if (tokens.size() >= 3) {
init = std::stod(tokens[2]);
}
li->value.var = init;

new_file.loop_index = li;
aprepro.ap_file_list.push(new_file);
// Get optional loop index increment value. Default to 1 if not specified.
if (tokens.size() >= 4) {
double increment = std::stod(tokens[3]);
new_file.loop_increment = increment;
}

tmp_file = new std::fstream(temp_f, std::ios::out);
loop_lvl++;
BEGIN(LOOP);
aprepro.isCollectingLoop = true;
if (aprepro.ap_options.debugging) {
fmt::print(
stderr,
"DEBUG LOOP: iteration count = {}, loop_index variable = {}, initial value = "
"{}, increment = {}\n",
loop_iterations, sym_name, init, new_file.loop_increment);
new_file.loop_index = li;
aprepro.ap_file_list.push(new_file);

tmp_file = new std::fstream(temp_f, std::ios::out);
loop_lvl++;
BEGIN(LOOP);
aprepro.isCollectingLoop = true;
if (aprepro.ap_options.debugging) {
fmt::print(
stderr,
"DEBUG LOOP: iteration count = {}, loop_index variable = {}, initial value = "
"{}, increment = {}\n",
loop_iterations, sym_name, init, new_file.loop_increment);
}
}
}
YY_BREAK
Expand Down
82 changes: 46 additions & 36 deletions packages/seacas/libraries/aprepro_lib/aprepro.ll
Original file line number Diff line number Diff line change
Expand Up @@ -177,48 +177,58 @@ integer {D}+({E})?
}
}
temp_f = get_temp_filename();
SEAMS::file_rec new_file(temp_f, 0, true, loop_iterations);
outer_file = &aprepro.ap_file_list.top();
new_file.loop_level = outer_file->loop_level + 1;
// Get optional loop index...
std::string sym_name;
if (tokens.size() == 1) {
// Default loop index variable name if not specified in loop command.
sym_name = fmt::format("__loop_{}", new_file.loop_level);
if (loop_iterations <= 0) {
BEGIN(LOOP_SKIP);
if (aprepro.ap_options.debugging) {
fmt::print(
stderr,
"DEBUG LOOP: iteration count = {}, Skipping loop...\n", loop_iterations);
}
}
else {
sym_name = tokens[1];
}
SEAMS::symrec *li = aprepro.getsym(sym_name);
if (li == nullptr) {
li = aprepro.putsym(sym_name, SEAMS::Aprepro::SYMBOL_TYPE::VARIABLE, true);
}
temp_f = get_temp_filename();
SEAMS::file_rec new_file(temp_f, 0, true, loop_iterations);
outer_file = &aprepro.ap_file_list.top();
new_file.loop_level = outer_file->loop_level + 1;
// Get optional loop index...
std::string sym_name;
if (tokens.size() == 1) {
// Default loop index variable name if not specified in loop command.
sym_name = fmt::format("__loop_{}", new_file.loop_level);
}
else {
sym_name = tokens[1];
}
SEAMS::symrec *li = aprepro.getsym(sym_name);
if (li == nullptr) {
li = aprepro.putsym(sym_name, SEAMS::Aprepro::SYMBOL_TYPE::VARIABLE, true);
}
// Get optional loop index initial value. Default to 0 if not specified.
double init = 0.0;
if (tokens.size() >= 3) {
init = std::stod(tokens[2]);
}
li->value.var = init;
// Get optional loop index initial value. Default to 0 if not specified.
double init = 0.0;
if (tokens.size() >= 3) {
init = std::stod(tokens[2]);
}
li->value.var = init;
// Get optional loop index increment value. Default to 1 if not specified.
if (tokens.size() >= 4) {
double increment = std::stod(tokens[3]);
new_file.loop_increment = increment;
}
// Get optional loop index increment value. Default to 1 if not specified.
if (tokens.size() >= 4) {
double increment = std::stod(tokens[3]);
new_file.loop_increment = increment;
}
new_file.loop_index = li;
aprepro.ap_file_list.push(new_file);
new_file.loop_index = li;
aprepro.ap_file_list.push(new_file);
tmp_file = new std::fstream(temp_f, std::ios::out);
loop_lvl++;
BEGIN(LOOP);
aprepro.isCollectingLoop = true;
if (aprepro.ap_options.debugging) {
fmt::print(stderr, "DEBUG LOOP: iteration count = {}, loop_index variable = {}, initial value = {}, increment = {}\n",
loop_iterations, sym_name, init, new_file.loop_increment);
tmp_file = new std::fstream(temp_f, std::ios::out);
loop_lvl++;
BEGIN(LOOP);
aprepro.isCollectingLoop = true;
if (aprepro.ap_options.debugging) {
fmt::print(stderr, "DEBUG LOOP: iteration count = {}, loop_index variable = {}, initial value = {}, increment = {}\n",
loop_iterations, sym_name, init, new_file.loop_increment);
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/seacas/libraries/aprepro_lib/test.inp_app
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,10 @@ $ Test string tokenization optimization
Word {_i} of list1 and list2 are {get_word(_i,list1,',')} and {get_word(_i,list2,',')}
{endloop}

{loop(0)}
This should not be echoed
{endloop}
{loop(undefined)}
This should not be echoed
{endloop}
$End of test file

0 comments on commit 1ff5373

Please sign in to comment.