module StackOverflow where -- yes, the source of this post compiles as is
Run Code Online (Sandbox Code Playgroud)
如果您想先玩这个(1/2向下),请跳到下面的操作以使其正常工作.如果我稍微
停下来,你会跳到我想要的东西,你只想找到我正在寻求的帮助.
:so我在我定义的命令中ghci.conf吗? :l可以用于.hs和.lhs像往常一样的文件,但使用我的手写预处理器的.so文件?Haskell支持.lhs源文件中的文字编程,有两种方式:
\begin{code}和\end{code}.>,其他任何东西都是评论.>).Bird不跟踪类似于StackOverflow代码块的规则声音吗?
参考文献: 1. 将.ghci手册
2. GHCI haskellwiki
3. 关于尼尔·米切尔的博客:{,并:}在.ghci
我喜欢在文本编辑器中编写SO答案,我喜欢发表一个包含有效代码的帖子,但最后还是>在发布之前我必须编辑的注释块或者s,这不那么有趣.
所以,我自己写了一个预处理器.
*或开头:.起初我们不知道(dunno)这行是代码还是文本:
dunnoNow :: [String] -> [String]
dunnoNow [] = []
dunnoNow (line:lines)
| all (==' ') line = line:dunnoNow lines -- next line could be either
| otherwise = let (first4,therest) = splitAt 4 line in
if first4 /=" " --
|| null therest -- so the next line won't ever crash
|| head therest `elem` "*:" -- special chars that don't start lines of code.
then line:knowNow False lines -- this isn't code, so the next line isn't either
else ('>':line):knowNow True lines -- this is code, add > and the next line has to be too
Run Code Online (Sandbox Code Playgroud)
但如果我们知道,我们应该保持相同的模式,直到我们打出一个空白行:
knowNow :: Bool -> [String] -> [String]
knowNow _ [] = []
knowNow itsCode (line:lines)
| all (==' ') line = line:dunnoNow lines
| otherwise = (if itsCode then '>':line else line):knowNow itsCode lines
Run Code Online (Sandbox Code Playgroud)
现在我们可以获取模块名称,预处理该文件,并告诉ghci加载它:
loadso :: String -> IO String
loadso fn = fmap (unlines.dunnoNow.lines) (readFile $ fn++".so") -- so2bird each line
>>= writeFile (fn++"_so.lhs") -- write to a new file
>> return (":def! rso (\\_ -> return \":so "++ fn ++"\")\n:load "++fn++"_so.lhs")
Run Code Online (Sandbox Code Playgroud)
我曾经默默地重新定义了这个:rso命令,因为我之前使用过的let currentStackOverflowFile = ....或者currentStackOverflowFile <- return ...
没有把我带到
任何地方.
现在我需要将它放在我的ghci.conf文件中,即appdata/ghc/ghci.conf
根据说明
:{
let dunnoNow [] = []
dunnoNow (line:lines)
| all (==' ') line = line:dunnoNow lines -- next line could be either
| otherwise = let (first4,therest) = splitAt 4 line in
if first4 /=" " --
|| null therest -- so the next line won't ever crash
|| head therest `elem` "*:" -- special chars that don't start lines of code.
then line:knowNow False lines -- this isn't code, so the next line isn't either
else ('>':line):knowNow True lines -- this is code, add > and the next line has to be too
knowNow _ [] = []
knowNow itsCode (line:lines)
| all (==' ') line = line:dunnoNow lines
| otherwise = (if itsCode then '>':line else line):knowNow itsCode lines
loadso fn = fmap (unlines.dunnoNow.lines) (readFile $ fn++".so") -- convert each line
>>= writeFile (fn++"_so.lhs") -- write to a new file
>> return (":def! rso (\\_ -> return \":so "++ fn ++"\")\n:load "++fn++"_so.lhs")
:}
:def so loadso
Run Code Online (Sandbox Code Playgroud)
现在我可以保存这整篇文章LiterateSo.so并在ghci中做一些可爱的事情
*Prelude> :so StackOverflow
[1 of 1] Compiling StackOverflow ( StackOverflow_so.lhs, interpreted )
Ok, modules loaded: StackOverflow.
*StackOverflow> :rso
[1 of 1] Compiling StackOverflow ( StackOverflow_so.lhs, interpreted )
Ok, modules loaded: StackOverflow.
*StackOverflow>
Run Code Online (Sandbox Code Playgroud)
万岁!
我更愿意让ghci更直接地支持它.摆脱中间.lhs文件会很好.
此外,似乎ghci文件名完成从最短的子串开始:load确定你实际上正在做load,所以使用:lso而:so不是欺骗它.
(我不喜欢重写我在C.代码我也没有想从源代码重新编译ghci的.)
:so我在我定义的命令中ghci.conf吗? :l可以用于.hs和.lhs像往常一样的文件,但使用我的手写预处理器的.so文件?我会尝试制作一个运行SO预处理代码或标准文学预处理器的独立预处理器,具体取决于文件扩展名.然后,只需使用:set -pgmL SO-preprocessor在ghci.conf.
对于标准文学预处理器,运行unlit程序或使用Distribution.Simple.PreProcess.Unlit.
这样,:load文件名完成就可以正常工作了.
GHCI按顺序将4个参数传递给预处理器:-h标签,源文件名和目标文件名.预处理器应该读取源并写入目标.标签用于输出#line编译指示.如果不改变源的行数(即用--注释或空行替换"注释"行),则可以忽略它.
| 归档时间: |
|
| 查看次数: |
1883 次 |
| 最近记录: |