Beispiel #1
0
        public string fileHash           = null;   //do not store this in protobuf

        private Databank_1_2()
        {
            //This is ONLY because protobuf-net needs it
            //without line below, protobuf probably crashes
            this.storage = new GekkoDictionary <string, IVariable_1_2>(StringComparer.OrdinalIgnoreCase);
            //this.storage = new GekkoDictionary<string, Series_1_2>(StringComparer.OrdinalIgnoreCase);  //cf. #db12
        }
Beispiel #2
0
        public static void PrintAST(CommonTree node, int depth, StringBuilder sb, GekkoDictionary <string, string> scalars)
        {
            if (depth > 0)
            {
                string s = null;
                if (node.Text == "ASTCOMMAND")
                {
                    s = HandleCommand123(node, sb, s, scalars);
                    return;  //all sub-nodes are handled
                }
                else
                {
                    s = ReplaceNames(node.Text);
                }

                //------ handling stuff not in COMMAND OPTION REST form

                if (s != null && s.StartsWith("()"))
                {
                    s = "//" + s.Substring(2);
                }

                if (s != null && s.Trim().ToLower().StartsWith("display"))
                {
                    int    i  = FindFirstNonBlank(s);
                    string ss = s.Substring(i + 7);
                    if (ss.EndsWith(G.NL))
                    {
                        ss = ss.Substring(0, ss.Length - 2);
                    }
                    s = "tell '" + ss + "';" + G.NL;;
                }

                if (s != null && s.Trim().ToLower().StartsWith("hdg"))
                {
                    int    i  = FindFirstNonBlank(s);
                    string ss = s.Substring(i + 3);
                    if (ss.EndsWith(G.NL))
                    {
                        ss = ss.Substring(0, ss.Length - 2);
                    }
                    s = "hdg '" + ss + "';" + G.NL;
                }

                sb.Append(s);
            }
            if (node.Children != null)
            {
                for (int i = 0; i < node.Children.Count; ++i)
                {
                    CommonTree child = (CommonTree)(node.Children[i]);
                    PrintAST(child, depth + 1, sb, scalars);
                }
            }
        }
Beispiel #3
0
 public Databank(string aliasName)
 {
     this.storage   = new GekkoDictionary <string, TimeSeries>(StringComparer.OrdinalIgnoreCase);
     this.aliasName = aliasName;
     //this.aliasNameOriginal = aliasName;
 }
Beispiel #4
0
        public string fileHash           = null; //do not store this in protobuf

        private Databank()
        {
            //This is ONLY because protobuf-net needs it
            this.storage = new GekkoDictionary <string, TimeSeries>(StringComparer.OrdinalIgnoreCase);
        }
Beispiel #5
0
        public static string Translate1(bool file, List <string> inputFileLines)
        {
            string textInput = null;

            textInput = G.ExtractTextFromLines(inputFileLines).ToString();

            string s3 = HandleUpd(textInput);

            ANTLRStringStream input = new ANTLRStringStream(s3);
            CommonTree        t     = null;
            // Create a lexer attached to that input
            T1Parser parser2 = null;
            T1Lexer  lexer2  = new T1Lexer(input);
            //usually debugTokens=false, and this is stepped into manually (otherwise the tokens are consumed and preliminary steps cannot be run)
            // Create a stream of tokens pulled from the lexer
            CommonTokenStream tokens = new CommonTokenStream(lexer2);

            // Create a parser attached to the token stream
            parser2 = new T1Parser(tokens);
            // Invoke the program rule in get return value
            T1Parser.expr_return r  = null;
            DateTime             t0 = DateTime.Now;


            try
            {
                r = parser2.expr();
            }
            catch (Exception e)
            {
                List <string> temp = new List <string>();
                temp.Add(e.Message);
                string input2 = textInput + "\r\n";
                if (file)
                {
                    Translator2.PrintError(e.Message, true);
                    G.Writeln2("*** ERROR: Could not parse the file");
                    throw new GekkoException(); //this will make a double error -- but the other one will be identified later on (both text and filename are null) and skipped -- a little bit hacky, but oh well...
                }
                else
                {
                    return("");
                }
            }

            if (parser2.GetErrors().Count > 0)
            {
                List <string> errors = parser2.GetErrors();
                if (file)
                {
                    foreach (string s in errors)
                    {
                        Translator2.PrintError(s, false);
                    }
                    G.Writeln2("*** ERROR: Could not parse the file");
                    throw new GekkoException(); //this will make a double error -- but the other one will be identified later on (both text and filename are null) and skipped -- a little bit hacky, but oh well...
                }
                else
                {
                    return("");
                }
            }
            t = (CommonTree)r.Tree;
            StringBuilder sb = new StringBuilder();
            GekkoDictionary <string, string> scalars = new GekkoDictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            hasBeenEndoExo = false;
            hasBeenCmdCall = false;
            PrintAST(t, 0, sb, scalars);
            List <string> xxx = G.ExtractLinesFromText(sb.ToString());
            string        sss = null;

            foreach (string s in xxx)
            {
                string s2 = s;
                if (s2 == ";")
                {
                    s2 = "";
                }
                s2 = s2.Replace(";;", ";");
                s2 = s2.Replace("; ;", ";");
                string alphabet = "abcdefghijklmnopqrstuvwxyz";
                foreach (char c in alphabet)
                {
                    string s5 = c.ToString();
                    string s6 = c.ToString().ToUpper();
                    s2 = s2.Replace(";" + s5, "; " + s5);
                    s2 = s2.Replace(";" + s6, "; " + s6);
                }
                s2 = s2.Replace("__dollar__", "$");

                sss += s2 + G.NL;
            }
            return(sss);
        }
