Exemplo n.º 1
0
        void ApplyRedirections()
        {
            var mapping = new Dictionary <Model.Element, Model.Element>();

            foreach (var name in new string[] { "U_2_bool", "U_2_int" })
            {
                Model.Func f = Model.TryGetFunc(name);
                if (f != null && f.Arity == 1)
                {
                    foreach (var ft in f.Apps)
                    {
                        mapping[ft.Args[0]] = ft.Result;
                    }
                }
            }

            Model.Substitute(mapping);
        }
Exemplo n.º 2
0
        internal override void Run()
        {
            var selectFunctions = new Dictionary <int, Model.Func>();
            var storeFunctions  = new Dictionary <int, Model.Func>();

            while (true)
            {
                var line = ReadLine();
                if (line == null)
                {
                    break; // end of model, everything fine
                }
                if (line == "Counterexample:" || line == "Error model: " || line == "*** MODEL")
                {
                    NewModel();
                    continue;
                }

                if (line.EndsWith(": Invalid.") || line.EndsWith(": Valid.") || line.StartsWith("labels:"))
                {
                    continue;
                }
                if (line == "END_OF_MODEL" || line == "." || line == "*** END_MODEL")
                {
                    continue;
                }

                var words = GetFunctionTokens(line);
                if (words.Count == 0)
                {
                    continue;
                }
                var lastWord = words[words.Count - 1];

                if (currModel == null)
                {
                    BadModel("model begin marker not found");
                }

                if (line.StartsWith("*** STATE "))
                {
                    var name = line.Substring(10);
                    Model.CapturedState cs;
                    if (name == "<initial>")
                    {
                        cs = currModel.InitialState;
                    }
                    else
                    {
                        cs = currModel.MkState(name);
                    }
                    while (true)
                    {
                        var tmpline = ReadLine();
                        if (tmpline == "*** END_STATE")
                        {
                            break;
                        }
                        var tuple = GetFunctionTokens(tmpline);
                        if (tuple == null)
                        {
                            BadModel("EOF in state table");
                        }
                        if (tuple.Count == 0)
                        {
                            continue;
                        }
                        if (tuple.Count == 3 && tuple[0] is string && tuple[1] is string && ((string)tuple[1]) == "->")
                        {
                            cs.AddBinding((string)tuple[0], GetElt(tuple[2]));
                        }
                        else if (tuple.Count == 2 && tuple[1] is string && ((string)tuple[1]) == "->")
                        {
                            // This line says that words[0] has no value in the model.
                            // Ignore this line.
                        }
                        else
                        {
                            BadModel("invalid state tuple definition");
                        }
                    }

                    continue;
                }

                if (words.Count == 3 && words[1] is string && ((string)words[1]) == "->")
                {
                    var        funName = (string)words[0];
                    Model.Func fn      = null;

                    if (lastWord is string && ((string)lastWord) == "{")
                    {
                        fn = currModel.TryGetFunc(funName);
                        while (true)
                        {
                            var tuple = GetFunctionTokens(ReadLine());
                            if (tuple == null)
                            {
                                BadModel("EOF in function table");
                            }
                            if (tuple.Count == 0)
                            {
                                continue;
                            }
                            string tuple0 = tuple[0] as string;
                            if (tuple.Count == 1)
                            {
                                if (fn == null)
                                {
                                    fn = currModel.MkFunc(funName, 1);
                                }
                                if (tuple0 == "}")
                                {
                                    break;
                                }
                                if (fn.Else == null)
                                {
                                    fn.Else = GetElt(tuple[0]);
                                }
                                continue;
                            }

                            string tuplePenultimate = tuple[tuple.Count - 2] as string;
                            if (tuplePenultimate != "->")
                            {
                                BadModel("invalid function tuple definition");
                            }
                            var resultName = tuple[tuple.Count - 1];
                            if (tuple0 == "else")
                            {
                                if (fn != null && !(resultName is string && ((string)resultName) == "#unspecified") && fn.Else == null)
                                {
                                    fn.Else = GetElt(resultName);
                                }

                                continue;
                            }

                            if (fn == null)
                            {
                                var arity = tuple.Count - 2;
                                if (Regex.IsMatch(funName, "^MapType[0-9]*Select$"))
                                {
                                    funName = string.Format("[{0}]", arity);
                                    if (!selectFunctions.TryGetValue(arity, out fn))
                                    {
                                        fn = currModel.MkFunc(funName, arity);
                                        selectFunctions.Add(arity, fn);
                                    }
                                }
                                else if (Regex.IsMatch(funName, "^MapType[0-9]*Store$"))
                                {
                                    funName = string.Format("[{0}:=]", arity);
                                    if (!storeFunctions.TryGetValue(arity, out fn))
                                    {
                                        fn = currModel.MkFunc(funName, arity);
                                        storeFunctions.Add(arity, fn);
                                    }
                                }
                                else
                                {
                                    fn = currModel.MkFunc(funName, arity);
                                }
                            }

                            var args = new Model.Element[fn.Arity];
                            for (int i = 0; i < fn.Arity; ++i)
                            {
                                args[i] = GetElt(tuple[i]);
                            }
                            fn.AddApp(GetElt(resultName), args);
                        }
                    }
                    else
                    {
                        fn = currModel.MkFunc(funName, 0);
                        fn.SetConstant(GetElt(lastWord));
                    }
                }
                else if (words.Count == 2 && words[1] is string && ((string)words[1]) == "->")
                {
                    // This line says that words[0] has no value in the model.
                    // Ignore this line.
                }
                else
                {
                    BadModel("unidentified line");
                }
            }
        }
