0

I am interested in writing a Pascal compiler in JavaScript. But I am confused about what makes a compiler - a compiler and not an interpreter. Specifically do I need to generate byte code or assembly code like C/C++, e.g. a.out?

So if I were to scan and parse hello_world.pas:

program helloworld;
begin
    Writeln('Hello world!');
end.

And generate something like this:

(function() {
  console.log('Hello world!');
})();

Did I just write an interpreter or a compiler?

Any hints or suggestions for writing a compiler in JavaScript would be much appreciated.

Sahat Yalkabov
  • 32,654
  • 43
  • 110
  • 175
  • 2
    I think you just wrote a translator... – Floris Jan 13 '14 at 22:35
  • 1
    What difference does it make? Not trying to be rude, just trying to understand the underlying reason why the term used is important to you here. – Oak Jan 13 '14 at 22:38
  • http://stackoverflow.com/a/3618119, http://stackoverflow.com/a/2377288 – Ken White Jan 13 '14 at 22:39
  • You certainly did not write an interpreter. There is some controversy over calling this sort of program compiler, because its output is not immediately runnable on any physical CPU, but personally I'm in the camp of calling everything which translates from one language to another (not just from a HLL to machine code) is a compiler. –  Jan 13 '14 at 22:41
  • For hints on writing compilers in javascript, take a look at Tachyon: http://pointersgonewild.wordpress.com/2011/08/27/the-tachyon-javascript-compiler/ – SK-logic Jan 14 '14 at 00:30

1 Answers1

6

Whether a program is a compiler or an interpreter is determined by its input and output, not by the architecture of the program itself.

A compiler reads in source code written in a high level language and translates it to something else. Most often, compilers generate an executable program in a low level language, usually machine code. From there, all you need is the compiled executable to run the program; you only need the source code and compiler if you want to modify the program.

An interpreter also reads high level source code, but instead of generating an executable it executes the program as it parses. To run a program like this you'll always need a copy of the source code and the interpreter.

Compilers and interpreters are usually compiled, and in the case of compilers, the compiler is often used to compile itself (this is called bootstrapping). But that's just common practice. There's no practical reason why you couldn't write a compiler in an interpreted language.

As to what type of low level language your compiler should output, that depends on what architecture you want your executable to run on. More than likely you'll want to generate assembly language for your platform and then use a preexisting assembler to convert that to machine code.

The code you wrote simply translated the Pascal code into Javascript code. You could think of that as a type of compilation: you're compiling Pascal into Javascript so it can be run by a Javascript interpreter. If you're trying to write something that allows the user to run Pascal code in a web browser, for example, then you've got the right idea.

user2752467
  • 864
  • 5
  • 16
  • 1
    So what are you saying, did OP write a compiler or an interpreter? Neither of your definitions seem to apply to it. –  Jan 13 '14 at 22:42
  • I added another paragraph to address that. – user2752467 Jan 13 '14 at 22:43
  • 1
    It's worth noting that many "interpreters" will take a source file and convert it to some sort of intermediate form before interpreting it. One could perhaps regard the conversion to intermediate form as "compilation" if the intermediate form could be persisted and was not expected to be a complete representation of the source file [many 1980's BASIC interpreters would preprocess each line as it was entered and discard the original; a `LIST` command would reconstruct the original form of each line as it was displayed. The preprocessed version wasn't text, but *was* nonetheless "source code".] – supercat Jan 29 '14 at 17:17