Beispiel #6
0
        private static string HandleCommand123(CommonTree node, StringBuilder sb, string s, GekkoDictionary <string, string> scalars)
        {
            string        comment = null;
            string        s1      = "";
            string        s2      = "";
            string        s3      = "";
            List <string> all1    = new List <string>();
            List <string> all2    = new List <string>();
            List <string> all3    = new List <string>();

            GetTokens(node, all1, all2, all3);
            //bool importHelper = false;

            // =========== command ========================

            string type = "";

            foreach (string xx1 in all1)
            {
                type += xx1.Trim().ToLower();
            }

            string typeOrig = "";

            foreach (string xx1 in all1)
            {
                typeOrig += xx1.Trim();
            }

            List <string> listTypes  = GetListTypes();
            bool          isListType = false;

            if (listTypes.Contains(type))
            {
                isListType = true;
            }

            List <string> prtTypes  = GetPrtTypes();
            bool          isPrtType = false;

            if (prtTypes.Contains(type))
            {
                isPrtType = true;
            }

            int z = 0;

            if (all1[0].Trim() == "")
            {
                z = 1;
            }

            if (type == "endo" || type == "exo" || type == "endogenize" || type == "exogenize")
            {
                hasBeenEndoExo = true;
            }
            else if (type == "add")
            {
                all1[z]        = Cap("RUN", typeOrig);
                hasBeenCmdCall = true;
            }
            else if (type == "clear")
            {
                if (all2.Contains("all", StringComparer.OrdinalIgnoreCase))
                {
                    all1[z] = Cap("RESET", typeOrig);
                }
                else
                {
                    all1[z] = Cap("RESTART", typeOrig);
                }
            }
            else if (type == "closeall")
            {
                all1[z] = Cap("RESTART", typeOrig);
            }
            else if (type == "closebanks")
            {
                all1[z] = Cap("RESTART", typeOrig);
            }
            else if (type == "cplot")
            {
                all1[z] = Cap("CLIP", typeOrig);
            }
            else if (type == "difprt")
            {
                all1[z] = Cap("COMPARE", typeOrig);
            }
            else if (type == "ndifprt")
            {
                all1[z] = Cap("COMPARE", typeOrig);
                AddOption(all2, "abs");
            }
            else if (type == "efter")
            {
                all1[z] = Cap("SIM", typeOrig);
                AddOption(all2, "after");
            }
            else if (type == "genr")
            {
                all1[z] = Cap("SERIES", typeOrig);
            }
            else if (type == "mulbk")
            {
                int ifirst = FindNextRealToken(all3, 0);
                if (ifirst == -12345)
                {
                    //"mulbk"
                    all1[z] = Cap("CLONE", typeOrig);
                }
                else
                {
                    //"mulbk adbk"
                    all1[z] = Cap("READ", typeOrig);
                    AddOption(all2, "ref");
                }
            }
            else if (type == "pplot")
            {
                all1[z] = Cap("PLOT", typeOrig);
            }
            else if (type == "string")
            {
                all1[z] = Cap("NAME", typeOrig);  //usually the best type
            }
            else if (type == "res")
            {
                all1[z] = Cap("SIM", typeOrig);
                AddOption(all2, "res");
            }
            else if (type == "sim")
            {
                all1[z] = Cap("SIM", typeOrig);
                if (hasBeenEndoExo)
                {
                    AddOption(all2, "fix");
                }
            }
            else if (type == "udvalg")
            {
                all1[z] = Cap("DECOMP", typeOrig);
            }
            else if (type == "wplot")
            {
                all1[z] = Cap("SHEET", typeOrig);
            }
            else if (type == "read")
            {
            }
            else if (type == "run")
            {
                hasBeenCmdCall = true;
            }
            else if (type == "trimvars")
            {
                all1[z] = Cap("DELETE", typeOrig);
                AddOption(all2, "nonmodel");
            }
            else if (type == "upd")
            {
                all1[z] = Cap("SERIES", typeOrig);
            }
            else if (type == "write")
            {
            }
            // ----------------------------------------------------
            // ----------------------------------------------------
            else if (type == "gmulprt")
            {
                all1[z] = Cap("MULPRT", typeOrig);
                AddOption(all2, "v");
            }

            if (!commands2.Contains(type))
            {
                if (FindNextRealToken(all2, 0) == -12345 && FindNextRealToken(all3, 0) == -12345)
                {
                    //standalone unknown ident
                    all1[FindNextRealToken(all1, 0)] = "RUN " + all1[FindNextRealToken(all1, 0)];
                    hasBeenCmdCall = true;
                }
            }

            for (int i = 0; i < all1.Count; i++)
            {
                s1 += all1[i];
            }

            // =========== options ========================

            for (int i = 0; i < all2.Count; i++)
            {
                if (i < all2.Count - 1 && all2[i] == "#" && G.IsIdentTranslate(all2[i + 1]))
                {
                    all2[i] = "%";  //always change # to % in option field i Gekko 1.8
                }
                s2 += all2[i];
            }

            // =========== rest of command ========================

            //handle stuff like "PRT 2010 2020 fy fe", but not for TIME!
            int    iStart = 0;
            string time   = null;
            int    first  = FindNextRealToken(all3, 0);
            int    second = FindNextRealToken(all3, first + 1);

            if (type != "time" && first != -12345 && second != -12345)
            {
                if (G.IsIntegerTranslate(all3[first]) && G.IsIntegerTranslate(all3[second]))
                {
                    time   = all3[first] + " " + all3[second];
                    iStart = second + 1;
                }
            }

            //register scalar types
            if (type == "val" || type == "date" || type == "string" || type == "name")
            {
                string name = null;
                if (first != -12345)
                {
                    name = all3[first];
                }
                if (G.IsIdentTranslate(name))
                {
                    if (!scalars.ContainsKey(name))
                    {
                        scalars.Add(name, "");
                    }
                }
            }

            for (int i = iStart; i < all3.Count; i++)
            {
                //change soft parens
                if (i > 0 && G.equal(all3[i], "cmd") && G.equal(all3[i - 1], "."))
                {
                    all3[i] = Globals.extensionCommand;
                }

                //Maybe change #x to %x
                if (i < all3.Count - 1 && all3[i] == "#" && G.IsIdentTranslate(all3[i + 1]))
                {
                    bool exist        = false;
                    bool isAllEndoExo = false;
                    if (Program.scalars != null && Program.scalars.ContainsKey(all3[i + 1].Trim()))
                    {
                        exist = true;
                    }
                    foreach (string ss in new string[] { "all", "endo", "exo", "exod", "exodjz", "exoj", "exotrue", "exoz" })
                    {
                        if (all3[i + 1].Trim().ToLower() == ss)
                        {
                            isAllEndoExo = true;
                        }
                    }
                    if (exist || scalars.ContainsKey(all3[i + 1].Trim()))
                    {
                        if (!isAllEndoExo)
                        {
                            all3[i] = "%";  //only if it has been defined before
                        }
                    }
                }

                if (isListType)
                {
                    //setting commas on stuff that looks like lists of idents, lists or scalars
                    if (G.IsIdentTranslate(all3[i]))
                    {
                        int i2 = FindNextRealToken(all3, i + 1);
                        if (i2 == i + 2)
                        {
                            if (G.IsIdentTranslate(all3[i2]) || all3[i2] == "#")
                            {
                                all3[i + 1] = "," + all3[i + 1];
                            }
                        }
                    }
                    s3 += all3[i];
                }
                else if (isPrtType)
                {
                    //setting commas on stuff that looks like lists of idents, lists or scalars
                    //handles "a13 a14" "a/5 b" "a 5*c" "[*] b" "b [*]" "(a+b) (c+d)"
                    if (G.IsIdentTranslate(all3[i]) || IsNumber(all3[i]) || all3[i] == "]" || all3[i] == ")" || all3[i] == "}")
                    {
                        int i2 = FindNextRealToken(all3, i + 1);
                        if (i2 == i + 2)
                        {
                            if (G.IsIdentTranslate(all3[i2]) || all3[i2] == "#" || IsNumber(all3[i2]) || all3[i2] == "[" || all3[i2] == "(" || all3[i2] == "{" || all3[i2] == "@")
                            {
                                all3[i + 1] = "," + all3[i + 1];
                            }
                        }
                    }
                    s3 += all3[i];
                }
                else
                {
                    s3 += all3[i];
                }
            }

            if (type == "option")  //perhaps overriding the s3 made above
            {
                string s10   = "";
                bool   hit   = false;
                string sTemp = "";
                for (int i = 0; i < all3.Count; i++)
                {
                    if (hit)
                    {
                        sTemp += all3[i];
                    }
                    else
                    {
                        string zz = all3[i].Trim().ToLower();
                        if (zz != "")
                        {
                            sTemp += " " + zz;
                        }
                    }

                    if (sTemp == " " + "databank file tsdx compress")
                    {
                        sTemp = " " + "databank file gbk compress";
                        hit   = true;
                    }

                    if (sTemp == " " + "databank file tsdx version")
                    {
                        sTemp = " " + "databank file gbk version";
                        hit   = true;
                    }

                    if (sTemp == " " + "folder cmd")
                    {
                        sTemp = " " + "folder command";
                        hit   = true;
                    }
                    if (sTemp == " " + "folder cmd1")
                    {
                        sTemp = " " + "folder command1";
                        hit   = true;
                    }
                    if (sTemp == " " + "folder cmd2")
                    {
                        sTemp = " " + "folder command2";
                        hit   = true;
                    }
                    if (sTemp == " " + "interface excel decimalseparator")
                    {
                        sTemp = " " + "interface clipboard decimalseparator";
                        hit   = true;
                    }
                    if (sTemp == " " + "option graph lines points")
                    {
                        sTemp = " " + "option plot lines points";
                        hit   = true;
                    }
                    if (sTemp == " " + "option solve data createvars")
                    {
                        sTemp = " " + "option solve data create auto";
                        hit   = true;
                    }
                    if (sTemp == " " + "option forward fair dump")
                    {
                        sTemp = " " + "option forward dump";
                        hit   = true;
                    }
                }
                if (hit)
                {
                    s3 = sTemp;
                }
            }

            // ========================

            if (s1.Trim().ToLower() == "stamp")
            {
                s1 = "tell currentDateTime();";
            }
            if (s1.Trim().ToLower() == "vers")
            {
                s1 = "tell gekkoVersion();";
            }

            if (s1 == null)
            {
                s1 = "";
            }
            if (s2 == null)
            {
                s2 = "";
            }
            if (s3 == null)
            {
                s3 = "";
            }
            if (s2 == "")
            {
                s = s1.TrimEnd() + " " + s3.Trim();
            }
            else
            {
                s = s1.TrimEnd() + " " + s2.Trim() + " " + s3.Trim();
            }
            if (time != null)
            {
                s = Cap("TIME", typeOrig) + " " + time + "; " + s;
            }

            //if (importHelper)
            //{
            //    s = Cap("CLEAR", typeOrig) + "<prim>; " + s + "; " + Cap("CLONE", typeOrig) + "; ";  //will get a superfluous ';' at the end
            //}

            sb.Append(s);

            if (comment != null)
            {
                sb.Append(" /* " + comment + " */ ");
            }

            return(s);
        }