Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meta-programming: write in one language X, cross-compile in multiple languages like C#, PHP, Java, C

Tags:

In all projects I've done through the years I never came across a requirement like this, though it seems so easy on paper: write a plugin for many well-known CMS's.

Obviously, each plugin-system (or extension system) is different and this requires specific bridging code through an adapter pattern. But the core should be written once. I don't expect WordPress users to use a PHP-Java bridge, and I don't expect DotNetNuke users to use a .NET-Native bridge (though that's easier conceived).

The way I see it, the core should be compilable in three main domains that cover most CMS systems:

  • native, intermediate language could be C, or C++. Target can be used as PHP extension.
  • MSIL/CIL for .NET based languages
  • Java byte code for Java based systems

C# and Java translate pretty well to and from each other, but C and C# is much harder. Ultimately, it would be nice to possibly add other targets, so as not to force a WordPress or WikiMedia user to install an extension prior to using a plugin.

I'm sure this has come up with other too. What's a common way of tackling such problems? Should I define a DSL first and use DMS or similar to transform? Other options?

like image 727
Abel Avatar asked Mar 16 '11 18:03

Abel


People also ask

Is C++ a meta programming language?

Static typed languages also have powerful metaprogramming techniques, for example the C++ template metaprogramming...

What programming language does Meta use?

Meta's primary supported server-side languages are Hack, C++, Rust, and Python. For performance-sensitive back-end services, we encourage C++ and Rust. Rust is a new addition to this list.

Can you combine 2 programming languages?

It is possible to combine different languages in one project, you just write two different programmes and let them communicate with each other. And in some cases it even makes sense. Say your project is written in Java or Python for the most part but you have a part that requires a little more computing power.

What is cross language in programming?

Cross language support is the ability provided by the common language runtime (CLR) and the common language specification (CLS), of the . NET Framework, for interaction with code written in a different programming language.


2 Answers

Haxe is a sort of meta-language that compiles for different platforms:

  • JavaScript
  • C++
  • C#
  • HashLink
  • Java
  • Lua
  • PHP
  • Python 3
  • NekoVM
  • Flash
  • ActionScript 3
like image 133
Jakub Hampl Avatar answered Oct 16 '22 03:10

Jakub Hampl


Since there's no one language that compiles directly to all of your targets, don't overlook the lowest common denominator. You could build your plug-in using C and then wrap the result for each platform (PInvoke, JNI, PHP extension). If you don't relish the idea of writing a complex plug-in in C, consider doing the heavy lifting using a small, easy-to-use, and embeddable scripting language. Lua seems reasonable. Your final result would be a C skeleton that simply delegates requests and responses to and from the host and scripts.

Program Transformation is related, though the effort to build an effective transformation is significant. In theory, you could transform from the source to source and use the platform-specific compiler. Take a look at TXL and Stratego / XT to get an idea of what's involved.

like image 24
Corbin March Avatar answered Oct 16 '22 02:10

Corbin March