Non-C++ languages for generative programming?

By : jwfearn
Source: Stackoverflow.com
Question!

C++ is probably the most popular language for static metaprogramming and Java doesn't support it.

Are there any other languages besides C++ that support generative programming (programs that create programs)?

By : jwfearn


Answers

Nim is a relatively new programming language that has extensive support for static meta-programming and produces efficient (C++ like) compiled code.

http://nim-lang.org/

It supports compile-time function evaluation, lisp-like AST code transformations through macros, compile-time reflection, generic types that can be parametrized with arbitrary values, and term rewriting that can be used to create user-defined high-level type-aware peephole optimizations. It's even possible to execute external programs during the compilation process that can influence the code generation. As an example, consider talking to a locally running database server in order to verify that the ORM definition in your code (supplied through some DSL) matches the schema of the database.

By : zah


let me list a few important details about how metaprogramming works in lisp (or scheme, or slate, or pick your favorite "dynamic" language):

  • when doing metaprogramming in lisp you don't have to deal with two languages. the meta level code is written in the same language as the object level code it generates. metaprogramming is not limited to two levels, and it's easier on the brain, too.
  • in lisp you have the compiler available at runtime. in fact the compile-time/run-time distinction feels very artificial there and is very much subject to where you place your point of view. in lisp with a mere function call you can compile functions to machine instructions that you can use from then on as first class objects; i.e. they can be unnamed functions that you can keep in a local variable, or a global hashtable, etc...
  • macros in lisp are very simple: a bunch of functions stuffed in a hashtable and given to the compiler. for each form the compiler is about to compile, it consults that hashtable. if it finds a function then calls it at compile-time with the original form, and in place of the original form it compiles the form this function returns. (modulo some non-important details) so lisp macros are basically plugins for the compiler.
  • writing a lisp function in lisp that evaluates lisp code is about two pages of code (this is usually called eval). in such a function you have all the power to introduce whatever new rules you want on the meta level. (making it run fast is going to take some effort though... about the same as bootstrapping a new language... :)

random examples of what you can implement as a user library using lisp metaprogramming (these are actual examples of common lisp libraries):

  • extend the language with delimited continuations (hu.dwim.delico)
  • implement a js-to-lisp-rpc macro that you can use in javascript (which is generated from lisp). it expands into a mixture of js/lisp code that automatically posts (in the http request) all the referenced local variables, decodes them on the server side, runs the lisp code body on the server, and returns back the return value to the javascript side.
  • add prolog like backtracking to the language that very seamlessly integrates with "normal" lisp code (see screamer)
  • an XML templating extension to common lisp (includes an example of reader macros that are plugins for the lisp parser)
  • a ton of small DSL's, like loop or iterate for easy looping


'metaprogramming' is really a bad name for this specific feature, at least when you're discussing more than one language, since this feature is only needed for a narrow slice of languages that are:

  • static
  • compiled to machine language
  • heavily optimised for performance at compile time
  • extensible with user-defined data types (OOP in C++'s case)
  • hugely popular

take out any one of these, and 'static metaprogramming', just doesn't make sense. therefore, i would be surprised if any remotely mainstream language had something like that, as understood on C++.

of course, dynamic languages, and several functional languages support totally different concepts that could also be called metaprogramming.

By : Javier


This video can help you solving your question :)
By: admin