Macros 如何将此函数转换为宏?
我很难理解新的宏观计划体系。在这条路径的某个地方,我开始先将“宏”作为函数编写,然后将其作为宏应用 因此,我的任务是改变以下结构:Macros 如何将此函数转换为宏?,macros,scheme,Macros,Scheme,我很难理解新的宏观计划体系。在这条路径的某个地方,我开始先将“宏”作为函数编写,然后将其作为宏应用 因此,我的任务是改变以下结构: ;; highlight-rules: rule id, color and the regexp matches (define highlight-rules `((important ,(with-esc "[1;33m") ("foo" "bob")) (unimp
;; highlight-rules: rule id, color and the regexp matches
(define highlight-rules
`((important ,(with-esc "[1;33m") ("foo"
"bob"))
(unimportant ,(with-esc "[1;30m") ("case of unimport"))
(urgent ,(with-esc "[1;31m") ("urgents"))))
进入此类cond
系列,并将匹配字符串编译为regexpes:
;; just an example. `line` is an argument bound by the function application
(cond
((string-match (regexp ".*sudo:session.*") line)
(with-color *important* line))
(else line))
我已经编写了一个函数,它似乎做到了这一点:
;; (cdar highlight-rules) -> (colorstring list-of-rules)
(define (parse-highlight-rules rules)
;; aux function to do one 'class' of patterns
(define (class-of-rules colorstr rulelist)
(map (lambda (rule)
`((string-match ,(regexp rule)) (with-color ,colorstr line)))
rulelist))
(define (do-loop accumulator rules)
(let* ((highlight-group (cdar rules))
(colorstr (car highlight-group))
(grouprules (cadr highlight-group))
(acc* (append (class-of-rules colorstr grouprules) accumulator))
(rest (cdr rules)))
(if (null? rest)
acc*
(do-loop acc* rest))))
; wrap the list in cond.
`(apply cond ,(do-loop '() rules)))
对于给定的突出显示规则
,函数返回正确的外观列表(除了应用应用
——在clojure中,可以使用拼接):
CSI>(解析突出显示规则突出显示规则)
(应用条件(((字符串匹配#))(颜色为“\x1b[1;31m”行))
((字符串匹配#)(带颜色“\x1b[1;30m”线))
((字符串匹配#)(带有颜色#0=“\x1b[1;33m”行))
((字符串匹配#)(带颜色#0#线)))
但是如何继续呢?我已经被这个问题困扰了一段时间。Chicken Scheme是我的方言。将函数转换为宏的最简单方法是使用Chicken的宏工具,该工具与Clojure的
defmacro
(除了显式重命名宏需要一些可用于保持卫生的附加参数)
拼接的工作方式基本上与Clojure中的相同。语法为,@
。因此,以下操作应有效:
(define-for-syntax (parse-highlight-rules rules)
;; ... insert missing code here ...
`(cond ,@(do-loop '() rules)))
(define-syntax highlight
(er-macro-transformer
(lambda (form rename compare)
(parse-highlight-rules (cdr form)))))
将函数转换为宏的最简单方法是使用Chicken的宏工具,该工具的工作原理与Clojure的
defmacro
类似(除了显式重命名宏需要一些额外的参数,这些参数可用于保持卫生)
拼接的工作方式基本上与Clojure中的相同。语法为,@
。因此,以下操作应有效:
(define-for-syntax (parse-highlight-rules rules)
;; ... insert missing code here ...
`(cond ,@(do-loop '() rules)))
(define-syntax highlight
(er-macro-transformer
(lambda (form rename compare)
(parse-highlight-rules (cdr form)))))
非常感谢。现在我有了一个更好的方向…只需要稍微修补一下,使碎片适合:)非常感谢。现在我有了一个更好的方向…只需要稍微修补一下,使碎片适合:)