31/10/2016

Roslyn - How to create a custom debuggable scripting language 2?

Home


A screenshot comes from Visual Studio 2015

In the previous post I explained how to create a simple debuggable scripting language based on Roslyn compiler as a service. By debuggable I mean that it can be debugged in Visual Studio as any "normal" program for example written in C#.

Unfortunately, the solution described by me works only with one script file. In other words it doesn't allow you to split your script among multiple files. Now I'll show the solution of this problem. It is really easy. Here is a modified version of a code from the previous post:
var sb = new StringBuilder();

foreach (var path in args)
{
   if (!File.Exists(path))
      return;

   var lines = File.ReadAllLines(path);

   sb.AppendLine($"#line 1 \"{path}\"");
   foreach (var l in lines)
   {
      if (l.StartsWith("write"))
      {
         var res = l.Substring(l.IndexOf(" ", StringComparison.Ordinal)).Trim();
          sb.AppendLine($"System.Console.WriteLine(\"{res}\");");
      }
      else
         sb.AppendLine(l);
   }
}
This code simply iterates through the list of script files. Each file is processed in exactly the same way. What is important for each file #line directive is added. Thanks to that proper symbols will be generated by Roslyn and Visual Studio will now where to find source files for a code being debugged. Easy, isn't it?

0 comments:

Post a Comment