concept compiler in category .net

This is an excerpt from Manning's book Concurrency in .NET.
There are also interesting open source projects, such as OpenMP (which provides pragmas [compiler-specific definitions that you can use to create new preprocessor functionality or to send implementation-defined information to the compiler] that you can insert into a program to make parts of it parallel) and OpenCL (a low-level language to communicate with Graphic Processing Units [GPUs]). GPU programming has traction and has been sanctioned by Microsoft with C++ AMP extensions and Accelerator .NET.
The real magic is that the asynchronous workflow will complete at a later time, but you don’t have to worry about waiting for the result because it will be passed as an argument in the continuation function when completed. The compiler takes care of all of this, organically converting the
Bindmember calls into the continuation constructs. This mechanism uses CPS for writing, implicitly, a structured callback-based program inside its body expression, which allows a linear style of coding over a sequence of operations.
Listing 9.2 shows the desugared version of the function
downloadMediaAsync, where the compiler translates the computation expression into a chain of method calls. This unwrapped code shows how the behavior of each single asynchronous part is encapsulated in the related primitive member of the computation builder. The keywordasynctells the F# compiler to instantiate theAsyncBuilder, which implements the essential asynchronous workflow membersBind,Return,Using,Combine, and so on. The listing shows how the compiler translates the computation expression into a chain of method calls of the code from Listing 9.1. (The code to note is in bold.)Listing 9.2 Desugared
DownloadMediaAsynccomputation expressionlet downloadMediaAsync(blobName:string) (fileNameDestination:string) = async.Delay(fun() -> #1 async.Bind(getCloudBlobContainerAsync(), fun container -> #2 let blockBlob = container.GetBlockBlobReference(blobName) async.Using(blockBlob.OpenReadAsync(), fun (blobStream:Stream) -> #3 let sizeBlob = int blockBlob.Properties.Length async.Bind(blobStream.AsyncRead(sizeBlob), fun bytes -> use fileStream = new FileStream(fileNameDestination, ➥ FileMode.Create, FileAccess.Write, FileShare.None, bufferSize, ➥ FileOptions.Asynchronous) async.Bind(fileStream.AsyncWrite(bytes, 0, bytes.Length), fun () -> fileStream.Close() blobStream.Close() async.Return()))))) #4 #1 Delays execution of the function until an explicit request #2 The Bind operator is the desugared version of the let! operator. #3 The Using operator translates to the use! operator. #4 Returns the operator that completes the computation expression !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":12}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":11}],[{\"line\":6,\"ch\":11},{\"line\":6,\"ch\":21}],[{\"line\":10,\"ch\":7},{\"line\":10,\"ch\":17}],[{\"line\":4,\"ch\":1},{\"line\":4,\"ch\":12}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":11}],[{\"line\":6,\"ch\":11},{\"line\":6,\"ch\":21}],[{\"line\":10,\"ch\":7},{\"line\":10,\"ch\":17}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":11}],[{\"line\":6,\"ch\":11},{\"line\":6,\"ch\":21}],[{\"line\":10,\"ch\":7},{\"line\":10,\"ch\":17}]]"} !@%STYLE%@!In the code, the compiler transforms the
let!binding construct into a call to theBindoperation, which unwraps the value from the computation type and executes the rest of the computation converted to a continuation. TheUsingoperation handles computation where the resulting value type represents a resource that can be disposed. The first member in the chain,Delay, wraps the expression as a whole to manage the execution, which can run later on demand.

This is an excerpt from Manning's book C# in Depth.
The first thing to spot in listing 1.5 is that you had to introduce an extra type to help with the sorting. That’s not a disaster, but it’s a lot of code if you only want to sort by name in one place. Next, look at the casts in the Compare method. Casts are a way of telling the compiler that you know more information than it does, and that usually means there’s a chance you’re wrong. If the ArrayList you returned from GetSampleProducts did contain a string, that’s where the code would go bang—where the comparison tries to cast the string to a Product.
You also have a cast in the code that displays the sorted list. It’s not obvious, because the compiler puts it in automatically, but the foreach loop implicitly casts each element of the list to Product. Again, that cast could fail at execution time, and once more generics come to the rescue in C# 2. The following listing shows the previous code with the use of generics as the only change.
Listing 1.6. Sorting a List<Product> using IComparer<Product> (C# 2)
class ProductNameComparer : IComparer<Product> { public int Compare(Product x, Product y) { return x.Name.CompareTo(y.Name); } } ... List<Product> products = Product.GetSampleProducts(); products.Sort(new ProductNameComparer()); foreach (Product product in products) { Console.WriteLine(product); }The code for the comparer in listing 1.6 is simpler because you’re given products to start with. No casting is necessary. Similarly, the invisible cast in the foreach loop is effectively gone now. The compiler still has to consider the conversion from the source type of the sequence to the target type of the variable, but it knows that in this case both types are Product, so it doesn’t need to emit any code for the conversion.
Listing 7.6. Using :: to tell the compiler to use aliases
using System; using WinForms = System.Windows.Forms; using WebForms = System.Web.UI.WebControls; class WinForms {} class Test { static void Main() { Console.WriteLine(typeof(WinForms::Button)); Console.WriteLine(typeof(WebForms::Button)); } }
Query expressions are effectively preprocessed by the compiler into “normal” C# 3, which is then compiled in an ordinary way. This is a neat way of integrating queries into the language without changing the specification in more than one small section. Most of this chapter is a list of the preprocessing translations performed by the compiler, as well as the effects achieved when the result uses the Enumerable extension methods.
The C# 3 query expression support is based on the compiler translating query expressions into normal C# code. It does this in a mechanical manner that doesn’t try to understand the code, apply type inference, check the validity of method calls, or perform any of the normal business of a compiler. That’s all done later, after the translation. In many ways, this first phase can be regarded as a preprocessor step.

This is an excerpt from Manning's book DSLs in Boo: Domain-Specific Languages in .NET.
Internal DSLs are built on top of an existing language, but they don’t try to remain true to the original programming language syntax. They try to express things in a way that makes sense to both the author and the reader, not to the compiler.
Listing 4.3. The Message-Routing DSL engine
public class RoutingDslEngine : DslEngine { protected override void CustomizeCompiler( BooCompiler compiler, CompilerPipeline pipeline, string[] urls) { // The compiler should allow late bound semantics compiler.Parameters.Ducky = true; pipeline.Insert(1, new ImplicitBaseClassCompilerStep( // the base type typeof (RoutingBase), // the method to override "Route", // import the following namespaces "Chapter4.MessageRouting.Handlers", "Chapter4.MessageRouting.Messages")); } }
But before we can start talking about compiler extensibility, we need to look at the compiler itself and define some common terms. A lot of Boo’s capabilities are exposed in ways that only make sense if you understand these concepts. I’ll assume compilers aren’t your area of expertise and make sure that you can understand and use the capabilities we’ll look at.
You won’t need this knowledge on a day-to-day basis. You can build most DSLs without going deep into the compiler, but it’s important to understand how things work under the hood. There are some situations when it’s easier to go into the compiler and make a change than work around the problem with the tools that are exposed on the surface.
The basic structure of the Boo compiler is the pipeline—it’s how the compiler transforms bits of text in a file into executable code.

This is an excerpt from Manning's book LINQ in Action.
Too often, programmers have to do a lot of plumbing work to tie together the different domains. Different APIs for each data type cause developers to spend an inordinate amount of time learning how to write, debug, and rewrite brittle code. The usual culprits that break the pipes are bad SQL query strings or XML tags, or content that doesn’t get checked until runtime. .NET languages such as C# and VB.NET assist developers and provide such things as IntelliSense, strongly typed code, and compile-time checks. Still, this can become broken if we start to include malformed SQL queries or XML fragments in our code, none of which are validated by the compiler.
C# 3.0 offers a new keyword that allows us to declare a local variable without having to specify its type explicitly: var. When the var keyword is used to declare a local variable, the compiler infers the type of this variable from the expression used to initialize it.
Anonymous types are types without names,[1] but types anyway. This means that a real type is created by the compiler. Our results variable points to an instance of a class that is created automatically based on our code. This class has three properties: TotalMemory, Top2Memory, and Processes. The types of the properties are deduced from the initializers.