What is the advantage of using languages that compile for other languages?

11

We were discussing in Stackoverflow's chat about languages like Moonscript and CoffeeScript.

They are two languages that compile for other languages. Moonscript for Lua and CoffeScript for Javascript.

I noticed in relation to both that the syntax of the two changes much in relation to the language that is intended to compile.

Moonscript example:

class Thing
  name: "unknown"

class Person extends Thing
  say_name: => print "Hello, I am #{@name}!"

with Person!
  .name = "MoonScript"
  \say_name!

That is compiled for Moon as follows:

local Thing
do
  local _base_0 = {
    name = "unknown"
  }
  _base_0.__index = _base_0
  local _class_0 = setmetatable({
    __init = function() end,
    __base = _base_0,
    __name = "Thing"
  }, {
    __index = _base_0,
    __call = function(cls, ...)
      local _self_0 = setmetatable({}, _base_0)
      cls.__init(_self_0, ...)
      return _self_0
    end
  })
  _base_0.__class = _class_0
  Thing = _class_0
end
local Person
do
  local _parent_0 = Thing
  local _base_0 = {
    say_name = function(self)
      return print("Hello, I am " .. tostring(self.name) .. "!")
    end
  }
  _base_0.__index = _base_0
  setmetatable(_base_0, _parent_0.__base)
  local _class_0 = setmetatable({
    __init = function(self, ...)
      return _parent_0.__init(self, ...)
    end,
    __base = _base_0,
    __name = "Person",
    __parent = _parent_0
  }, {
    __index = function(cls, name)
      local val = rawget(_base_0, name)
      if val == nil then
        return _parent_0[name]
      else
        return val
      end
    end,
    __call = function(cls, ...)
      local _self_0 = setmetatable({}, _base_0)
      cls.__init(_self_0, ...)
      return _self_0
    end
  })
  _base_0.__class = _class_0
  if _parent_0.__inherited then
    _parent_0.__inherited(_parent_0, _class_0)
  end
  Person = _class_0
end
do
  local _with_0 = Person()
  _with_0.name = "MoonScript"
  _with_0:say_name()
end

What are the advantages and disadvantages of using a language that compiles into another language?

    
asked by anonymous 09.12.2015 / 12:21

2 answers

8

Disadvantages

Generalizing thus, none: P Only has disadvantages.

Of course, if you look specifically, there may be advantages.

Compiling for another language in itself is not such a big problem. Or even a problem. After all a good deal of the languages are compiled into machine language.

The problem appears when this target language (this is the name given to the language that will have the code generated after the initial compilation) needs a compilation, and mainly interpretation.

The problem is that you create an extra step, it requires extra software. And the result is the code of another language that has its own structure. It is quite complicated to create tools to debug the code in the source language (to which the code was originally written). You have to do a mapping and intercept the execution to follow the execution of the final code as if it were the original code.

In addition, interoperability can usually be limited.

Is the implementation of the source language compiler good? If it is not, you may have problems. Will they have adequate and sufficient ancillary tools?

If the target language changes incompatible, the source language may be in a difficult situation.

Obviously, the source language may be somewhat limited to the capability and template that the target language works on.

Target languages

Taking the machine language, the most commonly used target languages for this are C / C ++ that are universal on computers (platforms), have an efficiency model, have very good compilers, and are very powerful and flexible. They are used to avoid creating a compiler that generates a lower level code with aggressive optimizations.

Assembly languages (not to be confused with machine code) of a processor or a virtual machine or intermediate code compilers are also used. But it's for a reason a little different from the focus of the question.

Another target language is JavaScript precisely because it is not only universal in browsers, but also the only option on this platform. Any language that wants to run in a browser will have to use it as a target. At least until WebAssembly is created and popularized.

It is possible for the compiler of a language to compile it for itself. This occurs when the language uses syntactic sugar . Of course this occurs internally in the compiler, nor is it noticed. And language knows how to handle it.

There are languages that compile for several others to take advantage of multiple platforms. Of course there is a common minimum denominator there. Haxe is an example.

Advantages

JS is known to be a limited language for building large code bases and with a syntax that could be a bit simpler. So several languages were created to give a better experience for programmers.

In general these languages on top of others are created because the target language does not facilitate some coding style or paradigm. As target languages are turing complete , they can simulate any paradigm.

There are cases of exaggeration in using this type of solution. Think of the MoonScript case that probably makes it easy to create a Lua code that works in a game. Imagine the difficulty of debugging this code in a game.

The advantage must be too great to compensate. I would avoid using this in script languages. But people use script languages for the wrong reasons, so I do not know ...

It has trans-compilation information at Wikipedia

    
09.12.2015 / 12:47
5

The biggest advantages are code reuse, interoperability, and the ability to use language in an environment that would not normally support you.

The design of a programming language, from its syntax and semantics to the final form how it will be implemented (whether compiled, interpreted or something in the way) is quite complex, but there is a desire on the part of many people to design languages for different reasons. Only in this decade 16 new languages have been released (includes larger versions of some already existing, type C ++ 11 and C ++ 14, but more than half are new), and there are at least 25 language families >.

This project has a high cost (even if it is time), and this would be even higher if each language implements the software stack from parsing to machine code generation, optimizations, profiling, etc. Reusing the capabilities of a second language significantly reduces the input barrier and time to market, so languages can be implemented, tested, improved, or even discarded (if the intended benefits with the language do not materialize), in a much more agile way. That is, ideas can be tested, and if they gain traction, the extra effort to give proper implementation becomes more justified.

As for interoperability, we have the [many] cases where there is a legacy system that can not be replaced without significant additional cost, but it is desirable that new modules be developed using modern programming capabilities. Implementing a language over an existing platform (eg Jython in JVM or IronPython in .Net) usually solves this problem more satisfactorily than establishing "bridges" between different languages (especially because many have processing / memory, which hinders interoperability).

When the platform below is not as "abstract" (eg JavaScript in browsers ), generating code in the target language and letting that code be compiled can offer a much higher performance than simply write an interpreter in that language, for example.

Finally, there is the question of using language in an environment not designed to receive new languages. The case of the browser is the most obvious (and worth as much for JS as for HTML and CSS), but there are others. A particularly interesting one is the case of GLSL, which is compiled for the GPU - writing a compiler for a different language, which generates code compatible with the diversity of GPUs on the market, would be an insane task ... What if a system (including games) has its own "scripting language", and it is seen as unsatisfactory, generating code for it may be the only way to use a distinct language in that environment.

How many disadvantages, bigown has already explained very well (both in answer to this question and to a related question ), I have nothing meaningful to add.

    
10.12.2015 / 21:41