7

Long-term C++ programmer (that's me) exploring other languages and is wondering whether it is worth learning much about D 2.0. (That is the clean slate re-write of D.) So far, I like what I see - many pragmatic, wise choices. Now, I want to learn a little more and want to know whether it is likely ever to be possible to do the following:

  1. Write some code in D 2.0. (Maybe it is a well-defined subset of D 2.0.)
  2. Compile the code from 1.
  3. Receive Javascript source code from the compiler.

In summary, I would like to have a D 2.0 compiler with a backend capable of generating Javascript.

So my question has two parts:

  1. Does such a compiler exist already?
  2. If such a compiler does not exist, and if I chose to implement one, where would be the best place to start?

For context, the reason that I would like the above is so that I could implement a bunch of business logic in D 2.0, and then use that business logic in a pure Javascript (that is no Flash/Silverlight/Java) Web Application.

Update

I have had three answers, two of which are on-topic, and one that is not and is therefore ignored.

@FeepingCreature reckons that it will never happen because "it's too incongruous with D's targeted environment". I am interested to know what that means, and have asked.

@Nekuromento , in contrast, points out that it is already theoretically possible using LDC, followed by the LLVM Javascript backend. I checked out both links. LDC support for D 2.0 looks like a work in progress. But I would guess it will get there at some point. The LLVM Javascript is also not optimised or minified, but it looks from the demos that they work. So I think that is enough to be useful at the very least.

Update 2

OK - this seems to be the best solution to date.

  1. Invoke LDC. (@Nekuromento) LDC combines the Digital Mars front-end with an LLVM backend.
  2. Pipe the output from 1. through the LLVM Javscript backend. (@Nekuromento)
  3. Pipe the output from 2. though Closure (@BCS)

I will choose a best answer fairly soon, but I would like some more-informed opinion really.

Update 3

Some more information on why I want to do this.

Here is a concrete example of one project that I wish to implement. It will be a geometric modelling library, probably written by just me, taking several years to write, and ending up at about 100,000 lines of code. I would like it to contain as close to zero bugs as possible, and, therefore, I do not wish to implement it in Javascript. I would like it to run on the server, on the desktop, and in the browser too. (By in "in the browser" I mean without the assistance of any plugin such as Flash, Silverlight or Java.) The truth is that code in the browser running on Javascript is here to stay for a while. Projects will increasingly need a strategy to deal with that.

Update 4

I was hoping for some more answers, but didn't get them. Ah well, never mind. Thanks for all the feedback, including @dsimicha 's stimulating comments. (I hope it is OK to disagree technically sometimes, while at the same time keeping things civil.) I have accepted @Nekuromento 's answer.

Update 5 2018-07-18

Seven years on, LDC v1.11 supports compiling and linking to WebAssembly.3

Paul Delhanty
  • 751
  • 8
  • 11
  • Out of curiosity, why do you want to do this? It seems like a huge [abstraction inversion](http://en.wikipedia.org/wiki/Abstraction_inversion), since the source language is lower level than the target language. – dsimcha Mar 27 '11 at 15:15
  • 1
    D 2.0 is a lower level language than Javascript? That's a pretty strong assertion to make. From the small amount I have read so far, D 2.0 has many high level features that Javascript lacks - a static type system (yes the absence of a static type system is not a feature), deep immutables, message-based concurrency, unit tests in the language, ... – Paul Delhanty Mar 28 '11 at 07:36
  • One thing I did not make clear - I want the code to be able to run in a WebApp on the client, and elsewhere - on the server for example. – Paul Delhanty Mar 28 '11 at 07:38
  • 2
    @Paul: D is definitely lower level than JavaScript, though this is not a criticism of D. D supports pointers and manual memory management. Even if you don't like duck typing, it's still higher level than nominative static typing with primitive mapping closely to machine types. I'll grant that the concurrency issue is an exception, though. Generally implementing low-level stuff on top of high-level stuff is a bad idea (for example implementing static typing on top of duck typing). Compatibility with standard web browsers is a good reason for doing this, though. – dsimcha Mar 28 '11 at 15:44
  • @dsmicha - you are conflating having a static type system with being a low-level language. Right now I am evaluating Haskell and D2.0, both of which have static type systems and support for deep immutability. In fact, although D 2.0 is billed as a systems language, it looks much closer to a multi-paradigm language to me.) Haskell has an unbounded precision Integer type and an Int type with at least 29 bits of precision. Does that make Haskell a low-level language? – Paul Delhanty Mar 29 '11 at 04:24
  • @dsmicha - and what of Javascript and integers? Javascript, being a high-level language must surely have unbounded precision Integers right? Unfortunately http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-to Javascript integers seem to be usually 64-bit floating bit values, except sometimes when they revert to being signed 32 bit integers. Sorry, but that does not seem very high-level to me. – Paul Delhanty Mar 29 '11 at 04:27
  • @dsmicha - here is one personal project I have in mind - a geometric modeller. It will take me several years, and end up at about 100,000 lines of code. Javascript as an implementation language? Well that would not be my 1st choice. Of course I want the code to be portable - on the desktop, on the server and in the browser. Increasingly, the reality is that if projects do not have some strategy to run in the browser on top of Javascript they will not get implemented at all. – Paul Delhanty Mar 29 '11 at 04:38

3 Answers3

4

Such a compiler currently does not exist, but there is some work on javascript LLVM-backend.

As there is LDC. It uses DMD front-end and LLVM, so theoretically one can add javascript output to it.

Nekuromento
  • 2,147
  • 2
  • 16
  • 17
  • +1: Thanks for the reply. Ah, that's interesting. Your answer somewhat contradicts @FeepingCreature . From what you write, what I want may already be possible. I visited the links that you kindly provided. There are some cavats. (1) LDC D 2.0 support is a work in progress. (2) javascript LLVM is no optimised or even minified. But so what! That might not matter too much. – Paul Delhanty Mar 27 '11 at 12:27
  • I had no idea that Javascript LLVM was a thing. – FeepingCreature Mar 27 '11 at 14:39
  • 2
    You should be able to run the output of that stage through something like Google's JS compiler: http://code.google.com/closure/compiler/ – BCS Mar 28 '11 at 14:49
  • @BCS +1 - I had heard of Closure and was wondering whether there was any value in doing just what you suggest. Thanks for the opinion that it might be worth my while. – Paul Delhanty Mar 29 '11 at 04:09
2

Keep your eye on the D newsgroups over the next few days, I'm working on something you might be interested in (or rather, exactly what you want). It can currently compile:

int foo()
{
    if (true)
    {
        return 3;
    }
}

I'm hoping to get an alpha/beta of some sort out within the next week.

Robert
  • 436
  • 2
  • 3
  • See also: http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D.announce&artnum=20860 – Robert Jun 29 '11 at 21:17
1

Short answer: No.

Long answer: Really. No.

Less Snarky Answer: it's too incongruous with D's targeted environment.

If you chose to implement one - hell, more power to you, but you have your work cut out for ya. Probably best to start with reading the DMD frontend, which is FOSS (and, afaik, distributed in the standard DMD zip).

FeepingCreature
  • 3,648
  • 2
  • 26
  • 25
  • +1: Thanks for the reply. Don't worry about being snarky - all informed opinion is welcome. I was wondering whether you could expand a little on the phrase "too incongruous with D's targeted environment"? Remember, a well-defined subset would of D 2.0 would still be useful to me. (Anybody who has used C++ for real know that you learn parts of the language to avoid.) In fact, if my compiler could only compile the programs that I wrote it still might be valuable to me. – Paul Delhanty Mar 27 '11 at 11:13
  • Well it's a systems programming language. Static typing, compilation to objects and binary, but I suppose my point is more that the work involved in writing a D-to-Javascript compiler is not significantly less than writing a CustomLanguage-to-Javascript compiler, so there's no reason to start with D in the first place. – FeepingCreature Mar 27 '11 at 14:38
  • 1
    OK - I thought you had a deeper point than the fact that D 2.0 is statically typed. – Paul Delhanty Mar 28 '11 at 07:39