Beispiel #1
0
        private bool CheckForInstructions( int CurrentLine, XmlDocument Dictionary)
        {
            XmlNodeList instructions = Dictionary.SelectNodes("//conversion_library//conversion_rules//instructions//instruction//old_instruction");
            class_types.Instruction_and_Argument old_code,new_code;
            class_types.IL_CodeLine codeline = new class_types.IL_CodeLine();
            class_types.CodeResult comments_operation = new class_types.CodeResult();

            string comment_marker_start =Dictionary.SelectSingleNode("//conversion_library//conversion_rules//comment_markers//start").InnerText;
            string comment_marker_end = Dictionary.SelectSingleNode("//conversion_library//conversion_rules//comment_markers//end").InnerText;

            bool success=true;
            new_code.argument = "";
            new_code.instruction = "";

            comments_operation = RemoveComment(InCode.ElementAt(CurrentLine), comment_marker_start, comment_marker_end);

            //check for comments only line
            if (comments_operation.code == "")
            {
                codeline.code = "";
                codeline.comment = comments_operation.comment;
                IL_file.codelines.AddLast(codeline);
            }
            else //line with code
            {

                for (int i = 0; i < instructions.Count; i++)
                {
                    //-----------Remove comments--------------------------------
                    comments_operation = RemoveComment(InCode.ElementAt(CurrentLine), comment_marker_start, comment_marker_end);
                    if (comments_operation.success == false)
                    {
                        success = false;
                        break;
                    }

                    old_code = SeparateInstFromArg(comments_operation.code);
                    XmlNodeList current_instruction = instructions.Item(i).ParentNode.ChildNodes;

                    //-------------End remove comments-------------------------------

                    //old now has the old line, separated by current "instruction" and the tail

                    if (CompareWithIgnores(instructions.Item(i).InnerText,old_code.instruction))
                    {
                        codeline.code = "";
                        codeline.comment = comments_operation.comment;//new line has comments only

                        //************************************************OUTCOME WRITER*******************************************
                        new_code = SeparateInstFromArg(GetXmlNodeValueByTag(current_instruction, "outcome"));

                        while (new_code.instruction != "")
                        {
                            //check for literal i.e. "
                            if (System.Text.Encoding.UTF8.GetBytes(new_code.instruction)[0] == 34 && System.Text.Encoding.UTF8.GetBytes(new_code.instruction)[System.Text.Encoding.UTF8.GetBytes(new_code.instruction).Length - 1] == 34)
                            {
                                new_code.instruction = new_code.instruction.Substring(1);
                                new_code.instruction = new_code.instruction.Substring(0, new_code.instruction.Length - 1);
                                codeline.code += new_code.instruction;
                                new_code = SeparateInstFromArg(new_code.argument);
                            }
                            else
                            {

                                switch (new_code.instruction)
                                {

                                    case "instruction":
                                        codeline.code += " " + GetXmlNodeValueByTag(current_instruction, "new_instruction");
                                        new_code = SeparateInstFromArg(new_code.argument);
                                        break;
                                    case "argument":
                                        codeline.code += " " + ReplaceIllegalChars(old_code.argument);
                                        new_code = SeparateInstFromArg(new_code.argument);
                                        break;
                                    case "old_instruction":
                                        codeline.code += " "+ old_code.instruction;
                                        new_code = SeparateInstFromArg(new_code.argument);

                                        break;
                                }
                            }
                        }
                        //add written line
                        if ((codeline.code != "" || codeline.comment != "") && new_code.instruction == "")
                        {
                            if (codeline.code.Length > 0 && codeline.code.Substring(0, 1) == " ")//clear first space, if there is one (typical)
                                codeline.code = codeline.code.Substring(1);

                            IL_file.codelines.AddLast(codeline);
                        }
                        //*******************************************************************************************************

                        //***************************************************RULES***********************************************************
                        LinkedList<string> rules = new LinkedList<string>();

                        rules = GetXmlNodeValuesByTag(current_instruction, "rules");

                        for (int j = 0; j < rules.Count; j++)
                        {
                            new_code.argument = "";
                            new_code.instruction = "";
                            new_code = SeparateInstFromArg(rules.ElementAt(j));

                            CheckForRules(CurrentLine, Dictionary, current_instruction, old_code, new_code);
                        }

                    }
                }
            }
            return success;
        }
Beispiel #2
0
        //remove comment function
        class_types.CodeResult RemoveComment(string line, string comment_marker_start,string comment_marker_end)
        {
            class_types.CodeResult result=new class_types.CodeResult();
            result.code = line;
            result.comment = "";

            if (!(line == ""))
            {
                if (line.Contains(comment_marker_start))
                {
                    if (comment_marker_end=="EOL")
                    {
                             result.comment = line.Substring(line.IndexOf(comment_marker_start) + comment_marker_start.Length);
                             if (line.IndexOf(comment_marker_start) != 0)
                                 result.code = line.Substring(0, line.IndexOf(comment_marker_start));
                             else
                                 result.code = "";
                             result.success = true;

                    }
                    else
                    {
                        if (line.Contains(comment_marker_end))
                        {
                            result.comment = line.Substring(line.IndexOf(comment_marker_start) + comment_marker_start.Length, line.IndexOf(comment_marker_end) - line.IndexOf(comment_marker_start) - comment_marker_start.Length);
                            result.code = line.Substring(0, line.IndexOf(comment_marker_start) - 1) + line.Substring(line.IndexOf(comment_marker_end) + comment_marker_end.Length);
                            result.success = true;

                        }
                        else
                        {
                            result.success = false; //TODO make this abort
                        }
                    }
                }
                else
                {
                    result.success = true;
                }
            }
            else
            {
                result.success = false;
            }

            return result;
        }