-3

I want to design and develop my own programming language and a compiler for it. I am still an undergraduate in Mathematics who knows C/C++. I did little web research and read some e-books, but most of them using specific tools to convert object/intermediate code to machine code, most of them explaining how to use assembler to convert new programming language into machine code. There are lot of tutorial/references out there that "How to convert assembly to machine operation codes" but they skip some levels for newcomers. I want to dive little deep in this subject. - Assume that I designed my own programming language(assume it's called = "MW") with proper syntaxes and operators.


Question

How to convert MW language code into machine code directly in order to generate executable for O/S ?

If you can give very definitive and explanatory answer it's really appreciated. Since I am not a Computer Science student I need some guidance and step by step descriptions how and why? If someone know great book or website for this stuff please reply.

  • Here my purpose it to build a system like "Wolfram Mathematica".Please do not get in to argument around programming languages types such as - Symbolic,Procedural,Numerical or etc.

Thank you.

  • Do you know C or do you know C++? Note that the two languages are not one and the same. You should pick one for your implementation. – fuz Jan 25 '17 at 04:38
  • 1
    Way too broad for SO, but give "Dragon Book" a google. – user4581301 Jan 25 '17 at 04:41
  • @fuz I know C++,my Intention is to build compiler in C++? what is your suggestion which one is flexible and better C or C++? –  Jan 25 '17 at 05:21
  • @user4581301 Thank you for the information. –  Jan 25 '17 at 05:22
  • About your main question and short answer: Machine code is just sequence of byte values, so you simply do produce the stream of instruction encodings and data values, and wrap it into some executable/object file headers, so other tools or OS will recognize it. How to produce the correct target machine instructions to recreate functionality specified in your high level language is a complete sub-science in IT and may well do for 1000+ pages answer. Also you may want to consider translating not into machine code, but C++, so your language will be portable then plus benefit of C++ optimizations. – Ped7g Jan 25 '17 at 06:19
  • @BuddhikaGamage Either can be used. If you are familiar with C++ and its conventions then use C++. – fuz Jan 25 '17 at 14:36

2 Answers2

3

Your question is hopelessly vague.

SUGGESTION:

Focus on one aspect of the problem. Study how somebody else implemented a "programming language". For example, download and experiment with this small, simple BASIC interpreter, written in C++:

Or download and study any (or several) of the other examples here:

https://sites.google.com/site/smallbasicinterpreters/source-code

Good luck!

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • If your intention is to "implement a language", please do NOT restrict yourself to "compiler". Which involves (at least) two extra levels of effort: generating reasonably assembly code, and requiring a "linker". I'd strongly urge you to play with an existing implementation - like one of the BASIC interpreters above. One other resource: [Let's Build a Compiler](http://compilers.iecc.com/crenshaw/) – paulsm4 Jan 26 '17 at 04:44
1

I can only provide some limited guidance on this topic since I am unfamiliar with how Mathematica works internally to resolve or compute answers from symbolic expressions. I believe I have encountered some books on the topic before (computer algebra systems) but I forget the titles. In general this is a topic in itself a bit removed from compiling languages to machine code in general since I believe the results are interpreted by said system.

That being said however for general computer languages you would be well served by a book on compiler theory- such as

compilers principles techniques and tools by Aho/Sethi/Ullman/Lam.

or

engineering a compiler by Cooper/Torczon

or something similar. These books tend to go over the basic principles of writing a parsing front-end and converting text into an intermediate language which then can be converted into machine code using various standard techniques.

Hope that helps.

C. Cheng
  • 80
  • 9
  • Thank you for the information about *engineering a compiler by Cooper/Torczon* it's great. Sorry I cannot up vote your answer since I have not got that much reputation points. –  Jan 25 '17 at 05:25