5

Okay, so I'm not good with all the terms and stuff, BUT here's what I have created thus far:

1: An app that reads character by character the contents of a source code file. And;

2: An app that determines whether character/string 'x' is an identifier, or integer, or whatever.

and 3: Another app that puts it all together, and creates another file that basically contains instructions to be sent to my 4th app, basically along the lines of:

Assign the value of Integer oldValue to Integer newValue.

So, then my 4th app receives this instruction, and "Compiles" it (well, in this case I'd say that I just want to interpret it), which will then create an application that does:

int newValue = oldValue;

So, since I have the parser etc already done, how can I translate my instructions into instructions that lead to actual actions, and generate an EXE file, so when I double-click the file, it will perform the above-mentioned action?

I hope this itsn't confusing for you.

So, basically, I guess what I'm asking is: "How can I programmatically create an "Event" from a string, save it to file and generate an exe?"

  • 1
    http://msdn.microsoft.com/en-us/library/system.reflection.emit.ilgenerator.aspx and http://msdn.microsoft.com/en-us/library/system.reflection.emit.assemblybuilder.save.aspx – Jaroslav Jandek Mar 06 '11 at 08:30
  • Aw, Intermediate Language Generator? I can see by the url itself that it sounds like what I want. Checking out now. Thank you very much @Jaroslav Jandek :-) –  Mar 06 '11 at 08:38
  • 1
    take a look here http://compilers.iecc.com/crenshaw/tutor16.txt don't say it too old – Shekhar_Pro Mar 06 '11 at 08:39

4 Answers4

2

There are several options to generate an EXE from a list of instructions. All options involve that you transform your instructions to elements of a different language.

dtb
  • 213,145
  • 36
  • 401
  • 431
1

This is a real working sample for a simple executable creation using what I have provided in the comment:

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace AssemblyGenerator
{
    partial class AssemblyGenerator
    {
        static void Main(string[] args)
        {
            string assemblyName = "Sample";
            string exeName = "Sample.exe";

            AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.Save);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(exeName);
            TypeBuilder typeBuilder = moduleBuilder.DefineType(assemblyName, TypeAttributes.Public | TypeAttributes.Class);
            MethodBuilder methodBuilder = typeBuilder.DefineMethod("Main", MethodAttributes.Static | MethodAttributes.Public, typeof(void), null);

            assemblyBuilder.SetEntryPoint(methodBuilder, PEFileKinds.ConsoleApplication);

            ILGenerator ilGenerator = methodBuilder.GetILGenerator();
            // BEGIN method IL.
            ilGenerator.EmitWriteLine("This shows on console output.");
            ilGenerator.Emit(OpCodes.Ret);
            // END method IL.

            typeBuilder.CreateType();
            assemblyBuilder.Save(exeName);
        }
    }
}

You can emit your code inside the method block as indicated by the comments.

Also, you can use an expression using the LambdaExpression.CompileToMethod Method instead of the ILGenerator.

Jaroslav Jandek
  • 9,463
  • 1
  • 28
  • 30
0

When it comes to the compilation phase your best be is to use the Reflection Emit APIs to emit the .NET IL and generate EXEs and DLLs.

Rather than get into everything over here I would point you to the following MSDN article Create a Language Compiler for the .NET Framework, which includes sample code that should help you on your way.

Another option includes generating C# code and using the CodeDOM to compile the code to an EXE, this would leverage the C# compiler as an intermediate step between your code generation and the final binary generation.

Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
0

The easiest way is probably to have your program generate source code for some language (e.g. C) and output that to a file which you would compile separately. That saves you the trouble of having to do code generation which can be rather involve.

Depending on the complexity of your task, it may be easier to use a tool such as Yacc (and Lex). These allow you to specify how to identify the parts of input you care about and a grammar for the language, and what is to be output as constructs are identified. You won't have to specify a grammar for all of C#, just the portions you care about for your task.

templets
  • 161
  • 6
  • Thank you very much @templets. I would love to use Yacc or Lex, to avoid all the complexity, but for some reason they seem so complex to figure out how to use :-S Which is why I just try to do it all on my own, but if you could point me in the right direction to learn one of those then i would be very grateful. –  Mar 06 '11 at 08:50