PDA

View Full Version : Multiple programming languages in one working file



garak
5 Mar 2005, 10:02 PM
http://incise.org:82/pub/py.or.c.txt

fun :)

Sam172
5 Mar 2005, 10:57 PM
You took the words right out of my mouth :p

Shadow
5 Mar 2005, 11:59 PM
Yes, very so.



#define shift int main() {
#define exit }
#include <stdio.h>

shift;
printf("Hello World!\n");
exit;


Ok, so it's not a separation, but it works. (Compiles in C and executes with perl)

garak
6 Mar 2005, 01:24 AM
It's neat no matter how it's done. :)

The one thing about yours though, shadow, is that a semicolon outside of a code block is technically not proper C, even though most compilers will allow it.

edit: you could do:

#define exit } int x

Shadow
6 Mar 2005, 02:03 AM
It's neat no matter how it's done. :)

The one thing about yours though, shadow, is that a semicolon outside of a code block is technically not proper C, even though most compilers will allow it.

edit: you could do:

#define exit } int x

Right (to the parser, one would think a semicolon outside the block would be just like an empty statement, but some compilers do not allow those). But........

The following does work as well (I've seen it in other code and have used it myself):




class X
{
public:
X() { }; // Note the semicolon
void SomeMethod() { }; // And here, too
};



Maybe such acceptance in the main function is an adaption to C++? I used gcc 3.3.4 to compile it, so it is definately accepting.

Edit: if you have never seen this, you may be interested in the programs at http://www.ioccc.org.

garak
6 Mar 2005, 02:38 AM
If you turn up your compiler's anal-ness flags, it will probably complain about those stray semicolons.

Yeah I have seen the ioccc before, and I swear I remember seeing a site with some multi-language things like this, but I can't find anything on google.

Shadow
6 Mar 2005, 04:02 AM
It sure does:



% gcc -c -ansi -pedantic test.c
test.c:7: warning: ISO C does not allow extra `;' outside of a function


Of interest, the following does not produce warnings (where test.cpp contains class X mentioned previously)
g++ -c -ansi -pedantic test.cpp

garak
6 Mar 2005, 04:10 AM
Hm, interesting. I wonder if the C++ standard allows it?

Shadow
6 Mar 2005, 04:38 AM
Hm, interesting. I wonder if the C++ standard allows it?

I'm not sure. There are many differences in the C++ standard relating to plain C code (like requiring explicit casting of pointers), so I wouldn't be surprised if it did.

I think it would be more consistent to accept extraneous semicolons at global scope. The compiler doesn't seem to complain about this (gcc -c -ansi -pedantic test3.c)



int main()
{
while(1);
while(1) { ; }
while(1) { ; } ;
while(1) {;;;;};;;;;;;;
}


Of course, these are in function context, but the concept is similar.