oct2mat script based on parse tree
Muthiah Annamalai
muthuspost at gmail.com
Wed Jan 9 01:13:30 CST 2008
John W. Eaton wrote:
> On 9-Jan-2008, Muthiah Annamalai wrote:
>
> | John W. Eaton wrote:
> | > On 8-Jan-2008, I wrote:
> | >
> | > | I don't think you need to modify the parse tree here when you
> | > | encounter something like "+=". You just need to emit the lhs, then
> | > | the binary op corresponding to the OP= operator (you can get that with
> | > | the octave_value::op_eq_to_binary_op method) and then emit the rhs.
> | >
> | > Oops, I meant to say
> | >
> | > emit lhs
> | > emit "="
> | > emit lhs again
> | > emit binary operator corresponding to original OP= operator
> | > emit rhs
> | >
> | > jwe
> | >
> | >
> | But I think to make this possible we must have a method by which
> | we can turn off the fold()-ing of the parse tree, which happens
> | automatically
> | right now.
>
> I don't understand why that would be necessary. As far as I can tell,
> the constant folding code would only affect the RHS of the expression,
> or possibly indices of the LHS expression. What case are you thinking
> of where it would cause trouble?
>
>
The case I see is,
x = "hello" | "world";
which is folded into a tree_constant.
I now handle this using some kludgy code like,
void
tree_print_matlab_code::visit_constant (tree_constant& val)
{
#ifdef O2M_DBG
os << "% visiting constant \n";
#endif
indent ();
print_parens (val, "(");
octave_value sval=val.rvalue();
std::string buf_str;
std::ostringstream buf;
int idx = 0;
if ( sval.is_string() )
{
buf_str=sval.string_value();
// see if you need to escape the string stxt.
os << "'";
for( idx=0; idx < buf_str.length(); ) {
if ( buf_str[idx] == '"' || buf_str[idx] == '\'' ) {
os << "\\" ;
os << buf_str[idx];
} else if ( buf_str[idx] == '\"' ) {
os << "'";
} else if ( buf_str[idx] == '\n' ) {
os << "\\n";
} else if ( buf_str[idx] == '\t' ) {
os << "\\t";
} else if ( buf_str[idx] == '\r' ) {
os << "\\r";
} else if ( !isprint(buf_str[idx]) ) {
os << "\\" << std::hex << buf_str[idx];
} else {
os << buf_str[idx];
}
idx++;
}
os << "'";
}
//
// FIXME: this needs to be done by avoiding folding at
// at the parse tree level of the expressions into constants while
// invoking yyparse();
//
else if ( sval.is_bool_scalar() || sval.is_bool_matrix()
|| sval.is_real_scalar() || sval.is_real_matrix()
|| sval.is_complex_scalar() || sval.is_complex_matrix()
|| sval.is_real_nd_array() )
{
val.print_raw (buf , true, print_original_text);
buf_str = buf.str();
//
// Convert the boolean ||, && strings
// to respective | or & symbols.
// '||' -> '|', '&&' -> '&', '!' -> '~'
// string-escaped sequence need to be printed out.
// also take care of strings. duh. OR parse this string
// thing again, and send it out! huh. more work.
//
for( idx=0; idx < buf_str.length()-1; ) {
if ( buf_str[idx] == '!' ) {
os << "~";
} else if ( buf_str[idx] == '|' && buf_str[idx+1]=='|') {
os << "|";
idx++;
} else if ( buf_str[idx] == '&' && buf_str[idx+1]=='&') {
os << "&";
idx++;
}else if ( buf_str[idx] == '\\' ) {
os << buf_str[idx];
idx++;
os << buf_str[idx];
} else if ( buf_str[idx] == '\"' ) {
os << "'";
} else {
os << buf_str[idx];
}
idx++;
}
if ( idx <= buf_str.length() ) {
switch ( buf_str[idx] ) {
case '\"':
os << '\'';
break;
case '!':
os << '~';
break;
default:
os << buf_str[idx];
}
}
}
else
{
val.print_raw (buf , true, print_original_text);
buf_str = buf.str();
// string-escaped sequence need to be printed out.
for( idx=0; idx < buf_str.length(); ) {
if ( buf_str[idx] == '\\' ) {
os << buf_str[idx];
idx++;
os << buf_str[idx];
} else if ( buf_str[idx] == '\"' ) {
os << "'";
} else {
os << buf_str[idx];
}
idx++;
}
}
print_parens (val, ")");
}
> | Since Octave is a moving target (which is nice actually!)
>
> Since it is a moving target, your code is more likely to remain
> synchronized with the sources if you help to integrate the changes now
> (by making them conditional modifications of the existing
> tree_print_code class and submitting patches) rather than working on
> your own completely separate project and waiting until later to merge
> a larger set of changes all at once.
>
>
OK, I will attempt this.
> | we have some
> | issues,like I didnt have boolean expressions involving strings folded
> | over.
>
> I don't understand what you mean by that.
>
> jwe
>
This is the boolean exp -> const , folding I was talking about.
Thanks,
-Muthu
More information about the Octave-maintainers
mailing list