compiler/
lib/
examples/
std/
arch/
char/
collections/
lang/
alloc/
ast/
gen/
il/
module/
printer.rad
2.8 KiB
tests.rad
13.5 KiB
parser/
resolver/
scanner/
alloc.rad
7.1 KiB
ast.rad
26.7 KiB
gen.rad
513 B
il.rad
20.3 KiB
lower.rad
315.0 KiB
module.rad
14.9 KiB
package.rad
1.3 KiB
parser.rad
91.2 KiB
resolver.rad
452.9 KiB
scanner.rad
17.9 KiB
sexpr.rad
6.7 KiB
strings.rad
2.2 KiB
types.rad
1.6 KiB
sys/
arch.rad
68 B
char.rad
855 B
collections.rad
39 B
fmt.rad
8.3 KiB
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
276 B
mem.rad
2.3 KiB
sys.rad
179 B
testing.rad
2.4 KiB
tests.rad
15.7 KiB
vec.rad
3.2 KiB
std.rad
281 B
scripts/
seed/
sublime/
test/
vim/
.gitignore
336 B
.gitsigners
112 B
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
5.4 KiB
README
2.5 KiB
STYLE
2.5 KiB
std.lib
1.5 KiB
std.lib.test
662 B
lib/std/lang/module/printer.rad
raw
| 1 | //! Module graph pretty printer using S-expression syntax. |
| 2 | |
| 3 | use std::fmt; |
| 4 | use std::mem; |
| 5 | use std::io; |
| 6 | use std::lang::sexpr; |
| 7 | use std::lang::alloc; |
| 8 | |
| 9 | /// Format a u32 and allocate the result in the arena. |
| 10 | unsafe fn formatId(a: &mut alloc::Arena, id: u32) -> *[u8] { |
| 11 | let mut digits: [u8; 10] = undefined; |
| 12 | let start = fmt::formatU32(id, &mut digits[..]); |
| 13 | let ptr = try alloc::allocSlice(a, 1, 1, digits.len - start) catch { return "?"; }; |
| 14 | let slice = ptr as *mut [u8]; |
| 15 | try mem::copy(slice, &digits[start..]) catch { return "?"; }; |
| 16 | return slice; |
| 17 | } |
| 18 | |
| 19 | /// Convert a module state into an S-expression symbol. |
| 20 | fn stateToExpr(state: super::ModuleState) -> sexpr::Expr { |
| 21 | match state { |
| 22 | case super::ModuleState::Vacant => return sexpr::sym("vacant"), |
| 23 | case super::ModuleState::Registered => return sexpr::sym("registered"), |
| 24 | case super::ModuleState::Parsing => return sexpr::sym("parsing"), |
| 25 | case super::ModuleState::Parsed => return sexpr::sym("parsed"), |
| 26 | case super::ModuleState::Analyzing => return sexpr::sym("analyzing"), |
| 27 | case super::ModuleState::Ready => return sexpr::sym("ready"), |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /// Recursively convert a module entry and its descendants to an S-expression. |
| 32 | unsafe fn subtreeToExpr( |
| 33 | a: &mut alloc::Arena, |
| 34 | graph: &super::ModuleGraph, |
| 35 | entry: *super::ModuleEntry |
| 36 | ) -> sexpr::Expr { |
| 37 | let idText = formatId(a, entry.id as u32); |
| 38 | let path = super::moduleQualifiedPath(entry); |
| 39 | let mut pathBuf: *[sexpr::Expr] = &[]; |
| 40 | if path.len > 0 { |
| 41 | let buf = try! sexpr::allocExprs(a, path.len); |
| 42 | for seg, i in path { set buf[i] = sexpr::sym(seg); } |
| 43 | set pathBuf = buf; |
| 44 | } |
| 45 | |
| 46 | let mut childBuf: *[sexpr::Expr] = &[]; |
| 47 | let childCount = super::childCount(entry); |
| 48 | if childCount > 0 { |
| 49 | let buf = try! sexpr::allocExprs(a, childCount); |
| 50 | for i in 0..childCount { |
| 51 | if let child = super::get(graph, super::childAt(entry, i)) { |
| 52 | set buf[i] = subtreeToExpr(a, graph, child); |
| 53 | } |
| 54 | } |
| 55 | set childBuf = buf; |
| 56 | } |
| 57 | |
| 58 | return sexpr::block(a, "module", &[ |
| 59 | sexpr::sym(entry.name), |
| 60 | sexpr::list(a, "id", &[sexpr::sym(idText)]), |
| 61 | sexpr::list(a, "state", &[stateToExpr((*entry.updates).state)]), |
| 62 | sexpr::Expr::Str(entry.filePath), |
| 63 | sexpr::Expr::List { head: "::", tail: pathBuf, multiline: false } |
| 64 | ], childBuf); |
| 65 | } |
| 66 | |
| 67 | /// Print the entire module graph in S-expression format. |
| 68 | export unsafe fn printGraph(graph: &super::ModuleGraph, arena: &mut alloc::Arena) { |
| 69 | // Print all root modules. |
| 70 | for i in 0..graph.entriesLen { |
| 71 | let entry = super::get(graph, i as u16) else panic; |
| 72 | if entry.parent == nil { |
| 73 | sexpr::print(subtreeToExpr(arena, graph, entry), 0); |
| 74 | io::print("\n"); |
| 75 | } |
| 76 | } |
| 77 | } |