lisp - 为什么我的 hylang 程序解析失败?

标签 lisp hy

我编写了一个解析日志的小 hylang 程序。但是,当我尝试评估它时,出现以下错误:

File "", line 8, column 38

        (setv rule (.next irule)))))))
                                   ^ LexException: Ran into a RPAREN where it wasn't expected.

有问题的函数(单独计算时也会出错)如下:

(defmain [&rest args]
  (setv inname (get args 1))
  (setv outname (get args 2))
  (with [ifile (open inname 'r')]
    (with [ofile (open outname 'w')]
      (setv tl (Tableline))
      (setv irule (iter (.get-rule tl)))
      (setv rule (.next irule))
      (for [line ifile]
        (setv match (re.match (get rule 0) line))
        (when match
          (for [(, group-num attr-data) (enumerate (get rule 1))]
            (setattr tl (get attr-data 1) (apply (get attr-data 0)
                                                 [(.group match (inc group-num))])))
          (if (. tl ready) (write ofile (str tl)))
          (setv rule (.next irule)))))))

据我所知,它是平衡的表达,所有括号都在各自的位置。为什么词法分析器会失败?

我的程序全文在这里:pastebin

最佳答案

您需要使用双引号来制作字符串。

在 lisps 中,单引号的用途与制作字符串完全不同——“引用”下一个形式。解析 lisp 代码时,像 'a 这样的表达式被转换为 (quote a)。同样,'(hello) 变为 (quote (hello))。请注意只有一个 ' 标记,因为它总是包含下一个表达式。引用形式允许您“关闭”单个表达式的评估。查找 lisp 引用 - 它非常有用,并且对于该语言的一些更强大的功能很重要。

关于lisp - 为什么我的 hylang 程序解析失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42811335/

相关文章:

python - 使用Hy宏生成Python代码

python - 用 Hy 中的索引替换列表/字典元素

python3/hy - 评估 python 中的 hy 表达式?

list - Lisp 合并模式

haskell - 在编程语言和范例的上下文中, "Pure"是什么意思?

lambda - 为什么不将函数值传递给 lambda 函数并进行评估?

python - 在 Hy 中指定元类的语法

hy - 为什么 '0 is false, but ' False 是true?

lisp - 同质性,它是如何工作的?

performance - 按需调用/按名称调用 Lisp 解释器策略的开销