| | @@ -0,0 +1,998 @@ |
| 1 | +(* Scan: the scanner. |
| 2 | + |
| 3 | + Reads a raz source file and hands the parser one symbol at a time: |
| 4 | + reserved words, identifiers, numbers, characters, strings and |
| 5 | + operators. Identifiers are interned, so that the rest of the compiler |
| 6 | + compares names as small integers. The scanner also says where the |
| 7 | + current symbol is, for messages. |
| 8 | + |
| 9 | + Depends on: Sys, to read files; Diag, to report errors; Jump, to go |
| 10 | + on after the parser's errors. |
| 11 | + Leaves out: recovery from errors in the text itself, such as a string |
| 12 | + not closed: the first such error stops the compiler. The parser goes |
| 13 | + on after its own errors, through recovery below. *) |
| 14 | +module Scan; |
| 15 | +import Sys, Heap, Mem, Fmt, Diag, Jump; |
| 16 | + |
| 17 | +type |
| 18 | + NamePtr = ^U8; |
| 19 | + |
| 20 | + Sym* = (sNull, sIdent, sInt, sReal, sChar, sString, |
| 21 | + sPlus, sMinus, sTimes, sSlash, sEql, sNeq, sLss, sLeq, sGtr, sGeq, |
| 22 | + sBecomes, sColon, sSemicolon, sComma, sPeriod, sUpto, sEllipsis, |
| 23 | + sLparen, sRparen, sLbrak, sRbrak, sLbrace, sRbrace, sArrow, sAmp, sTilde, sBar, |
| 24 | + sBlank, |
| 25 | + (* the reserved words, in the order of words below *) |
| 26 | + sAnd, sBegin, sBreak, sBy, sCode, sConst, sDefer, sDiv, sDo, sElse, |
| 27 | + sElsif, sEnd, sExtern, sFor, sForward, sIf, sImport, sIn, sLink, |
| 28 | + sMatch, sMod, sModule, sNot, sOf, sOr, sPacked, sProc, sRecord, |
| 29 | + sRepeat, sReturn, sSet, sShl, sShr, sThen, sTo, sType, sUntil, sVar, |
| 30 | + sVariant, sVolatile, sWhile, sXor, |
| 31 | + sEof); |
| 32 | + |
| 33 | + (* Where the scanner is. Importing a module compiles it in the middle |
| 34 | + of another, so the parser saves this and restores it afterwards. *) |
| 35 | + State* = record |
| 36 | + buf: ^U8; |
| 37 | + len, pos, line, lineStart: INT; |
| 38 | + tokPos, tokLine, tokLineStart: INT; |
| 39 | + prevEnd: INT; (* where the symbol before this one ends *) |
| 40 | + tok: Sym; |
| 41 | + ival: I64; |
| 42 | + rval: F64; |
| 43 | + id: INT; |
| 44 | + f: INT; (* the file, as Diag knows it *) |
| 45 | + end; |
| 46 | + |
| 47 | +const |
| 48 | + MaxIdent* = 63; |
| 49 | + MaxStr* = 65536; |
| 50 | + NumWords = 42; |
| 51 | + NumOps = 28; |
| 52 | + words: [NumWords][]U8 = [ |
| 53 | + "and", "begin", "break", "by", "code", "const", "defer", "div", "do", |
| 54 | + "else", "elsif", "end", "extern", "for", "forward", "if", "import", |
| 55 | + "in", "link", "match", "mod", "module", "not", "of", "or", "packed", |
| 56 | + "proc", "record", "repeat", "return", "set", "shl", "shr", "then", |
| 57 | + "to", "type", "until", "var", "variant", "VOLATILE", "while", "xor"]; |
| 58 | + (* sPlus to sBlank *) |
| 59 | + ops: [NumOps][]U8 = [ |
| 60 | + "+", "-", "*", "/", "=", "#", "<", "<=", ">", ">=", |
| 61 | + ":=", ":", ";", ",", ".", "..", "...", |
| 62 | + "(", ")", "[", "]", "{", "}", "^", "&", "~", "|", "_"]; |
| 63 | + PoolChunk = 65536; |
| 64 | + |
| 65 | +var |
| 66 | + tok*: Sym; (* the current symbol *) |
| 67 | + ival*: I64; (* sInt, sChar: its value *) |
| 68 | + rval*: F64; (* sReal: its value *) |
| 69 | + id*: INT; (* sIdent: its name *) |
| 70 | + slen*: INT; (* sString: how many bytes *) |
| 71 | + sbuf: [MaxStr + 1]U8; (* sString: its bytes, then a zero *) |
| 72 | + |
| 73 | + (* Where the parser goes on after an error: the innermost statement or |
| 74 | + declaration being read. nil stops the compiler at the first error. *) |
| 75 | + recovery: ^Jump.Env; |
| 76 | + (* The parser is skipping what it has read once already, after an |
| 77 | + error, so slips in it aren't reported again. *) |
| 78 | + skipping: BOOL; |
| 79 | + |
| 80 | + (* Whether a byte may be in a name: a letter, a digit or _. A table, |
| 81 | + since the loop that reads a name asks it of every byte. *) |
| 82 | + nameChar: [256]BOOL; |
| 83 | + |
| 84 | + cur: State; (* buf .. f; tok, ival, rval and id live above *) |
| 85 | + |
| 86 | + (* The name table: every distinct identifier once, found through an |
| 87 | + open-addressed hash table. A name's bytes are in a chunk of memory |
| 88 | + that is never moved, so that nameOf can hand out a slice of them. *) |
| 89 | + chunk: ^U8; (* where names are put; chunkLeft bytes are left *) |
| 90 | + chunkLeft: INT; |
| 91 | + namePtr: ^NamePtr; |
| 92 | + nameLen, nameHash: ^INT; |
| 93 | + numNames, nameCap: INT; |
| 94 | + table: ^INT; (* name + 1, or 0 for an empty slot *) |
| 95 | + tableMask: INT; |
| 96 | + |
| 97 | +(* Exported variables can't be set from outside, so these do it. *) |
| 98 | +proc setRecovery*(r: ^Jump.Env); |
| 99 | +begin |
| 100 | + recovery := r; |
| 101 | +end; |
| 102 | + |
| 103 | +proc setSkipping*(yes: BOOL); |
| 104 | +begin |
| 105 | + skipping := yes; |
| 106 | +end; |
| 107 | + |
| 108 | +(* The bytes of the string just read, without the zero after them *) |
| 109 | +proc strBytes*: []U8; |
| 110 | +begin |
| 111 | + return sbuf[0..slen]; |
| 112 | +end; |
| 113 | + |
| 114 | +(* Memory, or the end of the compile when there is none *) |
| 115 | +proc grab(size: INT): ^U8; |
| 116 | +var p: ^U8; |
| 117 | +begin |
| 118 | + p := Mem.alloc(Heap.heap, size, 8); |
| 119 | + if p = nil then Diag.fatal("out of memory"); end; |
| 120 | + return p; |
| 121 | +end; |
| 122 | + |
| 123 | +(* n zeroed words *) |
| 124 | +proc words64(n: INT): ^INT; |
| 125 | +var p: ^INT; i: INT; |
| 126 | +begin |
| 127 | + p := ^INT(grab(n * SIZE(INT))); |
| 128 | + for i := 0 to n - 1 do p[i] := 0; end; |
| 129 | + return p; |
| 130 | +end; |
| 131 | + |
| 132 | +(* Names *) |
| 133 | + |
| 134 | +proc hash(p: ^U8; n: INT): INT; |
| 135 | +var h: U32; i: INT; |
| 136 | +begin |
| 137 | + h := 2166136261; |
| 138 | + for i := 0 to n - 1 do h := (h xor U32(p[i])) * 16777619; end; |
| 139 | + return INT(h & 7FFFFFFFH); |
| 140 | +end; |
| 141 | + |
| 142 | +(* A table twice the size, and room for twice the names *) |
| 143 | +proc grow; |
| 144 | +var i, n, j: INT; ptrs: ^NamePtr; lens, hashes: ^INT; |
| 145 | +begin |
| 146 | + Mem.free(Heap.heap, ^U8(table)); |
| 147 | + tableMask := tableMask * 2 + 1; |
| 148 | + table := words64(tableMask + 1); |
| 149 | + for n := 0 to numNames - 1 do |
| 150 | + i := nameHash[n] & tableMask; |
| 151 | + while table[i] # 0 do i := (i + 1) & tableMask; end; |
| 152 | + table[i] := n + 1; |
| 153 | + end; |
| 154 | + j := nameCap * 2; |
| 155 | + ptrs := ^NamePtr(grab(j * SIZE(NamePtr))); |
| 156 | + lens := ^INT(grab(j * SIZE(INT))); |
| 157 | + hashes := ^INT(grab(j * SIZE(INT))); |
| 158 | + for n := 0 to numNames - 1 do |
| 159 | + ptrs[n] := namePtr[n]; |
| 160 | + lens[n] := nameLen[n]; |
| 161 | + hashes[n] := nameHash[n]; |
| 162 | + end; |
| 163 | + Mem.free(Heap.heap, ^U8(namePtr)); |
| 164 | + Mem.free(Heap.heap, ^U8(nameLen)); |
| 165 | + Mem.free(Heap.heap, ^U8(nameHash)); |
| 166 | + namePtr := ptrs; |
| 167 | + nameLen := lens; |
| 168 | + nameHash := hashes; |
| 169 | + nameCap := j; |
| 170 | +end; |
| 171 | + |
| 172 | +(* Whether the n bytes at p and at q are the same *) |
| 173 | +proc same(p, q: ^U8; n: INT): BOOL; |
| 174 | +var i: INT; |
| 175 | +begin |
| 176 | + for i := 0 to n - 1 do |
| 177 | + if p[i] # q[i] then return false; end; |
| 178 | + end; |
| 179 | + return true; |
| 180 | +end; |
| 181 | + |
| 182 | +(* The number of the name of the n bytes at p; a new name gets the next *) |
| 183 | +proc intern*(p: ^U8; n: INT): INT; |
| 184 | +var h, i, k: INT; |
| 185 | +begin |
| 186 | + h := hash(p, n); |
| 187 | + i := h & tableMask; |
| 188 | + while table[i] # 0 do |
| 189 | + k := table[i] - 1; |
| 190 | + if (nameHash[k] = h) and (nameLen[k] = n) and same(namePtr[k], p, n) then |
| 191 | + return k; |
| 192 | + end; |
| 193 | + i := (i + 1) & tableMask; |
| 194 | + end; |
| 195 | + if n > chunkLeft then |
| 196 | + chunk := grab(PoolChunk); |
| 197 | + chunkLeft := PoolChunk; |
| 198 | + end; |
| 199 | + COPY(chunk[0..n], p[0..n]); |
| 200 | + k := numNames; |
| 201 | + namePtr[k] := chunk; |
| 202 | + nameLen[k] := n; |
| 203 | + nameHash[k] := h; |
| 204 | + chunk := chunk + n; |
| 205 | + chunkLeft := chunkLeft - n; |
| 206 | + INC(numNames); |
| 207 | + table[i] := k + 1; |
| 208 | + if numNames * 2 > tableMask then grow; end; |
| 209 | + return k; |
| 210 | +end; |
| 211 | + |
| 212 | +(* The name n, which stays as it is *) |
| 213 | +proc nameOf*(n: INT): []U8; |
| 214 | +begin |
| 215 | + return namePtr[n][0..nameLen[n]]; |
| 216 | +end; |
| 217 | + |
| 218 | +(* Errors *) |
| 219 | + |
| 220 | +(* The current symbol *) |
| 221 | +proc tokPos*: Diag.Pos; |
| 222 | +begin |
| 223 | + return {f: cur.f, a: cur.tokPos, b: cur.pos}; |
| 224 | +end; |
| 225 | + |
| 226 | +(* The place just after the symbol before the current one, where a |
| 227 | + missing symbol belongs *) |
| 228 | +proc afterPrev*: Diag.Pos; |
| 229 | +begin |
| 230 | + return {f: cur.f, a: cur.prevEnd, b: cur.prevEnd}; |
| 231 | +end; |
| 232 | + |
| 233 | +(* Where to say a symbol is missing: at the current symbol, unless it |
| 234 | + starts a later line than the symbol before it, which is more likely |
| 235 | + where the missing one belongs *) |
| 236 | +proc missingPos*: Diag.Pos; |
| 237 | +var i: INT; p: Diag.Pos; |
| 238 | +begin |
| 239 | + p := tokPos(); |
| 240 | + for i := cur.prevEnd to cur.tokPos - 1 do |
| 241 | + if cur.buf[i] = '\n' then p := afterPrev(); end; |
| 242 | + end; |
| 243 | + return p; |
| 244 | +end; |
| 245 | + |
| 246 | +(* From the start of p to the end of the symbol before the current one: |
| 247 | + the whole of a construct that has just been read *) |
| 248 | +proc spanFrom*(p: Diag.Pos): Diag.Pos; |
| 249 | +begin |
| 250 | + p.b := cur.prevEnd; |
| 251 | + return p; |
| 252 | +end; |
| 253 | + |
| 254 | +proc curLine*: INT; |
| 255 | +begin |
| 256 | + return cur.tokLine; |
| 257 | +end; |
| 258 | + |
| 259 | +(* The line and column of byte a of the file, at or before the current |
| 260 | + symbol: counted back from the symbol's line, so that it costs only |
| 261 | + the distance. A column counts characters, as Diag's do. *) |
| 262 | +proc lineCol*(a: INT): (INT, INT); |
| 263 | +var i, line, start, col: INT; |
| 264 | +begin |
| 265 | + line := cur.tokLine; |
| 266 | + start := cur.tokLineStart; |
| 267 | + while a < start do |
| 268 | + DEC(line); |
| 269 | + DEC(start); |
| 270 | + while (start > 0) and (cur.buf[start - 1] # '\n') do DEC(start); end; |
| 271 | + end; |
| 272 | + col := 1; |
| 273 | + for i := start to a - 1 do |
| 274 | + if (cur.buf[i] & 0C0H) # 80H then INC(col); end; |
| 275 | + end; |
| 276 | + return line, col; |
| 277 | +end; |
| 278 | + |
| 279 | +proc curCol*: INT; |
| 280 | +var col: INT; |
| 281 | +begin |
| 282 | + _, col := lineCol(cur.tokPos); |
| 283 | + return col; |
| 284 | +end; |
| 285 | + |
| 286 | +(* After an error in the parser's construct, reported with its notes |
| 287 | + and help: the parser goes on from the recovery point. *) |
| 288 | +proc fail*; |
| 289 | +begin |
| 290 | + if recovery = nil then Diag.finish; end; |
| 291 | + Jump.jump(recovery); |
| 292 | +end; |
| 293 | + |
| 294 | +(* An error at p: reported, then the parser goes on from its recovery |
| 295 | + point. *) |
| 296 | +proc markAt*(p: Diag.Pos; msg: []U8); |
| 297 | +begin |
| 298 | + Diag.error(p, msg); |
| 299 | + fail; |
| 300 | +end; |
| 301 | + |
| 302 | +proc mark*(msg: []U8); |
| 303 | +begin |
| 304 | + markAt(tokPos(), msg); |
| 305 | +end; |
| 306 | + |
| 307 | +(* markAt, with a line of help after the message. *) |
| 308 | +proc markHelp*(p: Diag.Pos; msg, hint: []U8); |
| 309 | +begin |
| 310 | + Diag.error(p, msg); |
| 311 | + Diag.help(hint); |
| 312 | + fail; |
| 313 | +end; |
| 314 | + |
| 315 | +(* An error in the text at bytes a to b of the file being read, which |
| 316 | + stops the compiler: past it, the symbols can't be trusted. *) |
| 317 | +proc markBytes(a, b: INT; msg: []U8); |
| 318 | +begin |
| 319 | + Diag.setQuiet(false); |
| 320 | + Diag.error({f: cur.f, a: a, b: b}, msg); |
| 321 | + Diag.finish; |
| 322 | +end; |
| 323 | + |
| 324 | +proc lexError(msg: []U8); |
| 325 | +begin |
| 326 | + markBytes(cur.tokPos, cur.pos, msg); |
| 327 | +end; |
| 328 | + |
| 329 | +(* The n characters at the scanner are how another language writes t: |
| 330 | + an error, but reading goes on as if t were written. *) |
| 331 | +proc slip(n: INT; t: Sym; msg: []U8); |
| 332 | +begin |
| 333 | + if not skipping then Diag.error({f: cur.f, a: cur.pos, b: cur.pos + n}, msg); end; |
| 334 | + INC(cur.pos, n); |
| 335 | + tok := t; |
| 336 | +end; |
| 337 | + |
| 338 | +(* How an operator or reserved word is written, quoted, for messages. *) |
| 339 | +proc symText*(s: Sym): []U8; |
| 340 | +begin |
| 341 | + if (s >= sPlus) and (s <= sBlank) then |
| 342 | + return Diag.say("\"{}\"", [Fmt.str(ops[INT(s) - INT(sPlus)])]); |
| 343 | + end; |
| 344 | + return Diag.say("\"{}\"", [Fmt.str(words[INT(s) - INT(sAnd)])]); |
| 345 | +end; |
| 346 | + |
| 347 | +(* The current symbol, for "expected ..., found ...": a name as it is, |
| 348 | + a literal by its kind, an operator or reserved word quoted. *) |
| 349 | +proc found*: []U8; |
| 350 | +begin |
| 351 | + match tok of |
| 352 | + | sIdent: return nameOf(id); |
| 353 | + | sInt: return "a number"; |
| 354 | + | sReal: return "a number"; |
| 355 | + | sChar: return "a character"; |
| 356 | + | sString: return "a string"; |
| 357 | + | sEof: return "the end of the file"; |
| 358 | + | else: return symText(tok); |
| 359 | + end; |
| 360 | +end; |
| 361 | + |
| 362 | +(* The current symbol is the first on its line: at the top level of a |
| 363 | + module, by the usual layout, so where reading can start again. *) |
| 364 | +proc atLineStart*: BOOL; |
| 365 | +begin |
| 366 | + return cur.tokPos = cur.tokLineStart; |
| 367 | +end; |
| 368 | + |
| 369 | +(* The column the current symbol's line is indented to *) |
| 370 | +proc indent*: INT; |
| 371 | +var i: INT; |
| 372 | +begin |
| 373 | + i := cur.tokLineStart; |
| 374 | + while (cur.buf[i] = ' ') or (cur.buf[i] = '\t') do INC(i); end; |
| 375 | + return i - cur.tokLineStart + 1; |
| 376 | +end; |
| 377 | + |
| 378 | +(* Files *) |
| 379 | + |
| 380 | +(* Name n is a reserved word *) |
| 381 | +proc reserved*(n: INT): BOOL; |
| 382 | +begin |
| 383 | + return n < NumWords; |
| 384 | +end; |
| 385 | + |
| 386 | +(* Reads the file at path to scan it; "" if it can, else why not *) |
| 387 | +proc open*(path: []U8): []U8; |
| 388 | +var fd, n, got, r, err: INT; info: Sys.Info; |
| 389 | +begin |
| 390 | + info, err := Sys.info(path); |
| 391 | + if err = -2 then return "there is no such file"; end; |
| 392 | + if err = -13 then return "permission denied"; end; |
| 393 | + if err < 0 then return "it can't be opened"; end; |
| 394 | + if not info.file then return "it isn't a file"; end; |
| 395 | + fd := Sys.openRead(path); |
| 396 | + if fd = -2 then return "there is no such file"; end; |
| 397 | + if fd = -13 then return "permission denied"; end; |
| 398 | + if fd < 0 then return "it can't be opened"; end; |
| 399 | + n := INT(info.size); |
| 400 | + cur.buf := grab(n + 1); |
| 401 | + got := 0; |
| 402 | + while got < n do |
| 403 | + r := Sys.read(fd, cur.buf[got..n]); |
| 404 | + if r <= 0 then Diag.fatal(Diag.say("can't read {}", [Fmt.str(path)])); end; |
| 405 | + got := got + r; |
| 406 | + end; |
| 407 | + Sys.close(fd); |
| 408 | + cur.buf[n] := 0; (* the sentinel that ends every scan *) |
| 409 | + cur.len := n; |
| 410 | + cur.pos := 0; |
| 411 | + cur.line := 1; |
| 412 | + cur.lineStart := 0; |
| 413 | + cur.tokPos := 0; |
| 414 | + cur.tokLine := 1; |
| 415 | + cur.tokLineStart := 0; |
| 416 | + cur.f := Diag.addFile(path, cur.buf, n); |
| 417 | + return ""; |
| 418 | +end; |
| 419 | + |
| 420 | +proc saveScan*(var st: State); |
| 421 | +begin |
| 422 | + st := cur; |
| 423 | + st.tok := tok; |
| 424 | + st.ival := ival; |
| 425 | + st.rval := rval; |
| 426 | + st.id := id; |
| 427 | +end; |
| 428 | + |
| 429 | +proc restoreScan*(st: State); |
| 430 | +begin |
| 431 | + cur := st; |
| 432 | + tok := st.tok; |
| 433 | + ival := st.ival; |
| 434 | + rval := st.rval; |
| 435 | + id := st.id; |
| 436 | +end; |
| 437 | + |
| 438 | +(* UTF-8 *) |
| 439 | + |
| 440 | +(* Decodes the character at buf[pos], which is not ASCII, checking that |
| 441 | + it is well formed; returns its code point and moves pos past it. *) |
| 442 | +proc utf8(var pos: INT): INT; |
| 443 | +var b0, b1, n, i, cp: INT; lo, hi: INT; |
| 444 | +begin |
| 445 | + b0 := INT(cur.buf[pos]); |
| 446 | + lo := 80H; |
| 447 | + hi := 0BFH; |
| 448 | + n := 0; |
| 449 | + cp := 0; |
| 450 | + match b0 of |
| 451 | + | 0C2H..0DFH: n := 1; cp := b0 & 1FH; |
| 452 | + | 0E0H: n := 2; lo := 0A0H; |
| 453 | + | 0E1H..0ECH, 0EEH, 0EFH: n := 2; cp := b0 & 0FH; |
| 454 | + | 0EDH: n := 2; cp := 0DH; hi := 9FH; (* no surrogates *) |
| 455 | + | 0F0H: n := 3; lo := 90H; |
| 456 | + | 0F1H..0F3H: n := 3; cp := b0 & 7; |
| 457 | + | 0F4H: n := 3; cp := 4; hi := 8FH; (* up to 10FFFFH *) |
| 458 | + | else: |
| 459 | + cur.tokPos := pos; |
| 460 | + lexError("invalid UTF-8"); |
| 461 | + end; |
| 462 | + for i := 1 to n do |
| 463 | + b1 := INT(cur.buf[pos + i]); |
| 464 | + if (b1 < lo) or (b1 > hi) or (pos + i >= cur.len) then |
| 465 | + cur.tokPos := pos; |
| 466 | + lexError("bad UTF-8"); |
| 467 | + end; |
| 468 | + cp := cp shl 6 | (b1 & 3FH); |
| 469 | + lo := 80H; |
| 470 | + hi := 0BFH; |
| 471 | + end; |
| 472 | + pos := pos + n + 1; |
| 473 | + return cp; |
| 474 | +end; |
| 475 | + |
| 476 | +proc put(b: INT); |
| 477 | +begin |
| 478 | + if slen >= MaxStr then |
| 479 | + lexError(Diag.say("a string holds at most {} bytes", [Fmt.int(MaxStr)])); |
| 480 | + end; |
| 481 | + sbuf[slen] := U8(b); |
| 482 | + INC(slen); |
| 483 | +end; |
| 484 | + |
| 485 | +proc putUtf8(cp: INT); |
| 486 | +begin |
| 487 | + if cp < 80H then |
| 488 | + put(cp); |
| 489 | + elsif cp < 800H then |
| 490 | + put(0C0H | cp shr 6); put(80H | cp & 3FH); |
| 491 | + elsif cp < 10000H then |
| 492 | + put(0E0H | cp shr 12); put(80H | cp shr 6 & 3FH); put(80H | cp & 3FH); |
| 493 | + else |
| 494 | + put(0F0H | cp shr 18); put(80H | cp shr 12 & 3FH); |
| 495 | + put(80H | cp shr 6 & 3FH); put(80H | cp & 3FH); |
| 496 | + end; |
| 497 | +end; |
| 498 | + |
| 499 | +proc isDigit(c: U8): BOOL; |
| 500 | +begin |
| 501 | + return (c >= '0') and (c <= '9'); |
| 502 | +end; |
| 503 | + |
| 504 | +proc isLetter(c: U8): BOOL; |
| 505 | +begin |
| 506 | + return (c >= 'a') and (c <= 'z') or (c >= 'A') and (c <= 'Z') or (c = '_'); |
| 507 | +end; |
| 508 | + |
| 509 | +proc hexVal(c: U8): INT; |
| 510 | +begin |
| 511 | + if isDigit(c) then return INT(c) - '0'; |
| 512 | + elsif (c >= 'A') and (c <= 'F') then return INT(c) - 'A' + 10; |
| 513 | + elsif (c >= 'a') and (c <= 'f') then return INT(c) - 'a' + 10; |
| 514 | + end; |
| 515 | + return -1; |
| 516 | +end; |
| 517 | + |
| 518 | +(* The text of the file from byte a up to but not including b *) |
| 519 | +proc text(a, b: INT): []U8; |
| 520 | +begin |
| 521 | + return Diag.source({f: cur.f, a: a, b: b}); |
| 522 | +end; |
| 523 | + |
| 524 | +(* One character of a character or string literal, at buf[pos]: its code |
| 525 | + point, or for \xHH, minus one minus the byte. *) |
| 526 | +proc litChar(inString: BOOL): INT; |
| 527 | +var c: U8; v, d, n, esc, digits: INT; |
| 528 | +begin |
| 529 | + c := cur.buf[cur.pos]; |
| 530 | + esc := cur.pos; |
| 531 | + v := 0; |
| 532 | + if c = '\\' then |
| 533 | + INC(cur.pos); |
| 534 | + c := cur.buf[cur.pos]; |
| 535 | + INC(cur.pos); |
| 536 | + match c of |
| 537 | + | 'n': v := 10; |
| 538 | + | 't': v := 9; |
| 539 | + | 'r': v := 13; |
| 540 | + | '0': v := 0; |
| 541 | + | '\\', '\'', '"': v := INT(c); |
| 542 | + | 'x': |
| 543 | + if not inString then markBytes(esc, cur.pos, "\\x is only for strings"); end; |
| 544 | + d := hexVal(cur.buf[cur.pos]); |
| 545 | + n := hexVal(cur.buf[cur.pos + 1]); |
| 546 | + if (d < 0) or (n < 0) then markBytes(esc, cur.pos + 2, "\\x needs two hex digits"); end; |
| 547 | + INC(cur.pos, 2); |
| 548 | + v := -1 - (d * 16 + n); |
| 549 | + | 'u': |
| 550 | + if cur.buf[cur.pos] # '{' then markBytes(esc, cur.pos, "expected \"{\" after \\u"); end; |
| 551 | + INC(cur.pos); |
| 552 | + digits := cur.pos; |
| 553 | + v := 0; |
| 554 | + n := 0; |
| 555 | + while hexVal(cur.buf[cur.pos]) >= 0 do |
| 556 | + v := v * 16 + hexVal(cur.buf[cur.pos]); |
| 557 | + INC(n); |
| 558 | + INC(cur.pos); |
| 559 | + if n > 6 then markBytes(esc, cur.pos, "a code point has at most 6 hex digits"); end; |
| 560 | + end; |
| 561 | + if (n = 0) or (cur.buf[cur.pos] # '}') then |
| 562 | + markBytes(esc, cur.pos, "expected hex digits and \"}\" after \\u{"); |
| 563 | + end; |
| 564 | + INC(cur.pos); |
| 565 | + if (v > 10FFFFH) or (v >= 0D800H) and (v <= 0DFFFH) then |
| 566 | + markBytes(esc, cur.pos, Diag.say("{} isn't a code point", [Fmt.str(text(digits, cur.pos - 1))])); |
| 567 | + end; |
| 568 | + | else: |
| 569 | + markBytes(esc, cur.pos, Diag.say("unknown escape \\{}", [Fmt.char(c)])); |
| 570 | + end; |
| 571 | + elsif c >= 80H then |
| 572 | + v := utf8(cur.pos); |
| 573 | + elsif (c < ' ') or (c = 127) then |
| 574 | + if inString then lexError("string not closed"); else lexError("character not closed"); end; |
| 575 | + else |
| 576 | + v := INT(c); |
| 577 | + INC(cur.pos); |
| 578 | + end; |
| 579 | + return v; |
| 580 | +end; |
| 581 | + |
| 582 | +(* Symbols *) |
| 583 | + |
| 584 | +proc comment; |
| 585 | +var depth, open: INT; c: U8; |
| 586 | +begin |
| 587 | + depth := 1; |
| 588 | + open := cur.pos; |
| 589 | + INC(cur.pos, 2); |
| 590 | + repeat |
| 591 | + c := cur.buf[cur.pos]; |
| 592 | + if c = 0 then |
| 593 | + if cur.pos >= cur.len then markBytes(open, open + 2, "comment not closed"); end; |
| 594 | + INC(cur.pos); |
| 595 | + elsif (c = '(') and (cur.buf[cur.pos + 1] = '*') then |
| 596 | + INC(depth); |
| 597 | + INC(cur.pos, 2); |
| 598 | + elsif (c = '*') and (cur.buf[cur.pos + 1] = ')') then |
| 599 | + DEC(depth); |
| 600 | + INC(cur.pos, 2); |
| 601 | + elsif c = '\n' then |
| 602 | + INC(cur.pos); |
| 603 | + INC(cur.line); |
| 604 | + cur.lineStart := cur.pos; |
| 605 | + elsif c >= 80H then |
| 606 | + _ := utf8(cur.pos); |
| 607 | + else |
| 608 | + INC(cur.pos); |
| 609 | + end; |
| 610 | + until depth = 0; |
| 611 | +end; |
| 612 | + |
| 613 | +(* Reading a decimal real exactly: the F64 nearest to digits times 10 to |
| 614 | + an exponent, ties to the even one, which a library's reader doesn't |
| 615 | + promise. The number is a fraction of two big integers, num / den, |
| 616 | + scaled by 2^e until its whole part has 53 or 54 bits; that part and |
| 617 | + the remainder give the bits and the rounding. *) |
| 618 | +type |
| 619 | + Big = record |
| 620 | + n: INT; (* words in use; 0 is zero *) |
| 621 | + w: [96]U32; (* least significant first *) |
| 622 | + end; |
| 623 | +(* 96 words, 3072 bits, are plenty: a string holds at most 255 |
| 624 | + digits, so readReal's den is at most 10^(330 + 255), about 2^1944, |
| 625 | + and num below 10^310; the shift that follows makes the larger one at |
| 626 | + most 54 bits longer than den, about 2000 bits in all. *) |
| 627 | + |
| 628 | +(* a := a * m + add *) |
| 629 | +proc bigMul(var a: Big; m, add: U32); |
| 630 | +var i: INT; c: U64; |
| 631 | +begin |
| 632 | + c := U64(add); |
| 633 | + for i := 0 to a.n - 1 do |
| 634 | + c := U64(a.w[i]) * U64(m) + c; |
| 635 | + a.w[i] := U32(c); |
| 636 | + c := c shr 32; |
| 637 | + end; |
| 638 | + if c # 0 then |
| 639 | + a.w[a.n] := U32(c); |
| 640 | + INC(a.n); |
| 641 | + end; |
| 642 | +end; |
| 643 | + |
| 644 | +proc bigShl(var a: Big; k: INT); |
| 645 | +var i, ws, bits: INT; |
| 646 | +begin |
| 647 | + if a.n = 0 then return; end; |
| 648 | + ws := k div 32; |
| 649 | + bits := k mod 32; |
| 650 | + a.w[a.n + ws] := 0; |
| 651 | + for i := a.n - 1 to 0 by -1 do |
| 652 | + if bits > 0 then a.w[i + ws + 1] := a.w[i + ws + 1] | (a.w[i] shr (32 - bits)); end; |
| 653 | + a.w[i + ws] := a.w[i] shl bits; |
| 654 | + end; |
| 655 | + for i := 0 to ws - 1 do a.w[i] := 0; end; |
| 656 | + INC(a.n, ws + 1); |
| 657 | + while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end; |
| 658 | +end; |
| 659 | + |
| 660 | +proc bigShr1(var a: Big); |
| 661 | +var i: INT; |
| 662 | +begin |
| 663 | + for i := 0 to a.n - 1 do |
| 664 | + a.w[i] := a.w[i] shr 1; |
| 665 | + if i + 1 < a.n then a.w[i] := a.w[i] | (a.w[i + 1] shl 31); end; |
| 666 | + end; |
| 667 | + while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end; |
| 668 | +end; |
| 669 | + |
| 670 | +(* -1, 0 or 1 as a is below, equal to or above b *) |
| 671 | +proc bigCmp(a, b: Big): INT; |
| 672 | +var i: INT; |
| 673 | +begin |
| 674 | + if a.n # b.n then |
| 675 | + if a.n > b.n then return 1; end; |
| 676 | + return -1; |
| 677 | + end; |
| 678 | + for i := a.n - 1 to 0 by -1 do |
| 679 | + if a.w[i] # b.w[i] then |
| 680 | + if a.w[i] > b.w[i] then return 1; end; |
| 681 | + return -1; |
| 682 | + end; |
| 683 | + end; |
| 684 | + return 0; |
| 685 | +end; |
| 686 | + |
| 687 | +(* a := a - b, b not above a *) |
| 688 | +proc bigSub(var a: Big; b: Big); |
| 689 | +var i: INT; d: I64; |
| 690 | +begin |
| 691 | + d := 0; |
| 692 | + for i := 0 to a.n - 1 do |
| 693 | + d := d + I64(a.w[i]); |
| 694 | + if i < b.n then d := d - I64(b.w[i]); end; |
| 695 | + a.w[i] := U32(d); |
| 696 | + if d < 0 then d := -1; else d := 0; end; (* a borrow *) |
| 697 | + end; |
| 698 | + while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end; |
| 699 | +end; |
| 700 | + |
| 701 | +proc bigBits(a: Big): INT; |
| 702 | +var t: U32; bits: INT; |
| 703 | +begin |
| 704 | + if a.n = 0 then return 0; end; |
| 705 | + bits := (a.n - 1) * 32; |
| 706 | + t := a.w[a.n - 1]; |
| 707 | + while t # 0 do |
| 708 | + INC(bits); |
| 709 | + t := t shr 1; |
| 710 | + end; |
| 711 | + return bits; |
| 712 | +end; |
| 713 | + |
| 714 | +(* s is digits, a point, digits, and maybe E, a sign and digits. False if |
| 715 | + the value is past the largest F64; one below the smallest is 0. *) |
| 716 | +proc readReal(s: []U8): (F64, BOOL); |
| 717 | +var |
| 718 | + num, den, d: Big; i, digits, e, ex: INT; neg, half, sticky: BOOL; |
| 719 | + q, b: U64; x: F64; bits: ^F64; |
| 720 | +begin |
| 721 | + num.n := 0; |
| 722 | + den.n := 1; |
| 723 | + den.w[0] := 1; |
| 724 | + digits := 0; |
| 725 | + e := 0; |
| 726 | + i := 0; |
| 727 | + while (i < LEN(s)) and (s[i] # 'E') do |
| 728 | + if s[i] = '.' then |
| 729 | + e := 1; |
| 730 | + else |
| 731 | + bigMul(num, 10, U32(INT(s[i]) - '0')); |
| 732 | + if num.n > 0 then INC(digits); end; (* counted from the first that isn't 0 *) |
| 733 | + if e > 0 then INC(e); end; |
| 734 | + end; |
| 735 | + INC(i); |
| 736 | + end; |
| 737 | + if e > 0 then e := 1 - e; end; (* minus the digits after the point *) |
| 738 | + ex := 0; |
| 739 | + neg := false; |
| 740 | + if i < LEN(s) then |
| 741 | + INC(i); |
| 742 | + neg := s[i] = '-'; |
| 743 | + if (s[i] = '+') or (s[i] = '-') then INC(i); end; |
| 744 | + while i < LEN(s) do |
| 745 | + if ex < 100000 then ex := ex * 10 + INT(s[i]) - '0'; end; |
| 746 | + INC(i); |
| 747 | + end; |
| 748 | + end; |
| 749 | + if neg then ex := -ex; end; |
| 750 | + INC(e, ex); |
| 751 | + x := 0.0; |
| 752 | + if num.n = 0 then return x, true; end; |
| 753 | + if digits + e > 310 then return x, false; end; (* 10^310 is past every F64 *) |
| 754 | + if digits + e < -330 then return x, true; end; (* and 10^-330 below half the least *) |
| 755 | + for i := 1 to e do bigMul(num, 10, 0); end; |
| 756 | + for i := 1 to -e do bigMul(den, 10, 0); end; |
| 757 | + (* num / den 2^-e from 2^52 to 2^54, but not past the least exponent *) |
| 758 | + e := bigBits(num) - bigBits(den) - 53; |
| 759 | + if e < -1074 then e := -1074; end; |
| 760 | + if e < 0 then bigShl(num, -e); else bigShl(den, e); end; |
| 761 | + (* q, its whole part, by long division; num is left the remainder *) |
| 762 | + d := den; |
| 763 | + bigShl(d, 53); |
| 764 | + q := 0; |
| 765 | + for i := 53 to 0 by -1 do |
| 766 | + if bigCmp(num, d) >= 0 then |
| 767 | + bigSub(num, d); |
| 768 | + q := q | (U64(1) shl i); |
| 769 | + end; |
| 770 | + bigShr1(d); |
| 771 | + end; |
| 772 | + if q >= U64(1) shl 53 then (* a bit too many: it rounds *) |
| 773 | + half := (q & 1) # 0; |
| 774 | + sticky := num.n > 0; |
| 775 | + q := q shr 1; |
| 776 | + INC(e); |
| 777 | + else (* the remainder against half of den *) |
| 778 | + bigShl(num, 1); |
| 779 | + i := bigCmp(num, den); |
| 780 | + half := i >= 0; |
| 781 | + sticky := i > 0; |
| 782 | + end; |
| 783 | + if half and (sticky or ((q & 1) # 0)) then |
| 784 | + INC(q); |
| 785 | + if q = U64(1) shl 53 then |
| 786 | + q := q shr 1; |
| 787 | + INC(e); |
| 788 | + end; |
| 789 | + end; |
| 790 | + if e > 971 then return x, false; end; |
| 791 | + if q >= U64(1) shl 52 then |
| 792 | + b := U64(e + 1075) shl 52 | (q & (U64(1) shl 52 - 1)); |
| 793 | + else |
| 794 | + b := q; (* below the least normal, e is -1074 *) |
| 795 | + end; |
| 796 | + bits := ^F64(ADR(b)); |
| 797 | + return bits^, true; |
| 798 | +end; |
| 799 | + |
| 800 | +proc number; |
| 801 | +var start, i, d: INT; allDec, ok: BOOL; v: U64; |
| 802 | +begin |
| 803 | + start := cur.pos; |
| 804 | + allDec := true; |
| 805 | + while isDigit(cur.buf[cur.pos]) or (cur.buf[cur.pos] >= 'A') and (cur.buf[cur.pos] <= 'F') do |
| 806 | + if cur.buf[cur.pos] > '9' then allDec := false; end; |
| 807 | + INC(cur.pos); |
| 808 | + end; |
| 809 | + if cur.buf[cur.pos] = 'H' then |
| 810 | + v := 0; |
| 811 | + for i := start to cur.pos - 1 do |
| 812 | + if v shr 60 # 0 then lexError(Diag.say("{} is too big for U64", [Fmt.str(text(start, cur.pos))])); end; |
| 813 | + v := v * 16 + U64(hexVal(cur.buf[i])); |
| 814 | + end; |
| 815 | + INC(cur.pos); |
| 816 | + tok := sInt; |
| 817 | + ival := I64(v); (* 16 digits are the bits of a 64-bit word *) |
| 818 | + elsif (cur.buf[cur.pos] = '.') and isDigit(cur.buf[cur.pos + 1]) then |
| 819 | + if not allDec then lexError("a decimal number has only the digits 0 to 9"); end; |
| 820 | + INC(cur.pos); |
| 821 | + while isDigit(cur.buf[cur.pos]) do INC(cur.pos); end; |
| 822 | + if cur.buf[cur.pos] = 'E' then |
| 823 | + INC(cur.pos); |
| 824 | + if (cur.buf[cur.pos] = '+') or (cur.buf[cur.pos] = '-') then INC(cur.pos); end; |
| 825 | + if not isDigit(cur.buf[cur.pos]) then lexError("expected a digit in the exponent"); end; |
| 826 | + while isDigit(cur.buf[cur.pos]) do INC(cur.pos); end; |
| 827 | + end; |
| 828 | + if cur.pos - start > 255 then lexError("a number has at most 255 characters"); end; |
| 829 | + rval, ok := readReal(cur.buf[start..cur.pos]); |
| 830 | + if not ok then lexError(Diag.say("{} is out of the range of F64", [Fmt.str(text(start, cur.pos))])); end; |
| 831 | + tok := sReal; |
| 832 | + else |
| 833 | + if not allDec then lexError("a hex number ends with H"); end; |
| 834 | + v := 0; |
| 835 | + for i := start to cur.pos - 1 do |
| 836 | + d := INT(cur.buf[i]) - '0'; |
| 837 | + if v > (U64(9223372036854775807) - U64(d)) div 10 then |
| 838 | + lexError(Diag.say("{} is too big for I64", [Fmt.str(text(start, cur.pos))])); |
| 839 | + end; |
| 840 | + v := v * 10 + U64(d); |
| 841 | + end; |
| 842 | + tok := sInt; |
| 843 | + ival := I64(v); |
| 844 | + end; |
| 845 | + if nameChar[cur.buf[cur.pos]] then |
| 846 | + lexError("a name can't start with a digit"); |
| 847 | + end; |
| 848 | +end; |
| 849 | + |
| 850 | +(* Reads the next symbol *) |
| 851 | +proc next*; |
| 852 | +var c: U8; start, h, v, i: INT; |
| 853 | +begin |
| 854 | + cur.prevEnd := cur.pos; |
| 855 | + repeat |
| 856 | + c := cur.buf[cur.pos]; |
| 857 | + while (c = ' ') or (c = '\t') or (c = '\r') or (c = '\n') do |
| 858 | + if c = '\n' then |
| 859 | + INC(cur.line); |
| 860 | + cur.lineStart := cur.pos + 1; |
| 861 | + end; |
| 862 | + INC(cur.pos); |
| 863 | + c := cur.buf[cur.pos]; |
| 864 | + end; |
| 865 | + if (c = '(') and (cur.buf[cur.pos + 1] = '*') then |
| 866 | + comment; |
| 867 | + c := ' '; |
| 868 | + end; |
| 869 | + until c # ' '; |
| 870 | + cur.tokPos := cur.pos; |
| 871 | + cur.tokLine := cur.line; |
| 872 | + cur.tokLineStart := cur.lineStart; |
| 873 | + match c of |
| 874 | + | 'A'..'Z', 'a'..'z', '_': |
| 875 | + start := cur.pos; |
| 876 | + repeat INC(cur.pos); until not nameChar[cur.buf[cur.pos]]; |
| 877 | + if cur.pos - start > MaxIdent then |
| 878 | + lexError(Diag.say("a name has at most {} characters", [Fmt.int(MaxIdent)])); |
| 879 | + end; |
| 880 | + h := intern(cur.buf + start, cur.pos - start); |
| 881 | + if (cur.pos - start = 1) and (c = '_') then |
| 882 | + tok := sBlank; |
| 883 | + elsif h < NumWords then |
| 884 | + tok := Sym(INT(sAnd) + h); |
| 885 | + else |
| 886 | + tok := sIdent; |
| 887 | + id := h; |
| 888 | + end; |
| 889 | + | '0'..'9': number; |
| 890 | + | '\'': |
| 891 | + INC(cur.pos); |
| 892 | + if cur.buf[cur.pos] = '\'' then lexError("empty character"); end; |
| 893 | + v := litChar(false); |
| 894 | + if cur.buf[cur.pos] # '\'' then |
| 895 | + (* 'ab': several characters, as some languages write a string *) |
| 896 | + i := cur.pos; |
| 897 | + while (cur.buf[i] # '\'') and (cur.buf[i] # '\n') and (cur.buf[i] # 0) do INC(i); end; |
| 898 | + if cur.buf[i] = '\'' then |
| 899 | + markBytes(cur.tokPos, i + 1, |
| 900 | + Diag.say("a character is one character: the string is \"{}\"", [Fmt.str(text(cur.tokPos + 1, i))])); |
| 901 | + else |
| 902 | + lexError("character not closed"); |
| 903 | + end; |
| 904 | + end; |
| 905 | + INC(cur.pos); |
| 906 | + tok := sChar; |
| 907 | + ival := I64(v); |
| 908 | + | '"': |
| 909 | + INC(cur.pos); |
| 910 | + slen := 0; |
| 911 | + while cur.buf[cur.pos] # '"' do |
| 912 | + v := litChar(true); |
| 913 | + if v < 0 then put(-1 - v); else putUtf8(v); end; |
| 914 | + end; |
| 915 | + INC(cur.pos); |
| 916 | + sbuf[slen] := 0; |
| 917 | + tok := sString; |
| 918 | + | '+': INC(cur.pos); tok := sPlus; |
| 919 | + | '-': INC(cur.pos); tok := sMinus; |
| 920 | + | '*': INC(cur.pos); tok := sTimes; |
| 921 | + | '/': INC(cur.pos); tok := sSlash; |
| 922 | + | '=': |
| 923 | + if cur.buf[cur.pos + 1] = '=' then |
| 924 | + slip(2, sEql, "comparing is written =, not =="); |
| 925 | + else |
| 926 | + INC(cur.pos); |
| 927 | + tok := sEql; |
| 928 | + end; |
| 929 | + | '&': |
| 930 | + if cur.buf[cur.pos + 1] = '&' then |
| 931 | + slip(2, sAnd, "\"and\" is written and, not &&"); |
| 932 | + else |
| 933 | + INC(cur.pos); |
| 934 | + tok := sAmp; |
| 935 | + end; |
| 936 | + | '#': INC(cur.pos); tok := sNeq; |
| 937 | + | '<': |
| 938 | + INC(cur.pos); |
| 939 | + if cur.buf[cur.pos] = '=' then INC(cur.pos); tok := sLeq; else tok := sLss; end; |
| 940 | + | '>': |
| 941 | + INC(cur.pos); |
| 942 | + if cur.buf[cur.pos] = '=' then INC(cur.pos); tok := sGeq; else tok := sGtr; end; |
| 943 | + | ':': |
| 944 | + INC(cur.pos); |
| 945 | + if cur.buf[cur.pos] = '=' then INC(cur.pos); tok := sBecomes; else tok := sColon; end; |
| 946 | + | ';': INC(cur.pos); tok := sSemicolon; |
| 947 | + | ',': INC(cur.pos); tok := sComma; |
| 948 | + | '.': |
| 949 | + INC(cur.pos); |
| 950 | + if cur.buf[cur.pos] = '.' then |
| 951 | + INC(cur.pos); |
| 952 | + if cur.buf[cur.pos] = '.' then INC(cur.pos); tok := sEllipsis; else tok := sUpto; end; |
| 953 | + else |
| 954 | + tok := sPeriod; |
| 955 | + end; |
| 956 | + | '(': INC(cur.pos); tok := sLparen; |
| 957 | + | ')': INC(cur.pos); tok := sRparen; |
| 958 | + | '[': INC(cur.pos); tok := sLbrak; |
| 959 | + | ']': INC(cur.pos); tok := sRbrak; |
| 960 | + | '{': INC(cur.pos); tok := sLbrace; |
| 961 | + | '}': INC(cur.pos); tok := sRbrace; |
| 962 | + | '^': INC(cur.pos); tok := sArrow; |
| 963 | + | '|': |
| 964 | + if cur.buf[cur.pos + 1] = '|' then |
| 965 | + slip(2, sOr, "\"or\" is written or, not ||"); |
| 966 | + else |
| 967 | + INC(cur.pos); |
| 968 | + tok := sBar; |
| 969 | + end; |
| 970 | + | '~': INC(cur.pos); tok := sTilde; |
| 971 | + | '!': |
| 972 | + if cur.buf[cur.pos + 1] = '=' then |
| 973 | + slip(2, sNeq, "\"not equal\" is written #, not !="); |
| 974 | + else |
| 975 | + slip(1, sNot, "\"not\" is written not, not !"); |
| 976 | + end; |
| 977 | + | 0: |
| 978 | + if cur.pos < cur.len then markBytes(cur.pos, cur.pos + 1, "NUL byte in the text"); end; |
| 979 | + tok := sEof; |
| 980 | + | else: |
| 981 | + if c >= 80H then lexError("only ASCII outside strings, characters and comments"); end; |
| 982 | + markBytes(cur.pos, cur.pos + 1, Diag.say("unexpected character \"{}\"", [Fmt.char(c)])); |
| 983 | + end; |
| 984 | +end; |
| 985 | + |
| 986 | +var i: INT; |
| 987 | + |
| 988 | +begin |
| 989 | + for i := 0 to 255 do nameChar[i] := isLetter(U8(i)) or isDigit(U8(i)); end; |
| 990 | + chunkLeft := 0; |
| 991 | + nameCap := 1 shl 12; |
| 992 | + namePtr := ^NamePtr(grab(nameCap * SIZE(NamePtr))); |
| 993 | + nameLen := ^INT(grab(nameCap * SIZE(INT))); |
| 994 | + nameHash := ^INT(grab(nameCap * SIZE(INT))); |
| 995 | + tableMask := 2 * nameCap - 1; |
| 996 | + table := words64(tableMask + 1); |
| 997 | + for i := 0 to NumWords - 1 do _ := intern(ADR(words[i][0]), LEN(words[i])); end; |
| 998 | +end Scan. |