Introducing the Microsoft “Roslyn” CTP

Visual Studio Blog

Today we are releasing the first Community Technology Preview of the Roslyn Project!

What is Roslyn?

In the past, our compilers have acted as black boxes – you put source text in and out the other end comes an assembly. All of that rich knowledge and information that the compiler produces is thrown away and unavailable for anyone else to use.

As Soma mentions in his blog, a part of the Visual Studio languages team is working on a project called Roslyn with a goal to rewrite the C# and VB compilers and language services in managed code. With a clean, modern, managed codebase our team can be more productive, innovate faster, and deliver more features sooner and with better quality.

More importantly, we are opening up the C# and Visual Basic compilers and exposing all that rich information and code analysis to be available for your use. We expose a public API surface and provide extension points in the C# and VB language services.

This opens up new opportunities for VS extenders to write powerful refactorings and language analysis tools, as well as allow anyone to incorporate our parsers, semantic engines, code generators and scripting in their own applications.

Download the October 2011 CTP

The CTP and supporting materials can be downloaded from:

http://msdn.com/roslyn

The main goal of this early preview is to gather feedback on the API design and to introduce the C# Interactive window (also known as REPL, or Read-Eval-Print-Loop).

This first CTP is intended for preview-use only and does not allow redistribution of the Roslyn components or allow use in a production environment.

The CTP installs on Visual Studio 2010 SP1. It also requires the Visual Studio 2010 SP1 SDK.

Getting Started

After the installation succeeds, the best place to start is to open Start Menu -> Microsoft Codename Roslyn CTP -> Getting Started.

To get started, the “Roslyn Project Overview” document gives a look at the compiler API – how to work with syntax and semantics of your program. Several walkthrough documents are also included to provide a deep dive into various aspects of the Roslyn APIs.

The CTP ships with quite a few samples for Visual Studio Extensions, compiler API, code issues, refactorings and so on. Most of the samples are provided for both C# and Visual Basic. You can open the sample source code from the Getting Started page.

We also install several new project templates available in the New Project dialog:

ProjectTemplatesVB

ProjectTemplatesCSharp

These templates will help you to get started on a new Visual Studio extension that uses Roslyn.

Reference Assemblies

References

The Roslyn assemblies are also installed in the GAC. Switch to the Full Profile (instead of the Client Profile) to be able to also reference the Services assemblies (which contain the IDE support).

C# Interactive window

InteractiveWindow

You can invoke the C# Interactive window from View -> Other Windows -> C# Interactive Window. The Interactive window is powered by the new C# language service. The architecture of Roslyn is flexible enough to allow many of the IDE features such as IntelliSense and refactorings to work the same in a normal editor and in the Interactive window.

At this time, the Interactive window is only available for C#. We’re working hard on providing the VB Interactive at a future time.

C# Script File (.csx) Editing Support

The CTP introduces a concept of a C# Script File. You can create a .csx file through File -> New File (or also use any other editor such as notepad):

NewCSharpScriptFile

CSXScriptEditor

You can run scripts using the new rcsi.exe, which installs into %ProgramFiles(x86)%\Microsoft Codename Roslyn CTP\Binaries\rcsi.exe. You can add rcsi.exe to the path and then type rcsi <scriptfilename>.csx.

You can also copy chunks of code from a script file and send them to the C# Interactive Window (using the right-click context menu or a keyboard shortcut).

The editor for the script files is also powered by the new language services. Hence it is important to keep in mind that .csx scripts will only support the part of the language already implemented in the Roslyn compilers. For more details, see the “Introduction to Scripting” walkthrough.

Quick sample of the Roslyn API

Here’s a sample of compiling and executing a small program using the Roslyn API.

using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;

...

var text = @"class Calc { public static object Eval() { return 42; } }";
 
var tree = SyntaxTree.ParseCompilationUnit(text);
var compilation = Compilation.Create(
    "calc.dll",
    options: new CompilationOptions(assemblyKind: AssemblyKind.DynamicallyLinkedLibrary),
    syntaxTrees: new[] { tree },
    references: new[] { new AssemblyFileReference(typeof(object).Assembly.Location) });
 
Assembly compiledAssembly;
using (var stream = new MemoryStream())
{
    EmitResult compileResult = compilation.Emit(stream);
    compiledAssembly = Assembly.Load(stream.GetBuffer());
}
 
Type calc = compiledAssembly.GetType("Calc");
MethodInfo eval = calc.GetMethod("Eval");
string answer = eval.Invoke(null, null).ToString();
 
Assert.AreEqual("42", answer);

Note: At this stage, only a subset of the language features has been implemented in the current CTP. We’re moving forward at a fast pace, but features such as Linq query expressions, attributes, events, dynamic, async are not yet implemented. To see a full list of non-implemented language features, see the Roslyn forums.

Although not all the language features are supported, the shape of the public API is mostly complete, so we encourage you to write extensions and tools against the Syntax, Symbols, and Flow and Region Analysis APIs.

We’re very excited to get an early preview of this technology in your hands and we welcome your feedback, ideas and suggestions. Use the forums to ask questions and provide feedback, Microsoft Connect to log bugs and suggestions, and use the #RoslynCTP hashtag on Twitter.

 

Thanks,

Kirill Osenkov

QA (Roslyn Services Team)

Blog: http://blogs.msdn.com/b/kirillosenkov

Twitter: @KirillOsenkov

0 comments

Discussion is closed.

Feedback usabilla icon