Ejemplo n.º 1
0
        // Arithmetic Operations (ex: SUM OF)

        /*public void add_to_tables(int line_number, String line)
         * {
         *  Stack<String> op = new Stack<String>();
         *  Stack<String> ident1 = new Stack<String>();
         *  Stack<String> ident2 = new Stack<String>();
         *  add_to_lex(line_number, line, op, ident1, ident2);
         * }
         */
        //public void add_to_lex(int line_number, String line, Stack<String> op, Stack<String> ident1, Stack<String> ident2)
        public void add_to_tables(int line_number, String line)
        {
            Syntax lolcode = new Syntax();

            String arithop     = new Regex("^" + Syntax.aop).Match(line).Groups["aop"].Value;
            String identifier2 = lolcode.r[4].Match(line).Groups["identifier2"].Value;

            int index = 0, pos = -1;

            for (int a = 0; a < lolcode.r[4].Match(line).Groups["identifier1"].Captures.Count; a++)
            {
                if (pos == -1 || lolcode.r[4].Match(line).Groups["identifier1"].Captures[a].Index < pos)
                {
                    pos   = lolcode.r[4].Match(line).Groups["identifier1"].Captures[a].Index;
                    index = a;
                }
            }
            String identifier1 = lolcode.r[4].Match(line).Groups["identifier1"].Captures[index].Value;

            Program.main.lexemes.Rows.Add(arithop, "ARITHMETIC_OPERATOR_KEYWORD");

            if (lolcode.r[4].Match(identifier1).Success)
            {
                ArithmeticOperation aop = new ArithmeticOperation();
                aop.add_to_tables(line_number, identifier1);
            }
            else
            {
                Program.main.lexemes.Rows.Add(identifier1, "FIRST_ARITHMETIC_OPERAND");
            }

            Program.main.lexemes.Rows.Add("AN", "OPERAND_SEPARATOR_KEYWORD");

            if (lolcode.r[4].Match(identifier2).Success)
            {
                ArithmeticOperation aop = new ArithmeticOperation();
                aop.add_to_tables(line_number, identifier2);
            }
            else
            {
                Program.main.lexemes.Rows.Add(identifier2, "SECOND_ARITHMETIC_OPERAND");
            }

            /*
             * Regex r1 = new Regex(@"(?<aop>(SUM\s+OF)|(DIFF\s+OF)|(QUOSHUNT\s+OF)|(MOD\s+OF)|(PRODUKT\s+OF)|(BIGGR\s+OF)|(SMALLR\s+OF))\s+((?<identifier1>"".*""))?.*\s+(?<bop>AN)\s+((?<identifier2>"".*""))?");
             *
             * if (!r1.Match(line).Groups["identifier1"].Value.Equals(""))
             * {
             *  Program.main.console_output.AppendText("Warning at line " + (line_number + 1) + ": First arithmetic operand " + identifier1 + " was interpreted as either Numbar or Numbr to perform operation.\n");
             * }
             * if (!r1.Match(line).Groups["identifier2"].Value.Equals(""))
             * {
             *  Program.main.console_output.AppendText("Warning at line " + (line_number + 1) + ": Second arithmetic operand " + identifier2 + " was interpreted as either Numbar or Numbr to perform operation.\n");
             * }
             */
        }
