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?
Disadvantages, first three topics.
– Guilherme Lautert