The Meta Language



  • So, I work for a company that does a lot of work with ISBNs (and various related barcodes), in several languages.  There are a handful of functions we need, over and over again:
    Convert ISBN10 to ISBN 13
    Extract pricing information and ISBN13 from an EAN
    Generate ISBN10 & EAN check digits.

    I've written this set of functions out once in PHP, but we're finding we also need it in TCL for a palmtop application we have, and in Javascript for some parts of our webapp.  Also, over time, we find we need to tweak the algorithms themselves, which would then require porting said tweaks to the other two versions as well, which seems like a bit of a hassle.

    So it struck me -- the algorithms themselves are the same, no matter what language you implement them in.  The only things that differ are some minor syntax issues, and of course the libraries (PHP's substr() is JS's string.substr(), etc.)  So, can't we just write them in some meta-language, and 'compile' it to PHP, JS and TCL?

    Have there been any efforts to write something like this?  I can forsee a lot of issues (you'd have to have a set of translations from your 'meta-libraries' to the real libraries, which could be sizable and difficult to maintain), but it seems the need for something like this has grown, if anything, over the years, what with webapps usually involving code in both the server-side language (PHP, ASP, java, etc) and Javascript on the client end.  Additionally, having a universal library of basic algorithms would seem to be of great benefit to programming languages as a whole -- any new language could instantly have a robust set of libraries, once you wrote a few basic libraries, and the translation table.

    Have there been any attempts at this sort of thing in the past?



  • I think that one of the main ways that this problem is currently solved is to centralize the algorithm into a service, such as a web service.  So, you write the algorithm in PHP, and the Javascript will call the PHP through a http interface. Although, I don't know if thats viable for the TCL side of things.

    I suppose you could write a meta-language, but its as likely to become as complicated as a full language, with restrictions to keep the lowest denominator between the supported target languages.  What you're really asking for is a compiler from language a into language (b,c,d) where b,c,d happen to be PHP,TCL,Javascript in your case.
    The other alternative is to write a program that generates the algorithm programatically.  Either way it seems like a lot of work now to save you a little work later.

    I would suggest keeping it simple and seperate.



  • Unfortunately I don't know of any such human readable meta language.

    CIL, the .Net bytecode clone, would be an interesting candidate to solve this problem. All the languages you mention can be compiled to CIL, but writing decompilers that produced working and maintainable scripts would be a bitch.

    I think an easier solution would be to choose a strict typed language to write your functions being careful not to use any 'special' language features such as classes, then write regex for each destination language you need. Rewriting them by hand may prove a lot quicker and easier though....



  • I think MetaL was introduced in the SideBar a while ago.



  • I wrote a paper on the inverse of that (compiling multiple languages to a single language (lisp)):



    "Yaccscript: A Platform for Intersecting
    High-Level Languages
    "



    the paper describes a multi-language implementation of a single
    algorithm.  It's not exactly what you described, but the end goals
    are similar.



  • <font face="Arial">Do you have any C compiler installed?

    If yes, then you have cpp.exe (probably the most undervalued meta-language preprocessor in the world) and it doesn't care what the output language is.

    P.S cpp.exe is normally only executed internally so it doesn't tell you what parameters it expects (in fact, because it is often used in a stream it may not even halt without parameters).
    Syntax is nearly always:
    cpp.exe [inputfile] [outputfile]
    </font>



  • Xarium, there are operating systems - quite a lot of them - where a C compiler does not bring a cpp.exe with it. In fact, .exe means nothing on that operating systems.



  • @ammoQ said:

    Xarium, there are operating systems - quite a lot of them - where a C compiler does not bring a cpp.exe with it. In fact, .exe means nothing on that operating systems.


    Both of you are being naive.  First, ammoQ is right, cpp.exe is certainly not universal with C, however, many C compilers do come with a seperate program that will run the C preprocessor.  GNU GCC will, for instance. 
    There are, however, other templating languages availible.  You could use freemarker for all I care. yacc/bison might be a good choice too. It all depends on your needs.



  • @danielpitts said:

    @ammoQ said:
    Xarium, there are operating systems - quite a lot of them - where a C compiler does not bring a cpp.exe with it. In fact, .exe means nothing on that operating systems.


    Both of you are being naive.  First, ammoQ is right, cpp.exe is certainly not universal with C, however, many C compilers do come with a seperate program that will run the C preprocessor.  GNU GCC will, for instance. 


    Wow. Really? One would think with more than 10 years of professional (as in "for money") C programming experience, I should have noticed that.

    Now seriously, I just wanted to point out that not everyone is using Windows, so the name of the C preprocessor is not necessarily cpp.exe .





  • The best slution here  i think would be a webservice.  You can call a webservice from just about any language so that would be the way to go, the algorithm would only be in the one place and it's a lot easier than writing it in several different languages.  Personally i'd go for c# or maybe java for the webservice but i think you can write a web service fairly easily in most languages



  • Yacc/bison has already been mentioned, here's another alternative: http://www.antlr.org/

    I've seen at least one open-source meta-language recently that wasn't supposed to run standalone, but to be compiled into TCL/TK, javascript and whatnot. Unfortunately I don't remember what it was called, but they do exist. But none of them support all the target languages you want, to the best of my knowledge.



  • And there is a number of language-specific converters, like 'java-to-javascript' etc. Here's a list .

    On domain-specific languages:

    http://www.martinfowler.com/articles/languageWorkbench.html 


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.