Ejemplo n.º 2
0
        // Analyze button = interpret code, list errors in the Console Output, display Console (black window)
        private void analyze_Click(object sender, EventArgs e)
        {
            DataType              t            = new DataType();
            Syntax                lolcode      = new Syntax();
            Concatenation         c            = new Concatenation();
            ArithmeticOperation   aop          = new ArithmeticOperation();
            ComparisonOperation   cop          = new ComparisonOperation();
            LinkedList <Variable> variableList = new LinkedList <Variable>();     // linked list for declared variables
            LinkedList <Variable> IT           = new LinkedList <Variable>();

            IT.AddFirst(new Variable("IT", "NOOB", ""));

            String value = "((" + Syntax.stringSyntax + ")|(?<floatValue>" + Syntax.floatingPoint + ")|(?<intValue>" + Syntax.integer + ")|(?<troof>" + Syntax.troof + ")";

            String[] lines   = Program.main.code.Text.Split(new Char[] { '\n' }, StringSplitOptions.None);
            String   retType = "";

            float ITfloat;
            int   counter     = 0;
            int   i           = 0;                  // current line number
            int   start       = -1;                 // line number of HAI keyword
            int   end         = -1;                 // line number of KTHXBYE keyword
            int   matched     = -1;                 // 1 = line matched a regex; -1 = otherwise
            int   cstart      = -1;                 // conditional statement line number
            int   cmatched    = -1;                 // 1 = one of the conditions in if-else/case statements was already executed; -1 = otherwise
            int   performCond = 1;                  // 1 = line inside if-else or case statements; -1 = otherwise
            int   casestart   = -1;                 // case statement line number

            Program.main.lexemes.Rows.Clear();
            Program.main.symbol_table.Rows.Clear();
            Program.main.console_output.Text = "";

            AllocConsole();                             // Opens a console with the given dimensions
            Console.SetWindowSize(90, 50);
            Console.Clear();

            for (i = 0, matched = 0; i < lines.Count(); counter = 0, ITfloat = 0, retType = "", i++, matched = 0)
            {
                // HAI and KTHXBYE
                if (lolcode.r[0].Match(lines[i]).Success)
                {
                    matched = 1;
                    // if HAI is found for the first time
                    if (start == -1 && lolcode.r[0].Match(lines[i]).Groups["delimeter"].Value.Equals("HAI"))
                    {
                        Program.main.lexemes.Rows.Add("HAI", "PROGRAM_DELIMITER");
                        start = i;
                    }
                    // if KTHXBYE is found for the first time
                    else if (end == -1 && lolcode.r[0].Match(lines[i]).Groups["delimeter"].Value.Equals("KTHXBYE"))
                    {
                        Program.main.lexemes.Rows.Add("KTHXBYE", "PROGRAM_DELIMITER");
                        end = i;
                    }
                    // if HAI is found again, produce an error
                    else if (start != -1 && lolcode.r[0].Match(lines[i]).Groups["delimeter"].Value.Equals("HAI"))
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": HAI already found at line " + (start + 1) + ".\n");
                    }
                    // if KTHXBYE is found again, produce an error
                    else
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": KTHXBYE already found at line " + (end + 1) + ".\n");
                    }
                }

                // I HAS A <VARIABLENAME> ?(ITZ <VALUE>)
                else if (lolcode.r[2].Match(lines[i]).Success)
                {
                    matched = 1;
                    String varname = lolcode.r[2].Match(lines[i]).Groups["variableName"].Value;
                    Program.main.lexemes.Rows.Add("I HAS A", "VARIABLE_DECLARATION_KEYWORD");
                    Program.main.lexemes.Rows.Add(varname, "VARIABLE_IDENTIFIER");

                    // value = invalid
                    if (!lolcode.r[2].Match(lines[i]).Groups["invalid"].Value.Equals(""))
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Value assigned to " + varname + " is invalid.\n");
                        continue;
                    }

                    // value = int / float / boolean / string / variable / arithmetic operation
                    else if (!lolcode.r[2].Match(lines[i]).Groups["assop"].Value.Equals(""))
                    {
                        Program.main.lexemes.Rows.Add("ITZ", "ASSIGNMENT_OPERATOR");
                        if (performCond == 1)
                        {
                            retType = t.get_type(i, lolcode.r[2].Match(lines[i]).Groups["val"].Value, varname, 1, 1, IT.First(), variableList);
                        }
                        else
                        {
                            retType = t.get_type(i, lolcode.r[2].Match(lines[i]).Groups["val"].Value, varname, 1, -1, IT.First(), variableList);
                        }
                    }

                    // no value assigned
                    else
                    {
                        if (performCond == 1)
                        {
                            Program.main.symbol_table.Rows.Add(varname, "NOOB", "");
                            variableList.AddFirst(new Variable(varname, "NOOB", ""));
                        }
                        continue;
                    }

                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // <VARIABLENAME> R <VALUE>
                else if (lolcode.r[3].Match(lines[i]).Success)
                {
                    matched = 1;
                    String varname = lolcode.r[3].Match(lines[i]).Groups["variableName"].Value;
                    if (!lolcode.r[3].Match(lines[i]).Groups["invalid"].Value.Equals(""))
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Value assigned to " + varname + " is invalid.\n");
                        continue;
                    }
                    Program.main.lexemes.Rows.Add(varname, "VARIABLE_IDENTIFIER");
                    Program.main.lexemes.Rows.Add("R", "ASSIGNMENT_OPERATOR_KEYWORD");

                    foreach (Variable v in variableList)
                    {
                        // if a match is found, print the value of the variable
                        if (varname.Equals(v.getIdent()))
                        {
                            // value = arithmetic operation
                            if (lolcode.r[4].Match(lolcode.r[3].Match(lines[i]).Groups["val"].Value).Success)
                            {
                                aop.add_to_tables(i, lolcode.r[3].Match(lines[i]).Groups["val"].Value);
                                ITfloat = aop.perform(i, lolcode.r[3].Match(lines[i]).Groups["val"].Value, variableList);
                                if (performCond == 1)
                                {
                                    retType = t.get_type(i, Convert.ToString(ITfloat), varname, -1, 1, IT.First(), variableList);
                                }
                                else
                                {
                                    retType = t.get_type(i, Convert.ToString(ITfloat), varname, -1, -1, IT.First(), variableList);
                                }
                            }
                            else
                            {
                                if (performCond == 1)
                                {
                                    retType = t.get_type(i, lolcode.r[3].Match(lines[i]).Groups["val"].Value, varname, 1, 1, IT.First(), variableList);
                                }
                                else
                                {
                                    retType = t.get_type(i, lolcode.r[3].Match(lines[i]).Groups["val"].Value, varname, 1, -1, IT.First(), variableList);
                                }
                            }
                            break;
                        }
                        counter++;
                    }
                    // if the end of the linked list is reached yet none matched, then the variable is undeclared, hence produce an error
                    if (counter >= variableList.Count)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Variable undeclared.\n");
                    }
                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // VISIBLE
                else if (lolcode.r[5].Match(lines[i]).Success)
                {
                    matched = 1;
                    Program.main.lexemes.Rows.Add("VISIBLE", "OUTPUT_OPERATOR_KEYWORD");

                    String   temp = lolcode.r[5].Match(lines[i]).Groups["args"].Value;
                    String[] args = Regex.Matches(temp, Syntax.visible_arg).Cast <Match>().Select(m => m.Value).ToArray();

                    foreach (string arg in args)
                    {
                        if (arg.Equals("IT"))
                        {
                            if (IT.Count != 0)
                            {
                                if (performCond == 1)
                                {
                                    Console.Write(IT.First().getvalue());
                                }
                                Program.main.lexemes.Rows.Add("IT", "PROGRAM_ACCUMUlATOR");
                            }
                            else
                            {
                                Program.main.console_output.AppendText("Error at line " + (i + 1) + ": IT is null.\n");
                            }
                        }

                        // if VISIBLE's argument is an arithmetic operation (ex: SUM OF), perform the operation the print the result
                        else if (!lolcode.r[5].Match(lines[i]).Groups["aop"].Value.Equals(""))
                        {
                            aop.add_to_tables(i, lines[i]);
                            if (performCond == 1)
                            {
                                Console.Write(aop.perform(i, lines[i], variableList));
                            }
                        }

                        // if VISIBLE's argument is SMOOSH
                        else if (lolcode.r[1].Match(arg).Success)
                        {
                            c.add_to_tables(lines[i]);

                            if (performCond == 1)
                            {
                                Console.Write(c.concatenate(i, value, lines[i], variableList));
                            }
                        }

                        // VISIBLE's argument is a comparison statement(BOTH SAEM||DIFFRINT)
                        else if (lolcode.r[11].Match(arg).Success)
                        {
                            cop.add_to_tables(i, lines);
                            if (performCond == 1)
                            {
                                Console.Write(cop.compare(i, lines[i], variableList, IT.First()));
                            }
                        }

                        // if the VISIBLE's argument is a string
                        else if (new Regex(Syntax.stringSyntax).Match(arg).Success)
                        {
                            t.get_type(i, arg, "", 1, -1, IT.First(), variableList);
                            if (performCond == 1)
                            {
                                Console.Write(new Regex(Syntax.stringSyntax).Match(arg).Groups["stringValue"].Value);
                            }
                        }
                        // if the VISIBLE's argument is an int, float, or boolean
                        else if (new Regex("^" + value + ")$").Match(arg).Success)
                        {
                            if (performCond == 1 && !t.get_type(i, arg, "", 1, -1, IT.First(), variableList).Equals("NO MATCH"))
                            {
                                Console.Write(arg);
                            }
                        }
                        else
                        {
                            // if the VISIBLE's argument is a variable
                            if (new Regex(Syntax.var).Match(arg).Success)
                            {
                                Program.main.lexemes.Rows.Add(arg, "VARIABLE_IDENTIFIER");
                                // traverse each element of the variable linked list to find VISIBLE's argument
                                foreach (Variable v in variableList)
                                {
                                    // if a match is found, print the value of the variable
                                    if (arg.Equals(v.getIdent()))
                                    {
                                        if (performCond == 1)
                                        {
                                            Console.Write(v.getvalue());
                                        }
                                        break;
                                    }
                                    counter++;
                                }
                                // if the end of the linked list is reached yet none matched, then the variable is undeclared, hence produce an error
                                if (counter >= variableList.Count)
                                {
                                    Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Variable undeclared.\n");
                                }
                            }
                            // if none of the above syntax' matched the VISIBLE's argument, produce an error
                            else
                            {
                                Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Invalid VISIBLE argument.\n");
                            }
                        }
                    }
                    if (lolcode.r[5].Match(lines[i]).Groups["removenewline"].Value.Equals(""))
                    {
                        Console.Write("\n");
                    }
                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // GIMMEH
                else if (lolcode.r[6].Match(lines[i]).Success)
                {
                    matched = 1;
                    String varname = lolcode.r[6].Match(lines[i]).Groups["dest"].Value;

                    Program.main.lexemes.Rows.Add("GIMMEH", "INPUT_GETTER_KEYWORD");
                    Program.main.lexemes.Rows.Add(varname, "DESTINATION_STORAGE_VARIABLE");

                    if (performCond == 1)
                    {
                        String temp = Console.ReadLine();

                        counter = 0;
                        foreach (Variable v in variableList)
                        {
                            if (v.getIdent().Equals(varname))
                            {
                                retType = t.get_type(i, temp, varname, -1, 1, IT.First(), variableList);
                                break;
                            }
                            counter++;
                        }
                        if (counter >= variableList.Count)
                        {
                            Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Variable undeclared.\n");
                            continue;
                        }
                    }
                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // IS NOW A
                else if (lolcode.r[7].Match(lines[i]).Success)
                {
                    matched = 1;

                    String newtype = lolcode.r[7].Match(lines[i]).Groups["tp"].Value;
                    String varname = lolcode.r[7].Match(lines[i]).Groups["variableName"].Value;

                    Program.main.lexemes.Rows.Add(varname, "VARIABLE_IDENTIFIER");
                    Program.main.lexemes.Rows.Add("IS NOW A", "TYPE_CASTING_KEYWORD");
                    Program.main.lexemes.Rows.Add(newtype, "DATA_TYPE");

                    if (performCond == 1)
                    {
                        t.cast(i, newtype, varname, variableList);
                    }
                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // WHITESPACE
                else if (lolcode.r[9].Match(lines[i]).Success)
                {
                    matched = 1;
                }

                // MEBBE
                else if (lolcode.r[17].Match(lines[i]).Success)
                {
                    matched = 1;

                    Program.main.lexemes.Rows.Add("MEBBE", "IN_BETWEEN_CONDITIONAL_STATEMENT_KEYWORD");

                    if (cmatched == -1)
                    {
                        String cop_val = cop.compare(i, lines[i], variableList, IT.First());
                        if (cop_val == "WIN")
                        {
                            performCond = 1;
                            cmatched    = 1;
                        }
                        else
                        {
                            performCond = -1;
                        }
                    }

                    if (cstart == -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": MEBBE operator not in between the labels YA RLY and OIC.\n");
                        continue;
                    }
                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // NO WAI
                else if (lolcode.r[14].Match(lines[i]).Success)
                {
                    matched = 1;

                    Program.main.lexemes.Rows.Add("NO WAI", "FALSE_CONDITIONAL_STATEMENT_KEYWORD");
                    if (cmatched == -1)
                    {
                        performCond = 1;
                        cmatched    = 1;
                    }
                    else
                    {
                        performCond = -1;
                    }

                    if (cstart == -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": No O RLY? and/or YA RLY label/s found before NO WAI label.\n");
                        continue;
                    }
                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // OIC
                else if (lolcode.r[16].Match(lines[i]).Success)
                {
                    matched = 1;

                    Program.main.lexemes.Rows.Add("OIC", "CONDITIONAL_STATEMENT_END_KEYWORD");
                    cmatched    = -1;
                    performCond = 1;

                    if (cstart == -1 && casestart == -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": No O RLY? or WTF? label found before OIC label.\n");
                        continue;
                    }
                    cstart = -1;
                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // WTF?
                else if (lolcode.r[19].Match(lines[i]).Success)
                {
                    matched     = 1;
                    casestart   = i;
                    performCond = -1;

                    Program.main.lexemes.Rows.Add("WTF?", "CASE_BLOCK_START_KEYWORD");

                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // OMG
                else if (lolcode.r[20].Match(lines[i]).Success)
                {
                    matched = 1;
                    String val = lolcode.r[20].Match(lines[i]).Groups["lit"].Value;

                    Program.main.lexemes.Rows.Add("OMG", "CASE_KEYWORD");
                    Program.main.lexemes.Rows.Add(val, "CASE_STATEMENT_MATCHING_VALUE");

                    if (new Regex(Syntax.stringSyntax).Match(val).Success)
                    {
                        val = new Regex(Syntax.stringSyntax).Match(val).Groups["stringValue"].Value;
                    }
                    if ((cmatched == -1 && val.Equals(IT.First().getvalue())) | (cmatched == -1 && performCond == 1))
                    {
                        performCond = 1;
                    }
                    else
                    {
                        performCond = -1;
                    }

                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // GTFO
                else if (lolcode.r[21].Match(lines[i]).Success)
                {
                    matched = 1;

                    Program.main.lexemes.Rows.Add("GTFO", "CASE_BREAK_KEYWORD");
                    if (performCond == 1)
                    {
                        cmatched = 1;
                    }

                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // OMGWTF
                else if (lolcode.r[22].Match(lines[i]).Success)
                {
                    matched = 1;

                    Program.main.lexemes.Rows.Add("OMGWTF", "CASE_DEFAULT_KEYWORD");

                    if (cmatched == -1 || (cmatched == -1 && performCond == 1))
                    {
                        performCond = 1;
                    }
                    else
                    {
                        performCond = -1;
                    }

                    // if code is before HAI keyword or after KTHXBYE rd
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // Comparison Operations (BOTH SAEM|DIFFRINT)
                else if (lolcode.r[11].Match(lines[i]).Success)
                {
                    matched = 1;

                    int temp = cop.add_to_tables(i, lines);

                    if (performCond == 1)
                    {
                        String cop_val = cop.compare(i, lines[i], variableList, IT.First());
                        IT.AddFirst(new Variable("IT", "TROOF", Convert.ToString(cop_val)));
                        Program.main.symbol_table.Rows.Add(IT.First().getIdent(), IT.First().gettype(), IT.First().getvalue());
                    }

                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                    if (i != temp)
                    {
                        cstart      = i;
                        i           = temp;
                        performCond = -1;

                        if (IT.First().getvalue().Equals("WIN"))
                        {
                            performCond = 1;
                            cmatched    = 1;
                        }
                    }

                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // Boolean Operations (ex: BOTH OF)
                else if (lolcode.r[8].Match(lines[i]).Success)
                {
                    matched = 1;

                    Program.main.lexemes.Rows.Add(lolcode.r[8].Match(lines[i]).Groups["bop"].Value, "BOOLEAN_OPERATOR_KEYWORD");
                    Program.main.lexemes.Rows.Add(lolcode.r[8].Match(lines[i]).Groups["identifier1"].Value, "FIRST_ARITHMETIC_OPERAND");
                    Program.main.lexemes.Rows.Add("AN", "OPERAND_SEPARATOR_KEYWORD");
                    Program.main.lexemes.Rows.Add(lolcode.r[8].Match(lines[i]).Groups["identifier2"].Value, "SECOND_ARITHMETIC_OPERAND");

                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // Arithmetic Operations (ex: SUM OF)
                else if (lolcode.r[4].Match(lines[i]).Success)
                {
                    matched = 1;

                    aop.add_to_tables(i, lines[i]);

                    if (performCond == 1)
                    {
                        float arith_val = aop.perform(i, lines[i], variableList);
                        IT.AddFirst(new Variable("IT", t.get_type(i, Convert.ToString(arith_val), "", -1, -1, IT.First(), variableList), Convert.ToString(arith_val)));
                        Program.main.symbol_table.Rows.Add(IT.First().getIdent(), IT.First().gettype(), IT.First().getvalue());
                    }

                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // SMOOSH
                else if (lolcode.r[1].Match(lines[i]).Success)
                {
                    matched = 1;

                    c.add_to_tables(lines[i]);

                    if (performCond == 1)
                    {
                        String concatenated_value = c.concatenate(i, value, lines[i], variableList);
                        IT.AddFirst(new Variable("IT", "YARN", concatenated_value));
                        Program.main.symbol_table.Rows.Add("IT", "YARN", concatenated_value);
                    }

                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }
                // OBTW
                else if (lolcode.r[10].Match(lines[i]).Success)
                {
                    matched = 1;

                    Program.main.lexemes.Rows.Add("OBTW", "MULTIPLE_LINE_COMMENT_START_KEYWORD");
                    Program.main.lexemes.Rows.Add(lolcode.r[10].Match(lines[i]).Groups["comment"].Value, "COMMENT_VALUE");

                    while (i + 1 < lines.Count() && !lolcode.r[23].Match(lines[i + 1]).Success)
                    {
                        i++;
                        Program.main.lexemes.Rows.Add(lines[i], "COMMENT_VALUE");
                    }
                }
                // TLDR
                else if (lolcode.r[23].Match(lines[i]).Success)
                {
                    matched = 1;

                    Program.main.lexemes.Rows.Add("TLDR", "MULTIPLE_LINE_COMMENT_END_KEYWORD");

                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }
                // BTW
                else if (lolcode.r[18].Match(lines[i]).Success)
                {
                    matched = 1;

                    Program.main.lexemes.Rows.Add("BTW", "SINGLE_COMMENT_KEYWORD");
                    Program.main.lexemes.Rows.Add(lolcode.r[18].Match(lines[i]).Groups["comment"].Value, "COMMENT_VALUE");
                }

                // Literal or Variable (for case statement)
                else if (lolcode.r[24].Match(lines[i]).Success)
                {
                    matched = 1;
                    counter = 0;

                    String val = lolcode.r[24].Match(lines[i]).Groups["litorvar"].Value;
                    String tp  = t.get_type(i, val, "", 1, -1, IT.First(), variableList);
                    if (new Regex("^" + Syntax.var + "$").Match(val).Success)
                    {
                        // traverse the linked list of declared variables to find the second operand's variable name
                        foreach (Variable v in variableList)
                        {
                            if (val.Equals(v.getIdent()))
                            {
                                IT.AddFirst(new Variable("IT", tp, v.getvalue()));
                                Program.main.symbol_table.Rows.Add("IT", tp, v.getvalue());
                                break;
                            }
                            counter++;
                        }
                        if (counter >= variableList.Count)
                        {
                            Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Variable " + val + " is undeclared.\n");
                        }
                    }
                    else
                    {
                        IT.AddFirst(new Variable("IT", tp, val));
                        Program.main.symbol_table.Rows.Add("IT", tp, val);
                    }

                    // if code is before HAI keyword or after KTHXBYE keyword
                    if (start == -1 || end != -1)
                    {
                        Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Code not within program bounds.\n");
                    }
                }

                // Unrecognized Syntax
                if (matched == 0)
                {
                    Program.main.console_output.AppendText("Error at line " + (i + 1) + ": Unrecognized Syntax.\n");
                }
            }

            // errors on occurance of HAI and KTHXBYE
            if (start != -1 && end != -1 && start >= end)
            {
                Program.main.console_output.AppendText("Error at lines " + (end + 1) + " and " + (start + 1) + ": KTHXBYE keyword occured before HAI keyword.\n");
            }
            if (start == -1)
            {
                Program.main.console_output.AppendText("Error at line 1: HAI keyword not found.\n");
            }
            if (end == -1)
            {
                Program.main.console_output.AppendText("Error at line " + (i + 1) + ": KTHXBYE keyword not found.\n");
            }
        }
Ejemplo n.º 3
0
        // updates lexemes table and symbol table then returns the data type of the passed variable
        public String get_type(int line_number, String val, String ident, int lex, int sym, Variable IT, LinkedList <Variable> variableList)
        {
            int           counter = 0;
            Syntax        lolcode = new Syntax();
            Concatenation c       = new Concatenation();
            Regex         checker = new Regex("((" + Syntax.stringSyntax + ")|(?<floatValue>" + Syntax.floatingPoint + ")|(?<intValue>" + Syntax.integer + ")|(?<troof>" + Syntax.troof + ")|(?<var>" + Syntax.var + "))");

            if (val.Equals("IT"))
            {
                if (lex != -1)
                {
                    Program.main.lexemes.Rows.Add("IT", "PROGRAM_ACCUMULATOR");
                }
                if (sym != -1)
                {
                    variableList.AddFirst(new Variable(ident, IT.gettype(), IT.getvalue()));
                    Program.main.symbol_table.Rows.Add(ident, IT.gettype(), IT.getvalue());
                }
                type = "NUMBAR";
            }
            else if (!checker.Match(val).Groups["floatValue"].Value.Equals(""))
            {
                if (lex != -1)
                {
                    Program.main.lexemes.Rows.Add(val, "FLOATING_POINT_LITERAL_VALUE");
                }
                if (sym != -1)
                {
                    variableList.AddFirst(new Variable(ident, "NUMBAR", val));
                    Program.main.symbol_table.Rows.Add(ident, "NUMBAR", val);
                }
                type = "NUMBAR";
            }
            else if (!checker.Match(val).Groups["stringValue"].Value.Equals(""))
            {
                if (lex != -1)
                {
                    Program.main.lexemes.Rows.Add("\"", "STRING_LITERAL_INDICATOR");
                    Program.main.lexemes.Rows.Add(checker.Match(val).Groups["stringValue"].Value, "STRING_LITERAL_VALUE");
                    Program.main.lexemes.Rows.Add("\"", "STRING_LITERAL_INDICATOR");
                }
                if (sym != -1)
                {
                    variableList.AddFirst(new Variable(ident, "YARN", checker.Match(val).Groups["stringValue"].Value));
                    Program.main.symbol_table.Rows.Add(ident, "YARN", checker.Match(val).Groups["stringValue"].Value);
                }
                type = "YARN";
            }
            else if (!checker.Match(val).Groups["intValue"].Value.Equals(""))
            {
                if (lex != -1)
                {
                    Program.main.lexemes.Rows.Add(val, "INTEGER_LITERAL_VALUE");
                }
                if (sym != -1)
                {
                    variableList.AddFirst(new Variable(ident, "NUMBR", val));
                    Program.main.symbol_table.Rows.Add(ident, "NUMBR", val);
                }
                type = "NUMBR";
            }
            else if (!checker.Match(val).Groups["troof"].Value.Equals(""))
            {
                if (lex != -1)
                {
                    Program.main.lexemes.Rows.Add(val, "BOOLEAN_LITERAL_VALUE");
                }
                if (sym != -1)
                {
                    variableList.AddFirst(new Variable(ident, "TROOF", val));
                    Program.main.symbol_table.Rows.Add(ident, "TROOF", val);
                }
                type = "TROOF";
            }
            else if (lolcode.r[4].Match(val).Success)
            {
                ArithmeticOperation aop = new ArithmeticOperation();
                aop.add_to_tables(line_number, val);
                float  arith_val = aop.perform(line_number, val, variableList);
                String tp        = get_type(line_number, Convert.ToString(arith_val), ident, -1, -1, IT, variableList);
                variableList.AddFirst(new Variable(ident, tp, Convert.ToString(arith_val)));
                Program.main.symbol_table.Rows.Add(ident, tp, Convert.ToString(arith_val));
            }
            else if (lolcode.r[1].Match(val).Success)
            {
                c.add_to_tables(val);

                String value = "((" + Syntax.stringSyntax + ")|(?<floatValue>" + Syntax.floatingPoint + ")|(?<intValue>" + Syntax.integer + ")|(?<troof>" + Syntax.troof + ")";
                String concatenated_value = c.concatenate(line_number, value, val, variableList);
                variableList.AddFirst(new Variable(ident, "YARN", concatenated_value));
                Program.main.symbol_table.Rows.Add(ident, "YARN", concatenated_value);
            }
            else if (!checker.Match(val).Groups["var"].Value.Equals(""))
            {
                if (lex != -1)
                {
                    Program.main.lexemes.Rows.Add(val, "VARIABLE_IDENTIFIER");
                }
                foreach (Variable v in variableList)
                {
                    // if a match is found, print the value of the variable
                    if (val.Equals(v.getIdent()))
                    {
                        variableList.AddFirst(new Variable(ident, v.gettype(), v.getvalue()));
                        if (sym != -1)
                        {
                            Program.main.symbol_table.Rows.Add(ident, v.gettype(), v.getvalue());
                        }
                        type = v.gettype();
                        break;
                    }
                    counter++;
                }
                // if the end of the linked list is reached yet none matched, then the variable is undeclared, hence produce an error
                if (counter >= variableList.Count)
                {
                    //variableList.AddFirst(new Variable(ident, "NOOB", ""));
                    Program.main.console_output.AppendText("Error at line " + (line_number + 1) + ": Variable " + val + " is undeclared.\n");

                    /*
                     * if (sym != -1)
                     * {
                     *  Program.main.symbol_table.Rows.Add(ident, "NOOB", "");
                     * }*/
                }
            }
            return(type);
        }
Ejemplo n.º 4
0
        public float perform(int line_number, String line, LinkedList <Variable> variableList)
        {
            Syntax   lolcode  = new Syntax();
            DataType t        = new DataType();
            String   arithop  = new Regex("^" + Syntax.aop).Match(line).Groups["aop"].Value;
            String   ident2   = lolcode.r[4].Match(line).Groups["identifier2"].Value;
            float    ITfloat  = 0;
            float    ITfloat2 = 0;



            int index = 0, pos = -1;

            for (int a = 0; a < lolcode.r[4].Match(line).Groups["identifier1"].Captures.Count; a++)
            {
                if (pos == -1 || lolcode.r[4].Match(line).Groups["identifier1"].Captures[a].Index < pos)
                {
                    pos   = lolcode.r[4].Match(line).Groups["identifier1"].Captures[a].Index;
                    index = a;
                }
            }
            String ident1 = lolcode.r[4].Match(line).Groups["identifier1"].Captures[index].Value;

            // first operand = arithmetic operation
            if (lolcode.r[4].Match(ident1).Success)
            {
                ArithmeticOperation aop = new ArithmeticOperation();
                ITfloat = aop.perform(line_number, ident1, variableList);
            }
            // if the first operand is a literal
            else if (new Regex("^(" + Syntax.literals + ")$").Match(ident1).Success)
            {
                ITfloat = float.Parse(ident1);
            }
            // if the first operand is a variable
            else
            {
                counter = 0;
                // traverse the linked list of declared variables to find the first operand's variable name
                foreach (Variable v in variableList)
                {
                    // if a match was found, get its value and store it in ITfloat
                    if (ident1.Equals(v.getIdent()))
                    {
                        if (v.gettype() == "NOOB")
                        {
                            ITfloat = 0;
                        }
                        else if (v.gettype() == "YARN")
                        {
                            t.cast(line_number, "NUMBAR", v.getIdent(), variableList);
                            if (v.gettype() == "YARN")
                            {
                                return(0);
                            }
                        }
                        else
                        {
                            ITfloat = float.Parse(v.getvalue());
                        }
                        break;
                    }
                    counter++;
                }
                // if the end of linked list is reached yet none matched the variable name, then it isn't declared, hence produce an error
                if (counter >= variableList.Count)
                {
                    Program.main.console_output.AppendText("Error at line " + (line_number + 1) + ": Variable used as first operand is undeclared.\n");
                    ITfloat = 0;
                }
            }

            // second operand = arithmetic operation
            if (lolcode.r[4].Match(ident2).Success)
            {
                ArithmeticOperation aop = new ArithmeticOperation();
                ITfloat2 = aop.perform(line_number, ident2, variableList);
            }
            // if the second operand is a literal
            else if (new Regex("^(" + Syntax.literals + ")$").Match(ident2).Success)
            {
                //Program.main.console_output.AppendText("ident2:" + ident2 + "\n");
                ITfloat2 = float.Parse(ident2);
            }
            // if the second operand is variable name
            else
            {
                counter = 0;
                // traverse the linked list of declared variables to find the second operand's variable name
                foreach (Variable v in variableList)
                {
                    // if a match was found, get its value and store it in ITfloat2
                    if (ident2.Equals(v.getIdent()))
                    {
                        if (v.gettype() == "NOOB")
                        {
                            ITfloat2 = 0;
                        }
                        else if (v.gettype() == "YARN")
                        {
                            t.cast(line_number, "NUMBAR", v.getIdent(), variableList);
                            if (v.gettype() == "YARN")
                            {
                                return(0);
                            }
                        }
                        else
                        {
                            ITfloat2 = float.Parse(v.getvalue());
                        }
                        break;
                    }
                    counter++;
                }
                // if the end of linked list is reached yet none matched the variable name, then it isn't declared, hence produce an error
                if (counter >= variableList.Count)
                {
                    Program.main.console_output.AppendText("Error at line " + (line_number + 1) + ": Variable used as second operand is undeclared.\n");
                    ITfloat2 = 0;
                }
            }
            // after storing the value of first operand to ITfloat and the second operand to ITfloat2, process the operation
            if (new Regex(@"SUM\s+OF").Match(arithop).Success)
            {
                return(ITfloat + ITfloat2);
            }
            else if (new Regex(@"DIFF\s+OF").Match(arithop).Success)
            {
                return(ITfloat - ITfloat2);
            }
            else if (new Regex(@"PRODUKT\s+OF").Match(arithop).Success)
            {
                return(ITfloat * ITfloat2);
            }
            else if (new Regex(@"QUOSHUNT\s+OF").Match(arithop).Success)
            {
                if (ITfloat2 != 0)
                {
                    return(ITfloat / ITfloat2);
                }
                else
                {
                    Program.main.console_output.AppendText("Error at line " + (line_number + 1) + ": Math error due to division by 0.\n");
                    return(0);
                }
            }
            else if (new Regex(@"MOD\s+OF").Match(arithop).Success)
            {
                return(ITfloat % ITfloat2);
            }
            else if (new Regex(@"BIGGR\s+OF").Match(arithop).Success)
            {
                if (ITfloat >= ITfloat2)
                {
                    return(ITfloat);
                }
                else
                {
                    return(ITfloat2);
                }
            }
            else if (new Regex(@"SMALLR\s+OF").Match(arithop).Success)
            {
                if (ITfloat <= ITfloat2)
                {
                    return(ITfloat);
                }
                else
                {
                    return(ITfloat2);
                }
            }
            else
            {
                return(0);
            }
        }