在 REPL 中使用 #lang 设置语言 [英] Setting language with #lang in the REPL

查看:31
本文介绍了在 REPL 中使用 #lang 设置语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 #lang 即时设置 REPL 中的语言,而不是使用-I"命令行参数.但这给了我错误read: #lang not enabled in the current context".

I would like to set the language in the REPL on the fly, with #lang, not using "-I" command-line argument. But this gives me the error "read: #lang not enabled in the current context".

是否有我缺少的命令行开关?或者也许我可以使用,metacommand"?我需要这个的原因是因为我希望能够将 Emacs 缓冲区发送到 Racket REPL,但是如果文件以 #lang 开头,这将不起作用.

Is there a command-line switch that I'm missing? Or maybe a ",metacommand" I can use? The reason I need this is because I would like to be able to send an Emacs buffer to the Racket REPL, but that won't work if the file starts with #lang.

推荐答案

我也无法让 C-x C-b#lang 一起工作.

I can't get C-x C-b to work with #lang either.

但是包含 #lang 的缓冲区可以发送到从 Geiser 启动的 REPL,使用 C-c C-a.这是从 Geiser 下拉菜单中切换到 REPL 并进入模块.如果我有 bugsy.rkt 的缓冲区:

But a buffer containing #lang can be sent to a REPL started from Geiser with C-c C-a. This is Switch to REPL and Enter Module from the Geiser drop down menu. If I have a buffer for bugsy.rkt:

;; bugsy.rkt
#lang racket
(define k 6)
(define j 7)
(define (f lhs rhs)
   (+ lhs rhs))

在 REPL 中输入 C-c C-a 给我这个:

Typing C-c C-a gives me this in the REPL:

racket@> ,enter "<filepath>/bugsy.rkt"
racket@bugsy.rkt>

然后我可以访问 REPL 中的模块:

I can then access the module in the REPL:

racket@bugsy.rkt> k
6
racket@bugsy.rkt> (f 3 4)
7

如果我想切换到不同的模块[或文件的缓冲区],我可以在 REPL 中使用 ,enter 命令:

If I want to switch to a different module [or buffer of a file] I can use the ,enter command in the REPL:

racket@bugsy.rkt> ,enter "clyde.rkt"
racket@clyde.rkt> ,enter "bonny.rkt"
racket@bonny.rkt>

文档中有一个 ,enter 命令示例.俯视恐龙.

There is an example of the ,enter command in the documentation. Look above the Dinosaur.

[原文]

根据 Racket 文档 #lang 有非常简单的语法,读者本质上是从#lang之后的空格字符后面的任何内容引导语言语法.这意味着在某种意义上 #lang 不在 Racket 的 [或任何其他语言的] 语法中.相反,它是阅读器的一个实现特性,它构成了更大的Racket"开发生态系统的一部分.

According to the Racket documentation #lang has very simple syntax, the reader essentially bootstraps a language syntax from whatever follows the space character after #lang. This means in some sense that #lang is not in Racket's [or any other language's] syntax. Instead it is a implementation feature of the reader which forms part of the larger "Racket" development ecosystem.

Geiser [可能还有 Quack 和球拍模式] 通过在将代码传递给 Racket REPL 之前解析 elsip 中的 #lang 来处理这个问题.在 Geiser 中,工作在 geiser-racket 中完成.el.

Geiser [and presumably Quack and racket-mode] handle this by parsing #lang in elsip before passing code to the Racket REPL. In Geiser, the work is done in geiser-racket.el.

解析函数在第 132 行:

The parsing function is at line 132:

(defun geiser-racket--language ()
  (or (cdr (geiser-racket--explicit-module))
      (save-excursion
        (goto-char (point-min))
        (if (re-search-forward "^#lang +\\([^ ]+\\)" nil t)
            (geiser-syntax--form-from-string (match-string-no-properties 1))))
      "#f"))

它被 geiser-racket--geiser-procedure 在第 166 行调用.

And it is called by geiser-racket--geiser-procedure on line 166.

(defun geiser-racket--geiser-procedure (proc &rest args)
  (case proc
    ((eval compile)
     (format ",geiser-eval %s %s %s"
             (or (car args) "#f")
             (geiser-racket--language)
             (mapconcat 'identity (cdr args) " ")))
    ((load-file compile-file)
     (format ",geiser-load %S" (geiser-racket--find-module)))
    ((no-values) ",geiser-no-values")
    (t (format ",apply geiser:%s (%s)" proc (mapconcat 'identity args " ")))))

如果现有的 Emacs 模式之一不能满足您的需求,这可能会给您一个滚动自己代码的起点.

That may give you a starting point for rolling your own code if one of the existing Emacs modes does not meet your needs.

这篇关于在 REPL 中使用 #lang 设置语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