private static INode BuildSubNodes(INode lhs, TokenBuffer tokens) { var mulDivOp = MulDivOp.Produce(tokens); if (mulDivOp == null) return lhs; var rhs = ExpRoot.Produce(tokens); if (rhs == null) { tokens.RestorePosition(); return lhs; } lhs = new MulDiv(lhs, mulDivOp, rhs); return BuildSubNodes(lhs, tokens); }
/// <summary> /// 代理 /// </summary> private static void testDelegate() { AddSub ass = new AddSub(); MulDiv md = new MulDiv(); Calculate add = new Calculate(ass.add); Calculate sub = new Calculate(ass.sub); Calculate mul = new Calculate(md.mul); Calculate div = new Calculate(md.div); Console.WriteLine("{0} + {1} = {2}", 2, 3, add(2, 3)); Console.WriteLine("{0} - {1} = {2}", 4, 5, sub(4, 5)); Console.WriteLine("{0} * {1} = {2}", 6, 7, mul(6, 7)); Console.WriteLine("{0} / {1} = {2}", 9, 3, div(9, 3)); //组合代理 Calculate cc = ass.add; cc += ass.sub;//add 和 sub都会执行 Console.WriteLine("{0} // {1} = {2}", 9, 3, cc(9, 3)); cc -= ass.sub;//去掉sub 只执行add方法 Console.WriteLine("{0} // {1} = {2}", 9, 3, cc(9, 3)); }