• Advertisement
Sign in to follow this  

Manual syntax trees

Recommended Posts

I thought this thing I wrote might be a worthy candidate for a coding horror.

Expression* SymbolicMatrix::determinant3x3() const
     * Given the matrix
     *      / a b c \
     *  A = | d e f |
     *      \ g h i /
     * The determinant can be calculated with:
     *  det(A) = (aei + bfg + cdh) - (gec + hfa + idb)
     * The entries in the matrix are stored such that a=0, b=1, c=2, etc.
    return Expression::make(op::sub,                   // (aei + bfg + cdh) - (gec + hfa + idb)
            Expression::make(op::add,                  // aei + bfg + cdh
                Expression::make(op::add,              // aei + bfg
                    Expression::make(op::mul,          // aei
                        Expression::make(op::mul,      // ae
                            entries_[0]->clone(),      // a
                            entries_[4]->clone()),     // e
                        entries_[8]->clone()),         // i
                    Expression::make(op::mul,          // bfg
                        Expression::make(op::mul,      // bf
                            entries_[1]->clone(),      // b
                            entries_[5]->clone()),     // f
                        entries_[6]->clone())),        // g
                Expression::make(op::mul,              // cdh
                    Expression::make(op::mul,          // cd
                        entries_[2]->clone(),          // c
                        entries_[3]->clone()),         // d
                    entries_[2*3+1]->clone())),        // h
            Expression::make(op::add,                  // gec + hfa + idb
                Expression::make(op::add,              // gec + hfa
                    Expression::make(op::mul,          // gec
                        Expression::make(op::mul,      // ge
                            entries_[6]->clone(),      // g
                            entries_[4]->clone()),     // e
                        entries_[2]->clone()),         // c
                    Expression::make(op::mul,          // hfa
                        Expression::make(op::mul,      // hf
                            entries_[7]->clone(),      // h
                            entries_[5]->clone()),     // f
                        entries_[0]->clone())),        // a
                Expression::make(op::mul,              // idb
                    Expression::make(op::mul,          // id
                        entries_[8]->clone(),          // i
                        entries_[3]->clone()),         // d
                    entries_[1]->clone())));           // b

All jokes aside, how would one make this look better?

Edited by TheComet

Share this post

Link to post
Share on other sites

Maybe overload the operators on the Expression type itself (or a type specific to performing AST generation) to make the combined expression.  Then the C++ parser itself will construct your AST creation calls properly due to operator precedence rules.

Also it might be worth investing time in seeing whether your 'entries' can be immutable.  If so, you won't need to clone() them.

Edited by Nypyren

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Advertisement