Live data from Hacker News

Main is usually a function. So then when is it not? (2015)

jroweboy.github.io

51–59 of 59 posts

Re: Main is usually a function. So then when is it not? (2015)

#53
post #30

Earlier quoted context omitted.

how about before anything is executed, add a parse step that reads the whole file, looks for function definitions, and then goes back to the start to execute?

What counts as a function definition? def a(): b() a() import random with open("max", "r") as f: m = int(f.read()) b = (lambda: "foo") if int(input("Pick a number: ")) % m > random.randint(0, m) else None

[deleted]

Re: Main is usually a function. So then when is it not? (2015)

#54
post #30

Earlier quoted context omitted.

how about before anything is executed, add a parse step that reads the whole file, looks for function definitions, and then goes back to the start to execute?

What counts as a function definition? def a(): b() a() import random with open("max", "r") as f: m = int(f.read()) b = (lambda: "foo") if int(input("Pick a number: ")) % m > random.randint(0, m) else None

fair point, but this is why you need multiple parse steps. first parse you get the overall structure and note where forward references are made to things that are not known yet. in the second step you fill in those references as found.

it becomes problematic if a function definition is conditional. i suppose then that the compilation should fail because i guess conditional definitions would not be covered.

Re: Main is usually a function. So then when is it not? (2015)

#55
post #3

Back when compiler warnings possibly cost extra processing time to generate, it was possible to make gcc compile the craziest things. After much experimentation, it turned out that the smallest program that would compile and run was only 5 bytes long: main;

Welp that's a nerd snipe if I ever had one. Will report back once my bruteforce hack finishes crunching away.

Please do, I'm eager to find out how long it takes to compile and run 4,294,967,296 small C files on modern hardware ;)

Re: Main is usually a function. So then when is it not? (2015)

#56
post #18

This is fun. On a broader note though, entry point main-type functions have always bothered me. Maybe because I grew up as a simple script kiddie with BASIC and Bash and PHP. I like code that starts executing from the first line and then runs whatever functions it wants to run. I realize that's just an abstraction of main() but it's a pleasant one for me. There's something more constricting about there being one func…

> There's something more constricting about there being one function to bootstrap everything than there is about one file. As a compiler author, there are a bunch of nasty surprises to this approach. If you execute a file line-by-line, then functions only exist once you "reach" them. If you write: def a(): b() a() def b(): ... ...then a() needs to crash when first called, because b() hasn't been declared yet. So your…

Doesn't have to be that way. You can just do two passes.

Re: Main is usually a function. So then when is it not? (2015)

#57
post #55

Earlier quoted context omitted.

Welp that's a nerd snipe if I ever had one. Will report back once my bruteforce hack finishes crunching away.

Please do, I'm eager to find out how long it takes to compile and run 4,294,967,296 small C files on modern hardware ;)

I was able to feed stdin to `gcc` instead of all that IO, which considerably sped it up and spread over 5 cores.

But ultimately I gave up after the third time my computer crashed. I was at length 4 by that point, next step would have been to throttle the CPU usage.

Re: Main is usually a function. So then when is it not? (2015)

#58
post #5

Earlier quoted context omitted.

> make gcc compile the craziest things In languages like Haskell you can just make 2+2=5: https://codegolf.stackexchange.com/a/28794

That’s not that crazy. It just shadows the global + function. And thanks to lexical scope it has a rather limited impact.

It's magic to people like me who don't know Haskell...

Re: Main is usually a function. So then when is it not? (2015)

#59

Earlier quoted context omitted.

I still get a segmentation fault out of that when I run it. I think there's flags you can use to get the linker to not complain about missing main if you give gcc an empty file. The craziest thing I got gcc (the AVR version, specifically) to compile for real purposes was a preprocessor macro that spit out tens of thousands of asm blocks with memory barriers and nop (do nothing) instructions with "PORTB = 1;" in the m…

Must be the internal RC clock? it sucks.

Nope, it's an external one at 16mhz. I think the internal one on the chip can only get up to 8mhz, and is accurate to about +-5%

It's enough to work for most things. Only annoying thing is, if you use the ICSP header to set the clock to external, you need a working clock to even use ICSP. (the 328pb falls back to the internal clock in this case)

Post reply on HN