Add a little more structure to the codebase

This commit is contained in:
TEC 2024-05-01 23:18:23 +08:00
parent d4e923c23c
commit 4967bcd976
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
2 changed files with 48 additions and 12 deletions

View File

@ -15,12 +15,13 @@ include("types.jl")
include("values.jl")
"""
about(fn::Function, [signature::Tuple])
about(typ::Type)
about(obj::Any)
about([io::IO], fn::Function, [argtypes::Type...])
about([io::IO], typ::Type)
about([io::IO], val::Any)
Display information on the particular nature of the argument, whether
it be a function, type, or value.
Display information on the particular nature of the argument, whatever it may be.
TODO mention the mechanisms for extension here too.
"""
function about end
@ -33,6 +34,31 @@ function about(xs...)
end
end
"""
memorylayout(io::IO, T::DataType)
memorylayout(io::IO, val::T)
Print to `io` the memory layout of the type `T`, or `val` a particular instance
of the type.
Specialised implementations should be implemented freely to enhance the utility
and prettiness of the display.
"""
function memorylayout end
"""
elaboration(::IO, x::Any)
Elaborate on `x` to io, providing extra information that might be of interest
seperately from `about` or `memorylayout`.
Specialised implementations should be implemented freely to enhance the utility
and prettiness of the display.
By convention, this is not invoked when displaying `x` compactly.
"""
elaboration(::IO, ::Any) = nothing
const ABOUT_FACES = [
:about_module => Face(foreground=:bright_red),
:about_pointer => Face(foreground=:cyan),

View File

@ -1,8 +1,6 @@
const NUMBER_BIT_FACES = (
sign = :bright_blue,
exponent = :bright_green,
mantissa = :bright_red
)
# ------------------
# Structs, in general
# ------------------
function about(io::IO, value::T) where {T}
# Type information
@ -48,8 +46,6 @@ function about(io::IO, value::T) where {T}
end
end
elaboration(::IO, ::Any) = nothing
function memorylayout(io::IO, value::T) where {T}
if isprimitivetype(T)
get(io, :compact, false) || print(io, "\n ")
@ -114,6 +110,16 @@ function memorylayout(io::IO, value::T) where {T}
memorylayout(io, T)
end
# ------------------
# Numeric types
# ------------------
const NUMBER_BIT_FACES = (
sign = :bright_blue,
exponent = :bright_green,
mantissa = :bright_red
)
function memorylayout(io::IO, value::Bool)
bits = AnnotatedString(bitstring(value))
face!(bits[1:end-1], :shadow)
@ -199,6 +205,10 @@ function floatlayout(io::IO, float::AbstractFloat, expbits::Int)
end
end
# ------------------
# Char/String
# ------------------
function memorylayout(io::IO, char::Char)
chunks = reinterpret(NTuple{4, UInt8}, reinterpret(UInt32, char) |> hton)
get(io, :compact, false) || print(io, "\n ")