"Type checking" of provided compiler

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

"Type checking" of provided compiler

bupjae
class Main {
  function void main() {
    var #type# a;
    let a = null;         // line 4
    let a = 0;            // line 5
    let a = 1;            // line 6
    let a = true;         // line 7
    let a = false + a;    // line 8
    return;
  }
}

When #type# = Array, there are no errors.

When #type# = int, errors are following:
In Main.jack (line 3): In subroutine main: an int value is expected
In Main.jack (line 6): In subroutine main: an int value is expected
In Main.jack (line 8): In subroutine main: An int value is illegal here
In Main.jack (line 7): In subroutine main: an int value is expected

When #type# = boolean, error is following:
In Main.jack (line 3): In subroutine main: a boolean value is expected

So far, it seems that:
* If expression involves object type, almost no type checking is performed
* null can be assigned only to object type variable
* It is illegal to assign true/false to int variable, but it is legal to assign integer constant to boolean variable

It seems that provided JACK compiler have very arbitrary type rule and have no common pattern.

When I implement my own JACK compiler, I almost ignore "type rules" and treat null / true / false as same as constant 0 / -1 / 0. Now I'm not sure that it was my best :(
Reply | Threaded
Open this post in threaded view
|

Re: "Type checking" of provided compiler

bupjae
Update:

I just found that the provided compiler emits type error for
var int a; let a = true;
, but not for
var int a; let a = (true);

Now I'm more confused......
Reply | Threaded
Open this post in threaded view
|

Re: "Type checking" of provided compiler

WBahn
Administrator
Jack was specifically designed so that every type is represented as a 16-bit pattern, including characters, numbers, and memory addresses. This was so that type checking does not have to be performed, which is not to say that this doesn't result in the ability to write code that will do really strange things because of the ability to divide a character by an object's memory address. But the overriding goal was to craft a language for which it would be feasible for most students, having only a semester or so of experience working with a high-level language, to write a compiler in just two weeks.

Reply | Threaded
Open this post in threaded view
|

Re: "Type checking" of provided compiler

dolomiti7
In reply to this post by bupjae
I have experienced similar issues. The stock compiler does indeed some basic type checking - and by doing so deviates from the language specification as defined in the book for some corner cases.

The brackets seem to implicitly convert an expression to an int type in the internal implementation. I can only presume why this was done, perhaps to allow things like:
  let y = y + y - (x < 0) // carry-over msb from x
(to calculate with booleans and bypass the type checking.)

Since the language doesn't support any casting to override types, I haven't implemented any type checking in my own compiler.
Reply | Threaded
Open this post in threaded view
|

Re: "Type checking" of provided compiler

WBahn
Administrator
Yeah, the tools perform some checking in a number of places that goes beyond the specifications. While I don't have a fundamental problem with this, as they can be very helpful for debugging purposes, I think these should result in warnings and not errors.