Programming Language with Variable Rules?
May 8, 2007 11:42 PM   Subscribe

Is there an existing programming language with variable syntax rules that can be set at compile time/interpreter-time?

For my Master's work, I am doing some research on the psychology of computer programming. For user test purposes, it would be extremely useful to have a language (academic/not-popular ones are fine) that allowed you to determine some rules (static vs weak typing, space-sensitivity, variable scoping, etc) at compile-time, so that I'd have fine-grained control on what aspects of the language were varying from trial to trial.

I want to avoid the pitfall in programming research of comparing apples to oranges (or, say, Ruby to C++) and then concluding that something in the apple made it better than the orange, if that makes sense. Any ideas? Is such a language even feasible? Any interesting work I should have a look at in this field? Thanks!
posted by mikeyk to Technology (14 answers total) 1 user marked this as a favorite
God, I'm so positive that there are tons of languages that do what you are asking for, but I kind of slept through my programming languages class (sorry, Professor Jhala!) so I can't remember which. I would say you should certainly delve into the options you hand to GCC when compiling. Also look at Perl because if there is anything bogus in the world, Perl will let you do it.

I mean, heck, variable scoping is a two-line change when you're writing a compiler if you happen to have written a compiler like the one I wrote, so there's no reason not to allow it as an option other than shooting yourself in the foot reasons. And space sensitivity? I can't think of that many languages where space sensitivity makes any difference to anyone, other than Python, everyone's favorite counterexample.

I'm sure you will get much better answers from people who didn't sleep through languages...I'm going to go Google this :)
posted by crinklebat at 12:00 AM on May 9, 2007

Best answer: Fortran is everyone's favorite other counterexample when it comes to space (in)sensitivity.

Many languages support the creation of domain-specific minilanguages. But in those cases the lexical syntax of the minilanguage is usually fixed (probably inherited from the host language) since getting that kind of stuff right is the kind of finicky work people are usually trying to avoid in the first place.

Maybe you could find a compiler for any language (say, C) whose front-end is reasonably clean and modify its yacc/lex (or whatever) specification to meet your purposes.

I think one problem is that a programmer's productivity is affected by their tools, especially when they're learning new ones, and one big effect on learning a language is how informative the compiler's error messages are. It can be really a lot of work to generate good error messages.
posted by hattifattener at 12:15 AM on May 9, 2007

static vs weak typing, space-sensitivity, variable scoping, etc

I don't understand your question since only one of those are about syntax.
posted by rdr at 1:08 AM on May 9, 2007

Perl has a set of special global variables that can change some of the things you are talking about.

Smalltalk let's you do some very weird things to the object framework at runtime. And Squeak is a version of smalltalk.

And don't forget Brainfuck.

Hey, there is a list of 1090 different languages to choose from.
posted by YoBananaBoy at 1:22 AM on May 9, 2007

Best answer: If you're talking about putting different syntatical rules in front of the same language, then I hope you're aware of One Day Compilers. It's about building a make clone that sits on top of the Ocaml back end. It may not directly address your problem but it's a beautiful hack. It also points towards a community of people that are interested in Domain Specific Languages.
posted by rdr at 1:23 AM on May 9, 2007

I think maybe what you should be considering is a lexical preprocessor. It would scan the source and then regularize it. So what goes in is "C" source (or whatever) with the changes you want to experiment with, and what comes out is "C" source that conforms to the standard.

That kind of thing could be done without too much difficulty with "lex", if it can be found anywhere any more. (I played with lex and yacc when I was in college, lo these many decades ago.)
posted by Steven C. Den Beste at 1:32 AM on May 9, 2007


Asking "what language is just like C++, except with space-sensitivity" is a lot like asking "what language is just like English, but with latin-style cases" -- I don't believe there's an answer. In the case of human language, though, at least you can jot down a few alleged rules of English-prime and call it enough. In the case of a programming language, you have to actually write the compiler for that language.

Many times, nonstandard programming languages themselves prefer to un-ask the questions you're asking. You mention "static vs weak typing" -- as though the only two choices are to go through your program typing 'int foo' and 'String bar', or to suffer through a language that will happily add a number and a string together, sometimes giving (1 + '1') == 2, and sometimes giving (1 + 'one') == '1one'. Another alternative is to give the system the responsibility of determining at run-time whether each operation is permissible, without forcing the programmer to explicitly give a type to each variable. Here, 1+'1' and 1+'one' are both detected errors, even though you're not forced to type 'int foo' to tell the compiler that 'foo' will hold an integer.
posted by jepler at 5:39 AM on May 9, 2007

This only sort of answers your question, but in Flash 9 (CS3), you can choose between an AS2.0 file and an AS3.0 file. The Flash plug-in contains two virtual machines: one to interpret each language.

AS 2.0 is OPTIONALLY strongly typed. You can define vars via...

var highScore = 10;


var highScore:Number = 10;

AS 3.0 is always strongly typed.
posted by Evangeline at 5:43 AM on May 9, 2007

It might not be quite what you're looking for, but you might want to look into using a Scheme interpreter written in Scheme. I had to modify one for an assignment in one of my classes, and you can change from lexical to dynamic scoping by changing a few lines. I think it would probably be pretty straightforward to have different versions of the interpreter, or to build in some switches to control its operation, that would allow you to test the other behaviors you mentioned as well.

The one we used was pretty short. I think it was maybe five pages long, and pretty straightforward to understand/modify. If you're interested I can get you the file.
posted by benign at 7:05 AM on May 9, 2007

I think you want Perl. Using source filters, you can greatly change how the perl source is interpreted. For example:


On the more practical side:

posted by sbutler at 9:10 AM on May 9, 2007

Best answer: I don't know what the answer is, but you should ask the question at Lambda the Ultimate.

There are languages with optional type declarations (Strongtalk, a Smalltalk variant, for example) or with pluggable type systems.

I don't know of any languages that have switchable scope or space-sensitivity. It's possible you might want to look at languages with multiple implementations, like Python/Jython/IronPython.
posted by siskin at 10:45 AM on May 9, 2007

I've never heard of such a language.

Lisp dialects (including Scheme) are generally the champ at facilitating changing just about anything about the language. But not much less so in Lisp than in anything else, to change things as fundamental as you're talking about would mean something approaching implementing a new language. And the nature of that language would depend on the decisions you made while doing that. So you'd still end up comparing apples and oranges.
posted by Zed_Lopez at 10:54 AM on May 9, 2007

You might want to read up on denotational semantics, as that is what I think you're talking about. The course in Deno in my college was taught using Scheme, as it's pretty easy to bang out interpreters in Scheme and mess with the semantics.

You might also look at the features of Nemrle which has a pretty good macro extension that creates the illusion of new syntax fairly effectively.
posted by plinth at 11:13 AM on May 9, 2007 [1 favorite]

Response by poster: Thanks, everyone! There are several ideas and languages here I hadn't considered, and I'll certainly keep Zed and jepler's objections in mind.
posted by mikeyk at 11:23 AM on May 9, 2007

« Older Streaming MP3s from home PC to work PC (which has...   |   What's wrong with my back? Newer »
This thread is closed to new comments.