4

Suppose I have designed a new programming language for one of the managed code environments (.NET/JVM). Can I now implement it by simply writing a translator that translates the source code of this new language into the main language of the platform (C#/Java) and then letting the platform's compilers and other tools handle the rest of the process ? Are there any simple, proof of concept , examples of this approach ?

explorer
  • 11,710
  • 5
  • 32
  • 39

5 Answers5

6

Yes, you can do that so long as the semantics map properly (care must be taken, for instance, in mapping JavaScript code to a language such as C# because the scoping rules are different).

It is not on a managed platform, but you could look at Vala. It is a C#-like language that compiles to C. Eiffel also compiles to C (and supports compiling to Java).

If you are on a managed platform, however, you may want to look in to emitting bytecode directly. Java bytecode is not difficult to emit, as the VM takes care of and provides instructions for the trickier pieces of compiling (such as managing stack frames) and the VM eliminates other hairy corners such as register allocation.

Michael Ekstrand
  • 28,379
  • 9
  • 61
  • 93
1

Yes, you can certainly do that. The main issue you're going to run into is that it's difficult to provide source level run-time diagnostics/debugging for your language.

  • 1
    It's not as hard as it seems at first. Just have a "debug" mode compile that stores the original line number as the new code is executing. Then you just need to write a simple debugger that catches a top level exception and outputs the last line marker passed. Then you can get fancy and allow stepping through the markers or tracing them to a log file. I think that's relatively easy compared to the main task of developing a new language. – JOTN Nov 10 '10 at 01:16
1

Sure, the first C++ compiler I used translated the code to C and then used the system compiler and assembler to create the executable. I believe it was from Sun, but it's been a while. Really the C to assembly is doing the same thing.

JOTN
  • 6,120
  • 2
  • 26
  • 31
0

I'm not sure if this is a good example or not: http://www.mozilla.org/rhino/jsc.html

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
0

I suggest 2 steps:

First, make a translator or compiler from your language to C# or Java. Second, make a translator to .NET code (CIL or MSIL), or Java bytecode.

(another compiler & programming language design hobbyst)

umlcat
  • 4,091
  • 3
  • 19
  • 29