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.

Is there a programming language in which you can set your own syntax configurations and it would convert the code to a language you choose?

For instance, you would choose specific configurations like "Python's indexed blocks", [a,b,c] initializes arrays, ^ for exponentiation and others. A script would convert it to your choice language's equivalent.

share|improve this question
1  
I sure hope not. TIMTOWTDI is arguably bad, but "invent as many incompatible ways as you want" would definitely have the same downsides times hundred, and be even less useful. DSLs are sometimes worth it, but redefining half of the syntax (or worse - swapping two operators, giving apparent compability but different semantics) just for the heck of it... –  delnan Jan 21 '12 at 17:04
    
I ask this because specifialy I'm programming on javascript and would really, really like to be able to set up a few syntax sugars on my code but the language just won't allow it. Not even operator overloading is possible. –  Dokkat Jan 21 '12 at 17:07
2  
To some degree, what you're describing sounds like preprocessor macros. Now if only we could have a similar feature for perl... –  Yannis Rizos Jan 21 '12 at 17:25
5  
@Dokkat: Please update the question to specifically state that this is about JavaScript. The question -- as asked -- is a maintenance and support nightmare. Adding new syntax to an existing language (or using a pre-processor to change your private language to a public language) is truly awful. However. If you list specific JavaScript syntax sugar issues, you might get specific help. –  S.Lott Jan 21 '12 at 17:45
3  
These "personalized programming languages" are normally called "domain specific languages" or DSLs. –  Michael Dillon Jan 22 '12 at 6:24
show 6 more comments

11 Answers

Yes, but probably not in the way you’re thinking.

Lisp and its relatives have powerful macro systems that let you perform arbitrary transformations on your source, so in principle you can use macros (and reader macros, at the character level) to make just about any extensions you like. Concatenative languages (and languages by authors in related areas) have the potential for similar metaprogramming power. A good example of this is Forth, in which a lot of syntactic constructs (such as comments) can be user-defined. For a contemporary, non-esoteric example, there is an effort to give Perl 6 improved support over Perl 5 for user-defined syntax as well.

share|improve this answer
8  
I somehow knew Perl would be (part of) the answer. I guess C preprocessor macros also fit the description, to a small extend. –  Yannis Rizos Jan 21 '12 at 17:31
    
Also operator overloading in C++ can be nice. The hello world example comes to mind. cout << "Hi"; –  Lord Tydus Jan 22 '12 at 15:58
    
@LordTydus: Indeed, and even just operator overloading lets you do quite a bit of magic behind a sane interface. It would be nice if C++ had more general overloading capabilities, say, overloading operator[] for multiple arguments, or introducing new operators as in Haskell; but I doubt the committee would see past the parsing issues. –  Jon Purdy Jan 22 '12 at 18:48
add comment

Not to my knowledge.

I don't think it would be a particularly good idea either - one of the main benefits of a standard language syntax is that many people can read it, and if everyone invented their own syntax then nobody would be able to understand anyone else's code..... so this language might be fun for a hobbyist but not much use for anything practical.

The closest thing that exists would probably be Lisp, where the macro system lets you write your own new language constructs in a very flexible way. Normally you would still stick with Lisp syntax, but you can effectively redefine everything if you really want to.

As a result, Lisps tend to be particularly popular for implementing DSLs. A nice example is this data access DSL in Clojure (Korma) which takes care of all the boilerplate code required for database access.

share|improve this answer
    
+1 for the link to Korma. Thanks! –  Kevin Jan 22 '12 at 18:33
add comment

AFAIU you are looking for languages that can help you to write your own DSL (Domain Specific Language). There are many good candidates. Personally I would recommend Ruby or Io, because they offer 1) easy operator overloading, 2) meta-programming, and 3) fallback mechanisms like method_missing that you can use to create your own API. For example, take a look in sqldsl, a DSL written in Ruby that generates SQL by reading Ruby code.

share|improve this answer
add comment

Sounds like concept programming.

I'm not familiar with it, but XL is a language designed specifically for it.

XL features programmer-reconfigurable syntax and semantics. Compiler plug-ins can be used to add new features to the language.

Lisp and Forth also support concept programming, although less than XL. I suggest Lisp as it is very practical and effective. The most important thing is a clear and flexible syntax, not notation, which Lisp provides.


