开发者

Meta programming into several output languages

I'm looking for a way to have to write and maintain a certain algorithm (a graphics rendering sub-module of my code, act开发者_StackOverflowually) only once. I need the algorithm in C++, PHP and Javascript. Theoretically I could write it in C++ and wrap it into a PHP extension; but that has many issues of itself and doesn't solve the Javascript link.

What I'm looking for, I think, is a tool that converts from a language (doesn't matter which one) into the three (or two, if the source language is one of the three) output languages I'm targetting. I've found MetaL (http://www.meta-language.net/) which seems to do what I want but also looks dead (no updates since 2007) and only targets one of the three languages I need. It needs to be quite flexible and allow me to update the results - for example, I use Cairo in my C++ and PHP rendering, and HTML Canvas on the Javascript side. So I need to customize to the API for certain effects.

Alternatively, I'd settle for a PHP parser and lexer that would give me an AST with enough information for me to write generators for C++ and Javascript as an alternative backend.

Any ideas? Thanks.


You could take a look at Haxe. Haxe is an open source programming language. It can be compiled to JavaScript, Flash/ActionScript, PHP, C++, Java, C#, Python and Lua.


The Emscripten project (which I only spotted last week) might interest you: http://syntensity.blogspot.com/2011/04/emscripten-10.html

This guy has basically written a compiler for C/C++ that compiles to Javascript code.

That should solve the Javascript side of your problem.

Hope that helps.

Another product along the same lines, and a bit more well known is Google Web Toolkit (GWT). It's based on Java, but the end result is similar -- you write your web application in Java code and it compiles the front-end parts into Javascript and the back-end parts into regular Java bytecode. I know you're not asking for Java, but if it interest you, the link is here: http://code.google.com/webtoolkit/

Slightly less useful, but possibly more relevant to your question is PHPJS. This is a project to implement as much of the PHP language in Javascript as possible. They're doing it on a function-by-function basis, so it's only ever going to be an approximation, but given that the language syntaxes are similar, it may be possible to use it to write code that works unchanged in native PHP and also in Javascript on the client side.

Of course the one big down-side of compiling one language into another is that the resulting code is always going to be sub-optimal. There's not much you can do about that, but it's worth bearing in mind before you start down the path of writing a shared code-base in a single language.


Maybe look into 'coding' your original algorithm in xml and using various xslt templates to output to your target languages ? Or possibly antlr (http://www.antlr.org/ http://www.amazon.com/Definitive-Antlr-Reference-Domain-Specific-Programmers/dp/0978739256/ref=sr_1_1?s=books&ie=UTF8&qid=1303114884&sr=1-1).


Maybe you can just Write it in javascript and then use a C++ and PHP javascript interpreter.


A completely different approach would be to use assembly code. Write the algorithm in a language of your choice, compile it to ASM source. Then provide the interface wrappers in the deployment languages.

Of course this is all so much 'air pie'. It depends upon so many variables, number of target platforms, importance of optimization, frequency of interface change related to implementation change etc etc

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