Possible Duplicate:
Programming languages with a Lisp-like syntax extension mechanism
I’m making a programming language, and, having spent some time in Lisp/Scheme, I feel that my language should be malleable. Should I use macros, or is there something else I might/should use? Is malleable syntax even a good idea? Is it perhaps too powerful a concept?
In doing some research, I found fexprs
. I don’t really understand what these are. Help with that in an answer too please.
Is it possible to have a language with macros/something-of-a-similar-nature without having s-expressions?
3
You might want to take a look at Ruby, which has a great deal of support for metaprogramming. The book Eloquent Ruby covers the language well, with a significant amount of attention to metaprogramming in the later chapters. There is also a book called Metaprogramming Ruby that is well-reviewed, but which I have not personally read yet.
Metaprogramming in Ruby is far more flexible (and powerful . . . and dangerous) than what is provided by .NET’s Reflection libraries (since Guy Coder mentioned C# in his answer, I thought this comparison might be useful). In Ruby, you can use hooks to dynamically select which mixin modules you want to add to a class at runtime, use “method_missing” for flexible error handling and delegation, use monkey patching to choose what code to add in dynamically, and get yourself into all kinds of awesomely amazing trouble writing self-modifying code. This makes Ruby a very powerful language for, e.g., writing your own domain-specific language (DSL).
Note: I used a lot of jargon in the second paragraph. My hope is that this will provide people who are interested in more information with search terms for web searches, in case someone wants to know more but isn’t ready to buy a book on the topic yet.
2
As I don’t know exactly what you mean by malleable, for the purpose of this question I will take it to mean a language that can be extended.
While I do understand the concept of LISP macros, and how it extends the language, the language that I most associate with this ability is PROLOG.
PROLOG has the ability to define new clauses and add them to its knowledge base by using the assert command. Clauses can also be removed by the retract command.
Stepping further away from the PROLOG where the concept is built into the language and into languages where the dynamic runtime code can be modified with Meta programming or reflection, then we find languages such as Java and C#.
In my view if you are designing a new language and what to give it malleability, then start with the way PROLOG does it naturally in the language and build from there. Languages that use other means to achieve malleability in my view are treating it like an afterthought and it shows.
As an example, take a look at Mercury. This is functional/logic language that is malleable and yet is more than just PROLOG.
2