Skip to content

Commit

Permalink
docs: add OpCodes browser sample
Browse files Browse the repository at this point in the history
  • Loading branch information
rdavisau committed May 11, 2019
1 parent da1683b commit 88eadc8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion linqpad-samples/Examples/FileOrder.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Auto Charter.linq
Spritesheet Cutter.linq
Loan Balance Calculator.linq
Loan Balance Calculator.linq
OpCode Browser.linq
42 changes: 42 additions & 0 deletions linqpad-samples/Examples/OpCode Browser.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<Query Kind="Program">
<Reference Relative="..\..\..\..\Source\Repos\linqpad-dump-editable\src\DumpEditable\bin\Debug\net47\LINQPad.DumpEditable.dll">C:\Users\rdavis\Source\Repos\linqpad-dump-editable\src\DumpEditable\bin\Debug\net47\LINQPad.DumpEditable.dll</Reference>
<Namespace>LINQPad.DumpEditable</Namespace>
<Namespace>System.Reflection.Emit</Namespace>
</Query>

void Main()
{
EditableDumpContainer.DefaultOptions.StringBasedEditor =
Editors.TextBoxBasedStringEditor(liveUpdates: true);

var filter = new OpCodeFilter().DumpEditable(out var editor);
var opcodes = new DumpContainer().Dump();
var selectedOpcode = new DumpContainer() { DumpDepth = 2 }.Dump();

var filteredOpcodes =
AllOpCodes
.Where(filter.Matches)
.Select(o => new Hyperlinq(() => selectedOpcode.Content = o, o.Name));

editor.OnChanged += () => opcodes.Content = Util.HorizontalRun(true, filteredOpcodes);
editor.OnChanged();
}

public IEnumerable<OpCode> AllOpCodes
=> typeof(OpCodes)
.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.Default)
.Select(f => (OpCode) f.GetValue(null));

public class OpCodeFilter
{
public string Name { get; set; }
public OpCodeType? OpCodeType { get; set; }
public FlowControl? FlowControl { get; set; }
public OperandType? OperandType{ get; set; }

public bool Matches(OpCode o)
=> (String.IsNullOrWhiteSpace(Name) || o.Name.ToLower().Contains(Name.ToLower()))
&& (OpCodeType is null || o.OpCodeType == OpCodeType)
&& (FlowControl is null || o.FlowControl == FlowControl)
&& (OperandType is null || o.OperandType == OperandType);
}

0 comments on commit 88eadc8

Please sign in to comment.