Show HN: slcc, a C compiler that doesn't need semicolons or braces (github.com)

1 points by itzmetanjim ↗ HN
hi, i made this mostly as a joke. it started when i found that replacing (almost) all skip(';') in tcc with if(tok==';') skip(';') essentially adds some highly basic auto semicolon insertion. then i expanded on the idea and kept adding more stuff and fixing up the semicolon stuff a bit and added python style indentation.

so now it's a fork of tcc (fabrice bellard's tiny c compiler) where semicolons are optional and you can use indentation instead of braces, so you can write stuff like

    int main():
        printf("hello\n")
        return 0
its not just at the end of lines by the way

    int a, b float c // a semicolon is inserted between b and float
    for (int i=1 i <= 5 i++): // ... or between statements
        printf("%d ", i)
the indentation part is just a pass before lexing that turns lines ending in "):" or "else:" into "{" and adds the closing "}" when the indentation drops.

the obvious problem is that without semicolons a statement just keeps going as long as the next token can continue it. so if a line starts with *, ( or - it gets glued onto the previous line, same as js asi. "b = a" followed by a line with "-1" compiles fine and gives you b = a - 1. i listed the ones i know about in the readme.

normal c with semicolons still works and you can mix both. -run and shebangs work like in tcc. it can compile itself

demo page with a few examples: https://tanjim.org/slcc/demo.html

please don't use this for anything real

1 comment

[ 0.27 ms ] story [ 9.8 ms ] thread
Fun experiment. Thanks for posting. One case that came to mind:

  a = b
  ++c
I guess this ends up as

  a = b++; c;
since the lexer sees b ++ c and ++ gets taken as postfix on b? Or did you add some guards for cases like this?