private static SourceCodeLine m_nowProcessLine; //当前处理的代码行 #endregion Fields #region Methods public static string ComplieCmdStr(List<SourceCodeLine> sourceCodeLineList, string fxModel) { Dictionary<string, IFXCmd> cmdList = FXConfigReader.Cmds[fxModel]; Dictionary<string, FXRegister> registerList = FXConfigReader.Registers[fxModel]; List<OperandMap> mapList = FXConfigReader.OperandMaps[fxModel]; string result = ""; for (int i = 0; i < sourceCodeLineList.Count; i++) { //if (line.Index == 522) // Console.WriteLine(""); //Console.WriteLine(line.Index.ToString()); SourceCodeLine line = sourceCodeLineList[i]; FXComplier.m_nowProcessLine = line; IFXCmd iCmd = FXComplier.GetCmdFromDictionary(ref line, cmdList); string oneLineCmdStr = GetOneLineCmdStr(iCmd, line.Operands, registerList, mapList); if (oneLineCmdStr != null && oneLineCmdStr != string.Empty) { string[] oneLineCmdParts = oneLineCmdStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (string oneLineCmdPart in oneLineCmdParts) { result = result + " " + Utils.Switch2StringParts(oneLineCmdPart, 2); } } else throw new Exception("Complie Error. At " + FXComplier.m_nowProcessLine.BasicLineStr); } return result.Substring(1, result.Length - 1); }
public static List<SourceCodeLine> ConvertSourceToLineList(string wholeSourceCodeStr) { List<SourceCodeLine> resultList = new List<SourceCodeLine>(); string[] sourceLines = wholeSourceCodeStr.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < sourceLines.Length; i++) { SourceCodeLine codeLine = new SourceCodeLine(); codeLine.BasicLineStr = sourceLines[i].ToUpper(); string[] lineElements = codeLine.BasicLineStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int index = 0; bool result = int.TryParse(lineElements[0], out index); codeLine.Index = index; int startIndex = result ? 1 : 0; codeLine.Cmd = lineElements[startIndex]; for (int j = startIndex + 1; j < lineElements.Length; j++) { codeLine.Operands.Add(lineElements[j]); } resultList.Add(codeLine); } return resultList; }
/// <summary> /// 根据指定的命令名称从命令字典中取出相应的命令内容 /// 判断的顺序为:存在于字典中,如果以D开头则去掉D存在于字典中,如果以P结尾则去掉P存在与字典中,如果以D开头并以P结尾则分别去掉DP后是否存在于字典 /// </summary> /// <param name="cmdName">命令名称</param> /// <param name="cmdList">命令字典</param> /// <returns></returns> private static IFXCmd GetCmdFromDictionary(ref SourceCodeLine line, Dictionary<string, IFXCmd> cmdList) { if (cmdList.ContainsKey(line.Cmd)) //如果已经存在于字典中,那么直接返回 return cmdList[line.Cmd]; else { if (line.Cmd.StartsWith(FXConfigReader.PCMDPREFIX)) //如果命令以P开头,那么为指针命令 { string numStr = line.Cmd.Substring(1, line.Cmd.Length - 1); if (cmdList.ContainsKey(FXConfigReader.PCMDPREFIX)) { FXBasicCmd cmd = (FXBasicCmd)cmdList[FXConfigReader.PCMDPREFIX]; line.Operands.Add(FXConfigReader.KCONSTANT + numStr); return cmd; } } if (line.Cmd.StartsWith(FXConfigReader.DCMDPREFIX)) //如果命令由字母D开头,那么尝试去掉D后是否存在于字典中 { string tempName = line.Cmd.Substring(1, line.Cmd.Length - 1); if (cmdList.ContainsKey(tempName)) { FXAppCmd cmd = (FXAppCmd)cmdList[tempName]; cmd.AppCmdType = FXAppCmdType.D; return cmd; } } if (line.Cmd.EndsWith(FXConfigReader.PCMDSUFFIX)) //如果命令由字母P结尾,那么尝试去掉P后是否存在于字典中 { string tempName = line.Cmd.Substring(0, line.Cmd.Length - 1); if (cmdList.ContainsKey(tempName)) { FXAppCmd cmd = (FXAppCmd)cmdList[tempName]; cmd.AppCmdType = FXAppCmdType.P; return cmd; } } if (line.Cmd.StartsWith(FXConfigReader.DCMDPREFIX) && line.Cmd.EndsWith(FXConfigReader.PCMDSUFFIX)) //如果命令既由D开头,又有P结尾,那么尝试都去掉后是否存在于字典中 { string tempName = line.Cmd.Substring(1, line.Cmd.Length - 2); if (cmdList.ContainsKey(tempName)) { FXAppCmd cmd = (FXAppCmd)cmdList[tempName]; cmd.AppCmdType = FXAppCmdType.DP; return cmd; } } //如果到这还没返回,说明只剩最后一种可能情况,即是D类型的接点比较命令(以>,<,=,<=,>=结尾,这种命令没有P类型的),这类的命令D标识存在于原命令后,大小比较符前 string tryContactCompareCmd = line.Cmd.Replace(">", "").Replace("<", "").Replace("=", ""); if (tryContactCompareCmd.LastIndexOf(FXConfigReader.DCMDPREFIX) == tryContactCompareCmd.Length - 1) //如果去掉可能的比较符号后的字符串的最后字符为D { string tempName = line.Cmd.Substring(0, tryContactCompareCmd.Length - 1) + line.Cmd.Substring(tryContactCompareCmd.Length, line.Cmd.Length - tryContactCompareCmd.Length); if (cmdList.ContainsKey(tempName)) { FXAppCmd cmd = (FXAppCmd)cmdList[tempName]; cmd.AppCmdType = FXAppCmdType.D; return cmd; } } throw new Exception("No such command " + line.Cmd + ". At " + FXComplier.m_nowProcessLine.BasicLineStr); } }