views:

816

answers:

7

Before answering this question, understand that I am not asking how to create my own programming language, I am asking how, using vb.net code, I can create a compiler for a language like vb.net itself. Essentially, the user inputs code, they get a .exe. By NO MEANS do I want to write my own language, as it seems other compiler related questions on here have asked. I also do not want to use the vb.net compiler itself, nor do I wish to duplicate the IDE.

The exact purpose of what I wish to do is rather hard to explain, but all I need is a nudge in the right direction for writing a compiler (from scratch if possible) which can simply take input and create a .exe. I have opened .exe files as plain text before (my own programs) to see if I could derive some meaning from what I assumed would be human readable text, yet I was obviously sorely disappointed to see the random ascii, though it is understandable why this is all I found.

I know that a .exe file is simply lines of code, being parsed by the computer it is on, but my question here really boils down to this: What code makes up a .exe? How could I go about making one in a plain text editor if I wanted to? (No, I do not want to do that, but if I understand the process my goals will be much easier to achieve.) What makes an executable file an executable file? Where does the logic of the code fit in?

This is intended to be a programming question as opposed to a computer question, which is why I did not post it on SuperUser. I know plenty of information about the System.IO namespace, so I know how to create a file and write to it, I simply do not know what exactly I would be placing inside this file to get it to work as an executable file.

I am sorry if this question is "confusing", "dumb", or "obvious", but I have not been able to find any information regarding the actual contents of an executable file anywhere.

One of my google searches

Something that looked promising

EDIT: The second link here, while it looked good, was an utter fail. I am not going to waste hours of my time hitting keys and recording the results. "Use the "Alt" and the 3-digit combinations to create symbols that do not appear on the keyboard but that you need in the program." (step 4) How the heck do I know what symbols I need???

Thank you very much for your help, and my apologies if this question is a nooby or "bad" one.

To sum this up simply: I want to create a program in vb.net that can compile code in a particular language to a single executable file. What methods exist that can allow me to do this, and if there are none, how can I go about writing my own from scratch?

