Make Inspect a builtin

This commit is contained in:
Richard Feldman 2023-08-10 21:23:52 -04:00
parent 75f6293e12
commit 700776fad7
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
14 changed files with 63 additions and 29 deletions

View file

@ -1,10 +1,11 @@
interface Inspect
exposes [
Inspect,
InspectFormatter,
Inspector,
KeyValWalker,
InspectFormatter,
ElemWalker,
KeyValWalker,
inspect,
init,
list,
set,
@ -30,10 +31,12 @@ interface Inspect
dec,
custom,
apply,
inspect,
toInspector,
]
imports []
imports [
Bool.{ Bool },
Num.{ U8, U16, U32, U64, U128, I8, I16, I32, I64, I128, F32, F64, Dec },
]
KeyValWalker state collection key val : collection, state, (state, key, val -> state) -> state
ElemWalker state collection elem : collection, state, (state, elem -> state) -> state

View file

@ -15,6 +15,7 @@ pub fn module_source(module_id: ModuleId) -> &'static str {
ModuleId::ENCODE => ENCODE,
ModuleId::DECODE => DECODE,
ModuleId::HASH => HASH,
ModuleId::INSPECT => INSPECT,
ModuleId::JSON => JSON,
_ => internal_error!(
"ModuleId {:?} is not part of the standard library",
@ -34,4 +35,5 @@ const BOOL: &str = include_str!("../roc/Bool.roc");
const ENCODE: &str = include_str!("../roc/Encode.roc");
const DECODE: &str = include_str!("../roc/Decode.roc");
const HASH: &str = include_str!("../roc/Hash.roc");
const INSPECT: &str = include_str!("../roc/Inspect.roc");
const JSON: &str = include_str!("../roc/TotallyNotJson.roc");

View file

@ -25,6 +25,7 @@ const MODULES: &[(ModuleId, &str)] = &[
(ModuleId::ENCODE, "Encode.roc"),
(ModuleId::DECODE, "Decode.roc"),
(ModuleId::HASH, "Hash.roc"),
(ModuleId::INSPECT, "Inspect.roc"),
(ModuleId::JSON, "TotallyNotJson.roc"),
];

View file

@ -212,6 +212,7 @@ const BOX: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/Box.dat")) as &[_];
const ENCODE: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/Encode.dat")) as &[_];
const DECODE: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/Decode.dat")) as &[_];
const HASH: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/Hash.dat")) as &[_];
const INSPECT: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/Inspect.dat")) as &[_];
fn deserialize_help(bytes: &[u8]) -> TypeState {
let (state, _offset) = TypeState::deserialize(bytes);
@ -242,6 +243,7 @@ fn read_cached_types() -> MutMap<ModuleId, TypeState> {
output.insert(ModuleId::DECODE, deserialize_help(DECODE));
output.insert(ModuleId::HASH, deserialize_help(HASH));
output.insert(ModuleId::INSPECT, deserialize_help(INSPECT));
}
output

View file

@ -2290,6 +2290,7 @@ fn update<'a>(
extend_header_with_builtin(header, ModuleId::ENCODE);
extend_header_with_builtin(header, ModuleId::DECODE);
extend_header_with_builtin(header, ModuleId::HASH);
extend_header_with_builtin(header, ModuleId::INSPECT);
}
state
@ -3494,6 +3495,7 @@ fn load_module<'a>(
"Encode", ModuleId::ENCODE
"Decode", ModuleId::DECODE
"Hash", ModuleId::HASH
"Inspect", ModuleId::INSPECT
"TotallyNotJson", ModuleId::JSON
}
@ -5257,6 +5259,7 @@ fn canonicalize_and_constrain<'a>(
| ModuleId::DICT
| ModuleId::SET
| ModuleId::HASH
| ModuleId::INSPECT
);
if !name.is_builtin() || should_include_builtin {

View file

@ -25,5 +25,6 @@ pub const BUILTIN_MODULES: &[(ModuleId, &str)] = &[
(ModuleId::ENCODE, "Encode"),
(ModuleId::DECODE, "Decode"),
(ModuleId::HASH, "Hash"),
(ModuleId::INSPECT, "Inspect"),
(ModuleId::JSON, "TotallyNotJson"),
];

View file

@ -85,6 +85,7 @@ impl Default for ModuleCache<'_> {
ENCODE,
DECODE,
HASH,
INSPECT,
JSON,
}

View file

