C (gcc), 96 7171 70 bytes
Awesome -25 solution by ceilingcat
And another -1 by ceilingcat. ASCII bit hacks ftw
c;p;main(i){for(;read(0,&c,1);printf("%*c\n",i,p=c))c-p?:c-47c&1?++i:--i;i:++i;}
Ungolfed
c; /* character */
p; /* previous character */
main(i /* indent */)
{
for (; read(0, &c, 1); printf("%*c\n", i, p = c))
c - p ? : c -& 471 ? ++i--i : --i;++i;
}
Explanation
read
takes 1
byte from the file descriptor 0
(stdin
) and stores it in c
.
c - p
checks if c
and p
are different. c -& 471
checks if c
is not a forward slash. So the entire conditional increments the indent if the character and the previous character are both backslashes, and decrements the indent if they're both forward slashes.
Then we print c
with i
leading spaces. We also assign p = c
for the next iteration.