+2  A: 
Process.Start(String.Format("vbc.exe {0}", sourceFilePath))
Justin Niessner
"how can I go about writing my own from scratch?" I cannot assume the user has the vb ide installed on their computer.
Cyclone
You don't need the IDE to get the compiler...only the SDK which is free to download.
Justin Niessner
@Cyclone - In all seriousness, re-writing a VB.NET compiler in VB.NET is a massive undertaking and not one that I (or, I'm guessing, anybody else here would want to undertake). Using the freely distributable .NET SDK would be MUCH easier.
Justin Niessner
K, can I have a link? Also, this is for vb.net correct? With the string "sourceFilePath", what should be the contents of the source file? This is vb.net, not vb6, so a vbscript file is not a part of this at all, and a vbscript file is the only file I could think of that would be compilable into it as a single file.
Cyclone
Justin Niessner
k, now what goes into the file at sourceFilePath?
Cyclone
The path to the .vb files you want to compile into the .exe ...but if you're going this route, don't even worry about writing the app. Just call the compiler directly...
Justin Niessner
There is the issue though, I do not want it to compile a normal project though....thanks anyway.
Cyclone
+6  A: 

What you're asking is a pretty complex question. Sure, at its core it seems pretty basic:

  1. Interpret the code itself
  2. Write out the interpreted code

but each of those steps can be pretty intense. Step 1 should be somewhat achievable with some time and a LOT of elbow grease - you need to parse the code into a number of control statements based upon the specification of the language. Check out http://en.wikipedia.org/wiki/Parsing for more information on this step. In essence you're converting the typed code into a common format that represents the functionality you want.

Once you've parsed the code, the next step would be to take that parsed code and convert it into machine-runnable code (typically assembly, though with VB.NET you can write Microsoft Intermediate Language code as the output and then run it in the CLR). This is what will actually create the executable file in a manner that lets the computer run the program.

Unfortunately, the best advice for solving this problem is to either:

  1. Go purchase several books on a programming language, machine code, assembly language, compilers, and so on, then spend several months or years reading and experimenting until the knowledge you gain from the books results in your writing a successful compiler.
  2. Enroll in a computer science program at a local university. Writing compilers and programming languages are usually covered at a rudimentary level during the second or third year of a BS in computer science, and then in much more depth at a graduate level.

Good luck!

EDIT: If all you're looking for is a way to write code and then write a way to execute it, you might try looking at writing an interpreter for one of the scripting languages that already exist - Ruby, Python, Lua, etc.

MBillock
Look at my edit about the second link, that is why I said it looked promising, yet in reality it made no sense. I don't intend on specifically getting a degree just so I can make a compiler, or purchase any books on the subject. This seems really more complex than it should be lol...
Cyclone
Saw the edit after I posted - honestly that link is a travesty :)True, at its core all you're doing is taking a certain pattern of text and converting it into a certain pattern of 1s and 0s - if you define it that way, then yeah it's definitely easy. Of course, if we follow that path:- Basketball is really just putting the ball in the hoop- Building a house is really just combining wood and nails- Speaking a foreign language is really just memorizationSome people spend years learning to write compilers - it's deceptively complex :)
MBillock
I have become convinced at this point that it would nearly be simpler for me to write my own language entirely, and simply write all logic myself as a parser. Of course, this is also ridiculous, as I have no need for an entirely new language (though I did once attempt to write a Lolcode parser in vb.net, it is the only language that uses "OH NOES" as an exception handler.
Cyclone
@Cyclone: It would not be simpler for you to write your own language, since the complexity is in compiling anything. Writing any sort of compiler is going to be a massive learning experience for you and a tremendous amount of work, and getting a computer science degree may well be the easiest way to do it.
David Thornley
@Cyclone: "This seems really more complex than it should be..." How simple do you think it *should* it be to translate human-understandable text into a binary something that can be mashed by a series of electronic logic gates to produce some result that's still human-sensible?
DaveE
I really dislike this answer. It looks like its trying to make it seem really difficult. You dont explain what an interpreter is; your link to parsing discusses the theory of it, instead of linking to a parser generator; and the end just says "you aren't good enough, get a degree, or at least a book". Really, a travesty of an answer. -1.
Paul Biggar
+1  A: 

You create a compiler for vb.net the same way you create any other compiler. You need a lexer/parser. Entire books have been written on this topic, the most famous probably being The Dragon Book.

To provide a definitive answer: No, you cannot create a decent compiler that will generate an executable file using Notepad. You need a compiler to convert from human-readable text into the machine language (assembly or IL) that a linker or interpreter can then execute.

Ken White
I think I am about to throw in the towel here...
Cyclone
Did you follow the link I provided? That should be enough to get you to scrap the idea of writing it in Notepad...
Ken White
I never intended to write an executable in notepad?
Cyclone
Ah... I guess this part of your original post confused me: "How could I go about making one in a plain text editor if I wanted to?"
Ken White
I was asking more about the actual structure of the .exe file itself than going out and MAKING one in notepad.
Cyclone
It's binary machine code that contains instructions for the CPU to execute, regardless of what language it's written in. (OK, not quite true - if you're writing in VB script, for example, it's text that the VB script runtime compiles into something it interprets and turns into machine code. However, you asked about .EXE files.)
Ken White
+2  A: 

Here is one book that explains it all at a very basic level. Including sample code that you can get working in a matter of hours.

As with many programming endevors, it will take you many months of study to accomplish what you want. Good luck!!

TonyOssa
Ill add that to my shopping cart and see if I can find any cheaper solutions till then. Thanks!
Cyclone
+2  A: 
Paul Biggar
A: 

Cyclone, I'm wondering exactly what are you trying to achieve? You say "the exact purpose of what I wish to do is rather hard to explain" and "essentially, the user inputs code, they get a .exe".

If you just want the user to be able to enter code and then execute it, you might consider an existing scripting language. VBScript is built into Windows and the language is fairly similar to VB.Net, or there are various excellent free languages you can download like Python.

If the user really needs to be able to create a .exe - I think it's likely scripting might do - then why not use an existing free compiler like FreeBasic, or even Visual Basic.Net Express Edition.

MarkJ
+1  A: 

The Visual Basic .NET compiler is shipped for free as part of the .NET Framework - you don't even need the SDK or Express Editions. The compiler for VB (and C#) is located at c:\windows\microsoft.net\framework[version]\vbc.exe (or csc.exe for C#). Therefore, any computer which can run a VB.NET program can compile one. The .NET Framework also includes the System.CodeDom namespace which provides a way from within a program to compile a program, either from a document model or from a string (or file) into a .NET assembly (.exe or .dll) and generate code in both VB and C#.

Regards,

Anthony D. Green | Program Manager | Visual Basic Compiler

Anthony D. Green
Yes, I have found the necessary code in the namespace you mentioned.
Cyclone