Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I like languages that give me the ability to extend the syntax of the language. At the same time, all the languages that I know will let me extend the syntax have relatively heavy type systems. Are there lighter-weight languages that allow the same sort of syntax extensibility? Or does flexible syntax require that you would be able to define new data types (and operations on those types)?

Basically, I want the syntactic flexibility of Haskell without Haskell's type system. Is there a way to get this, or does that syntactic flexibility require the assistance of a type system?

Examples of languages where I know the type system can be used to create new syntax are Haskell, C++, and Python.

Possible examples of where syntax could be modified without a strong type system maybe Scheme, Forth, and Common Lisp.

However, while I can see how to overload operators in the first list to generate new syntactic constructs, I don't know enough to know how to do it in the second list. Maybe the answer is for me to go read up on macros in Common Lisp.

What I would like is a language with the conceptual simplicity of C, Lua or Erlang, but with the ability to create additional syntactic operators as desired.

TLDR - basic summary of question:

Is it possible for weakly typed language to support syntax extension, or does the ability to extend the syntax of a language (from within the language itself) require a type system to implement?

share|improve this question

migrated from stackoverflow.com Apr 9 '11 at 7:11

This question came from our site for professional and enthusiast programmers.

    
@rsenna I guess otherwise. –  Bedwyr Humphreys Apr 8 '11 at 16:03
    
@Bedwyr Humphreys: care to explain us why? Also take the time to read the faq. The very first question, "What kind of questions can I ask here?", will be enlightening to you. –  rsenna Apr 8 '11 at 16:11
    
I think this one comes under "matters that are unique to the programming profession" –  Bedwyr Humphreys Apr 8 '11 at 16:23

4 Answers 4

You can really play around with the syntax of Common Lisp. Not only are there regular macros, but read macros as well.

However, it isn't weakly typed; it's actually pretty strongly typed. Perhaps you were looking for dynamic typing rather than weak typing.

share|improve this answer
    
That's what I was afraid of actually, I suspected that Common Lisp might be more strongly typed than I was thinking (MOP was tickling at the edge of my mind). What I'm really interested in here is whether a language that has no concept of objects can be amenable to syntax extension from within the language itself. –  sea6ear Apr 8 '11 at 21:20
    
Thinking further, the only way I can see for a weakly typed language to allow this, is if the language contained parsing constructs as part of the language itself. I believe Forth does have parsing constructs, not sure about Scheme. –  sea6ear Apr 8 '11 at 21:26
    
@sea6ear: It's been a long time since I did anything in Forth, but it was possible to make constructs with compile-time and run-time behavior, much like Lisp macros. Forth had a simple parser and simple syntax. It is possible to create any binary operator you want, similar to the + word, but + doesn't work like + in any other language I'm familiar with. I don't know how far you can practically distort the syntax. –  David Thornley Apr 8 '11 at 21:29

Perl has pluggable keywords since version 5.12 delta . Prior to that Perl had filters. This has led to new breed of modules which are very helpful. You might want to have a look at Moose link. Devel::Declare link. And its associated modules like Moosex::Declare, Try::Tiny etc. A search on CPAN will give you all the things you are looking for.

Lisp is another example of highly extensible language. I don't think Python is a very extensible when compared to both Lisp and Perl. In fact Python emphasizes on the exact opposite, code readability at the expense of sacrificing power. Which isn't bad goal either, it only serves a different audience.

I guess since you are specifically looking extensibility in scripting kinda languages Lisp is your best bet. And if doesn't look too pragmatic for the real world, Perl is your friend.

share|improve this answer

Common Lisp is probably the classic example of a language where you can pretty much define any syntax you want with macros and read macros (as @David Thornley says) - it's great for DSLs, for example.

I'd also consider Smalltalk here. It's got very little syntax of its own, and everything else is a message send. It really depends what you're trying to do.

share|improve this answer

PFront is a dynamically typed language with an ability to extend its own syntax. No strict typing whatsoever, it is just a Lisp with a modified reader, nothing more. I see no connection between type system and syntax extensions, as long as your type system allows you to represent an underlying language AST. It is all the same for MetaOCaml, for Template Hakell, for Katahdin, for PFront, etc.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.