public
Authored by avatar Nick Yurchenko

Boolean expression AST traversal in JS

Edited
BoolExpr.js 843 bytes
// naive approach

var expr = { _type : "BoolExpr", _const : "And"
           , values : [ { _type : "BoolExpr", _const : "True", values : {} }
                      , { _type : "BoolExpr", _const : "Or"
                        , values : [ { _type : "BoolExpr", _const : "False", values : {} }
                                   , { _type : "BoolExpr", _const : "True", values : {} }
                                   ]
                        }
                      ]
           }

function view(expr) {
    switch(expr._const){
        case "True":
            return "1";
        case "False":
            return "0";
        case "And":
            return "(" + view(expr.values[0]) + " /\\ " + view(expr.values[1]) + ")";
        case "Or":
            return "(" + view(expr.values[0]) + " \\/ " + view(expr.values[1]) + ")";
    }
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment