From fa7ec67e8f34766aa81e1001de1d49401cde32fa Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Mon, 31 May 2021 14:20:13 -0400 Subject: Added macro for easily logging local variables during debugging. --- stdlib/source/lux/debug.lux | 61 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) (limited to 'stdlib') diff --git a/stdlib/source/lux/debug.lux b/stdlib/source/lux/debug.lux index 912cc38d7..2c6175601 100644 --- a/stdlib/source/lux/debug.lux +++ b/stdlib/source/lux/debug.lux @@ -4,7 +4,7 @@ ["." type] ["." ffi (#+ import:)] [abstract - [monad (#+ do)]] + ["." monad (#+ do)]] [control [pipe (#+ case> new>)] ["." function] @@ -21,7 +21,8 @@ ["." json]] [collection ["." array] - ["." list ("#\." functor)]]] + ["." list ("#\." functor)] + ["." dictionary]]] ["." meta ["." location]] [macro @@ -508,3 +509,59 @@ [location meta.location expectedT meta.expected_type] (meta.fail (exception.construct ..type_hole [location expectedT])))) + +(type: Target + [Text (Maybe Code)]) + +(def: target + (.Parser Target) + (<>.either (<>.and .local_identifier + (\ <>.monad wrap #.None)) + (.record (<>.and .local_identifier + (\ <>.monad map (|>> #.Some) .any))))) + +(exception: #export (unknown_local_binding {name Text}) + (exception.report + ["Name" (%.text name)])) + +(syntax: #export (here {targets (: (.Parser (List Target)) + (|> ..target + <>.some + (<>.default (list))))}) + (do {! meta.monad} + [location meta.location + locals meta.locals + #let [environment (|> locals + list.concat + ## The list is reversed to make sure that, when building the dictionary, + ## later bindings overshadow earlier ones if they have the same name. + list.reverse + (dictionary.from_list text.hash))] + targets (: (Meta (List Target)) + (case targets + #.Nil + (|> environment + dictionary.keys + (list\map (function (_ local) [local #.None])) + wrap) + + _ + (monad.map ! (function (_ [name format]) + (if (dictionary.key? environment name) + (wrap [name format]) + (meta.fail (exception.construct ..unknown_local_binding [name])))) + targets)))] + (wrap (list (` (..log! ("lux text concat" + (~ (code.text (%.format (%.location location) text.new_line))) + ((~! exception.report) + (~+ (|> targets + list.reverse + (list\map (function (_ [name format]) + (let [format (case format + #.None + (` (~! ..inspect)) + + (#.Some format) + format)] + (` [(~ (code.text name)) + ((~ format) (~ (code.local_identifier name)))])))))))))))))) -- cgit v1.2.3