The official Jack compiler didn't automatically convert int to char ?

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

The official Jack compiler didn't automatically convert int to char ?

social_loser
I'm trying to write the String class. In chapter 9, section 9.2.3, the book said assign a int to a char is allowed. So I use it  to write the String Class in project 12.
Following is my String.setInt() method.
    method void setInt(int number) {
        var int i;
        var int neg;
        var char c ;
        var int remain;
        var int pass;
        let pass = number;    // save the original number for pass to the next call.
        let neg=0;
        if (number < 0){
            let neg=1;
            let number = Math.abs(number);
        }
        let remain= number - (number / 10 * 10 ) ;
        let c= remain + 48;
        if (number < 10 ){
            do setCharAt(0,0);
            if (neg=1) {
                do appendChar("-");
            }
            do appendChar(c);
            return ;
        }
        do setInt( pass / 10 );
        do appendChar(c);
        return ;
    }

but when I 'm trying to use the official compiler to compile it , there comes an error message :

In String.jack (line 89): In subroutine setInt: a char value is expected

so did I misunderstand it ?  is auto-convert only allowed when the right side is constant ?
Reply | Threaded
Open this post in threaded view
|

Re: The official Jack compiler didn't automatically convert int to char ?

cadet1620
Administrator
This post was updated on .
    var char c ;
    var int remain;
    ...
    let remain= number - (number / 10 * 10 ) ;

    let c= remain + 48;    // "In subroutine setInt: a char value is expected"
This is a compiler bug. Jack is supposed to be an untyped language so this construct should be legal.

You can change c to an "int".


Also note that "-" creates a String object not a char constant. You need to use the numeric value of the '-' character:
    do appendChar(45);

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: The official Jack compiler didn't automatically convert int to char ?

social_loser
This post was updated on .
thank you for your reply. I changed type of c to be 'int' and have passed it now. however , code :

        var char c ;
        var int remain;
        let remain=remain+48;
        let c= remain;

still not being allowed. I write a test class :

class Test {
        function void test(){
                var char c ;
                var int remain;
                let remain= 20;
                let c= remain;
                return ;
        }
}

won't compile too. so the official compiler do some check the variable type. only assigning a integerConstant to a char is allowed.
Reply | Threaded
Open this post in threaded view
|

Re: The official Jack compiler didn't automatically convert int to char ?

cadet1620
Administrator
Thanks, I updated my post.

--Mark