我有一个“QuasiQuoter”,它在Haskell的源代码中很有用,但也是一个独立的应用程序。所以,我需要能够运行QuasiQuoter
在shell - mygrammar 'someCommand'
中Haskell -mygrammar 'someCommand'
运行时(运行时编译)编译期间的
第一部分很简单,但是如果用运行时生成的一些代码调用编译器,那么第二部分可能会有点笨拙。
我想用Haskell中的一些不错的方法解决第二部分的问题,它不只是接受源代码,而是接受QuasyQuoter数据类型,这样代码就不那么笨拙了。但我找不到这样的编译方法。
你知道什么吗?谢谢。
使用实例
Haskell
该函数采用元组(a,b,c,d,e),并返回带有产品的字符串列表。
function = [lsql| {1..5}, r=[ a.* |> (*) ], "Product of a.1 * a.2 * ... * a.5 is &a.r"|]
Bash
该命令使用至少5个数字列从stdin csv读取,并返回其产品列表(每行一个)。
lsql-csv '-, r=[ a.* |> (*) ], "Product of a.1 * a.2 * ... * a.5 is &a.r"'
发布于 2022-02-07 15:24:46
我认为问题是如何在准商程序和其他代码块之间以统一的方式解析和处理字符串。如果这个解释是对的那你就..。就这么做吧。例如:
-- implementation of these is left to the reader, but can use standard Haskell
-- programming techniques and libraries, like parsec and ADTs and stuff
command :: Parser Command
interpret :: Command -> IO ()
jit :: Command -> Exp -- or Q Exp
然后,在您的lsql-csv.hs
中,您将编写以下内容
main = do
[s] <- getArgs
case parse command s of
Left err -> die (show err)
Right com -> interpret com
在你的LSql/CSV/QQ.hs
中,你会写一些类似的东西
lsql = QuasiQuoter { quoteExp = \s -> case parse command s of
Left err -> qReport True (show err) >> fail ""
Right com -> return (jit com) -- or just jit com if that's already a Q Exp
}
https://stackoverflow.com/questions/71016567
复制相似问题