Your task is to tack on a feature to a programming language, either by implementing a very clever library, or by processing the input text and/or tweaking the compilation process.
Ideas:
- Add PHP-style presentation interleaving to C (e.g.
<?c printf("Hello,"); ?> world!
). - Add a null coalescing operator to one of those languages that isn't C#.
- Add macros to PHP.
- Add
goto
to JavaScript. - Add pattern matching to language X.
- Add namespace support to a language that doesn't have it.
- Make C look like PHP.
- Make Haskell look like Pascal.
- ... (feel free to post ideas in the comment section)
Rules:
- Bring something to the table. Don't just say "Template Haskell" to add metaprogramming facilities to Haskell. This isn't StackOverflow.
- The entire implementation should fit in one screenful (not counting the example).
- Do not host code on an external site specifically for this task.
- The most impressive or surprising feature wins.
Don't worry about implementing the feature 100% correctly. Far from it! The main challenge is figuring out what you want to do and viciously cutting away details until your planned undertaking becomes feasible.
Example:
Add a lambda operator to the C programming language.
Initial approach:
Okay, I know I'd want to use libgc so my lambdas will solve the upward and downward funarg problems. I guess the first thing I'd need to do is write/find a parser for the C programming language, then I'd need to learn all about C's type system. I'd have to figure out how to make sense of it as far as types go. Would I need to implement type inference, or should I simply require that the formal parameter be typed as given? What about all those crazy features in C I don't know about yet?
It's quite clear that implementing lambda in C correctly would be a huge undertaking. Forget about correctness! Simplify, simplify.
Better:
Screw upward funargs, who needs 'em? I might be able to do something tricky with GNU C's nested functions and statement expressions. I wanted to show off an amazing syntactic transformation on C with terse, hacky code, but I won't even need a parser for this. That can wait for another day.
Result (requires GCC):
#include <stdio.h>
#include <stdlib.h>
#define lambda(d,e)({d;typeof(e)f(d){return(e);};f;})
#define map(F,A)({typeof(F)f=(F);typeof(*(A))*a=(A);({int i,l=((int*)(a))[-1]; \
typeof(f(*a))*r=(void*)((char*)malloc(sizeof(int)+l*sizeof(*r))+sizeof(int)); \
((int*)r)[-1]=l;for(i=0;i<l;i++)r[i]=f(a[i]);r;});})
#define convert_to(T) lambda(T x, x)
#define print(T, fmt) lambda(T x, printf(fmt "\n", x))
int main(void)
{
int *array = 1 + (int[]){10, 1,2,3,4,5,6,7,8,9,10};
map(print(int, "%d"), array);
double *array2 = map(lambda(int x, (double)x * 0.5), array);
map(print(double, "%.1f"), array2);
long *array3 = map(convert_to(long), array2);
map(print(long, "%ld"), array3);
long product = 1;
map(lambda(int x, product *= x), array);
printf("product: %ld\n", product);
return 0;
}
That was easy, wasn't it? I even threw in a map
macro to make it useful and pretty.