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

classic Classic list List threaded Threaded
6 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
Reply | Threaded
Open this post in threaded view
|

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

Ichiro
Hi,

I tried to output a letter on the screen by using the printChar function, but it seems I cannot substitute a letter for a variable. Following is my main function code and when I compiled it, it returned the error message on the line of "let ~" as shown below.

class Main {
   function void main() {  
      var char a;  
      let a='r';                             #In subroutine main: Expected - or ~ or ( in term
      do Output.printChar(a);
    return;
   }
}


Another thing that puzzled me was when I substituted an int constant for a char variable, the function printChar did not work while the function printInt succeded as shown below.

class Main {
   function void main() {  
      var char a;  
      let a=3;
      do Output.printInt(a);
    return;
   }
}


As I am just writing some programs to get used to the Jack language, I can forget about them and work on other kinds of programs without using them. I just wanted to confirm that these were because of the compiler bugs.

Thanks,
Ichiro
Reply | Threaded
Open this post in threaded view
|

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

WBahn
Administrator
The Jack language specification has no support for character literals. This would not be terribly difficult to implement, but would greatly increase the level of difficultly for many students.