Ask HN: What would Your_Favorite_Compiler do?
11–20 of 28 posts
Re: Ask HN: What would Your_Favorite_Compiler do?
#12Think about how this is compiled. In machine language there are no nested expressions, so the compiler will have to split the expression up. expression( ... ++x ... ) will probably be translated to x = x+1; expression( ... x ... ) This is the simplest way to compile ++x: 1. put x=x+1 before the expression 2. replace ++x with x So I think most languages would get 22. But in languages where + is a function there's a go…
gcc says 22 and it gives you a warning, that this expression is illegal, we looked it up, because you cannot use a variable which is ++incremented more than once in the same expression.
Re: Ask HN: What would Your_Favorite_Compiler do?
#13y is 22. But my favourite imaginary compiler doesn't allow you to compile the second statement unless 1. your IQ is above a certain threshold 2. you explained in a comment - why?...
Re: Ask HN: What would Your_Favorite_Compiler do?
#14 # let x = 10;;
val x : int = 10
# let y = x + ++x;;
Error: Syntax error
Hmm. Let's try that again: # let x = ref 10;;
val x : int ref = {contents = 10}
# let y = !x + incr x;;
Error: This expression has type unit but an expression was expected of type int
So nothing, I guess!Re: Ask HN: What would Your_Favorite_Compiler do?
#15Think about how this is compiled. In machine language there are no nested expressions, so the compiler will have to split the expression up. expression( ... ++x ... ) will probably be translated to x = x+1; expression( ... x ... ) This is the simplest way to compile ++x: 1. put x=x+1 before the expression 2. replace ++x with x So I think most languages would get 22. But in languages where + is a function there's a go…
Compilers don't magically filter assignments out of expressions. The compiler already has the parse tree (+ x (1+ x)), and the arguments are pushed on the stack in reverse order because that's consistent with function calls (in c++ when you were to overload operator+ nothing would change, I suspect). So first 1+ x is evaluated and the value of x is updated, and the number 22 is returned.
Or to illustrate with a stack machine:
PUSH X
INC
STORE X
PUSH X
ADD
STORE Y
But the whole discussion is still silly, because the whole deal is undefined in any sane language (and for good reason). Unless variables become immutable you can't really prevent this kind of ambiguity from occurring, so it's no big deal.Re: Ask HN: What would Your_Favorite_Compiler do?
#16Think about how this is compiled. In machine language there are no nested expressions, so the compiler will have to split the expression up. expression( ... ++x ... ) will probably be translated to x = x+1; expression( ... x ... ) This is the simplest way to compile ++x: 1. put x=x+1 before the expression 2. replace ++x with x So I think most languages would get 22. But in languages where + is a function there's a go…
Sounds plausible, but I don't think it's true. Compilers don't magically filter assignments out of expressions. The compiler already has the parse tree (+ x (1+ x)), and the arguments are pushed on the stack in reverse order because that's consistent with function calls (in c++ when you were to overload operator+ nothing would change, I suspect). So first 1+ x is evaluated and the value of x is updated, and the numbe…
That's why you can infer that y = 21 in Java.
Would you call Java insane for that?
Re: Ask HN: What would Your_Favorite_Compiler do?
#17Interpreter, really: >>> x = 10 >>> x + ++x 20 I suspect my other favorite compiler will say 'Type error: Could not match "Num a => a" against "Num a => [a] -> [a]"', but I don't want to wait for it to finish installing.
yup, that's python alright. Very odd behaviour
Re: Ask HN: What would Your_Favorite_Compiler do?
#18 y = operator+(x, operator++(x));
and therefore operator++(x) would be called before operator+()Re: Ask HN: What would Your_Favorite_Compiler do?
#19My_Favourite_Compiler would compile "x + ++x" as a call to abort(3). I think "undefined behaviour" is a horrible idea.