public override void Run(MacroAsm masm, ref string text, ref int pos) { MatchCollection res = Lexer.Run(Parse(ref text, ref pos)); if (res.Count < 4) { throw new Exception("[if] - Недостаточное количество параметров"); } string expression = ""; for (int i = 1; i < res.Count; i++) { expression += res[i].Value; } bool check = _engine.Execute(expression); if (check) { var item = GetBlock(text, pos); pos = item.Index; text = text.Remove(item.Index, item.Length); if (item.Value == DirNames.Else.Value) { Skip(ref text, pos); } } else { Skip(ref text, pos); } }
public override void Run(MacroAsm masm, ref string text, ref int pos) { MatchCollection res = Lexer.Run(Parse(ref text, ref pos)); string value = null; switch (res.Count) { case 1: throw new Exception("[define] - Ожидалось имя и значение закрепленное за ним"); case 2: masm.Table.Add(res[1].Value, value); break; default: for (int i = 2; i < res.Count; i++) { value += res[i].Value; } if (value[0] != '\'' && value[value.Length - 1] != '\'') { value = _engine.Execute(value).ToString(); } masm.Table.Add(res[1].Value, value); break; } Regex r = new Regex($"\\b{res[1].Value}\\b"); if (value != null) { text = r.Replace(text, value); } }
public override void Run(MacroAsm masm, ref string text, ref int pos) { _args.Clear(); var parsePart = Parse(ref text, ref pos); var endFirstLine = parsePart.IndexOf('\n'); MatchCollection headLexems = Lexer.Run(parsePart.Substring(0, endFirstLine)); var body = parsePart.Remove(0, endFirstLine + 1); if (headLexems.Count < 3) { throw new Exception("[foreach] - Не указаны значения для подстановки в след. блок"); } _var = headLexems[1].Value; for (int i = 2; i < headLexems.Count; i++) { var tmp = headLexems[i].CurGroup(); if (tmp == (int)Lexer.Lexems.Ident || tmp == (int)Lexer.Lexems.Number) { _args.Add(headLexems[i].Value); } } _localRegex = new Regex($"\\${_var}"); string result = ""; foreach (var item in _args) { result += _localRegex.Replace(body, item); } text = text.Insert(pos, result); }
public override void Run(MacroAsm masm, ref string text, ref int pos) { // получаем все макроопределение // разделяем все макроопределение на голову и тело // голова содержит имя и аргументы // тело содержит команды ассемблера var body = Parse(ref text, ref pos); var head = body.Substring(0, body.IndexOf("\n", 0)); body = body.Substring(head.Length, body.Length - head.Length); MatchCollection res = Lexer.Run(head); if (res.Count < 2) { throw new Exception("Ожидалось имя макроса"); } if (res[1].CurGroup() != (int)Lexer.Lexems.Ident) { throw new Exception("Ожидалось имя макроса"); } // устанавливаем название макроопределения // а также аргументы _name = res[1].Value; _args = GetArgs(res, 2); FindAndChangeMacros(ref text, body); }
public override void Run(MacroAsm masm, ref string text, ref int pos) { _filename = null; MatchCollection res = Lexer.Run(Parse(ref text, ref pos)); if (res.Count < 4) { throw new Exception("[import] - Неверный формат директивы: #import <...>"); } if (res[1].Value != "<") { throw new Exception(@"[import] - Ожидался символ '<'"); } // путь до файла, который необходимо импортировать for (int i = 2; i < res.Count; i++) { switch (res[i].CurGroup()) { case (int)Lexer.Lexems.Sign: if (res[i].Value == ">") { i = res.Count; } else if (res[i].Value == "<") { throw new Exception(@"[import] - Ошибочный символ '<'"); } else { _filename += res[i].Value; } break; default: _filename += res[i].Value; break; } } using (StreamReader sr = new StreamReader(_filename)) { text = text.Insert(pos, sr.ReadToEnd()); Directive.EraseComments(ref text); } }
public override void Run(MacroAsm masm, ref string text, ref int pos) { _filename = ""; // очищаем имя файла MatchCollection res = Lexer.Run(Parse(ref text, ref pos)); if (res.Count < 4) { /* кинуть иск. неверный формат директивы: #import <...> */ } if (res[1].Value == "<") { /* ожидалось <*/ } // путь до файла, который необходимо импортировать for (int i = 1; i < res.Count; i++) { switch (res[i].CurGroup()) { case (int)Lexer.Lexems.Sign: if (res[i].Value == ">") { i = res.Count; } else if (res[i].Value == "<") { /* Ошибочный символ < */ } else { _filename += res[i].Value; } break; default: _filename += res[i].Value; break; } } using (StreamReader sr = new StreamReader(_filename)) { text = text.Insert(pos, sr.ReadToEnd()); EraseComments(ref text); } }
public override void Run(MacroAsm masm, ref string text, ref int pos) { MatchCollection res = Lexer.Run(Parse(ref text, ref pos)); bool check = masm.Table.ContainsKey(res[1].Value); if (($"#{res[0].Value}" == DirNames.IfDef.Value && check) || ($"#{res[0].Value}" == DirNames.IfNDef.Value && !check)) { var item = GetBlock(text, pos); pos = item.Index; text = text.Remove(item.Index, item.Length); if (item.Value == DirNames.Else.Value) { Skip(ref text, pos); } } else { Skip(ref text, pos); } }
public override void Run(MacroAsm masm, ref string text, ref int pos) { MatchCollection res = Lexer.Run(Parse(ref text, ref pos)); bool check = masm.Defines.ContainsKey(res[1].Value); if ((res[0].Value == "ifdef" && check) || (res[0].Value == "ifndef" && !check)) { var item = GetBlock(text, pos); pos = item.Index; text = text.Remove(item.Index, item.Length); if (DefineType(item.Value) == AllCommands.Else) { Skip(ref text, pos); } } else { Skip(ref text, pos); } }
public override void Run(MacroAsm masm, ref string text, ref int pos) { var parsePart = Parse(ref text, ref pos); var endFirstLine = parsePart.IndexOf('\n'); var headLexems = Lexer.Run(parsePart.Substring(0, endFirstLine)); if (headLexems.Count < 2) { throw new Exception("[repeat] - Не указано количество повторений"); } if (headLexems[1].CurGroup() != (int)Lexer.Lexems.Number) { throw new Exception("[repeat] - Количество повторений должно быть целым числом"); } _countOfRepeat = Int32.Parse(headLexems[1].Value); var body = parsePart.Remove(0, endFirstLine + 1); string result = ""; for (int i = 0; i < _countOfRepeat; i++) { result += body; } text = text.Insert(pos, result); }
public override void Run(MacroAsm masm, ref string text, ref int pos) { MatchCollection res = Lexer.Run(Parse(ref text, ref pos)); try { string value = ""; switch (res.Count) { case 1: throw new Exception("Ожидалось имя и значение закрепленное за ним"); case 2: value = MacroAsm.NULL; masm.Defines.Add(res[1].Value, value); break; default: for (int i = 2; i < res.Count; i++) { value += res[i].Value; } masm.Defines.Add(res[1].Value, value); break; } Regex r = new Regex($"\\b{res[1].Value}\\b"); if (value != MacroAsm.NULL) { text = r.Replace(text, value); } } catch (ArgumentException) { /*кинуть исключение, такое имя уже объявлено*/ }; }
public abstract void Run(MacroAsm masm, ref string text, ref int pos);