public void Test() { const int rows = 2; const int columns = 3; int[,] table = new int[rows, columns]; for (int r = 0; r != rows; ++r) { for (int c = 0; c != columns; ++c) { table[r, c] = r * columns + c; } } var target = new ReadOnlyTableGenerator(new MutableTable <int>(table), emit => emit.Ldarg(0), emit => emit.Ldarg(1)); var getCell = new CachedMethod <Func <int, int, int> >("TableSerializerTest.Assembly0", (emit, args) => { target.Build(emit); return(emit.Ret()); }).Delegate; for (int r = 0; r != rows; ++r) { for (int c = 0; c != columns; ++c) { Assert.AreEqual(table[r, c], getCell(r, c), "r={0}, c={1}", r, c); } } }
public SRegex(string pattern, SRegexOptions options = SRegexOptions.Default) { var ast = Language.Parse(new SreSyntax(), pattern).Result.Node; switch (options) { case SRegexOptions.ByteCodeCompilation: var compiler = new NfaVMBytecodeBackend(ast); matcher = (int[] input) => { var vm = new PikeNfaVM(compiler.Code.ToArray()); vm.Feed(input.Select(ch => (int)ch)).Done(); return vm.HasMatch; }; break; case SRegexOptions.ILCompilation: string methodName = "MatchSrePattern" + PatternID++; var builder = new CachedMethod<MatchDelegate>( methodName, (emit, args) => EmitAst(emit, ast, args[0])); matcher = builder.Delegate; break; case SRegexOptions.NfaCompilation: var nfa = new Nfa(ast); matcher = nfa.Match; break; case SRegexOptions.DfaCompilation: var dfa = new RegularToDfaAlgorithm(new RegularTree(ast)); var simulation = new DfaSimulation(dfa.Data); matcher = input => simulation.Match(input); break; } }
/// <summary> /// Get all methods of specified type. /// </summary> public List <CachedMethod> GetMethods(BindingFlags flags) { if (mMethods == null) { mMethods = new Dictionary <BindingFlags, List <CachedMethod> >(); } List <CachedMethod> list = null; if (!mMethods.TryGetValue(flags, out list)) { list = new List <CachedMethod>(); var methods = type.GetMethods(flags); for (int im = 0, imm = methods.Length; im < imm; ++im) { var cmi = new CachedMethod(); cmi.method = methods[im]; cmi.name = cmi.method.Name; cmi.parameters = cmi.method.GetParameters(); var c = cmi.parameters.Length; cmi.paramTypes = new Type[c]; for (int i = 0; i < c; ++i) { cmi.paramTypes[i] = cmi.parameters[i].ParameterType; } list.Add(cmi); } mMethods[flags] = list; } return(list); }
public Pipe <TContext> Compile(string input) { SppfNode root = BuildTree(input); var cachedMethod = new CachedMethod <Pipe <TContext> >("temp", (emit, args) => Build(emit, args, root)); return(cachedMethod.Delegate); }
public void Test() { const string SampleString = "Hello world 01234567890!"; var target = new CachedMethod<TestDelegate>( "print", (emit, args) => emit .Ldarg(args[0]) .Ret() ); Assert.AreEqual(SampleString, target.Delegate(SampleString)); }
public void Test() { const string SampleString = "Hello world 01234567890!"; var target = new CachedMethod <TestDelegate>( "print", (emit, args) => emit.Ldarg(args[0]).Ret() ); Assert.AreEqual(SampleString, target.Delegate(SampleString)); }
private ProductionActionDelegate BuildExecuteRuleAction() { var generator = new ProductionActionGenerator(); var result = new CachedMethod <ProductionActionDelegate>( "Bootstrap." + source.FullLanguageName + ".GrammarActions", (emit, args) => { generator.BuildBody(emit, data, args); return(emit.Ret()); }).Delegate; return(result); }
public void Test() { var originalGrammar = new Grammar(); var red = originalGrammar.Symbols.Add("red"); var green = originalGrammar.Symbols.Add("green", SymbolCategory.ExplicitlyUsed); var blue = originalGrammar.Symbols.Add("blue"); originalGrammar.Productions.Define(red, new[] { green, blue }); originalGrammar.Productions.Define(blue, new[] { red, green }); originalGrammar.Productions.Define(blue, new Symbol[0]); originalGrammar.Start = red; GrammarSerializer target = new GrammarSerializer(originalGrammar); var factory = new CachedMethod<Func<Grammar>>("GrammarSerializerTest.Assembly0", (emit, args) => { target.Build(emit); return emit.Ret(); }).Delegate; var recreated = factory(); Assert.IsTrue(GrammarEquals(originalGrammar, recreated)); }
public void Test() { var originalGrammar = new Grammar(); var red = originalGrammar.Symbols.Add("red"); var green = originalGrammar.Symbols.Add("green", SymbolCategory.ExplicitlyUsed); var blue = originalGrammar.Symbols.Add("blue"); originalGrammar.Productions.Define(red, new[] { green, blue }); originalGrammar.Productions.Define(blue, new[] { red, green }); originalGrammar.Productions.Define(blue, new Symbol[0]); originalGrammar.Start = red; GrammarSerializer target = new GrammarSerializer(originalGrammar); var factory = new CachedMethod <Func <Grammar> >("GrammarSerializerTest.Assembly0", (emit, args) => { target.Build(emit); return(emit.Ret()); }).Delegate; var recreated = factory(); Assert.IsTrue(GrammarEquals(originalGrammar, recreated)); }
public void Test() { const int rows = 2; const int columns = 3; int[,] table = new int[rows, columns]; for (int r = 0; r != rows; ++r) for (int c = 0; c != columns; ++c) { table[r,c] = r * columns + c; } var target = new ReadOnlyTableGenerator(new MutableTable<int>(table), emit => emit.Ldarg(0), emit=> emit.Ldarg(1)); var getCell = new CachedMethod<Func<int,int,int>>("TableSerializerTest.Assembly0", (emit, args) => { target.Build(emit); return emit.Ret(); }).Delegate; for (int r = 0; r != rows; ++r) for (int c = 0; c != columns; ++c) { Assert.AreEqual(table[r, c], getCell(r, c), "r={0}, c={1}", r, c); } }
public SRegex(string pattern, SRegexOptions options = SRegexOptions.Default) { var ast = Language.Parse(new SreSyntax(), pattern).Result.Node; switch (options) { case SRegexOptions.ByteCodeCompilation: var compiler = new NfaVMBytecodeBackend(ast); matcher = (int[] input) => { var vm = new PikeNfaVM(compiler.Code.ToArray()); vm.Feed(input.Select(ch => (int)ch)).Done(); return(vm.HasMatch); }; break; case SRegexOptions.ILCompilation: string methodName = "MatchSrePattern" + PatternID++; var builder = new CachedMethod <MatchDelegate>( methodName, (emit, args) => EmitAst(emit, ast, args[0])); matcher = builder.Delegate; break; case SRegexOptions.NfaCompilation: var nfa = new Nfa(ast); matcher = nfa.Match; break; case SRegexOptions.DfaCompilation: var dfa = new RegularToDfaAlgorithm(new RegularTree(ast)); var simulation = new DfaSimulation(dfa.Data); matcher = input => simulation.Match(input); break; } }