For instance, you would choose specific configurations like "Python's indexed blocks", "[a,b,c]" initializes arrays, "^" for exponentiation and others.

You may be interested in a language like Haskell which has a very flexible yet uniform syntax.

share|improve this answer
    
This is what I was looking for. –  Dokkat Jan 31 '12 at 15:35
add comment

I haven't actually used it per se, but if I recall correctly, TXL is specifically designed for translating source code. As such you should be able to use it to specify how to translate from your language and/or language extensions to any target real language.

It does appear to have a grammar available for Javascript. But, not having used it before, I'm not sure exactly how that would help you.

share|improve this answer
add comment

Scala is often used in this way to create DSLs, Domain Specific Languages.

Mainly this is because Scala has no operators, and the Scala method calling syntax can be abbreviated. For instance:

5 is an Integer object. To calculate 5 plus 7 you could write

val ans = 5.add(7) except that the addition method is actually named "+" so you would write:

val ans = 5.+(7) but in Scala, you do not need to include the "." in method calls or the parentheses "()" around arguments so to call the + method on the object 5, you would write

val ans = 5 + 7 which works perfectly because Scala has no operators to interfere with your clever method naming scheme. Now extend that idea to your own classes and objects, including the fact that you can "override" methods like + as well as create your own methods named >>> or ::! or @*@ or just plain text names like fancify.

share|improve this answer
    
This is awesome. I'll give a look on scala. –  Dokkat Jan 31 '12 at 15:38
add comment

There is a nice preprocessor out there called PPWizard, quite powerful. Once you learned its syntax you could create macro defs to target different languages -- not that it would be foolproof or fun for that matter, but it is a useful tool.

share|improve this answer
add comment

Not saying you should use it as it is still mostly experimental, but one interesting recent programming paradigm is Language Oriented Programming (LOP).

The concept of Language Oriented Programming takes the approach to capture requirements in the user's terms, and then to try to create an implementation language as isomorphic as possible to the user's descriptions, so that the mapping between requirements and implementation is as direct as possible.

Sergey Dmitriev explains it in more detail.

That's the ideology of it, but practically it boils down that it supports the behavior you are asking for. You can:

  • Extend an existing language.
  • Compose different languages.
  • Use an existing 'transformation language' in order to convert your newly defined keywords to a base language.

The most active programming environment I know of following this paradigm is the free Meta Programming System (MPS) by JetBrains.

It's actually starting to be used in commercial software as well, so perhaps it's starting to get out of it's 'experimental' phase:

  • Realaxy is an actionscript editor built entirely on top of MPS. Using the power of LOP they were able to implement e.g. closures for AS3.
  • mbeddr C is an extensible C language based on MPS.
share|improve this answer
add comment

Domain-specific languages are quite commonly used in the Tcl community. For example, it's not at all uncommon to see a Tcl program that contains both C and SQL as embedded DSLs (via the Critcl and Sqlite packages). About the only restriction on an embedded DSL is that it is much better if the embedded language balances its curly braces, and that turns out to be a really non-onerous requirement in practice!

Another language which makes DSLs easy is Standard ML, which allows you to have access to raw tokens out of the generic system tokenizer (from before the point where identifiers are converted into keywords). This makes doing a wide class of DSLs easy — provided they obey some of the basic syntax rules of SML, especially in relation to comments, strings and token boundaries — and is in fact used heavily in a number of the key driving applications for the language (theorem provers, etc.)

Not all languages make this so easy. In particular, languages which automatically parse out the contents of braced terms (many many of them!) can't do this, so they require the DSL to be built by either overriding operators, through putting the sub-language inside a string, or by the use of a pre-processor which rewrites the DSL into something that can be handled inside the host language.

share|improve this answer
add comment

May be what you are looking for is as compiler compiler. A compiler compiler takes a language definition written in a formal language and creates a compiler for this language.

Have a look at http://en.wikipedia.org/wiki/Compiler-compiler

share|improve this answer
add comment

One common way of partially personalizing a programming language is to use a macro pre-processor. For instance you could run "dokkat-lang" though the C preprocessor before putting the result where some javascript environment would download it.

How much you can do depends on the sophistication of your preprocessor. Note that early versions of C++, Objective C, and various experimental languages were done by preprocessing the source code before feeding to a stock C compiler toolchain.

share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.