Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1,170 @@
|
||||
#! @file reading.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how the files of a program are read: each lexed, then parsed, as
|
||||
#! many at once as there are threads asked for. Each thread reading files lexes
|
||||
#! them with names of its own, which it keeps from file to file. The names a
|
||||
#! file uses join the program's in the order the file first uses them, and the
|
||||
#! files' in the order of the files, each once those of the files before it
|
||||
#! have, so every word is given the ID it would have were the files read one
|
||||
#! after another, and what is read is the same however many threads read it.
|
||||
#! What reading notes is heard as it is noted, on the thread noting it, rather
|
||||
#! than kept.
|
||||
|
||||
import std::thread;
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! What hears what reading files notes, as it is noted.
|
||||
pub struct listener {
|
||||
pub var noted: def(&mut note): void; #! Hears each note as it is made, on the thread reading its file, which it may move.
|
||||
pub var read: def(u32): void; #! Hears that the file of the index was read whole, after which nothing more is noted of reading it.
|
||||
}
|
||||
|
||||
#! Lexes and parses some of the files of a program, those from one index up to
|
||||
#! another, as many at once as there are threads asked for, each taking the
|
||||
#! next file none has taken. A program read by reference reads its files so, a
|
||||
#! round at a time, each round's after those before it.
|
||||
#! @param sources The source of each file, which stays where it is, as what
|
||||
#! is read refers to it.
|
||||
#! @param syntaxes Where each file is read into: one, empty, for each source.
|
||||
#! @param roots Where the `document` node of each file is written.
|
||||
#! @param names The program's names, which the words of each file join.
|
||||
#! @param threads How many threads read files, the one calling among them;
|
||||
#! where it is one, none starts, and the files are read one
|
||||
#! after another, with the program's names.
|
||||
#! @param heard What hears what reading notes.
|
||||
#! @param from The index of the first file read, whose names join the
|
||||
#! program's after those of every file before it.
|
||||
#! @param to The index past the last.
|
||||
pub def read_files(
|
||||
sources: &vector<source>,
|
||||
syntaxes: &mut vector<syntax>,
|
||||
roots: &mut vector<u32>,
|
||||
names: &mut names,
|
||||
threads: u32,
|
||||
heard: &listener,
|
||||
from: u64,
|
||||
to: u64
|
||||
): void {
|
||||
return if to <= from;
|
||||
val count := to - from;
|
||||
if roots.length < to {
|
||||
roots.resize(to);
|
||||
}
|
||||
var helpers: u64 = threads <= 1 || count <= 1 ? 0 : (threads - 1) as u64;
|
||||
if helpers > count - 1 {
|
||||
helpers = count - 1;
|
||||
}
|
||||
# The lexer and parser are given a vector to keep notes in, which stays
|
||||
# empty, as they hear each note instead.
|
||||
if helpers == 0 {
|
||||
var kept := vector::<note>::new();
|
||||
for j in from..to {
|
||||
var lexing := lexer::new(sources.at(j), names, &mut kept, j as u32);
|
||||
lexing.sink.heard = heard.noted;
|
||||
lexing.tokenize_all(syntaxes.at(j));
|
||||
roots[j] = parse_heard(syntaxes.at(j), names, j as u32, heard);
|
||||
heard.read(j as u32);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
# The names of each file join the program's in its turn: `turn` holds the
|
||||
# index of the file whose names join next.
|
||||
var next := atomic::<u64>::new(from);
|
||||
var turn := thread::mutex::<u64>::new(from);
|
||||
var turned := thread::condition::new();
|
||||
val reader := def(): void {
|
||||
var kept := vector::<note>::new();
|
||||
# The names this thread lexes with; of each, one more than the index of the
|
||||
# last file seen to use it, and its ID in the program's names, there; and
|
||||
# the names the file being read uses, in the order it first uses them.
|
||||
var own := names::new();
|
||||
var seen := vector::<u64>::new();
|
||||
var ids := vector::<u32>::new();
|
||||
var used := vector::<u32>::new();
|
||||
while true {
|
||||
val j := next.fetch_add(1);
|
||||
return if j >= to;
|
||||
var lexing := lexer::new(sources.at(j), &mut own, &mut kept, j as u32);
|
||||
lexing.sink.heard = heard.noted;
|
||||
lexing.tokenize_all(syntaxes.at(j));
|
||||
|
||||
# The file is parsed with the names it was lexed with, before its turn to
|
||||
# join them to the program's, which other threads write meanwhile. The
|
||||
# parser knows its words by the IDs every table gives them, and the tree
|
||||
# holds no ID of its own.
|
||||
roots[j] = parse_heard(syntaxes.at(j), &own, j as u32, heard);
|
||||
|
||||
# Every word lexed is a token, so the order the file first uses each name
|
||||
# in is that of its tokens.
|
||||
seen.resize(own.count);
|
||||
ids.resize(own.count);
|
||||
used.clear();
|
||||
for &id in syntaxes.at(j).token_names {
|
||||
if seen[id as u64] != j + 1 {
|
||||
seen[id as u64] = j + 1;
|
||||
used.append(id);
|
||||
}
|
||||
}
|
||||
join_names(&mut turn, &mut turned, j, names, &own, used.view(), &mut ids);
|
||||
syntaxes.at(j).rename(ids.view());
|
||||
heard.read(j as u32);
|
||||
}
|
||||
};
|
||||
var started := vector::<thread::handle<void>>::new();
|
||||
for _ in 0..helpers {
|
||||
var made := thread::spawn(reader) else (_) { break; };
|
||||
started.append(move(&mut made));
|
||||
}
|
||||
reader();
|
||||
for &mut handle in started {
|
||||
handle.join();
|
||||
}
|
||||
}
|
||||
|
||||
#! Parses a file lexed, the notes heard as they are made.
|
||||
#! @param syntax The file's tokens, which its nodes join.
|
||||
#! @param names The names the file was lexed with.
|
||||
#! @param file The index of the file.
|
||||
#! @param heard What hears the notes.
|
||||
#! @returns The file's `document` node.
|
||||
def parse_heard(syntax: &mut syntax, names: &names, file: u32, heard: &listener): u32 {
|
||||
var kept := vector::<note>::new();
|
||||
var parsing := parser::new(syntax, names, &mut kept, file);
|
||||
parsing.sink.heard = heard.noted;
|
||||
return parsing.parse();
|
||||
}
|
||||
|
||||
#! Waits for a file's turn, which comes once the names of the files before it
|
||||
#! have joined the program's, then adds those it uses.
|
||||
#! @param turn The index of the file whose names join next.
|
||||
#! @param turned What is notified where the turn passes.
|
||||
#! @param file The index of the file.
|
||||
#! @param names The program's names.
|
||||
#! @param own The names the file was lexed with.
|
||||
#! @param used Those the file uses, in the order it first uses them.
|
||||
#! @param ids Where the ID in the program's names of each it uses is
|
||||
#! written, by its ID in its own.
|
||||
def join_names(
|
||||
turn: &mut thread::mutex<u64>,
|
||||
turned: &mut thread::condition,
|
||||
file: u64,
|
||||
names: &mut names,
|
||||
own: &names,
|
||||
used: span<u32>,
|
||||
ids: &mut vector<u32>
|
||||
): void {
|
||||
var held := turn.lock();
|
||||
while held.value() != file {
|
||||
turned.wait(&mut held);
|
||||
}
|
||||
for &id in used {
|
||||
ids[id as u64] = names.intern(&own.text(id));
|
||||
}
|
||||
held.value() = file + 1;
|
||||
turned.notify_all();
|
||||
}
|
||||
Reference in new issue
Block a user