C 168 175
Correctly handles escaped newline inside source code
Edit 1 fixed bug when last newline missing
Edit 2 fixed bug when line inside comment ends with *
: add a tab char before the //
comment
(and golfed more)
b[999999];main(c,z){char*p,for(p=b;(*p=c=getchar())>=0;z=c,p++)c-10||(z-92?*p++=9,*p++=47,*p++=47,*p=c:(p-=2));*p=47;for(p=b;*p;)putchar(*p++);for(;p>b;)putchar(*--p);}
C99 Standard, valid code, many warnings
Ungolfed
b[999999]; // working buffer ~ 4M on 32 bit machine, max source size
// c is current char, z is previous char,
main(c,z) // z start as argv pointer, will be out of char range
{
char *p;
for(p = b;
(*p=c=getchar()) >= 0; // while testing EOF copy char to buffer set c variable
z=c, p++) // at end loop increment p and set previous = current
{
c-'\n' || // if newline
(z - '\\' // check if escaped
? *p++='\t',*p++='/',*p++='/', *p=c // if not escaped, add tab,/,/ and newline
: (p-=2) // if escaped, drop both escape and newline
);
}
*p='/'; // if last newline missing, will add a comment anyway
for(p=b;*p;) putchar(*p++); // ouput buffer
for(;--p>=b;) putchar(*p); // outbut buffer reversed
}
Can we define the language L? – Greg Hewgill – 2014-06-10T03:13:37.080
1@GregHewgill Yes. L is the language you choose to write your program in. – Justin – 2014-06-10T03:14:07.133
In some languages, this is surprisingly tough. – Justin – 2014-06-10T04:33:57.017
3With a turing complete subset of Python, this is a valid entry:
x=input();print(x+'#'+x[::-1])
. The subset is the set of all programs that don't include newlines. – Justin – 2014-06-10T04:58:22.667