chapel-lang / chapel

a Productive Parallel Programming Language
https://chapel-lang.org
Other
1.79k stars 421 forks source link

Request: Convert expression to string #14150

Open BryantLam opened 5 years ago

BryantLam commented 5 years ago

This issue initially started out as a request for a macro system, but I think most of the functionality I would want is (mostly) resolved with the Reflection module. That said, macros would still be nice for reducing code boilerplate.

Anyway, there's no way to do the following today that I'm aware of.

proc exprToString(expr) param: string { ... }

writeln(
  exprToString(3 * (4 + 5))
);
// prints:  3 * (4 + 5)

This could be added to the Reflection module (but maybe Reflection should serve as inspiration for a macro system in the long term).

Note that this is only useful if it can be composed, which seems problematic:

proc evaluate(expr: SomeChapelExpression) {
  writeln(exprToString(expr), " = ", expr);
}

writeln(evaluate(3 * (4 + 5));
// prints:  3 * (4 + 5) = 27
BryantLam commented 5 years ago

I don't think this works. My goal was to emulate something like Rust's dbg macro, but printing the filename or line number doesn't make sense because those Reflection functions are called inside the dbg procedure instead of at the callsite of dbg.

mppf commented 5 years ago

@BryantLam - can you say more about why you want something like this? Is it just for debugging?

BryantLam commented 5 years ago

This specific feature request is to help with debugging. The Chapel project could provide a compiler-assisted function to implement a dbg macro and I'd be satisfied.

What's the current stance on having a macro system in Chapel? Macros would enable things like abstracting boilerplate based on an operator, though a functional approach #14153 works without macros.

Java doesn't have macros and I don't know whether they just live without them or if they do something else (I think an IDE like Eclipse can extract from the Java bytecode the expression to print during a debug session, but I'm just guessing; it's been a while since I used Eclipse). You could just abuse the C preprocessor if really needed...