charAt()

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

charAt()

Lozminda
I'm really having some trouble.. Am sure the explanation is simple but after two and half hours of searching the internet and fiddling with my program all to no avail.
This is the code:

         do Output.printChar(InStr.charAt(0));       // prints "-"      InStr[0]="-".       InStr="-123456789"

         if (InStr.charAt(0) = "-")                             // however this if statement is false. Surely it should be true ?

I have fiddled endlessly, please help, I'm sure it's something blindingly obvious that I can't see.
(At this rate C is turning out to be much less frustrating than jack !!)

Thanks in advance
Reply | Threaded
Open this post in threaded view
|

Re: charAt()

Lozminda
The solution is

 if (InStr.charAt(0) = "-")    // takes a number not a char so the code should read :-

 if (InStr.charAt(0) = 45)   // 45 being the UNICODE for '-' (the minus sign)

Doh !



See it's the healing power of the Nabble site....
Reply | Threaded
Open this post in threaded view
|

Re: charAt()

WBahn
Administrator
Glad you figured it out.

One extension that could be made to a Jack compiler would be the ability to translate literal characters. In C and many other languages, you can do stuff like

c = 'A';

The 'A' syntax is actually an expression, dealt with by the preprocessor IIRC, that returns the character code for that symbol in the execution character set (which is usually, but not always, ASCII).

Adding a similar ability to Jack would not be too difficult, but it was something that the authors probably felt added more complexity that it warranted (and I think they were correct in that assessment).
Reply | Threaded
Open this post in threaded view
|

Re: charAt()

Lozminda
Even geniuses have time constraints !
Reply | Threaded
Open this post in threaded view
|

Re: charAt()

ivant
In reply to this post by WBahn
WBahn wrote
In C and many other languages, you can do stuff like

c = 'A';

The 'A' syntax is actually an expression, dealt with by the preprocessor IIRC, that returns the character code for that symbol in the execution character set (which is usually, but not always, ASCII).
I don't think it's a preprocessor thing, at least not anymore. I tried with a small program and the preprocessor didn't change the character. If it was ever supported by the preprocessor, it must have been in the very early days of C.

I would be very interested if you can share some links which mention this.
Reply | Threaded
Open this post in threaded view
|

Re: charAt()

WBahn
Administrator
It may well not be the preprocessor. I only touched on that rather peripherally when I got sidetracked a few years ago and was reading up on the various character sets (primarily source and execution) described by the language standard.

A quick check shows that you are correct. The preprocessor does everything in the source character set. About the first thing AFTER the preprocessor finishes is translating string and character constants to the execution character set.