From f8c9375490f00d39729c0e969b60ce825d29e7ea Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 22 Jan 2019 00:27:53 -0400 Subject: Some improvements to macro machinery. --- stdlib/source/lux/macro.lux | 27 ++++++++++++++++++++------- stdlib/source/lux/macro/syntax.lux | 9 +++------ 2 files changed, 23 insertions(+), 13 deletions(-) (limited to 'stdlib') diff --git a/stdlib/source/lux/macro.lux b/stdlib/source/lux/macro.lux index ead5b366e..a2e17e2d9 100644 --- a/stdlib/source/lux/macro.lux +++ b/stdlib/source/lux/macro.lux @@ -384,6 +384,11 @@ _ (fail (text/compose "Code is not a local identifier: " (code.to-text ast))))) +(def: #export wrong-syntax-error + (-> Name Text) + (|>> name/encode + (text/compose "Wrong syntax for "))) + (macro: #export (with-gensyms tokens) {#.doc (doc "Creates new identifiers and offers them to the body expression." (syntax: #export (synchronized lock body) @@ -406,7 +411,7 @@ (~ body)))))) _ - (fail "Wrong syntax for with-gensyms"))) + (fail (..wrong-syntax-error (name-of ..with-gensyms))))) (def: #export (expand-1 token) {#.doc "Works just like expand, except that it ensures that the output is a single Code token."} @@ -692,7 +697,7 @@ (function (_ compiler) (#error.Success [compiler (get@ #.type-context compiler)]))) -(do-template [ ] +(do-template [ ] [(macro: #export ( tokens) {#.doc (doc "Performs a macro-expansion and logs the resulting code." "You can either use the resulting code, or omit them." @@ -716,7 +721,7 @@ (do Monad [cursor ..cursor output ( token) - #let [_ (log! ($_ text/compose " @ " (.cursor-description cursor))) + #let [_ (log! ($_ text/compose (name/encode (name-of )) " @ " (.cursor-description cursor))) _ (list/map (|>> code.to-text log!) output) _ (log! "")]] @@ -725,9 +730,17 @@ output))) #.None - (fail ($_ text/compose "Wrong syntax for " "."))))] + (fail (..wrong-syntax-error (name-of )))))] - [log-expand! expand "log-expand!"] - [log-expand-all! expand-all "log-expand-all!"] - [log-expand-once! expand-once "log-expand-once!"] + [log-expand! expand] + [log-expand-all! expand-all] + [log-expand-once! expand-once] ) + +(macro: #export (multi tokens) + (case tokens + (^ (list [_ (#.Tuple parts)])) + (:: Monad wrap parts) + + _ + (fail (..wrong-syntax-error (name-of ..multi))))) diff --git a/stdlib/source/lux/macro/syntax.lux b/stdlib/source/lux/macro/syntax.lux index 02c3ad1ae..cb235043f 100644 --- a/stdlib/source/lux/macro/syntax.lux +++ b/stdlib/source/lux/macro/syntax.lux @@ -195,10 +195,6 @@ (wrap [real value])))) ## [Syntax] -(def: (quote name) - (-> Text Text) - ($_ text/compose "'" name "'")) - (macro: #export (syntax: tokens) {#.doc (doc "A more advanced way to define macros than 'macro:'." "The inputs to the macro can be parsed in complex ways through the use of syntax parsers." @@ -253,8 +249,9 @@ _ (//.fail "Syntax pattern expects records or identifiers.")))) args) + this-module //.current-module-name #let [g!state (code.identifier ["" "*compiler*"]) - error-msg (code.text ($_ text/compose "Wrong syntax for " (quote name) ": ")) + error-msg (code.text (//.wrong-syntax-error [this-module name])) export-ast (: (List Code) (if exported? (list (' #export)) @@ -275,4 +272,4 @@ (~ body))))))))))))) _ - (//.fail (text/compose "Wrong syntax for " (quote "syntax:")))))) + (//.fail (//.wrong-syntax-error (name-of ..syntax:)))))) -- cgit v1.2.3