LangProc / langproc-2018-cw

5 stars 4 forks source link

Translate (2>3)-1 to python #5

Open BenShen98 opened 5 years ago

BenShen98 commented 5 years ago

So in C we could write code such as below, which is syntactically valid, but somewhat meaningless.

printf("%x",(2>3)-1);

In spec for C, it does say

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int.

As result of this do we need translate such exprssion to python?\ It can't be done by just print (2>3)-1, as 2>3 returns nontype

ymherklotz commented 5 years ago

One possible solution to this could be to wrap all expressions, or just the expressions outputting a Bool in python, with int(...). I believe that would still work in if statements too:

if int(5 < 3):
   ... 

And would convert True to 1 and False to 0 in expressions.

ymherklotz commented 5 years ago

Actually, I think that the problem is that you are using python 3 with python 2 syntax. If you try:

print((2>3)-1)

It should convert the Bool to int automatically and print the correct answer.

Your print statement works fine for me on python 2. In python 3 it is seeing the print(2>3) call and is trying to add the result None to 1.

BenShen98 commented 5 years ago

Actually, I think that the problem is that you are using python 3 with python 2 syntax. If you try:

print((2>3)-1)

It should convert the Bool to int automatically and print the correct answer.

Your print statement works fine for me on python 2. In python 3 it is seeing the print(2>3) call and is trying to add the result None to 1.

Yes, that's the problem, thanks