Example #1
0
        /**
         * Fix constants in a given AST
         * Example: convert relations over real constants (listed as integers) and real variables to floats
         */
        public static void FixTypes(ref CommonTree ast)
        {
            if (ast != null)
            {
                switch (ast.Type)
                {
                case guardLexer.FLOAT:
                {
                    // get lowest parent with at least one other child
                    CommonTree p = (CommonTree)ast.Parent;
                    while (p.ChildCount < 2)
                    {
                        p = (CommonTree)p.Parent;
                    }
                    // get first child on other side of parent
                    int        i      = 0;
                    CommonTree otherC = (CommonTree)p.Children[i];
                    while (otherC == ast)
                    {
                        otherC = (CommonTree)p.Children[++i];
                    }
                    String var = otherC.Children[0].Text;         // TODO: CASE CHECKS
                    if (Controller.Instance.GlobalVariables.ContainsKey(var) && Controller.Instance.GlobalVariables[var].Sort != Controller.Instance.RealType ||
                        Controller.Instance.UndefinedVariables.ContainsKey(var) && Controller.Instance.UndefinedVariables[var].Sort != Controller.Instance.RealType)
                    // TODO: ADD OTHER CASES
                    {
                        if (Controller.Instance.GlobalVariables.ContainsKey(var) && Controller.Instance.GlobalVariables[var].Sort != Controller.Instance.IntType)
                        {         // bitvector / boolean
                            ast = copyTree(ast, ast, guardLexer.BOOLEAN);
                        }
                        else
                        {
                            //ast.Type = guardLexer.INTEGER; // no settor...simplest method actually apears to just do a (non-recursive) copy, where you CAN modify the type of the token, e.g., ast.Token.Type, which is rather annoying
                            ast = copyTree(ast, ast, guardLexer.INTEGER);
                        }
                        // TODO: STRIP ALL DECIMAL POINTS???
                    }
                    break;
                }

                default:
                {
                    for (int i = 0; i < ast.ChildCount; i++)
                    {
                        CommonTree tmp = (CommonTree)ast.Children[i];
                        FixTypes(ref tmp);
                        ast.SetChild(i, tmp);
                    }
                    break;
                }
                }
            }
        }