Is MQL compiled or interpreted?

 

I've always taken the "knowledge" that MQL is a "compiled" language at face value. Someone in another forum is claiming that MQL is in fact encrypted and then interpreted. He makes a good argument for his case because you can take the following code and discard its source and the "compiled" program will still raise an array out of range error and point to the exact line number and character indentation (from the left hand margin in the editor) where the error is occurring. How is this possible if the file is in fact "compiled"? 


Steps to reproduce:

Compile the following and discard the source file. 

//|                                                      nicholishen |
//|                         https://www.forexfactory.com/nicholishen |
//+------------------------------------------------------------------+
#property copyright "nicholishen"
#property link      "https://www.forexfactory.com/nicholishen"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+

/*
Is this really compiled?
*/
void OnStart()
{
            int array[1];
            
            /*              */array[1] = 1;
}
//+------------------------------------------------------------------+

Once the source is deleted, run the "compiled" ex4. You will get the following:

array out of range in 'IsCompiled.mq4' (20,36)

How is this possible?

 
nicholi shen:

I've always taken the "knowledge" that MQL is a "compiled" language at face value. Someone in another forum is claiming that MQL is in fact encrypted and then interpreted. He makes a good argument for his case because you can take the following code and discard its source and the "compiled" program will still raise an array out of range error and point to the exact line number and character indentation (from the left hand margin in the editor) where the error is occurring. How is this possible if the file is in fact "compiled"? 


Steps to reproduce:

Compile the following and discard the source file. 

Once the source is deleted, run the "compiled" ex4. You will get the following:

How is this possible?

Compiler has the source in its internals as tables of tokens and verbs together with line and column numbers. It is in fact a must for compilation.
I am not near computer but guess you can verify that with another experiment: compile the same file but instead of discarding the whole file, just delete a line before the array out of range(delete a remarked line). Then see that compiler points to the incorrect current line (but the correct original line).
 

I don't believe that MQL is a compiled language. There must be an interpreter (MT) for running MQL code.

See a definition of interpreted language:

https://en.wikipedia.org/wiki/Interpreted_language


Compiled languages produce machine code and it can be executed by CPU (ex4 or ex5 can't be executed directly by CPU).

See a definition of compiled language:

https://en.wikipedia.org/wiki/Compiled_language

 
Petr Nosek:

I don't believe that MQL is a compiled language. There must be an interpreter (MT) for running MQL code.

See a definition of interpreted language:

https://en.wikipedia.org/wiki/Interpreted_language


Compiled languages produce machine code and it can be executed by CPU (ex4 or ex5 can't be executed directly by CPU).

See a definition of compiled language:

https://en.wikipedia.org/wiki/Compiled_language

No, No.
An interpreter takes one statement at a time and executes it. It does not scan all source beforehand, and it thus encounter an error only at runtime.

Mql4/5 are clearly compiled.
 
Amir Yacoby:
No, No.
An interpreter takes one statement at a time and executes it. It does not scan all source beforehand, and it thus encounter an error only at runtime.

Mql4/5 are clearly compiled.
I don't want to argue with you but I believe you're wrong. When MT "compiles" your code it just encrypts it and does (semantic) error checking. MT doesn't produce machine code. If you don't believe me then try to run "compiled" MQL code outside MT. I admit that the definition of compiled and interpreted languages is a little bit vague but I'm convinced that MQL is clearly interpreted language ;-)
 
Petr Nosek:
I don't want to argue with you but I believe you're wrong. When MT "compiles" your code it just encrypts it and does (semantic) error checking. MT doesn't produce machine code. If you don't believe me then try to run "compiled" MQL code outside MT. I admit that the definition of compiled and interpreted languages is a little bit vague but I'm convinced that MQL is clearly interpreted language ;-)
Same here.
In the old times the difference was very clear. Compilers were passing on source code (mostly more then one pass) and translate it to another labguage (once was machine, today it does not have to be machine, example is vb6 and more). Interpreter would take the source and start executing it immediately, one statement at a time, interpreting and executing. This is more prone to runtime errors, and its another reason that mt would not use this for trading, it does not suit.

You seem to think that machine code is the output of compiling, but today that is many times not the case. The output is another language, ready to be executed. 
 
* Vb6 and c# are compiled to intermidiate languahes. C# to cil and then executed by the jit compiler. The running of that cil can be seen as interpreter, because it takes an order, translates to machine and executes.
 
Amir Yacoby:
Same here.
In the old times the difference was very clear. Compilers were passing on source code (mostly more then one pass) and translate it to another labguage (once was machine, today it does not have to be machine, example is vb6 and more). Interpreter would take the source and start executing it immediately, one statement at a time, interpreting and executing. This is more prone to runtime errors, and its another reason that mt would not use this for trading, it does not suit.

You seem to think that machine code is the output of compiling, but today that is many times not the case. The output is another language, ready to be executed. 
I know that machine code needn't be the output of compiling. But the output must be native code, byte code, C code or some other code that can usually be compiled into machine code. And machine code can be run directly in CPU. Where is such a output from MQL? MQL need for its running MT (its interpreter). You can keep your opinion and I will keep my own. Have a nice day.
 
Petr Nosek:
I know that machine code needn't be the output of compiling. But the output must be native code, byte code, C code or some other code that can usually be compiled into machine code. And machine code can be run directly in CPU. Where is such a output from MQL? MQL need for its running MT (its interpreter). You can keep your opinion and I will keep my own. Have a nice day.
The .mql5 file is compiled into an mq5 intermidiate code. 
This is the code you are looking for. And MT is the interpreter of this code (and not the source code) into machine code. It does not compile it, because its already checked. It just interpret and executes.

* actually, mq5 is an interpreted language, not mql5 which is a compiled one.

Have a nice day
 
Ernst Van Der Merwe:

Trying to access an element that doesn't exist will always give 'array out of range'.

But it wasn't the matter.

 

This is not a question of beleif. This is a fact. MQL5 is definitely a compiled language, producing native 32 and 64 bit code. You can find some posts about compilation on the russian forum, for example this one.

The main proof of the fact is that the speed of compiled mql5 program is very close to a compiled c++ program. This can't be for an interpreter.

Yes, the final output is encrypted, but this does not mean that compilation is somehow impossible.

As for the original question, I'm not sure I get the point. If you change the source code and do not recompile it, you'll get old error messages from the running old executable.

 
Stanislav Korotky:

This is not a question of beleif. This is a fact. MQL5 is definitely a compiled language, producing native 32 and 64 bit code. You can find some posts about compilation on the russian forum, for example this one.

The main proof of the fact is that the speed of compiled mql5 program is very close to a compiled c++ program. This can't be for an interpreter.

Yes, the final output is encrypted, but this does not mean that compilation is somehow impossible.

As for the original question, I'm not sure I get the point. If you change the source code and do not recompile it, you'll get old error messages from the running old executable.

Interpreters are just not suited for trading. It involves interpreting each statement while running it, very poor for trading. The only thing I was not sure of is if it produce native or intermidiate code. Although I guess any one who designs such a platform would go for native compiled code, the most reasonable choice. 
Reason: