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
- using System.CodeDom.Compiler;
- using System.Collections.Specialized;
- 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.
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <style type="text/css">
- .style2
- {
- width: 178px;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <center>
- <div>
-
- <table>
- <tr>
- <td>
- <h3>Code Area</h3>
- <asp:TextBox ID="txtCode" runat="server" Height="347px" TextMode="MultiLine"
- Width="780px" BorderStyle="Solid" BorderWidth="1px">
- using System;
- class Test
- {
- public static void Main()
- {
- Console.WriteLine("Hello World");
- }
- }</asp:TextBox>
- </td>
- <td align="center" class="style2" valign="bottom">
-
- <asp:Button ID="btnCompiler" runat="server" Height="35px" Text="Compile"
- Width="150px" onclick="btnCompiler_Click" />
- <br />
- <br />
- <asp:Button ID="btnExecute" runat="server" Height="35px" Text="Execute"
- Width="150px" onclick="btnExecute_Click" />
- <br />
- <br />
- <asp:Button ID="btnReset" runat="server" Height="35px" Text="Reset"
- Width="150px" onclick="btnReset_Click" />
- <br />
- <br />
- <asp:Button ID="btnClose" runat="server" Height="35px" Text="Close"
- Width="150px" onclick="btnClose_Click" />
-
- </td>
- </tr>
- <tr>
- <td colspan="2"><h3>Result Area</h3>
- <p>
- <asp:ListBox ID="lstCompilerOutput" runat="server" Height="184px" Width="959px">
- </asp:ListBox>
- </p>
- </td>
- </tr>
- </table>
- </div>
- </center>
- </form>
- </body>
- </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.
- public CompilerResults CompileCode(string strCode)
- {
- CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("CSharp");
- CompilerParameters parameters = new CompilerParameters();
- parameters.GenerateExecutable = true;
- string path = Server.MapPath("Output/testFile.exe");
- parameters.OutputAssembly = path;
- parameters.GenerateInMemory = true;
- return codeDomProvider.CompileAssemblyFromSource(parameters, strCode);
- }
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."
- lstCompilerOutput.Items.Clear();
- CompilerResults cResult = CompileCode(txtCode.Text);
- foreach (var item in cResult.Errors)
- {
- lstCompilerOutput.Items.Add(item.ToString());
- }
- if (cResult.Errors.Count == 0)
- {
- lstCompilerOutput.Items.Add("No Error");
- }
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.
- string strCmdLine = Server.MapPath("Output/testFile.exe");
- Process proc = new Process();
- proc.StartInfo.CreateNoWindow = true;
- proc.StartInfo.UseShellExecute = true;
- proc.StartInfo.FileName = strCmdLine;
- proc.Start();
Step 7 Enjoy. Your compiler is ready to work.