- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有自己的 JavaScript Lisp 解释器,我已经工作了一段时间,现在我想像在 Common Lisp 中一样实现阅读器宏。
我已经创建了 Streams(除了 、@、` '
等特殊符号外,几乎可以正常工作)但是当它加载包含脚本(lisp 文件)的页面时,浏览器会卡住几秒钟有 400 行代码)。这是因为我的流是基于子串函数的。如果我先拆分 token ,然后使用迭代 token 的 TokenStream,它就可以正常工作。
所以我的问题是,字符串流真的是 Common Lisp 中的东西吗?你能添加阅读器宏来创建像 CL 中的 Python 这样的全新语法吗,这简化了我可以实现 """
宏的问题(不确定你是否可以有 3 个字符作为阅读器宏)或其他字符例如,将在 lisp 中实现模板文字:
(let ((foo 10) (bar 20))
{lorem ipsum ${baz} and ${foo}})
或
(let ((foo 10) (bar 20))
""lorem ipsum ${baz} and ${foo}"")
或
(let ((foo 10) (bar 20))
:"lorem ipsum ${baz} and ${foo}")
会产生字符串
"lorem ipsum 10 and 20"
在 Common Lisp 中是否可能实现类似的功能?将 #\{
或 #\:
作为阅读器宏实现有多难?
我能想到的在 Lisp 中使用模板文字的唯一方法是这样的:
(let ((foo 10) (bar 20))
(tag "lorem ipsum ${baz} and ${foo}")))
其中 tag 是一个宏,它返回带有 ${} 作为自由变量的字符串。阅读器宏是否也可以返回已评估的 lisp 代码?
还有一个问题,你能不能像这样实现阅读器宏:
(list :foo:bar)
(list foo:bar)
其中:是阅读器宏,如果它在符号之前,它将符号转换为
foo.bar
如果它在里面就会抛出错误。我问这个是因为基于标记的宏 :foo:bar
和 foo:bar
将是符号,不会被我的阅读器宏处理。
还有一个问题,阅读器宏可以放在一行,第二行使用吗?这肯定只有在字符串流中才有可能,而且根据我的测试,在用 JavaScript 编写的解释器中这是不可能的。
最佳答案
例如,除了“从头开始实现您自己的 token 解释器”之外,很难以任何方式干预 token 的解释,这在某种意义上存在一些局限性。但是,好吧,如果你只想这样做,你可以这样做:问题是你的代码需要像现有代码一样处理数字和事物,而像浮点解析这样的事情是非常繁琐的。
但是与宏字符关联的宏函数获取正在读取的流,它们可以自由地读取尽可能多或尽可能少的流,并返回任何类型的对象(或不返回任何对象,这是注释是如何实现的)。
我强烈建议阅读章节 2 & 23 hyperspec,然后玩一个实现。当您使用该实现时,请注意通过与读者混在一起非常容易地完全楔入事物。至少我会建议这样的代码:
(defparameter *my-readtable* (copy-readtable nil))
;;; Now muck around with *my-readtable*, *not* the default readtable
;;;
(defun experimentally-read ((&key (stream *standard-input*)
(readtable *my-raedtable*)))
(let ((*readtable* readtable))
(read stream)))
这至少给了你一些从灾难中恢复的机会:如果你可以一次中止 experimentally-read
那么你就回到了 *readtable*
的位置明智的。
这是一个相当无用的例子,它显示了你可以用宏字符破坏多少语法:一个宏字符定义将导致 ( ...)
被读取为一个字符串。这可能没有完全调试,正如我所说,我看不出它有什么用。
(defun mindless-parenthesized-string-reader (stream open-paren)
;; Cause parenthesized groups to be read as strings:
;; - (a b) -> "a b"
;; - (a (b c) d) -> "a (b c) d"
;; - (a \) b) -> "a ) b"
;; This serves no useful purpose that I can see. Escapes (with #\))
;; and nested parens are dealt with.
;;
;; Real Programmers would write this with LOOP, but that was too
;; hard for me. This may well not be completely right.
(declare (ignore open-paren))
(labels ((collect-it (escaping depth accum)
(let ((char (read-char stream t nil t)))
(if escaping
(collect-it nil depth (cons char accum))
(case char
((#\\)
(collect-it t depth accum))
((#\()
(collect-it nil (1+ depth) (cons char accum)))
((#\))
(if (zerop depth)
(coerce (nreverse accum) 'string)
(collect-it nil (1- depth) (cons char accum))))
(otherwise
(collect-it nil depth (cons char accum))))))))
(collect-it nil 0 '())))
(defvar *my-readtable* (copy-readtable nil))
(set-macro-character #\( #'mindless-parenthesized-string-reader
nil *my-readtable*)
(defun test-my-rt (&optional (stream *standard-input*))
(let ((*readtable* *my-readtable*))
(read stream)))
现在
> (test-my-rt)
12
12
> (test-my-rt)
x
x
> (test-my-rt)
(a string (with some parens) and \) and the end)
"a string (with some parens) and ) and the end"
关于macros - Common Lisp 中阅读器宏的局限性是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56306111/
我会保持简短:我正在尝试循环遍历画廊的 xml 文档。我有一个应该可以工作的脚本,但没有。谁能告诉我哪里做错了? 我不想让它变得更长,因为问题很简单,并且从昨天开始就一直在思考这个问题,这是我得到的最
我正在使用 PHPExcel从 Excel 工作表中读取数据并存储在 mysql 表中,直到现在我能够上传 .xls 和 .xlsx 文件,在上传 xls 后我得到了下面的数据表结构 name
我正在构建一个在线 Rss 阅读器。我希望能够与文章标题和描述一起显示图像。 我正在使用谷歌提要 API 从 CNN ( http://rss.cnn.com/rss/edition.rss ) 读取
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 10年前关闭。 Improve this
我正在开发 BB 应用程序,我需要在其中实现 QR 阅读器或扫描仪。我知道 RIM 在 OS6 和 ZXing 中支持它的库,但实际上我需要阅读一个示例,说明如何在我的代码中实现它。 最佳答案 你可以
我将工作应用程序的 clojurescript 版本升级到 0.0-2030,突然读取器/读取字符串返回空值,例如: (js/alert (str "reader returned [" (read
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我想编写一个小应用程序,可以从任何 RSS 提要 URL 中提取 RSS 提要。如果有人能给我关于如何实现这一目标的非常基本的帮助? 我刚刚开始接触 AJAX 之类的东西,所以任何帮助将不胜感激。 谢
我已经创建了一个 RSS 阅读器,如下教程所示: http://techiedreams.com/android-rss-reader-part-3-action-bar-with-animated-
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
请帮我解决这个问题。我是 extJs 的新手,我需要一点帮助。我有这个代码 Ext.onReady(function() { var datesStore = new Ext.data.JsonSt
我需要一个 CSV 读取器,它将输出 NxN(加权)邻接矩阵(N 从一开始就不知道)。当然,我可以使用 strtok() 和 friend 来解析它,但是如果某些东西已经可用并且足够完整,我将不胜感激
实际上我想开发一个 EPUB 格式的图书列表(列表将从网络服务中检索)。 当选择一个项目(一本书)时,它应该会提示手机中安装了可用的 epub 阅读器。类似于“分享”将调用 SMS、FB、Twitte
我是 Java 的新手,但真的想在这方面做得更好。我正在尝试编写一个简单的 RSS 阅读器。这是代码: import java.io.*; import java.net.*; public clas
我按照一个简单的教程 (http://www.cse.nd.edu/courses/cse40814/www/RSS_Android.pdf) 将给定 URL 中的 RSS 提要读取到 ListVie
最近我一直在尝试学习如何在 Xcode 6 beta 中制作 RSS 阅读器应用程序的教程,尽管我使用的是 Xcode 6.1。我遇到了一行似乎是错误的。 完整代码为: import UIKit cl
代码使用条形码扫描仪检查条形码。Search_code 由用户(键盘)填写,insert_code 由条码扫描仪自动填写。目前,如果在条形码扫描仪值中引入两个输入,则代码可以工作,这对我来说不起作用。
是否可以通过编程方式更改 Windows 中的默认 PDF 阅读器。 例如, 如果我的机器中的默认阅读器是“Foxit”,但我需要在 C# 或 javascript 中将默认阅读器应用程序更改为“Ad
我想将 CSV 文件的每一行与其自身以及一列中的每一行进行比较。 例如,如果列值是这样的: 值_1 值_2 值_3 代码应该选择 Value_1 并将其与 Value_1(是的,也与它本身)、Valu
我有以下片段 import csv data = {} with open('data.csv', 'rb') as csvfile: spamreader = csv.reader(csvf
我是一名优秀的程序员,十分优秀!