Expression Evaluation

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Expression Evaluation

GregT2
Given 'let x = y + 2 > 5 + 4 < 7';

I believe the grammar specified on Page 209 would accept this as valid. My question is, what is the correct way to handle it during code generation?

1. Deduce that it is invalid (over and above grammar checking which, after all, doesn't deal with lots of things, such as invalid symbols).

2. Following the VM logic, evaluate '2 > 5' as false/0 and '4 < 7' as true/-1, and the final result as x = y -1.

This (#2) seems like the only way to handle it without getting into type checking, but I might be way off base here. Thanks for any comments.
Reply | Threaded
Open this post in threaded view
|

Re: Expression Evaluation

GregT2
Sorry, the above 'final result' assumes that > and < would have precedence over +, and the language intentionally omits precedence complications, so this is not correct. My question still applies, but the 'final result' wouldn't be 'x = y - 1'.
Reply | Threaded
Open this post in threaded view
|

Re: Expression Evaluation

cadet1620
Administrator
In reply to this post by GregT2
GregT2 wrote
Given 'let x = y + 2 > 5 + 4 < 7';

I believe the grammar specified on Page 209 would accept this as valid. My question is, what is the correct way to handle it during code generation?

The only difference between operators in Jack is what VM command they generate.

a - b * c + da > b + c < d
Compiles to push a
push b
sub
push c
mult
pushd
add
push a
push b
gt
push c
add
pushd
lt

The compiler doesn't need to worry about the semantics of the various operators.

--Mark