#! @file names.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the table of names, which gives every word written in a source an #! ID of its own. The keywords are given theirs first, so a keyword's ID is its #! token kind, and the reserved words and the words the parser knows by ID #! follow them, in the order their lists give. import std::containers; module sakura::analyzer; #! Responsible for keeping and tracking the names of non-punctuator tokens of #! a given, or multiple, source code file(s). #! #! Each name's text is copied into a block of names, so a name outlives the #! source it was first read in, as one an editor replaces does. A block is #! filled but never grown, so a name's text stays where it was copied, and its #! view, by its ID, is good for as long as the table is. A name is found by its #! text through the IDs of its hash, comparing only those whose hash shares its #! top half, and those by their length before their bytes. pub struct names { var views: vector; #! The text of each name, by its ID, where a block holds it. var blocks: vector; #! The blocks the texts are copied into, the last being filled. var index: id_index; #! The names of each hash, which finding a name's ID by its text walks. } #! How many bytes a block of names holds, but for one of a name longer than a #! sixteenth of that, which is a block of its own. const names::block_size: u64 = 16384; #! Makes a new instance of the `names` structure to be used by the various #! stages of the Sakura analyzer. #! @returns The created names instance pre-containing the language keywords. pub def names::new(): names { var made := names { views = vector::::new(), blocks = vector::::new(), index = id_index::new() }; made.views.reserve(512); # The lexer reads a keyword's ID as its token kind, so a list out of step with # `tk` stops here rather than lexing one word as another. for &word in _keywords { val id := made.intern(word.text); expects(id == word.kind as u32); } # The parser knows these words by where they are interned, which a word listed # twice would move. for &word in _reserved { made.intern(word); } for &word in _words { made.intern(word); } # The checker knows these by their members of `word`, which follow those. for &each in _known { expects(made.intern(each.text) == each.id); } expects(made.intern("this") == names::this_id); expects(made.intern("get") == names::get_id); expects(made.intern("set") == names::set_id); expects(made.intern("type_of") == names::type_of_id); expects(made.intern("old") == names::old_id); expects(made.intern("self") == names::self_id); return made; } #! Maps names that are seen in the source code to a specific integer to reduce #! copies and save memory. #! @param this The names instance to copy the name into. #! @param text The name to copy and mark by an ID. #! @returns The newly created ID or an existing ID for the given name. pub def names::intern(&mut this, text: &strview): u32 { val hash := text.hash(); var walk := this.index.walk(hash); var at := this.index.next(&mut walk); while at != 0 { return at - 1 if same_text(this.views[at - 1], text); at = this.index.next(&mut walk); } val id := this.views.length as u32; this.views.append(this.copy_text(text)); this.index.add(hash, id); return id; } # A copy of a name's text in a block, which a new block is made for where the # last has no room, or where the name is long. def names::copy_text(&mut this, text: strview): strview { if text.length * 16 > names::block_size { this.blocks.append(string::from(text, nothing {})); return this.blocks.last().view(); } if this.blocks.length == 0 || this.blocks.last().length + text.length > this.blocks.last().capacity { var made := string::new(nothing {}); made.reserve(names::block_size); this.blocks.append(move(&mut made)); } val block := &mut this.blocks.last(); val at := block.length; block.append(text); return strview::over(block.view().data + at, text.length); } # Whether two texts are the same, their lengths first, then their bytes, a # word at a time, the last word ending where they do, so a text of a few # words is compared in as many loads, as `hash_bytes` reads one. @inline def same_text(a: strview, b: strview): bool { val count := a.length; return false if count != b.length; val x := a.data; val y := b.data; if count >= 8 { var at: u64 = 0; while at + 8 < count { return false if *((x + at) as *u64) != *((y + at) as *u64); at += 8; } return *((x + count - 8) as *u64) == *((y + count - 8) as *u64); } if count >= 4 { return *(x as *u32) == *(y as *u32) && *((x + count - 4) as *u32) == *((y + count - 4) as *u32); } if count >= 2 { return *(x as *u16) == *(y as *u16) && *((x + count - 2) as *u16) == *((y + count - 2) as *u16); } return count == 0 || x[0] == y[0]; } #! The text of the name of the ID. #! @param this The table of names. #! @param id The name's ID. #! @returns The text, which lives as long as the table does. pub def names::text(&this, id: u32): strview { return this.views[id]; } #! How many names there are. #! @param this The table of names. #! @returns The number of names, which is one more than the newest's ID. pub val names::count: u64 { get => this.views.length; } #! Whether a name is one of the language's keywords. #! @param this The table of names. #! @param id The name's ID. #! @returns Whether it is. pub def names::is_keyword(&this, id: u32): bool { return id >= tk::kw_beg && id <= tk::kw_end; } #! Determines if the given ID corresponds to one of the language reserved words. #! @param this The names instance to compare against. #! @param id The ID of the name to check. #! @returns Whether the ID belongs to a reserved word. pub def names::is_reserved(&this, id: u32): bool { return id >= names::this_id && id < names::get_id; } #! A keyword, and the token kind it lexes as. struct names::keyword { val text: strview; val kind: tk; } const _keywords: names::keyword[31] = [ names::keyword { text = "import", kind = tk::kw_imp }, names::keyword { text = "module", kind = tk::kw_mod }, names::keyword { text = "extern", kind = tk::kw_extern }, names::keyword { text = "pub", kind = tk::kw_pub }, names::keyword { text = "const", kind = tk::kw_const }, names::keyword { text = "var", kind = tk::kw_var }, names::keyword { text = "val", kind = tk::kw_val }, names::keyword { text = "def", kind = tk::kw_def }, names::keyword { text = "mut", kind = tk::kw_mut }, names::keyword { text = "return", kind = tk::kw_ret }, names::keyword { text = "if", kind = tk::kw_if }, names::keyword { text = "else", kind = tk::kw_else }, names::keyword { text = "for", kind = tk::kw_for }, names::keyword { text = "in", kind = tk::kw_in }, names::keyword { text = "while", kind = tk::kw_while }, names::keyword { text = "break", kind = tk::kw_break }, names::keyword { text = "continue", kind = tk::kw_cont }, names::keyword { text = "type", kind = tk::kw_type }, names::keyword { text = "enum", kind = tk::kw_enum }, names::keyword { text = "struct", kind = tk::kw_struct }, names::keyword { text = "union", kind = tk::kw_union }, names::keyword { text = "trait", kind = tk::kw_trait }, names::keyword { text = "as", kind = tk::kw_as }, names::keyword { text = "is", kind = tk::kw_is }, names::keyword { text = "not", kind = tk::kw_not }, names::keyword { text = "match", kind = tk::kw_match }, names::keyword { text = "when", kind = tk::kw_when }, names::keyword { text = "where", kind = tk::kw_where }, names::keyword { text = "true", kind = tk::kw_true }, names::keyword { text = "false", kind = tk::kw_false }, names::keyword { text = "null", kind = tk::kw_null }, ]; # Words that name what the language gives, which nothing may declare. `this` # comes first, which the parser knows by ID. const _reserved: strview[18] = [ "this", "bool", "int", "uint", "float", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64", "void", "self", "_" ]; # Identifiers the grammar gives a meaning in one place each, which stay names # everywhere else, so the parser knows them by ID. const _words: strview[9] = [ "get", "set", "type_of", "expects", "asserts", "ensures", "old", "undefined", "value" ]; #! The IDs of the names the parser reads as words, by where `names::new` interns #! them: the reserved words right after the keywords, then `_words`. pub const names::this_id: u32 = tk::kw_end as u32 + 1; #! `this`. pub const names::get_id: u32 = names::this_id + _reserved.length as u32; #! `get`. pub const names::set_id: u32 = names::get_id + 1; #! `set`. pub const names::type_of_id: u32 = names::get_id + 2; #! `type_of`. pub const names::expects_id: u32 = names::get_id + 3; #! `expects`, a contract at a statement's start. pub const names::asserts_id: u32 = names::get_id + 4; #! `asserts`, a contract at a statement's start. pub const names::ensures_id: u32 = names::get_id + 5; #! `ensures`, a contract at a statement's start. pub const names::old_id: u32 = names::get_id + 6; #! `old`, an earlier value within an `ensures`. pub const names::undefined_id: u32 = names::get_id + 7; #! `undefined`, a variable's value that opts out of zeroing. pub const names::value_id: u32 = names::get_id + 8; #! `value`, what a `set` is given. pub const names::self_id: u32 = names::this_id + 16; #! `self`, among the reserved words. pub const names::underscore_id: u32 = names::this_id + 17; #! `_`, the last of the reserved words. #! The words the checker knows by ID, which `names::new` interns after the #! parser's, in this order, so each is a constant. A member's name is its word. pub enum word : u32 { std = names::value_id + 1, #! `std`. core, #! `core`, the library of the language's own, whose root every file sees. system, #! `system`, the library of the platforms. string, #! `string`. strview, #! `strview`. span, #! `span`. simd, #! `simd`. mask, #! `mask`. slice, #! `slice`. maybe, #! `maybe`. result, #! `result`. nothing, #! `nothing`. equatable, #! `equatable`. ordered, #! `ordered`. numeric, #! `numeric`. integer, #! `integer`. ordinal, #! `ordinal`. real, #! `real`. hashable, #! `hashable`. owning, #! `owning`. closeable, #! `closeable`. zeroable, #! `zeroable`. lockfree, #! `lockfree`. pointer, #! `pointer`. copyable, #! `copyable`. moveable, #! `moveable`. indexable, #! `indexable`. signature, #! `signature`. params, #! `params`. invokable, #! `invokable`. invoke, #! `invoke`. iterable, #! `iterable`. iterator, #! `iterator`. range, #! `range`. attribute, #! `attribute`. usage, #! `usage`. optional, #! `optional`. intrinsic, #! `intrinsic`. foreign, #! `foreign`, an attribute C's declarations no longer take. link, #! `link`, an attribute C's declarations no longer take. nodiscard, #! `nodiscard`. allow, #! `allow`. inline, #! `inline`. cfn, #! `cfn`. equals, #! `equals`. hash, #! `hash`. compare, #! `compare`. add, #! `add`. sub, #! `sub`. mul, #! `mul`. div, #! `div`. rem, #! `rem`. negate, #! `negate`. bit_and, #! `bit_and`. bit_or, #! `bit_or`. bit_xor, #! `bit_xor`. bit_not, #! `bit_not`. shift_left, #! `shift_left`. shift_right, #! `shift_right`. add_assign, #! `add_assign`, which `+=` calls where a type has it. sub_assign, #! `sub_assign`. mul_assign, #! `mul_assign`. div_assign, #! `div_assign`. rem_assign, #! `rem_assign`. bit_and_assign, #! `bit_and_assign`. bit_or_assign, #! `bit_or_assign`. bit_xor_assign, #! `bit_xor_assign`. shift_left_assign, #! `shift_left_assign`. shift_right_assign, #! `shift_right_assign`. close, #! `close`. at, #! `at`. iter, #! `iter`. next, #! `next`. next_back, #! `next_back`. length, #! `length`. data, #! `data`. view, #! `view`. main, #! `main`. some, #! `some`. none, #! `none`. error, #! `error`. os, #! `os`. arch, #! `arch`. backend, #! `backend`. } #! A word the checker knows, and its member of `word`. struct names::known { val text: strview; val id: word; } # The text of each word the checker knows, which is its member's name, listed # as `word` lists them, so `names::new` stops where the two are out of step. const _known: names::known[84] = [ names::known { text = "std", id = word::std }, names::known { text = "core", id = word::core }, names::known { text = "system", id = word::system }, names::known { text = "string", id = word::string }, names::known { text = "strview", id = word::strview }, names::known { text = "span", id = word::span }, names::known { text = "simd", id = word::simd }, names::known { text = "mask", id = word::mask }, names::known { text = "slice", id = word::slice }, names::known { text = "maybe", id = word::maybe }, names::known { text = "result", id = word::result }, names::known { text = "nothing", id = word::nothing }, names::known { text = "equatable", id = word::equatable }, names::known { text = "ordered", id = word::ordered }, names::known { text = "numeric", id = word::numeric }, names::known { text = "integer", id = word::integer }, names::known { text = "ordinal", id = word::ordinal }, names::known { text = "real", id = word::real }, names::known { text = "hashable", id = word::hashable }, names::known { text = "owning", id = word::owning }, names::known { text = "closeable", id = word::closeable }, names::known { text = "zeroable", id = word::zeroable }, names::known { text = "lockfree", id = word::lockfree }, names::known { text = "pointer", id = word::pointer }, names::known { text = "copyable", id = word::copyable }, names::known { text = "moveable", id = word::moveable }, names::known { text = "indexable", id = word::indexable }, names::known { text = "signature", id = word::signature }, names::known { text = "params", id = word::params }, names::known { text = "invokable", id = word::invokable }, names::known { text = "invoke", id = word::invoke }, names::known { text = "iterable", id = word::iterable }, names::known { text = "iterator", id = word::iterator }, names::known { text = "range", id = word::range }, names::known { text = "attribute", id = word::attribute }, names::known { text = "usage", id = word::usage }, names::known { text = "optional", id = word::optional }, names::known { text = "intrinsic", id = word::intrinsic }, names::known { text = "foreign", id = word::foreign }, names::known { text = "link", id = word::link }, names::known { text = "nodiscard", id = word::nodiscard }, names::known { text = "allow", id = word::allow }, names::known { text = "inline", id = word::inline }, names::known { text = "cfn", id = word::cfn }, names::known { text = "equals", id = word::equals }, names::known { text = "hash", id = word::hash }, names::known { text = "compare", id = word::compare }, names::known { text = "add", id = word::add }, names::known { text = "sub", id = word::sub }, names::known { text = "mul", id = word::mul }, names::known { text = "div", id = word::div }, names::known { text = "rem", id = word::rem }, names::known { text = "negate", id = word::negate }, names::known { text = "bit_and", id = word::bit_and }, names::known { text = "bit_or", id = word::bit_or }, names::known { text = "bit_xor", id = word::bit_xor }, names::known { text = "bit_not", id = word::bit_not }, names::known { text = "shift_left", id = word::shift_left }, names::known { text = "shift_right", id = word::shift_right }, names::known { text = "add_assign", id = word::add_assign }, names::known { text = "sub_assign", id = word::sub_assign }, names::known { text = "mul_assign", id = word::mul_assign }, names::known { text = "div_assign", id = word::div_assign }, names::known { text = "rem_assign", id = word::rem_assign }, names::known { text = "bit_and_assign", id = word::bit_and_assign }, names::known { text = "bit_or_assign", id = word::bit_or_assign }, names::known { text = "bit_xor_assign", id = word::bit_xor_assign }, names::known { text = "shift_left_assign", id = word::shift_left_assign }, names::known { text = "shift_right_assign", id = word::shift_right_assign }, names::known { text = "close", id = word::close }, names::known { text = "at", id = word::at }, names::known { text = "iter", id = word::iter }, names::known { text = "next", id = word::next }, names::known { text = "next_back", id = word::next_back }, names::known { text = "length", id = word::length }, names::known { text = "data", id = word::data }, names::known { text = "view", id = word::view }, names::known { text = "main", id = word::main }, names::known { text = "some", id = word::some }, names::known { text = "none", id = word::none }, names::known { text = "error", id = word::error }, names::known { text = "os", id = word::os }, names::known { text = "arch", id = word::arch }, names::known { text = "backend", id = word::backend }, ];