compiler construction - Compiling a for statement from BASIC to C++ -


i'm writing compiler migrate legacy vb6 program c++. need translate for statement in vb6 language c++ for statement:

for var = start end step s   ... next var 

the naive translation not work since s might negative:

for (var = start; var <= end; var += s)  

i've came translation ternary if in condition ugly:

for (var = start; (s > 0) ? (var <= end) : (var >= end); var += s) 

it's generated code. you'll ever looking @ when debugging code generator. it's totally irrelevant if it's ugly. matters if correct , simpler generate, better.


update: however, if it's migration, might indeed make sense try make code readable. i'd either:

  1. resolve operator use in translator if possible, since step constant.
  2. hide logic in auxiliary definition , use range-based for:

    for(auto var : basic_range(start, end, s)) 

    unfortunately boost::irange did not make c++11 , defined using half-open range usual c++, i.e. end not included while want include it. have define range yourself. you'd hide direction logic in it, not obscure code. @ boost::irange inspiration.

the largest issue object lifecycle anyway. vb6 (unlike earlier basics) managed. you'll end using smart pointers things , it's not efficient thing do.


Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -