



TryNumber (lines 4215-4275)
Back to List
Browsing parserm.h
4215 #Endif; ! TARGET_
4216
4217 ! ----------------------------------------------------------------------------
4218 ! TryNumber is the only routine which really does any character-level
4219 ! parsing, since that's normally left to the Z-machine.
4220 ! It takes word number "wordnum" and tries to parse it as an (unsigned)
4221 ! decimal number, returning
4222 !
4223 ! -1000 if it is not a number
4224 ! the number if it has between 1 and 4 digits
4225 ! 10000 if it has 5 or more digits.
4226 !
4227 ! (The danger of allowing 5 digits is that Z-machine integers are only
4228 ! 16 bits long, and anyway this isn't meant to be perfect.)
4229 !
4230 ! Using NumberWord, it also catches "one" up to "twenty".
4231 !
4232 ! Note that a game can provide a ParseNumber routine which takes priority,
4233 ! to enable parsing of odder numbers ("x45y12", say).
4234 ! ----------------------------------------------------------------------------
4235
4236 [ TryNumber wordnum i j c num len mul tot d digit;
4237 i = wn; wn = wordnum; j = NextWord(); wn = i;
4238 j = NumberWord(j);
4239 if (j >= 1) return j;
4240
4241 #Ifdef TARGET_ZCODE;
4242 i = wordnum*4+1; j = parse->i; num = j+buffer; len = parse->(i-1);
4243 #Ifnot; ! TARGET_GLULX
4244 i = wordnum*3; j = parse-->i; num = j+buffer; len = parse-->(i-1);
4245 #Endif; ! TARGET_
4246
4247 tot=ParseNumber(num, len);
4248 if (tot ~= 0) return tot;
4249
4250 if (len >= 4) mul=1000;
4251 if (len == 3) mul=100;
4252 if (len == 2) mul=10;
4253 if (len == 1) mul=1;
4254
4255 tot = 0; c = 0; len = len-1;
4256
4257 for (c=0 : c<=len : c++) {
4258 digit=num->c;
4259 if (digit == '0') { d = 0; jump digok; }
4260 if (digit == '1') { d = 1; jump digok; }
4261 if (digit == '2') { d = 2; jump digok; }
4262 if (digit == '3') { d = 3; jump digok; }
4263 if (digit == '4') { d = 4; jump digok; }
4264 if (digit == '5') { d = 5; jump digok; }
4265 if (digit == '6') { d = 6; jump digok; }
4266 if (digit == '7') { d = 7; jump digok; }
4267 if (digit == '8') { d = 8; jump digok; }
4268 if (digit == '9') { d = 9; jump digok; }
4269 return -1000;
4270 .digok;
4271 tot = tot+mul*d; mul = mul/10;
4272 }
4273 if (len > 3) tot=10000;
4274 return tot;
4275 ];
Last updated 27 February 2004. The librarian in charge of this page is Graham Nelson (graham@gnelson.demon.co.uk) assisted by C Knight. Please email any comments, suggestions or corrections to cedenqs@inform-fiction.org.