Create a compiler using ASP.Net

I am going to explain to create your own C# compiler using ASP.Net.
 
What is a compiler
 
A compiler is a program which used to processes statements (programming statement) written in specific programming language and convert this program into machine language.
 
Step 1 Add namespaces which used to provide classes and api to work with compilation and execution process 
  1. using System.CodeDom.Compiler;  
  2. using System.Collections.Specialized;  
  3. using System.Diagnostics;  
Step 2 Create a user interface which provide interface to users to writing code, checking errors, and buttons to compilations and executions.  
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head runat="server">  
  3.     <title></title>  
  4.     <style type="text/css">  
  5.         .style2  
  6.         {  
  7.             width: 178px;  
  8.         }  
  9.     </style>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.     <center>  
  14.     <div>  
  15.       
  16.     <table>  
  17.         <tr>  
  18.             <td>  
  19.                 <h3>Code Area</h3>  
  20.                 <asp:TextBox ID="txtCode" runat="server" Height="347px" TextMode="MultiLine"   
  21.                     Width="780px" BorderStyle="Solid" BorderWidth="1px">  
  22.                     using System;   
  23.                     class Test   
  24.                     {   
  25.                         public static void Main()   
  26.                         {   
  27.                             Console.WriteLine("Hello World");   
  28.                         }   
  29.                      }</asp:TextBox>  
  30.             </td>  
  31.             <td align="center" class="style2" valign="bottom">  
  32.               
  33.                 <asp:Button ID="btnCompiler" runat="server" Height="35px" Text="Compile"   
  34.                     Width="150px" onclick="btnCompiler_Click" />  
  35.                     <br />  
  36.                     <br />  
  37.                 <asp:Button ID="btnExecute" runat="server" Height="35px" Text="Execute"   
  38.                     Width="150px" onclick="btnExecute_Click" />  
  39.                     <br />  
  40.                     <br />  
  41.                 <asp:Button ID="btnReset" runat="server" Height="35px" Text="Reset"   
  42.                     Width="150px" onclick="btnReset_Click" />  
  43.                     <br />  
  44.                     <br />  
  45.                 <asp:Button ID="btnClose" runat="server" Height="35px" Text="Close"   
  46.                     Width="150px" onclick="btnClose_Click" />  
  47.               
  48.             </td>  
  49.         </tr>  
  50.         <tr>  
  51.             <td colspan="2"><h3>Result Area</h3>  
  52.                 <p>  
  53.                     <asp:ListBox ID="lstCompilerOutput" runat="server" Height="184px" Width="959px">  
  54.                     </asp:ListBox>  
  55.                 </p>   
  56.             </td>  
  57.         </tr>  
  58.     </table>  
  59.     </div>  
  60.     </center>  
  61.     </form>  
  62. </body>  
  63. </html>  
Step 3 Switch your program to code file and create a method which used to define compilation language, return exception message if any and after successful compilation generate assembly as .exe. 
  1. public CompilerResults CompileCode(string strCode)  
  2. {  
  3.     CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("CSharp");  
  4.     CompilerParameters parameters = new CompilerParameters();  
  5.     parameters.GenerateExecutable = true;  
  6.     string path = Server.MapPath("Output/testFile.exe");  
  7.     parameters.OutputAssembly = path;  
  8.     parameters.GenerateInMemory = true;  
  9.     return codeDomProvider.CompileAssemblyFromSource(parameters, strCode);  
  10. }  
Note: Output is name of folder which used to store assembly. testFile.exe is name of assembly.
 
Step 4 Generate click event of btnCompiler button to call CompileCode method and display error message on user interface."
  1. lstCompilerOutput.Items.Clear();  
  2. CompilerResults cResult = CompileCode(txtCode.Text);  
  3. foreach (var item in cResult.Errors)  
  4. {  
  5.        lstCompilerOutput.Items.Add(item.ToString());  
  6. }  
  7. if (cResult.Errors.Count == 0)  
  8. {  
  9.        lstCompilerOutput.Items.Add("No Error");  
  10. }  
Step 5 No Error. mean your code is compile successfully without any error. For testing open your Output folder and check testFile.exe file is there.
 
Step 6 To Execute. Generate click event of btnExecute button to execute your created assembly as testFile.exe. 
  1. string strCmdLine = Server.MapPath("Output/testFile.exe");  
  2. Process proc = new Process();  
  3. proc.StartInfo.CreateNoWindow = true;  
  4. proc.StartInfo.UseShellExecute = true;  
  5. proc.StartInfo.FileName = strCmdLine;  
  6. proc.Start();  
Step 7 Enjoy. Your compiler is ready to work.
Ebook Download
View all
Learn
View all