Given that I am now writing a compiler in C++ the question of term rewriting and following RAII comes up. A typical scenario following RAII is given below.
Say your simple term language has elements like this:
class AppTerm: public Term {
public:
AppTerm(const Term &l, const Term &r): _left(l), _right(r) {};
Term left() {
return _left;
}
Term right() {
return _right;
}
private:
Term _left;
Term _right;
}
Then, since most compilers do a lot of term rewriting, you'll have an enormous amount of code like:
Term rewrite_app(const AppTerm &t) {
SomeResult r0 = rewrite_term(t.left());
SomeResult r1 = rewrite_term(t.right());
// do something
return AppTerm(r0.value(), r1.value());
}
If I get C semantics right, then r0 and r1 will go out of scope and the resulting term will receive copies of whatever was constructed in the rewrite of both constituents of an application term.
I say it again: copies. Think about that for a moment. Copies.
If every copying rewrite function is going to copy entire terms for each intermediate step, as I think it will do when implemented naively, you're going to have one hell slow compiler.
Did I get C++ semantics right? The only way out is to use smart pointers it seems.
No comments:
Post a Comment