int AddConstant(float constant, string type) { Const tempConst = ConstantList.Find(x => x._Const == constant); if (tempConst == null) { ConstantList.Add(new Const(constant, ConstantList.Count, type)); return(ConstantList.Count - 1); } else { return(tempConst.Index); } }
private void AddValueToList(IConstantRange range) { if (LanguageConstant.TryParseInt(range.Minimum, out int IntMinimum) && LanguageConstant.TryParseInt(range.Maximum, out int IntMaximum)) { ConstantList.Add(range.Minimum); if (IntMinimum < IntMaximum) { for (int i = IntMinimum + 1; i < IntMaximum; i++) { ConstantList.Add(new NumberLanguageConstant(new Number(i))); } ConstantList.Add(range.Maximum); } } else if (range.Minimum is DiscreteLanguageConstant AsDiscreteMinimum && AsDiscreteMinimum.IsValueKnown && range.Maximum is DiscreteLanguageConstant AsDiscreteMaximum && AsDiscreteMaximum.IsValueKnown) { ConstantList.Add(AsDiscreteMinimum); if (AsDiscreteMaximum.IsConstantGreater(AsDiscreteMinimum)) { IClass EmbeddingClass = AsDiscreteMinimum.Discrete.EmbeddingClass; int DiscreteMinimumIndex = EmbeddingClass.DiscreteList.IndexOf(AsDiscreteMinimum.Discrete); int DiscreteMaximumIndex = EmbeddingClass.DiscreteList.IndexOf(AsDiscreteMaximum.Discrete); if (DiscreteMinimumIndex >= 0 && DiscreteMaximumIndex >= 0) { for (int Index = DiscreteMinimumIndex + 1; Index < DiscreteMaximumIndex; Index++) { IDiscrete MiddleDiscrete = EmbeddingClass.DiscreteList[Index]; DiscreteLanguageConstant MiddleConstant = new DiscreteLanguageConstant(MiddleDiscrete); ConstantList.Add(MiddleConstant); } } } } }
static void Main(string[] args) { string programScript = null; string device_str = ""; string device_name = ""; try { ConstantList constants = new ConstantList(); //foreach(var p in args) //{ // Console.WriteLine(p); //} try { foreach (var p in args) { Match m; if ((m = Regex.Match(p, @"-(?<op>[fdx])(?<key>[^=]*)(?>=(?<value>.*))?")).Success) { var op = m.Groups[@"op"].Value; var key = m.Groups[@"key"]?.Value; var value = m.Groups[@"value"]?.Value; switch (op) { case @"f": if (programScript != null) { throw new Exception("Duplicate program block"); } string fname = ""; if (key.EndsWith(".asm") || key.EndsWith(".ASM") || key.EndsWith(".sasm") || key.EndsWith(".SASM")) { fname = key; } else { if (File.Exists(key + ".sasm")) { fname = key + ".sasm"; } if (File.Exists(key + ".asm")) { fname = key + ".asm"; } } if (fname != "") { programScript = File.ReadAllText(fname); } else { throw new Exception("Assembler file not found"); } break; case @"d": constants.Add(new Constant() { Name = key, Value = value }); break; case @"x": Device device; try { device_name = key + ".xml"; device_str = File.ReadAllText(key + ".xml"); device_str.Replace("\r", ""); device_str.Replace("\n", ""); DeviceXmlParser _parser = new DeviceXmlParser(device_str); foreach (var port in _parser.Nodes.Where(n => n.Name.CompareTo("Port") == 0)) { constants.Add(new Constant() { Name = port.GetPropertyValue("Alias"), Value = port.GetPropertyValue("Address") }); } } catch (Exception e) { device = new Device(); string errorMessage = ""; errorMessage += "Device XML serialize exception! Message:" + e.Message; string msg = ""; var _exc = e; while (_exc != null && _exc.InnerException != null) { msg += _exc.Message; _exc = _exc.InnerException; } errorMessage += $" IE: {msg}"; errorMessage += $" ST: {e.StackTrace}"; errorMessage = errorMessage.Replace("/n", "").Replace("/r", ""); Console.WriteLine($"{{\"result\": -4, \"message\": \"{errorMessage}\"}}"); return; } break; default: throw new Exception("Unknown switch '" + op + "'"); } } else { if (programScript != null) { throw new Exception("Duplicate program block"); } programScript = p; } } } catch (Exception exc) { Console.WriteLine($"{{\"result\": -3, \"message\": \"{exc.Message}\"}}"); return; } try { programScript = programScript.Replace("\\r\\n", "\r\n"); programScript = programScript.Replace("\\n", "\n"); var assembly = new Assembly(programScript, constants); var listing = assembly.GetListing(); var programBytes = Assembly.Compile(listing); string binPropgram = ""; foreach (var b in programBytes) { binPropgram += b.ToString("X2"); } if (string.IsNullOrWhiteSpace(binPropgram)) { Console.WriteLine($"{{\"result\": -5, \"message\": \"Bad program\"}}"); } else if (binPropgram.Length > 510) { Console.WriteLine($"{{\"result\": -6, \"message\": \"Program is huge\"}}"); } else { Console.WriteLine($"{{\"result\": 1, \"message\":\"{binPropgram}\"}}"); } return; } catch (Exception exc) { Console.WriteLine($"{{\"result\": -1, \"message\": \"{exc.Message}\"}}"); return; } } catch (Exception e) { string errorMessage = ""; errorMessage += "Can't compile script! " + e.Message; errorMessage += "Device file name:" + device_name + " with data:" + device_str; Console.WriteLine($"{{\"result\": -2, \"message\": \"{errorMessage}\"}}"); return; } }
private void ParserLine(List <LexicalUnit> lexUnitInRowList) { int tempInt; float tempFloat; foreach (var unit in lexUnitInRowList) { //if we have this lexem in reserved lexem tempInt = Check.IsReservedLexem(unit.Substring); if (tempInt >= 0) { LexemList.Add(new Lexem(unit.Row, unit.Substring, tempInt)); continue; } //if this lexem is a constant if (float.TryParse(unit.Substring.Replace('.', ','), out tempFloat)) { if (int.TryParse(unit.Substring, out tempInt)) { var const_ = ConstantList.Find(x => x._Const == tempInt); if (const_ != null) { LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: const_.Index)); } else { ConstantList.Add(new Const(tempInt, ConstantList.Count, "int")); LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: ConstantList.Count - 1)); } } else { var const_ = ConstantList.Find(x => x._Const == tempInt); if (const_ != null) { LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: const_.Index)); } else { ConstantList.Add(new Const(tempInt, ConstantList.Count, "float")); LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: ConstantList.Count - 1)); } } continue; } //if this lexem is a idnt; Idnt tempIdnt = IdentifierList.Find(x => x.Name == unit.Substring); if (tempIdnt != null) { LexemList.Add(new Lexem(unit.Row, tempIdnt.Name, 35, indexIdnt: tempIdnt.Index)); } else { if (lexUnitInRowList.Exists(x => (x.Substring.Contains("float"))) && (lexUnitInRowList.Exists(x => x.Substring.Contains("=")))) { IdentifierList.Add(new Idnt(unit.Substring, IdentifierList.Count, "float")); LexemList.Add(new Lexem(unit.Row, unit.Substring, 35, indexIdnt: IdentifierList.Count - 1)); } else if (lexUnitInRowList.Exists(x => (x.Substring.Contains("int"))) && (lexUnitInRowList.Exists(x => x.Substring.Contains("=")))) { IdentifierList.Add(new Idnt(unit.Substring, IdentifierList.Count, "int")); LexemList.Add(new Lexem(unit.Row, unit.Substring, 35, indexIdnt: IdentifierList.Count - 1)); } else if ((lexUnitInRowList.Exists(x => x.Substring.Contains("program")))) { IdentifierList.Add(new Idnt(unit.Substring, IdentifierList.Count, "program")); LexemList.Add(new Lexem(unit.Row, unit.Substring, 35, indexIdnt: IdentifierList.Count - 1)); } else { throw new Exception($"not defined variable {unit.Substring} "); } } } }