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; }
private void CheckForRules(int CurrentLine, XmlDocument Dictionary, XmlNodeList current_instruction, class_types.Instruction_and_Argument old_code, class_types.Instruction_and_Argument new_code) { class_types.IL_CodeLine codeline = new class_types.IL_CodeLine(); string location = ""; string reference = ""; string which = ""; int index = 0; while (new_code.instruction != "") { switch (new_code.instruction) { //--------------------No spaces--------------------------- case "delete": //delete what where new_code = SeparateInstFromArg(new_code.argument); bool special = false; string to_delete = ""; //literal 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); to_delete = new_code.instruction; } else { switch (new_code.instruction) { case "space": to_delete = " "; break; case "line": special = true; to_delete = "line"; break; } } if (!special) { //delete a string element from the place //TODO: cleanup new_code = SeparateInstFromArg(new_code.argument); switch (new_code.instruction) { case "outcome": codeline = IL_file.codelines.Last(); IL_file.codelines.RemoveLast(); codeline.code = RemoveString(codeline.code, to_delete); IL_file.codelines.AddLast(codeline); break; case "argument": break; } } else {//case of special new_code = SeparateInstFromArg(new_code.argument); //find index location = ""; reference = ""; which = ""; location = new_code.instruction;//location now contains "before", "after" or "at" new_code = SeparateInstFromArg(new_code.argument); which = new_code.instruction;//which now contains "first","last", "line" TODO: add numbered option? new_code = SeparateInstFromArg(new_code.argument);//TODO: check for literals reference = new_code.instruction;//reference now contains "new_stack", "..." TODO: complete //where? //case of literal, accepts number of line if (System.Text.Encoding.UTF8.GetBytes(reference)[0] == 34 && System.Text.Encoding.UTF8.GetBytes(reference)[System.Text.Encoding.UTF8.GetBytes(reference).Length - 1] == 34) { reference = reference.Substring(1); reference = reference.Substring(0, reference.Length - 1); try { IL_file.codelines = RemoveLinkedListCodeline(IL_file.codelines, int.Parse(reference)); } catch { } } else { index = GetIndexFromOrders(Dictionary, location, which, reference); if (index < 0 && index > IL_file.codelines.Count) break; IL_file.codelines= RemoveLinkedListCodeline(IL_file.codelines, index); } } break; //---------------------------------------------------- //-------------------Declare--------------------------- case "declare": class_types.IL_variable declare_line = new class_types.IL_variable(); declare_line.type = null; bool exists = false; new_code = SeparateInstFromArg(new_code.argument); for (int j = 0; j < IL_file.variables.Count; j++) { if (IL_file.variables.ElementAt(j).name == ReplaceIllegalChars(old_code.argument)) { exists = true; break; } } if (!exists && new_code.instruction == "argument") { XmlNodeList var_types = Dictionary.SelectNodes("//conversion_library//variable_types//variable_type"); XmlNodeList var_type_result = CompareVarType(var_types, old_code.argument);//run through variable types class_types.Instruction_and_Argument type_node = SeparateInstFromArg(GetXmlNodeValueByTag(var_type_result, "new"));//TODO: update according to dictionary string argument = SeparateInstFromArg(type_node.argument).instruction; string remainder = declare_line.name = ReplaceIllegalChars(old_code.argument); declare_line.IO_type = type_node.instruction; if (argument != "") { string type_real = ""; if (argument == "binary") type_real = "BOOL"; if (argument == "digital") type_real = "BOOL"; declare_line.type = type_real; } if (SeparateInstFromArg(type_node.argument).argument == "" && declare_line.type != null) { IL_file.variables.AddLast(declare_line); new_code.argument = ""; new_code.instruction = ""; } else break; } else { new_code.argument = ""; new_code.instruction = ""; } break; //-------------------End declare--------------------------- //-------------------Add--------------------------- case "add": string simbolic_codeline = ""; codeline.code = ""; codeline.comment = ""; while (true) { new_code = SeparateInstFromArg(new_code.argument); if (new_code.instruction == "" || new_code.instruction == "at" || new_code.instruction == "before" || new_code.instruction == "after") break; if (simbolic_codeline == "") simbolic_codeline += new_code.instruction; else simbolic_codeline += " " + new_code.instruction; } //here, the codeline is simbolic //TODO: accept literals //find index location = ""; reference = ""; which = ""; location = new_code.instruction;//location now contains "before", "after" or "at" new_code = SeparateInstFromArg(new_code.argument); which = new_code.instruction;//which now contains "first" or "last" TODO: add numbered option? new_code = SeparateInstFromArg(new_code.argument);//TODO: check for literals reference = new_code.instruction;//reference now contains "new_stack", "..." TODO: complete //TODO: check for end if literal is found index = GetIndexFromOrders(Dictionary, location, which, reference); if (index < 0 && index > IL_file.codelines.Count) break; while (simbolic_codeline != "")//knowing the index and what to write, generate the actual codeline from the simbolic one { if (codeline.code != "") codeline.code += " "; switch (SeparateInstFromArg(simbolic_codeline).instruction) { case "new_instruction": codeline.code += GetXmlNodeValueByTag(current_instruction, "new_instruction"); break; case "argument": codeline.code += SeparateInstFromArg(IL_file.codelines.ElementAt(index).code).argument; break; } simbolic_codeline = SeparateInstFromArg(simbolic_codeline).argument; } IL_file.codelines = InsertCodeAtIndexOfList(IL_file.codelines, codeline, index); break; //-------------------End add----------------------------- default: new_code.instruction = ""; break; } } //**********************************************************END RULES***************************************************** }
private void ApplyGeneralRules(XmlDocument Dictionary) { class_types.IL_CodeLine codeline = new class_types.IL_CodeLine(); XmlNodeList Rules = Dictionary.SelectNodes("//conversion_library//conversion_rules//general_rules//general_rule//outcome"); string outcome_instruction = ""; if (Rules != null) { foreach (XmlNode node in Rules) { //outcome_instruction = GetXmlNodeValueByTag(node, "outcome"); outcome_instruction = node.InnerText; //literal if (System.Text.Encoding.UTF8.GetBytes(outcome_instruction)[0] == 34 && System.Text.Encoding.UTF8.GetBytes(outcome_instruction)[System.Text.Encoding.UTF8.GetBytes(outcome_instruction).Length - 1] == 34) { outcome_instruction = outcome_instruction.Substring(1); outcome_instruction = outcome_instruction.Substring(0, outcome_instruction.Length - 1); //-----------Remove comments-------------------------------- 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; class_types.CodeResult comments_operation = RemoveComment(outcome_instruction, comment_marker_start, comment_marker_end); codeline.comment = comments_operation.comment; outcome_instruction = comments_operation.code; //-------------End remove comments------------------------------- IL_file.codelines.AddLast(codeline); } } } }