Exemplo n.º 3
0
        internal override void Run()
        {
            var selectFunctions = new Dictionary <int, Model.Func> ();
            var storeFunctions  = new Dictionary <int, Model.Func> ();

            arrayNum = 0;

            for (; ;)
            {
                var line = ReadLine();
                if (line == null)
                {
                    break;                     // end of model, everything fine
                }
                if (line == "Counterexample:" || line == "Error model: " || line == "*** MODEL")
                {
                    NewModel();
                    continue;
                }
                if (line.EndsWith(": Invalid.") || line.EndsWith(": Valid.") || line.StartsWith("labels:"))
                {
                    continue;
                }
                if (line == "END_OF_MODEL" || line == "." || line == "*** END_MODEL")
                {
                    continue;
                }

//				Console.WriteLine("\n\n# :: " + line);

                var words = GetParsedTokens(line);
                if (words.Count == 0)
                {
                    continue;
                }
                var exprs = TrySplitExpr(words);

                foreach (List <string> expr in exprs)
                {
//					Console.WriteLine ("");
//					for (int i = 0; i < expr.Count; i++) {
//						Console.Write (expr [i] + " ");
//					}

                    var lastToken = expr [expr.Count - 1];
                    if (currModel == null)
                    {
                        BadModel("model begin marker not found");
                    }

                    if (expr.Count > 3 && expr [1] == "->")
                    {
                        var        funName = (string)expr [0];
                        Model.Func fn      = null;
                        int        i       = 4;

                        if (expr [2] != "{" && expr [6] != "{")
                        {
                            BadModel("unidentified function definition 5");
                        }

                        fn = currModel.TryGetFunc(funName);

                        for (; ;)
                        {
                            if (expr [i] == "}")
                            {
                                if (i == expr.Count - 1)
                                {
                                    if (fn == null)
                                    {
                                        fn = currModel.MkFunc(funName, 1);
                                    }
                                    break;
                                }
                                else
                                {
                                    i++;
                                    continue;
                                }
                            }

                            for (; ;)
                            {
                                if (expr [i] == " ")
                                {
                                    i++;
                                    break;
                                }

                                if ((i == 4 || i == 8) && expr [i - 1] == " " && expr [i + 1] == " ")
                                {
                                    if (fn.Else == null)
                                    {
                                        fn.Else = GetElt(expr [i]);
                                    }
                                    i++;
                                    continue;
                                }

                                if (expr [i] == "else")
                                {
                                    if (expr [i + 1] == "->")
                                    {
                                        i += 2;

                                        if (expr [i] == "}")
                                        {
                                            BadModel("unidentified function definition 1");
                                        }

                                        if (expr [i] == "{")
                                        {
                                            i++;
                                            continue;
                                        }
                                        else
                                        {
                                            if (fn != null && !(expr [i] == "#unspecified") && fn.Else == null)
                                            {
                                                fn.Else = GetElt(expr [i]);
                                            }
                                            i++;
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        BadModel("unidentified function definition 2");
                                    }
                                }

                                int arity = 0;
                                for (; ;)
                                {
                                    arity++;
                                    if (expr [arity + i] == " ")
                                    {
                                        arity -= 2;
                                        break;
                                    }
                                }

                                if (expr [i + arity] == "->")
                                {
                                    i += arity + 1;

                                    if (expr [i] == "}")
                                    {
                                        BadModel("unidentified function definition 3");
                                    }
                                }
                                else
                                {
                                    BadModel("unidentified function definition 4");
                                }

                                if (fn == null)
                                {
                                    if (Regex.IsMatch(funName, "^MapType[0-9]*Select$"))
                                    {
                                        funName = string.Format("[{0}]", arity);

                                        if (!selectFunctions.TryGetValue(arity, out fn))
                                        {
                                            fn = currModel.MkFunc(funName, arity);
                                            selectFunctions.Add(arity, fn);
                                        }
                                    }
                                    else if (Regex.IsMatch(funName, "^MapType[0-9]*Store$"))
                                    {
                                        funName = string.Format("[{0}:=]", arity);

                                        if (!storeFunctions.TryGetValue(arity, out fn))
                                        {
                                            fn = currModel.MkFunc(funName, arity);
                                            storeFunctions.Add(arity, fn);
                                        }
                                    }
                                    else
                                    {
                                        fn = currModel.MkFunc(funName, arity);
                                    }
                                }

                                var args = new Model.Element[fn.Arity];

                                for (var idx = 0; idx < fn.Arity; ++idx)
                                {
                                    args [idx] = GetElt(expr [idx + i - arity - 1]);
                                }

                                fn.AddApp(GetElt(expr [i]), args);

                                i++;
                                break;
                            }
                        }
                    }
                    else if (expr.Count == 3 && expr [1] == "->")
                    {
                        var        funName = (string)expr [0];
                        Model.Func fn      = null;

                        fn = currModel.MkFunc(funName, 0);
                        fn.SetConstant(GetElt(lastToken));
                    }
                    else
                    {
                        BadModel("unidentified line");
                    }
                }
            }
        }