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:
- resolve operator use in translator if possible, since step constant.
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
Post a Comment