Esempio n. 1
0
        public override WExpr InterpretModel(Context context, Model model, ProgramBuilder builder)
        {
            WCond cond1Conc = (WCond)cond1.InterpretModel(context, model, builder);

            WCCompound.Logic op        = (WCCompound.Logic)GetConcChoice(context, model);
            WCond            cond2Conc = (WCond)cond2.InterpretModel(context, model, builder);
            WExpr            rv        = new WCCompound(cond1Conc, op, cond2Conc);

            builder.CheckExpr(rv);
            return(rv);
        }
        public WExpr parseWExprFromXML(XElement xml)
        {
            //WEVars of program must be well-formed: If a program uses n distinct WEVars, they must have the ids 0...n-1.
            string type = xml.Name.ToString();

            if (type == "var")
            {
                int id = int.Parse(xml.Value);
                return(x[id]);
            }
            if (type == "const")
            {
                int value = int.Parse(xml.Value);
                return(new WEConst(value));
            }
            if (type == "concat")
            {
                XElement[] children = xml.Elements().ToArray();
                WExpr      expr1    = parseWExprFromXML(children[0]);
                WExpr      expr2    = parseWExprFromXML(children[1]);
                return(new WEConcat(expr1, expr2));
            }
            if (type == "while")
            {
                XElement[] children = xml.Elements().ToArray();
                WCond      cond     = (WCond)parseWExprFromXML(children[0]);
                WExpr      body     = parseWExprFromXML(children[1]);
                return(new WEWhile(cond, body));
            }
            if (type == "if")
            {
                XElement[] children = xml.Elements().ToArray();
                WCond      cond     = (WCond)parseWExprFromXML(children[0]);
                WExpr      thenBody = parseWExprFromXML(children[1]);
                if (children.Count() >= 3)
                {
                    WExpr elseBody = parseWExprFromXML(children[2]);
                    return(new WEIf(cond, thenBody, elseBody));
                }
                else
                {
                    return(new WEIf(cond, thenBody));
                }
            }
            if (type == "arith")
            {
                WEArith.ArithOp op;
                if (Enum.TryParse(xml.Attribute("op").Value, out op))
                {
                    XElement[] children = xml.Elements().ToArray();
                    WEVar      lhs      = (WEVar)parseWExprFromXML(children[0]);
                    WEOperand  arg1     = (WEOperand)parseWExprFromXML(children[1]);
                    WEOperand  arg2     = (WEOperand)parseWExprFromXML(children[2]);
                    return(new WEArith(lhs, arg1, op, arg2));
                }
                else
                {
                    //TODO: exception
                    return(null);
                }
            }
            if (type == "not")
            {
                WCond cond = (WCond)parseWExprFromXML((XElement)xml.FirstNode);
                return(new WCNot(cond));
            }
            if (type == "compare")
            {
                WCComparison.CompareType op;
                if (Enum.TryParse(xml.Attribute("op").Value, out op))
                {
                    XElement[] children = xml.Elements().ToArray();
                    WEVar      arg1     = (WEVar)parseWExprFromXML(children[0]);
                    WEOperand  arg2     = (WEOperand)parseWExprFromXML(children[1]);
                    return(new WCComparison(arg1, op, arg2));
                }
                else
                {
                    //TODO: exception
                    return(null);
                }
            }
            if (type == "compound")
            {
                WCCompound.Logic op;
                if (Enum.TryParse(xml.Attribute("op").Value, out op))
                {
                    XElement[] children = xml.Elements().ToArray();
                    WCond      cond1    = (WCond)parseWExprFromXML(children[0]);
                    WCond      cond2    = (WCond)parseWExprFromXML(children[1]);
                    return(new WCCompound(cond1, op, cond2));
                }
                else
                {
                    //TODO: exception
                    return(null);
                }
            }
            //TODO: exception
            return(null);
        }