IT rekvalifikace s garancí práce. Seniorní programátoři vydělávají až 160 000 Kč/měsíc a rekvalifikace je prvním krokem. Zjisti, jak na to!
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

EBLparser

java

        static public void StrToParts(string str)
        {
            parts.RemoveRange(0, parts.Count);
            string buf = "";
            int len = str.Length;
            str += "     ";
            for (int i = 0; i < len; i++)
            {
                if ((str[i] == ' ') || (str[i] == '\t') || (str[i] == '\v') || (str[i] == '\n'))
                {
                    if (buf.Length > 0)
                    {
                        parts.Add(new CodePart(buf, i - buf.Length));
                        buf = "";
                    }
                }
                else if ((str[i] == ';') || (str[i] == ',') || ((str[i] == '.') && (!StrIsNumber(buf))) ||
                    (str[i] == '(') || (str[i] == ')') || (str[i] == '[') || (str[i] == ']') ||
                    (str[i] == '{') || (str[i] == '}') || (str[i] == ':') || (str[i] == '?'))
                {
                    if (buf.Length > 0)
                    {
                        parts.Add(new CodePart(buf, i - buf.Length));
                        buf = "";
                    }
                    parts.Add(new CodePart(str[i].ToString(), i));
                }
                else if (str[i] == '~')
                {
                    if (buf.Length > 0)
                    {
                        parts.Add(new CodePart(buf, i - buf.Length));
                        buf = "";
                    }
                    if ((i < len - 1) && (str[i + 1] == '/'))
                    {
                        parts.Add(new CodePart("~", i));
                        i += 2;
                        while ((i < len) && (str[i] != '\n'))
                        {
                            buf += str[i];
                            i++;
                        }
                        parts.Add(new CodePart(buf, i - buf.Length));
                        buf = "";
                        parts.Add(new CodePart("~", i));
                    }
                    else
                    {
                        parts.Add(new CodePart("~", i));
                        i++;
                        while ((i < len) && (str[i] != '~'))
                        {
                            buf += str[i];
                            i++;
                        }
                        parts.Add(new CodePart(buf, i - buf.Length));
                        buf = "";
                        parts.Add(new CodePart("~", i));
                    }
                }
                else if ((str[i] == '<') || (str[i] == '>') || (str[i] == '=') || (str[i] == '!'))
                {
                    if (buf.Length > 0)
                    {
                        parts.Add(new CodePart(buf, i - buf.Length));
                        buf = "";
                    }
                    if (((i + 1) < len) && (str[i + 1] == '='))
                    {
                        parts.Add(new CodePart(str[i] + "=", i));
                        i++;
                    }
                    else
                        parts.Add(new CodePart(str[i].ToString(), i));
                }
                else if ((str[i] == '+') || (str[i] == '-') || (str[i] == '*') || (str[i] == '/'))
                {
                    if (buf.Length > 0)
                    {
                        parts.Add(new CodePart(buf, i - buf.Length));
                        buf = "";
                    }
                    if (((i + 1) < len) && (str[i + 1] == str[i]))
                    {
                        parts.Add(new CodePart(str[i].ToString() + str[i], i));
                        i++;
                    }
                    else if (((i + 1) < len) && (str[i + 1] == '='))
                    {
                        parts.Add(new CodePart(str[i] + "=", i));
                        i++;
                    }
                    else
                        parts.Add(new CodePart(str[i].ToString(), i));
                }
                else if (str[i] == '"')
                {
                    if (buf.Length > 0)
                    {
                        parts.Add(new CodePart(buf, i - buf.Length));
                        buf = "";
                    }
                    buf += '"';
                    i++;
                    while ((i < len) && (str[i] != '"'))
                    {
                        buf += str[i];
                        i++;
                    }
                    buf += '"';
                    parts.Add(new CodePart(buf, i - buf.Length));
                    buf = "";
                }
                else if (str[i] == '\'')
                {
                    if (buf.Length > 0)
                    {
                        parts.Add(new CodePart(buf, i - buf.Length));
                        buf = "";
                    }
                    buf += '\'';
                    i++;
                    while ((i < len) && (str[i] != '\''))
                    {
                        buf += str[i];
                        i++;
                    }
                    buf += '\'';
                    parts.Add(new CodePart(buf, i - buf.Length));
                    buf = "";
                }
                else
                    buf += str[i];
            }
        }

        static public void PartsToCommands()
        {
            commands.RemoveRange(0, commands.Count);
            commands.Add(new Command());
            int c = parts.Count, actual_c = 0;
            for (int i = 0; i < c; i++)
            {
                if (parts[i].data.Equals(";", StringComparison.Ordinal))
                {
                    commands.Add(new Command());
                    actual_c++;
                }
                else if (parts[i].data.Equals("{", StringComparison.Ordinal))
                {
                    commands.Add(new Command());
                    actual_c++;
                    commands[actual_c].words.Add(parts[i]);
                    commands.Add(new Command());
                    actual_c++;
                }
                else if ((parts[i].data.Length > 0) && (parts[i].data[0] == '#'))
                {
                    commands.Add(new Command());
                    actual_c++;
                    commands[actual_c].words.Add(parts[i]);
                    commands.Add(new Command());
                    actual_c++;
                }
                else if (parts[i].data.Equals("~", StringComparison.Ordinal) && (i < (c - 3)) && parts[i + 2].data.Equals("~", StringComparison.Ordinal))
                {
                    commands.Add(new Command());
                    actual_c++;
                    commands[actual_c].words.Add(parts[i]);
                    i++;
                    commands[actual_c].words.Add(parts[i]);
                    i++;
                    commands[actual_c].words.Add(parts[i]);
                    commands.Add(new Command());
                    actual_c++;
                }
                else if (parts[i].data.Equals("}", StringComparison.Ordinal))
                {
                    commands[actual_c].words.Add(parts[i]);
                    commands.Add(new Command());
                    actual_c++;
                }
                else if ((i < (c - 2)) &&
                    (parts[i].data.Equals("<", StringComparison.Ordinal)) &&
                    (parts[i + 2].data.Equals(">", StringComparison.Ordinal)))
                {
                    commands.Add(new Command(true));
                    actual_c++;
                    commands[actual_c].words.Add(parts[i + 1]);
                    i += 2;
                    commands.Add(new Command());
                    actual_c++;
                }
                else
                {
                    commands[actual_c].words.Add(parts[i]);
                }
            }
            for (int i = 0; i < commands.Count; )
            {
                if (commands[i].words.Count == 0)
                    commands.RemoveAt(i);
                else
                    i++;
            }

            ////****************************////
            Command.FreeOwnWords();
            Command cc;
            for (int i = 0; i < CodeHandler.GetCommandCount(); i++)
            {
                cc = CodeHandler.GetCommand(i);
                if ((cc.words.Count == 2) && (cc.words[0].data == "class"))
                    Command.AddOwnWord(cc.words[1].data, new Color[] {
                        Color.FromArgb(64, 64, 160),
                        Color.FromArgb(128, 128, 255)
                    });
            }
        }

        public void CompileJSPage()
        {
            string output = "";
            bool IsInEvent = false;
            bool[] events = new bool[3];
            bool errorFound = false;
            string errorString = "";
            Command c;
            for (int i = 0; i < CodeHandler.GetCommandCount(); i++)
            {
                c = CodeHandler.GetCommand(i);
                if (c.words.Count == 0 || c.words[0].data == "~")
                    continue;
                if (c.isEvent)
                {
                    if (IsInEvent)
                        output += "}\n";
                    IsInEvent = true;
                    output += "function " + c.words[0].data + '(';
                    if (c.words[0].data == "start")
                    {
                        events[(int)MyEvents.Start] = true;
                        output += ")\n{";
                    }
                    else if (c.words[0].data == "step")
                    {
                        events[(int)MyEvents.Step] = true;
                        output += ")\n{";
                    }
                    else if (c.words[0].data == "end")
                    {
                        events[(int)MyEvents.End] = true;
                        output += "endReturnValue)\n{";
                    }
                }
                else if ((c.words[0].data == "real" ||
                    c.words[0].data == "text" || c.words[0].data == "char" ||
                    c.words[0].data == "long" || c.words[0].data == "sint" || c.words[0].data == "uint" || c.words[0].data == "byte" ||
                    c.words[0].data == "bool") && (c.words.Count > 2) && c.words[2].data != "(")
                {// proměnná
                    if (!variables.ContainsKey(c.words[1].data))
                    {
                        if (c.words[2].data != "=")
                        {
                            output += "var " + c.words[1].data + " = ";
                            for (int j = 2; j < c.words.Count; j++)
                                output += c.words[j].data;
                        }
                        else
                        {
                            output += "var " + c.words[1].data + " = ";
                            for (int j = 3; j < c.words.Count; j++)
                                output += c.words[j].data;
                        }
                        output += ';';
                        variables.Add(c.words[1].data, new EBLvariable(c.words[1].data, c.words[0].data, ""));
                    }
                    else
                    {
                        errorFound = true;
                        errorString = "This variable already exists: " + c.words[1].data;
                        break;
                    }
                }
                else if ((c.words[0].data == "real" ||
                    c.words[0].data == "text" || c.words[0].data == "char" ||
                    c.words[0].data == "long" || c.words[0].data == "sint" || c.words[0].data == "uint" || c.words[0].data == "byte" ||
                    c.words[0].data == "bool") && (c.words.Count == 2))
                {// proměnná
                    if (!variables.ContainsKey(c.words[1].data))
                    {
                        output += "var " + c.words[1].data + ';';
                        variables.Add(c.words[1].data, new EBLvariable(c.words[1].data, c.words[0].data, ""));
                    }
                    else
                    {
                        errorFound = true;
                        errorString = "This variable already exists: " + c.words[1].data;
                        break;
                    }
                }
                else if ((c.words[0].data == "inst") && (c.words.Count > 5))
                {// objekt
                    if (!variables.ContainsKey(c.words[1].data))
                    {
                        if (c.words[2].data != "=")
                        {
                            output += "var " + c.words[1].data + " = new " + c.words[3].data;
                            for (int j = 4; j < c.words.Count; j++)
                                output += c.words[j].data;
                        }
                        else
                        {
                            output += "var " + c.words[1].data + " = new " + c.words[4].data;
                            for (int j = 5; j < c.words.Count; j++)
                                output += c.words[j].data;
                        }
                        output += ';';
                        variables.Add(c.words[1].data, new EBLvariable(c.words[1].data, c.words[0].data, ""));
                    }
                    else
                    {
                        errorFound = true;
                        errorString = "This variable already exists: " + c.words[1].data;
                        break;
                    }
                }
                else if ((c.words[0].data == "&end") && (c.words.Count > 1))
                {// ukončení
                    output += "appContinuesLoop = false;\nreturn ";
                    for (int j = 1; j < c.words.Count; j++)
                        output += c.words[j].data;
                    output += ';';
                }
                else if ((c.words[0].data == "std_out") && (c.words.Count > 3) &&
                    (c.words[1].data == "(") && (c.words[c.words.Count - 1].data == ")"))
                {// výstup
                    output += "document.getElementById(\"appOutputPlace\").innerHTML += htmlspecialchars(";

                    string strstr = "";

                    for (int j = 2; j < c.words.Count - 1; j++)
                    {
                        if (
                            (
                              (c.words[j].data.Length > 2) &&
                              (c.words[j].data[0] == '"') &&
                              (c.words[j].data[c.words[j].data.Length - 1] == '"')
                            ) ||
                            (
                              (c.words[j].data.Length > 3) &&
                              (c.words[j].data[0] == '\'') &&
                              (c.words[j].data[c.words[j].data.Length - 1] == '\'')
                            )
                          )
                        {
                            strstr = "";
                            for (int h = 1; h < (c.words[j].data.Length - 1); h++)
                            {
                                if ((c.words[j].data[h] == '\\') && (c.words[j].data[h + 1] == '@'))
                                {
                                    strstr += '@';
                                    h++;
                                }
                                else if (c.words[j].data[h] == '@')
                                {
                                    strstr += "\"+";
                                    h++;
                                    while ((h < (c.words[j].data.Length - 1)) && (c.words[j].data[h] != '@'))
                                    {
                                        strstr += c.words[j].data[h];
                                        h++;
                                    }
                                    strstr += "+\"";
                                }
                                else
                                    strstr += c.words[j].data[h];
                            }
                            output += '"' + strstr + '"';
                        }
                        else
                            output += c.words[j].data;
                    }
                    output += ");";
                }
                else if ((c.words[0].data == "box_out") && (c.words.Count > 3) &&
                    (c.words[1].data == "(") && (c.words[c.words.Count - 1].data == ")"))
                {// výstup
                    output += "alert(htmlspecialchars(";

                    string strstr = "";

                    for (int j = 2; j < c.words.Count - 1; j++)
                    {
                        if (
                            (
                              (c.words[j].data.Length > 2) &&
                              (c.words[j].data[0] == '"') &&
                              (c.words[j].data[c.words[j].data.Length - 1] == '"')
                            ) ||
                            (
                              (c.words[j].data.Length > 3) &&
                              (c.words[j].data[0] == '\'') &&
                              (c.words[j].data[c.words[j].data.Length - 1] == '\'')
                            )
                          )
                        {
                            strstr = "";
                            for (int h = 1; h < (c.words[j].data.Length - 1); h++)
                            {
                                if ((c.words[j].data[h] == '\\') && (c.words[j].data[h + 1] == '@'))
                                {
                                    strstr += '@';
                                    h++;
                                }
                                else if (c.words[j].data[h] == '@')
                                {
                                    strstr += "\"+";
                                    h++;
                                    while ((h < (c.words[j].data.Length - 1)) && (c.words[j].data[h] != '@'))
                                    {
                                        strstr += c.words[j].data[h];
                                        h++;
                                    }
                                    strstr += "+\"";
                                }
                                else
                                    strstr += c.words[j].data[h];
                            }
                            output += '"' + strstr + '"';
                        }
                        else
                            output += c.words[j].data;
                    }
                    output += "));";
                }
                else if ((c.words[0].data == "{") || (c.words[0].data == "}"))
                {// {}
                    output += c.words[0].data;
                }
                else if (((c.words[0].data == "while") || (c.words[0].data == "if")) &&
                    (c.words.Count() > 3) &&
                    (c.words[1].data == "(") && (c.words[c.words.Count() - 1].data == ")"))
                {// while cyklus, if
                    output += c.words[0].data + "(";
                    for (int j = 2; j < c.words.Count - 1; j++)
                        output += c.words[j].data;
                    output += ")";
                }
                else if ((c.words.Count() == 2) && ((c.words[1].data == "++") || (c.words[1].data == "--")))
                {// ...++, ...--
                    output += c.words[0].data + c.words[1].data + ';';
                }
                else if ((c.words.Count() == 2) && (c.words[1].data == "**"))
                {// ...**
                    output += c.words[0].data + "=" + c.words[0].data + "*" + c.words[0].data + ';';
                }
                else if ((c.words.Count() == 2) && (c.words[1].data == "//"))
                {// ...//
                    output += c.words[0].data + "=Math.sqrt(" + c.words[0].data + ");";
                }
                else if ((c.words.Count() == 2) && ((c.words[0].data == "++") || (c.words[0].data == "--")))
                {// ++..., --...
                    output += c.words[1].data + c.words[0].data + ';';
                }
                else if ((c.words.Count() == 2) && (c.words[0].data == "**"))
                {// **...
                    output += c.words[1].data + "=" + c.words[1].data + "*" + c.words[1].data + ';';
                }
                else if ((c.words.Count() == 2) && (c.words[0].data == "//"))
                {// //...
                    output += c.words[1].data + "=Math.sqrt(" + c.words[1].data + ");";
                }
                else if ((c.words.Count() > 2) && (
                    (c.words[1].data == "=") ||
                    (c.words[1].data == "+=") ||
                    (c.words[1].data == "-=") ||
                    (c.words[1].data == "*=") ||
                    (c.words[1].data == "/=") ||
                    (c.words[1].data == "%=")))
                {// ... = ...
                    for (int j = 0; j < c.words.Count; j++)
                        output += c.words[j].data;
                    output += ';';
                }





                output += '\n';
            }
            if (IsInEvent)
                output += "}\n";

            if(!errorFound)
            {
                try
                {
                    using (StreamWriter sw = new StreamWriter("CompilerOutput\\WEB_PAGE_platform\\index.html"))
                    {
                        sw.Write("<!DOCTYPE html>\n<html>\n\t<head>\n\t\t<meta charset=\"utf-8\">\n");
                        sw.Write("\t\t<title>Aplication</title>\n\t</head>\n\t<body>\n");
                        sw.Write("\t\t<div style=\"width: 100%;\" id=\"appOutputPlace\"></div>\n");
                        sw.Write("\t\t<script type=\"text/javascript\">\n");
                        sw.Write("function htmlspecialchars(str) {\n");
                        sw.Write("\tif (typeof(str) == \"string\") {\n");
                        sw.Write("\t\tstr = str.replace(/&/g, \"&amp;\");\n");
                        sw.Write("\t\tstr = str.replace(/\\\"/g, \"&quot;\");\n");
                        sw.Write("\t\tstr = str.replace(/'/g, \"&#039;\");\n");
                        sw.Write("\t\tstr = str.replace(/</g, \"&lt;\");\n");
                        sw.Write("\t\tstr = str.replace(/>/g, \"&gt;\");\n");
                        sw.Write("\t\tstr = str.replace(/\\n/g, \"<br />\");\n");
                        sw.Write("\t}\n\treturn str;\n}");
                        sw.Write("var appContinuesLoop = true;\nvar appEndReturnValue = 0;\n");
                        sw.Write(output);
                        if (events[(int)MyEvents.Start])
                            sw.Write("start();\n");
                        if (events[(int)MyEvents.Step])
                        {
                            sw.Write("function appRunLoop(){\n");
                            sw.Write("\tif(appContinuesLoop === true){\n");
                            sw.Write("\t\tsetTimeout(appRunLoop, 1000 / 60);\n");
                            sw.Write("\t\tstep();\n");
                            sw.Write("\t}\n");
                            sw.Write("}\n");
                            sw.Write("appRunLoop();\n");
                        }
                        if (events[(int)MyEvents.End])
                            sw.Write("end(appEndReturnValue);\n");
                        sw.Write("\t\t</script>\n\t</body>\n</html>");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An error has occured while saving the output\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                MessageBox.Show(output);
            }
            else
                MessageBox.Show("\t" + errorString, "Error found in code", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

Neformátovaný

Přidáno: 10.2.2014
Expirace: Neuvedeno

Aktivity