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

Asked

Viewed 1,691 times

14

We were discussing in Stackoverflow’s chat about languages like Moonscript and Coffeescript.

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

I realized in relation to the two that the syntax of the two changes a lot in relation to the language that one tries to compile.

Example of Moonscript:

class Thing
  name: "unknown"

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

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

Which is compiled for Lua 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?

2 answers

10


Disadvantages

Generalizing like this, none :P Only has disadvantages.

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

Compiling for another language itself is not such a big problem, or even a problem, after all most 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 it creates an extra step, 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 (the one that was originally written). You have to map and intercept the execution to follow the execution of the final code as if it were the original code.

In addition, interoperability can normally be limited.

Is the source language compiler implementation good? If not, you may have problems. They will have adequate and sufficient helper tools?

If the target language changes in an incompatible way the source language may get in complicated situation.

Obviously the source language may be somewhat limited to the capacity and model that the target language works.

Target languages

Apart from the machine language, the most commonly used target languages for this are C/C++ which 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 lower-level code with aggressive optimizations.

Linguagens Assembly (not to be confused with machine code) of a processor or a virtual machine or intermediate code of compilers are also used, but it is for a reason a little different from the focus of the question.

Other Javascript is the language used as a target precisely because it is not only universal in browsers, but also because it was (no longer) the only option on this platform. Any language you want to run in a browser will have to use it as a target. Now you have the Webassembly.

It is possible that the compiler of a language Compile it to itself. This occurs when the language uses syntactic sugar. Of course this happens internally in the compiler, you can’t even tell. And the language knows how to deal with this.

There are languages that compile for several others to take advantage of multiple platforms. Of course there’s a common denominator there and it’s usually bad. Hashish is an example.

Perks

JS is known for being a limited language to create large code bases and with a syntax that could be a little simpler. Then several languages were created to give a better experience to programmers.

In general these languages on top of others are created because the target language does not facilitate some style or coding paradigm. How target languages are Turing Complete they can simulate any paradigm.

There are cases of exaggeration in using this type of solution. Think of the case of Moonscript which probably facilitates creating a Lua code that works in a game. Imagine the difficulty of debugging this code in a game.

The advantage needs to be very large to compensate. I would avoid using this in languages of script, but people use the languages of script for the wrong reasons, so I don’t know...

Information about trans-compilation on Wikipedia

  • I like Typescript to do SPA. A SPA can become a very complex code for JS. But for scripts small, why use very organized language? Language without ceremony is what is suitable in these cases. JS is great for this. PHP tb. Or was :)

  • Boss, what is spa?

  • Single Page Applications.

6

The biggest advantages are the reuse of code, interoperability and the possibility of using a language in an environment that normally would not support it.

The design of a programming language, from its syntax and semantics to the final way it will be implemented (whether compiled, interpreted or something in between) is quite complex, but there is a desire on the part of many people to design new languages, for various reasons. In this decade alone 16 new languages have been released (includes larger versions of existing ones, 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 time), and this would be even higher if each language implemented the whole stack software since the Parsing up to machine code generation, optimizations, Profiling, etc. Reusing the resources of a second language significantly reduces the entry barrier and the "time to market", so that languages can be implemented, tested, enhanced, or even discarded (if the intended benefits of 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 a proper implementation is more justified.

As for interoperability, we have the [many] cases where there is a legacy system that cannot be replaced without a significant additional cost, but it is desirable that new modules be developed using modern programming resources. Implement a language on top of an existing platform (e.g., Jython on JVM or Ironpython on . NET) usually solves this problem more satisfactorily than to establish "bridges" between different languages (especially because many have different processing/memory models, which makes interoperability difficult).

When the platform underneath is not so "abstract" (e.g.: Javascript in browsers), generate code in the target language and let that code be compiled can offer a much higher performance than simply writing an interpreter in that language, for example.

Finally, there is the question of using the language in an environment not designed to receive new languages. The case of browser It’s the most obvious (and it goes for JS as well as 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 manages code compatible with the diversity of Gpus existing on the market, would be an insane task... And 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 the disadvantages, Maniero already explained very well (both in answer to that question how much is a related question), I have nothing significant to add.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.