ok> "Hello World" <ENTER> 4298101456 ok>What we see here (4298101456) is the memory address where the string "Hello World" has been stored. Since we have no type information on our data stack we only can display the number which represents an address.
The simplest way to identify strings is to have a single string area. To know if the value possibly can be a string we can check the value about within the string area.
Next we put the content on screen
ok> "Hello World" type cr Hello World ok>
static char *word(void) { // symbol might have maximal 256 bytes
static char buffer[256], *end=buffer+sizeof(buffer)-1;
char *p=buffer, ch;
if(!(ch=skip_space())) return 0; // no more input
*p++=ch;
if(ch=='"') { // +course02: string handling
while(p<end && (ch=next_char()) && ch!='"') *p++=ch;
} else {
while(p<end && (ch=next_char()) && !isspace(ch)) *p++=ch;
}
*p=0; // zero terminated string
return buffer;
}
And here we see the changes to out interpret() function
static void interpret(char *w) {
if(*w=='"') { // +course02: string handling
sp_push((cell_t)to_pad(w+1)); // store the string
} else if((current_xt=find(w))) {
current_xt->prim();
} else { // not found, may be a number
char *end;
int number=strtol(w, &end, 0);
if(*end) terminate("word not found");
else sp_push(number);
}
}
static char *to_pad(char *str) { // add for course02: copy str into scratch pad
static char scratch[1024];
int len=strlen(str);
if(len>sizeof(scratch)-1) len=sizeof(scratch)-1;
memcpy(scratch, str, len);
scratch[len]=0; // zero byte at string end
return scratch;
}
And now lets see how to implement the compiler course03 ⇒