Основы объектно-ориентированного программирования на языке C# book | Page 195
8.5 Комплексна вправа до роздiлу 7
}
private static Expression Parse(IInput input, char? stop)
{
return RawExpression.Parse(input, stop);
}
public static Expression Parse(IInput input)
{
input.MoveNext();
return Parse(input, null);
}
public static Expression Parse(string input)
{
return Parse((StringInput)input);
}
}
public class ArithmeticOps : IEnumerable
{
private static IEnumerable ops =
new Expression.Operation[] {
new Expression.UnaryOp(’-’, x => -x),
new Expression.BinaryOp(1, ’+’, (x, y) => x + y),
new Expression.BinaryOp(1, ’-’, (x, y) => x - y),
new Expression.BinaryOp(2, ’*’, (x, y) => x * y),
new Expression.BinaryOp(2, ’/’, (x, y) => x / y)
};
private static bool Registered = false;
public static void Register()
{
if (!Registered)
{
Expressions.Expression.OperationsRegistry.Register(
new ArithmeticOps()
);
}
}
private ArithmeticOps() { }
IEnumerator
IEnumerable.GetEnumerator()
{
return ops.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ops.GetEnumerator();
}
}
}
class Program
{
195