2

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
  • Can you provide an objective definition of malleable? Commented Sep 28, 2012 at 21:38
  • By malleable I mean, like on the wikipedia page for macros, "Macros transform the program structure itself" Commented Sep 28, 2012 at 21:41
  • 3
    See programmers.stackexchange.com/questions/164665/… Commented Sep 28, 2012 at 21:47

2 Answers 2

5

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
  • I was actually planning on learning Ruby, coincidentally. Is metaprogramming in ruby as powerful as in lisp? Commented Sep 28, 2012 at 23:42
  • 3
    @mhr: Ruby doesn't even remotely come close to Lisp. It doesn't have macros. It doesn't have a meta-object protocol. It doesn't have changeable syntax. It doesn't have literal overloading. It doesn't allow user-defined operators. It doesn't even allow overloading all the existing operators. Commented Sep 29, 2012 at 8:34
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
  • I don't really see Prolog as meta-programming; it's more like an inference engine with a knowledge-base attached. It may seem like meta-programming, but it falls squarely into the "data, not code" camp. Commented May 28, 2013 at 14:49
  • You can meta-interpret and alter the semantics of Prolog terms inside the language, seems like meta-programming to me. Some examples: metalevel.at/acomip Commented Oct 1, 2023 at 16:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.