@ -113,6 +113,7 @@ impl ModuleName {
pub const ENCODE: &'static str = "Encode";
pub const DECODE: &'static str = "Decode";
pub const HASH: &'static str = "Hash";
pub const INSPECT: &'static str = "Inspect";
pub const JSON: &'static str = "TotallyNotJson";
pub fn as_str(&self) -> &str {

View file

@ -1588,9 +1588,43 @@ define_builtins! {
20 HASH_HASH_LIST: "hashList"
21 HASH_HASH_UNORDERED: "hashUnordered"
}
14 JSON: "TotallyNotJson" => {
14 INSPECT: "Inspect" => {
0 INSPECT_INSPECT_ABILITY: "Inspect" exposed_type=true
1 INSPECT_INSPECTOR: "Inspector" exposed_type=true
2 INSPECT_INSPECT_FORMATTER: "InspectFormatter" exposed_type=true
3 INSPECT_ELEM_WALKER: "ElemWalker" exposed_type=true
4 INSPECT_KEY_VAL_WALKER: "KeyValWalker" exposed_type=true
5 INSPECT_INSPECT: "inspect"
6 INSPECT_INIT: "init"
7 INSPECT_LIST: "list"
8 INSPECT_SET: "set"
9 INSPECT_DICT: "dict"
10 INSPECT_TAG: "tag"
11 INSPECT_TUPLE: "tuple"
12 INSPECT_RECORD: "record"
13 INSPECT_BOOL: "bool"
14 INSPECT_STR: "str"
15 INSPECT_OPAQUE: "opaque"
16 INSPECT_U8: "u8"
17 INSPECT_I8: "i8"
18 INSPECT_U16: "u16"
19 INSPECT_I16: "i16"
20 INSPECT_U32: "u32"
21 INSPECT_I32: "i32"
22 INSPECT_U64: "u64"
23 INSPECT_I64: "i64"
24 INSPECT_U128: "u128"
25 INSPECT_I128: "i128"
26 INSPECT_F32: "f32"
27 INSPECT_F64: "f64"
28 INSPECT_DEC: "dec"
29 INSPECT_CUSTOM: "custom"
30 INSPECT_APPLY: "apply"
31 INSPECT_TO_INSPECTOR: "toInspector"
}
15 JSON: "TotallyNotJson" => {
0 JSON_JSON: "TotallyNotJson"
}
num_modules: 15 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro)
num_modules: 16 // Keep this count up to date by hand! (TODO: see the mut_map! macro for how we could determine this count correctly in the macro)
}

View file

@ -7,9 +7,7 @@ interface Community
Person,
walkFriendNames,
]
imports [
Inspect.{ InspectFormatter, Inspector, Inspect },
]
imports []
Community := {
people : List Person,

View file

@ -3,12 +3,7 @@ interface GuiFormatter
GuiFormatter,
toGui,
]
imports [
Inspect.{
Formatter,
Inspector,
},
]
imports []
## This can't depend on the platform, so I just copied all of this.
@ -49,7 +44,7 @@ GuiFormatter := { nodes : List Elem }
init : {} -> GuiFormatter
init = \{} -> @GuiFormatter { nodes: [] }
list : list, Inspect.ElemWalker GuiFormatter list elem, (elem -> Inspector GuiFormatter) -> Inspector GuiFormatter
list : list, ElemWalker GuiFormatter list elem, (elem -> Inspector GuiFormatter) -> Inspector GuiFormatter
list = \content, walkFn, toInspector ->
f0 <- Inspect.custom
# Use a temporary buffer for the children nodes
@ -63,7 +58,7 @@ list = \content, walkFn, toInspector ->
addNode f0 (Col nodes)
set : set, Inspect.ElemWalker GuiFormatter set elem, (elem -> Inspector GuiFormatter) -> Inspector GuiFormatter
set : set, ElemWalker GuiFormatter set elem, (elem -> Inspector GuiFormatter) -> Inspector GuiFormatter
set = \content, walkFn, toInspector ->
f0 <- Inspect.custom
# Use a temporary buffer for the children nodes
@ -77,7 +72,7 @@ set = \content, walkFn, toInspector ->
addNode f0 (Col nodes)
dict : dict, Inspect.KeyValWalker GuiFormatter dict key value, (key -> Inspector GuiFormatter), (value -> Inspector GuiFormatter) -> Inspector GuiFormatter
dict : dict, KeyValWalker GuiFormatter dict key value, (key -> Inspector GuiFormatter), (value -> Inspector GuiFormatter) -> Inspector GuiFormatter
dict = \d, walkFn, keyToInspector, valueToInspector ->
f0 <- Inspect.custom
# Use a temporary buffer for the children nodes

View file

@ -3,12 +3,7 @@ interface LogFormatter
LogFormatter,
toStr,
]
imports [
Inspect.{
InspectFormatter,
Inspector,
},
]
imports []
LogFormatter := { data : Str }
has [
@ -43,7 +38,7 @@ LogFormatter := { data : Str }
init : {} -> LogFormatter
init = \{} -> @LogFormatter { data: "" }
list : list, Inspect.ElemWalker (LogFormatter, Bool) list elem, (elem -> Inspector LogFormatter) -> Inspector LogFormatter
list : list, ElemWalker (LogFormatter, Bool) list elem, (elem -> Inspector LogFormatter) -> Inspector LogFormatter
list = \content, walkFn, toInspector ->
f0 <- Inspect.custom
write f0 "["
@ -62,7 +57,7 @@ list = \content, walkFn, toInspector ->
|> .0
|> write "]"
set : set, Inspect.ElemWalker (LogFormatter, Bool) set elem, (elem -> Inspector LogFormatter) -> Inspector LogFormatter
set : set, ElemWalker (LogFormatter, Bool) set elem, (elem -> Inspector LogFormatter) -> Inspector LogFormatter
set = \content, walkFn, toInspector ->
f0 <- Inspect.custom
write f0 "{"
@ -81,7 +76,7 @@ set = \content, walkFn, toInspector ->
|> .0
|> write "}"
dict : dict, Inspect.KeyValWalker (LogFormatter, Bool) dict key value, (key -> Inspector LogFormatter), (value -> Inspector LogFormatter) -> Inspector LogFormatter
dict : dict, KeyValWalker (LogFormatter, Bool) dict key value, (key -> Inspector LogFormatter), (value -> Inspector LogFormatter) -> Inspector LogFormatter
dict = \d, walkFn, keyToInspector, valueToInspector ->
f0 <- Inspect.custom
write f0 "{"

View file

@ -1,7 +1,6 @@
app "inspect-gui"
packages { pf: "gui/platform/main.roc" }
imports [
Inspect,
Community,
GuiFormatter,
]

View file

@ -2,7 +2,6 @@ app "inspect-logging"
packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.3.2/tE4xS_zLdmmxmHwHih9kHWQ7fsXtJr7W7h3425-eZFk.tar.br" }
imports [
pf.Stdout,
Inspect.{ Formatter, Inspector, Inspect },
LogFormatter,
Community,
]