public CanCauseError <byte[]> GetData(IExpression <int>[] code, Func <string, int?> getSymbolValue, ScopeStructure <int> scope) { if (!canBeRepeated) { return(GetDataUnit(code, getSymbolValue, scope)); } if (code.Length == 0) { return(CanCauseError <byte[]> .Error("Encountered {0} code with no parameters", Name)); } List <byte> byteList = new List <byte>(code.Length * this.LengthInBytes); int num = code.Length / AmountOfParams; for (int index = 0; index < num; ++index) { CanCauseError <byte[]> dataUnit = GetDataUnit(new IExpression <int>[1] { code[index] }, getSymbolValue, scope); if (dataUnit.CausedError) { return(dataUnit.ConvertError <byte[]>()); } byteList.AddRange(dataUnit.Result); } return((CanCauseError <byte[]>)byteList.ToArray()); }
public CanCauseError <ICodeTemplate> FindTemplate(byte[] code, int index, IEnumerable <Priority> allowedPriorities) { int num = (int)code[index] + (int)code[index + 1] * 256; foreach (Priority allowedPriority in allowedPriorities) { List <ICodeTemplate> codeTemplateList; if (this.disassemblyCodes.TryGetValue(new KeyValuePair <Priority, int>(allowedPriority, num), out codeTemplateList) || num != 0 && this.disassemblyCodes.TryGetValue(new KeyValuePair <Priority, int>(allowedPriority, 0), out codeTemplateList)) { List <ICodeTemplate> collection = new List <ICodeTemplate>(); foreach (ICodeTemplate codeTemplate in codeTemplateList) { if (codeTemplate.CanBeDisassembled && codeTemplate.Matches(code, index)) { collection.Add(codeTemplate); } } if (collection.Count > 0) { return(CanCauseError <ICodeTemplate> .NoError(collection.Max <ICodeTemplate>(this.templateComparer))); } } } return(CanCauseError <ICodeTemplate> .Error("No code found.")); }
public CanCauseError Apply(string[] parameters, IDirectivePreprocessor host) { CanCauseError canCauseError; if (parameters.Length > 1) { int length = parameters[0].IndexOf('('); int num = parameters[0].LastIndexOf(')'); string[] strArray; string name; if (length != -1 && num != -1 && length < num) { strArray = parameters[0].Substring(length + 1, num - length - 1).Split(macroSeparators, uniters); name = parameters[0].Substring(0, length); } else { strArray = new string[0]; name = parameters[0]; } for (int index = 0; index < strArray.Length; ++index) { strArray[index] = strArray[index].Trim(); } if (name.Equals(parameters[1])) { canCauseError = CanCauseError.Error("Defining something as itself. "); } else if (!host.DefCol.IsValidName(name)) { canCauseError = CanCauseError.Error(name + " is not valid name to define."); } else if (host.IsValidToDefine(name)) { canCauseError = CanCauseError.Error(name + " cannot be redefined."); } else { if (host.DefCol.ContainsName(name, strArray)) { host.Log.AddWarning(host.Input.GetPositionString() + ", Warning: Redefining " + name); host.DefCol.Remove(name, strArray); } host.DefCol.Add(name, parameters[1].Trim('"'), strArray); canCauseError = CanCauseError.NoError; } } else if (parameters.Length == 1) { host.DefCol.Add(parameters[0], ""); canCauseError = CanCauseError.NoError; } else { canCauseError = CanCauseError.NoError; } return(canCauseError); }
public CanCauseError Apply(string[] parameters, IDirectivePreprocessor host) { if (host.Include.Count <= 0) { return(CanCauseError.Error("#endif used without #ifdef or #ifndef.")); } host.Include.Pop(); return(CanCauseError.NoError); }
public CanCauseError AddNewSymbol(string symbol, IExpression <T> value) { if (definedSymbols.ContainsKey(symbol)) { return(CanCauseError.Error("Symbol \"{0}\" already exists (ignoring second definition).", symbol)); } definedSymbols[symbol] = value; return(CanCauseError.NoError); }
private CanCauseError <ICodeTemplate> GetTemplateFrom(string codeName, Language.Types.Type[] parameterTypes, List <ICodeTemplate> templates) { IEnumerable <ICodeTemplate> codeTemplates = templates.Where <ICodeTemplate>((Func <ICodeTemplate, bool>)(template => template.Matches(parameterTypes))); if (codeTemplates.Any <ICodeTemplate>()) { return(CanCauseError <ICodeTemplate> .NoError(codeTemplates.Min <ICodeTemplate>(this.templateComparer))); } return(CanCauseError <ICodeTemplate> .Error(parameterTypes.Length != 0? "Incorrect parameters in code {0} {1}" : "Incorrect parameters in code {0}", (object)codeName, (object)((IEnumerable <Language.Types.Type>)parameterTypes).ToElementWiseString <Nintenlord.Event_Assembler.Core.Code.Language.Types.Type>(" ", "", ""))); }
public CanCauseError Apply(string[] parameters, IDirectivePreprocessor host) { if (host.Include.Count <= 0) { return(CanCauseError.Error("#else before any #ifdef or #ifndef.")); } bool flag = host.Include.Pop(); host.Include.Push(!flag); return(CanCauseError.NoError); }
public CanCauseError <bool> SecondPass(string[] code, Context context) { if (context.MoveToNextScope()) { return(false); } else { return(CanCauseError <bool> .Error("INTERNAL: No scope to move to.")); } }
public CanCauseError FirstPass(string[] code, Context context) { if (context.ScopesOnStack == 0) { return(CanCauseError.Error("No scope to end")); } else { context.EndScope(); return(CanCauseError.NoError); } }
public CanCauseError Apply(string[] parameters, IDirectivePreprocessor host) { /* * for (int i=1; i<parameters.Length; i++) * { * CanCauseError<string> res = host.DefCol.ApplyDefines(parameters[i]); * if (!res.CausedError) * parameters[i] = res.Result; * }*/ string file = IO.IOHelpers.FindFile(host.Input.CurrentFile, getFileName(parameters[0])); if (file.Length <= 0) { return(CanCauseError.Error("Tool " + parameters[0] + " not found.")); } //from http://stackoverflow.com/a/206347/1644720 // Start the child process. System.Diagnostics.Process p = new System.Diagnostics.Process(); // Redirect the output stream of the child process. p.StartInfo.WorkingDirectory = Path.GetDirectoryName(host.Input.CurrentFile); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.FileName = file; string[] passedParams = parameters.GetRange(1); for (int i = 0; i < passedParams.Length; i++) { if (passedParams[i].ContainsWhiteSpace()) { passedParams[i] = "\"" + passedParams[i] + "\""; } } p.StartInfo.Arguments = passedParams.ToElementWiseString(" ", "", " --to-stdout"); p.Start(); // Do not wait for the child process to exit before // reading to the end of its redirected stream. // p.WaitForExit(); // Read the output stream first and then wait. MemoryStream outputBytes = new MemoryStream(); p.StandardOutput.BaseStream.CopyTo(outputBytes); p.WaitForExit(); byte[] output = outputBytes.GetBuffer().GetRange(0, (int)outputBytes.Length); if (output.Length >= 7 && Encoding.ASCII.GetString(output.GetRange(0, 7)) == "ERROR: ") { return(CanCauseError.Error(Encoding.ASCII.GetString(output.GetRange(7)))); } host.Input.AddBytes(output); return(CanCauseError.NoError); }
public CanCauseError <int> Parse(string text) { int result; if (text.TryGetValue(out result)) { return(result); } else { return(CanCauseError <int> .Error("Improperly formatted integer.")); } }
public CanCauseError <string> Replace(string textToEdit) { StringBuilder textToEdit1 = new StringBuilder(textToEdit); CanCauseError canCauseError = this.Replace(textToEdit1); if (canCauseError.CausedError) { return(CanCauseError <string> .Error(canCauseError.ToString())); } return(CanCauseError <string> .NoError(textToEdit1.ToString())); }
public CanCauseError AddNewSymbol(string symbol, IExpression <T> value) { //Detect if adds cycles if (definedSymbols.ContainsKey(symbol)) { return(CanCauseError.Error("Symbol already exists.")); } else { definedSymbols[symbol] = value; return(CanCauseError.NoError); } }
public CanCauseError FirstPass(string[] code, Context context) { int value; if (code[1].TryGetValue(out value)) { context.Offset = context.Offset.ToMod(value); return(CanCauseError.NoError); } else { return(CanCauseError.Error(code[1] + " is not a valid number.")); } }
public CanCauseError <string> Replace(string textToEdit) { StringBuilder bldr = new StringBuilder(textToEdit); var result = this.Replace(bldr); if (result.CausedError) { return(CanCauseError <string> .Error(result.ToString())); } else { return(CanCauseError <string> .NoError(bldr.ToString())); } }
private CanCauseError HandleParameter(string parameter) { switch (paramCount) { case 0: operation = parameter; break; case 1: language = parameter; break; case 2: DisassemblyMode disassemblyMode; if (!parameter.TryGetEnum(out disassemblyMode)) { return(CanCauseError.Error("{0} is not a valid disassembly mode.", parameter)); } this.disassemblyMode = disassemblyMode; break; case 3: int offset; if (!parameter.TryGetValue(out offset)) { return(CanCauseError.Error("{0} is not a valid offset.", parameter)); } this.offset = offset; break; case 4: Priority priority; if (!parameter.TryGetEnum(out priority)) { return(CanCauseError.Error("{0} is not a valid priority.", parameter)); } this.priority = priority; break; case 5: int size; if (!parameter.TryGetValue(out size) || size < 0) { return(CanCauseError.Error("{0} is not a valid size.", parameter)); } this.size = size; break; default: return(CanCauseError.Error("Too many parameters.")); } paramCount++; return(CanCauseError.NoError); }
public CanCauseError FirstPass(string[] code, Context context) { int newOffset; if (code[1].GetMathStringValue(out newOffset)) { context.Offset = newOffset; return(CanCauseError.NoError); } else { return(CanCauseError.Error(code[1] + " is not a valid offset.")); } }
public CanCauseError <IExpression <T> > GetSymbolValue(string symbol) { IExpression <T> result; if (definedSymbols.TryGetValue(symbol, out result)) { return(CanCauseError <IExpression <T> > .NoError(result)); } if (ParentScope != null) { return(ParentScope.GetSymbolValue(symbol)); } return(CanCauseError <IExpression <T> > .Error("Symbol {0} not defined", symbol)); }
public CanCauseError Replace(StringBuilder textToEdit) { if (currentIter == maxIter) { return(CanCauseError.Error("Maximum amount of replacement iterations exceeeded while applying macro.")); } ++currentIter; foreach (KeyValuePair <int, Tuple <int, IMacro, string[]> > macro in FindMacros(textToEdit)) { string[] strArray = macro.Value.Item3; string[] parameters = new string[strArray.Length]; for (int index = 0; index < strArray.Length; ++index) { if (macro.Value.Item2.ShouldPreprocessParameter(index)) { CanCauseError <string> canCauseError = this.Replace(strArray[index]); if (canCauseError.CausedError) { return((CanCauseError)canCauseError); } parameters[index] = canCauseError.Result; } else { parameters[index] = strArray[index]; } } CanCauseError <string> canCauseError1 = this.Replace(macro.Value.Item2.Replace(parameters)); if (canCauseError1.CausedError) { return((CanCauseError)canCauseError1); } string @string = textToEdit.Substring(macro.Key, macro.Value.Item1).ToString(); textToEdit.Replace(@string, canCauseError1.Result, macro.Key, @string.Length); } --currentIter; return(CanCauseError.NoError); }
public CanCauseError <ICodeTemplate> FindTemplate(string codeName, Language.Types.Type[] parameterTypes) { List <ICodeTemplate> templates; if (this.assemblyCodes.TryGetValue(codeName, out templates)) { return(this.GetTemplateFrom(codeName, parameterTypes, templates)); } if ((int)codeName[0] == '_') { if (this.assemblyCodes.TryGetValue(codeName.TrimStart('_'), out templates)) { return(this.GetTemplateFrom(codeName, parameterTypes, templates)); } } return(CanCauseError <ICodeTemplate> .Error("No code named {0} found.", (object)codeName)); }
public CanCauseError <IExpression <T> > GetSymbolValue(string symbol) { IExpression <T> value; if (definedSymbols.TryGetValue(symbol, out value)) { return(CanCauseError <IExpression <T> > .NoError(value)); } else if (ParentScope != null) { return(ParentScope.GetSymbolValue(symbol)); } else { return(CanCauseError <IExpression <T> > .Error("Symbol {0} not defined", symbol)); } }
public CanCauseError <ICodeTemplate> FindTemplate(string name, Priority priority) { List <ICodeTemplate> codeTemplateList = this.assemblyCodes[name]; foreach (KeyValuePair <KeyValuePair <Priority, int>, List <ICodeTemplate> > disassemblyCode in (IEnumerable <KeyValuePair <KeyValuePair <Priority, int>, List <ICodeTemplate> > >) this.disassemblyCodes) { if (disassemblyCode.Key.Key == priority) { foreach (ICodeTemplate result in disassemblyCode.Value) { if (codeTemplateList.Contains(result)) { return(CanCauseError <ICodeTemplate> .NoError(result)); } } } } return(CanCauseError <ICodeTemplate> .Error("No code named {0} found in priority {1}", (object)name, (object)priority)); }
public CanCauseError Apply(string[] parameters, IDirectivePreprocessor host) { string file = IOHelpers.FindFile(host.Input.CurrentFile, parameters [0]); if (file.Length <= 0) { if (!host.IncludeListener.AllowsMissingFiles()) { return(CanCauseError.Error("File " + parameters [0] + " not found.")); } file = IOHelpers.GetPrefferedFileName(host.Input.CurrentFile, parameters [0]); } else { host.Input.OpenSourceFile(file); } host.IncludeListener.IncludeTextFile(file); return(CanCauseError.NoError); }
public CanCauseError Replace(StringBuilder textToEdit) { if (currentIter == maxIter) { return(CanCauseError.Error("Maximun amount of replacement iterations exceeeded while applying macro.")); } currentIter++; IDictionary <int, Tuple <int, IMacro, string[]> > replace = FindMacros(textToEdit); foreach (var item in replace) { string[] parameters = item.Value.Item3; string[] newParameters = new string[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { var innerResult = Replace(parameters[i]); if (innerResult.CausedError) { return((CanCauseError)innerResult); } else { newParameters[i] = innerResult.Result; } } string tempString = item.Value.Item2.Replace(newParameters); var secondResult = Replace(tempString);//, out tempString if (secondResult.CausedError) { return((CanCauseError)secondResult); } string toReplace = textToEdit.Substring(item.Key, item.Value.Item1).ToString(); textToEdit.Replace(toReplace, secondResult.Result, item.Key, toReplace.Length); } currentIter--; return(CanCauseError.NoError); }
public CanCauseError Apply(string[] parameters, IDirectivePreprocessor host) { string fileName = IO.IOHelpers.FindFile(host.Input.CurrentFile, GetToolFileName(parameters[0])); if (fileName.Length <= 0) { return(CanCauseError.Error("Tool " + parameters[0] + " not found.")); } // Based on http://stackoverflow.com/a/206347/1644720 // Start the child process. System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.WorkingDirectory = Path.GetDirectoryName(host.Input.CurrentFile); process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.FileName = fileName; // Redirect the output stream of the child process. process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; string[] passedParams = parameters.GetRange(1); for (int i = 0; i < passedParams.Length; i++) { if (passedParams[i].ContainsWhiteSpace()) { passedParams[i] = "\"" + passedParams[i] + "\""; } } process.StartInfo.Arguments = passedParams.ToElementWiseString(" ", "", " --to-stdout"); process.Start(); // Do not wait for the child process to exit before // reading to the end of its redirected stream. // p.WaitForExit(); // Read the output stream first and then wait. MemoryStream outputBytes = new MemoryStream(); process.StandardOutput.BaseStream.CopyTo(outputBytes); MemoryStream errorBytes = new MemoryStream(); process.StandardError.BaseStream.CopyTo(errorBytes); process.WaitForExit(); // For tools that err using stderr if (errorBytes.Length > 0) { return(CanCauseError.Error(Encoding.ASCII.GetString(errorBytes.GetBuffer()))); } // For tools that err using stdout and "ERROR: ..." byte[] output = outputBytes.GetBuffer().GetRange(0, (int)outputBytes.Length); if (output.Length >= 7 && Encoding.ASCII.GetString(output.GetRange(0, 7)) == "ERROR: ") { return(CanCauseError.Error(Encoding.ASCII.GetString(output.GetRange(7)))); } return(ApplyIncludeTool(output, host)); }
private bool HandleBuiltInCode(Code <T> code, AssemblyContext <T> assemblyContext, bool addToLog) { string text; switch (code.CodeName.Name) { case messagePrinterCode: if (addToLog) { text = ExpressionToString(code, assemblyContext); assemblyContext.Log.AddMessage(text.Substring(code.CodeName.Name.Length + 1)); } return(true); case errorPrinterCode: if (addToLog) { text = ExpressionToString(code, assemblyContext); assemblyContext.Log.AddError(text.Substring(code.CodeName.Name.Length + 1)); } return(true); case warningPrinterCode: if (addToLog) { text = ExpressionToString(code, assemblyContext); assemblyContext.Log.AddWarning(text.Substring(code.CodeName.Name.Length + 1)); } return(true); case currentOffsetCode: case offsetAlinger: if (code.ParameterCount.IsInRange(1, 1)) { if (code[0] is ExpressionList <T> ) { if (addToLog) { assemblyContext.AddNotAtomTypeParameter(code[0]); } } else { var align = Folding.Fold(code[0], x => CanCauseError <T> .Error("No symbols available."), intType); if (align.CausedError) { if (addToLog) { assemblyContext.AddError(code, align); } } else { assemblyContext.CurrentOffset = assemblyContext.CurrentOffset.ToMod(intType.GetIntValue(align.Result)); } } } else { if (addToLog) { assemblyContext.AddNotCorrectParameters(code, 1); } } return(true); case offsetChanger: if (code.ParameterCount.IsInRange(1, 1)) { if (code[0] is ExpressionList <T> ) { if (addToLog) { assemblyContext.AddNotAtomTypeParameter(code[0]); } } else { var newOffset = Folding.Fold(code[0], x => CanCauseError <T> .Error("No symbols available."), intType); if (newOffset.CausedError) { if (addToLog) { assemblyContext.AddError(code, newOffset); } } else { assemblyContext.CurrentOffset = intType.GetIntValue(newOffset.Result); } } } else { if (addToLog) { assemblyContext.AddNotCorrectParameters(code, 1); } } return(true); default: return(false); } }
/* * static public int Fold(IExpression<int> expression) * { * CanCauseError<int> result = TryFold(expression); * if (result.CausedError) * { * throw new ArgumentException(); * } else * { * return result.Result; * } * }*/ static public CanCauseError <int> TryFold(IExpression <int> expression, Func <string, int?> symbolVals = null) { BinaryOperator <int> op = expression as BinaryOperator <int>; Func <int, int, int> func; switch (expression.Type) { case EAExpressionType.Value: return(((ValueExpression <int>)expression).Value); case EAExpressionType.Division: func = (x, y) => x / y; break; case EAExpressionType.Multiply: func = (x, y) => x * y; break; case EAExpressionType.Modulus: func = (x, y) => x % y; break; case EAExpressionType.Minus: func = (x, y) => x - y; break; case EAExpressionType.Sum: func = (x, y) => x + y; break; case EAExpressionType.XOR: func = (x, y) => x ^ y; break; case EAExpressionType.AND: func = (x, y) => x & y; break; case EAExpressionType.OR: func = (x, y) => x | y; break; case EAExpressionType.LeftShift: func = (x, y) => x << y; break; case EAExpressionType.RightShift: func = (x, y) => (int)(((uint)x) >> y); break; case EAExpressionType.ArithmeticRightShift: func = (x, y) => x >> y; break; case EAExpressionType.Symbol: string name = ((Symbol <int>)expression).Name; int? val = symbolVals != null?symbolVals(name) : null; return(val != null ? val : CanCauseError <int> .Error("Symbol {0} isn't in scope", name)); default: return(CanCauseError <int> .Error("Unsupported type: {0}", expression.Type)); } return(func.Map( TryFold((op.First ?? zeroExpression), symbolVals), TryFold((op.Second ?? zeroExpression), symbolVals) )); }
private CanCauseError HandleFlags(string flagName, string option) { switch (flagName) { case "addEndGuards": addEndGuards = true; break; case "raws": if (File.Exists(option)) { rawsFolder = option; isDirectory = false; } else if (Directory.Exists(option)) { rawsFolder = option; isDirectory = true; } else { return(CanCauseError.Error("File or folder {0} doesn't exist.", option)); } break; case "rawsExt": if (!option.ContainsAnyOf(Path.GetInvalidFileNameChars())) { rawsExtension = option; } else { return(CanCauseError.Error("Extension {0} is not valid.", option)); } break; case "input": inputFile = option; break; case "output": outputFile = option; break; case "error": if (IsValidFileName(option)) { errorFile = option; } else { return(CanCauseError.Error("Name {0} isn't valid for a file.", option)); } break; case "docHeader": if (IsValidFileName(option)) { docHeader = option; } else { return(CanCauseError.Error("Name {0} isn't valid for a file.", option)); } break; case "docFooter": if (IsValidFileName(option)) { docFooter = option; } else { return(CanCauseError.Error("Name {0} isn't valid for a file.", option)); } break; default: return(CanCauseError.Error("Flag {0} doesn't exist.", flagName)); } return(CanCauseError.NoError); }