开发者

Languages that can alter its own syntax?

开发者 https://www.devze.com 2023-03-22 09:28 出处:网络
开发者_StackOverflow中文版Are there any existing languages where the programmer can change the language\'s syntax at any point?
开发者_StackOverflow中文版

Are there any existing languages where the programmer can change the language's syntax at any point?

That means one can write the language in the syntax one likes, and there is some translator to translate it back to some "standard code", which can be compiled or interpreted.

The code will also be readable, because "standard code" can be translated to the version of code another coder likes.

I had this question because some people might like the design of a language, but doesn't like the syntax. (for example, Lisp with [] instead of (), Lisp with f(x) instead of (f x)).

Also, it be nice if anyone can provide some academic sources on topics like this.

Edit: I found one, FreeForm.


In the specific case of Lisp:

  • Lisp has macros! You can transform the language in any way you please using Lisp macros.
  • You can also create reader macros to change the way source expressions are parsed in Lisp.
  • For more extensive changes to the source format, you can even make your own reader. See David A. Wheeler's readable S-expressions proposal for an example.


There's various things that lean in that direction... But I suspect my list will suffer from the "Oh no, that's not magic at all!" syndrome.

But...

  • The C Preprocessor
  • Code reformatters (to make code indented the way you prefer, or whatever)
  • Operator overloading
  • Various language precompilers, like the Coffeescript-to-Javascript in Rails 3.1


Your question need some clarification.

  1. The transformation (f x) <---> f(x) seems to be named lens, not a syntactic sugar. Lenses are bidirectional transformations, where as syntactic sugar mostly considered as one-way transformation. Boomerang project provides some tools for making lenses; some research papes are listed here: http://www.seas.upenn.edu/~harmony/. Still, I agree that some kinds of syntactic sugar may be implemented as lenses.

  2. Generally, it is allowed to use both "sugared" and "de-sugared" syntactic forms at the same place, where as f(x) and (f x) seems to be mutually exclusive. Does (f x g(h y)) mean (f x g (h y)) or (f x (g h y))? Yes, you may disambiguate this by treating spaces to be significant, but there is some drawback.

  3. Changing language syntax at any point is a different matter. Such inline clauses as "now forget (f x) syntax and use f(x) syntax" may be seen as a source of inconsistency; at the other hand, such clauses as "extend current binary operator set with binary operator #$" may be seen as a usefull feature.

  4. There is need for even more clarification.

I plan to list all the languages that can change their syntax after additional clarification; probably, this will be new question at http://programmers.stackexchange.com

0

精彩评论

暂无评论...
验证码 换一张
取 消