Scheme/Racket: 这个 Honu lang 如何创建没有评估括号的语法?

Scheme/Racket: How does this Honu lang create syntax without evaluation parentheses?

这是用 Racket 创建的 Honu lang:https://docs.racket-lang.org/honu/Examples.html?q=hon

该语言看起来就像其他结构化语言,但它建立在需要括号 ( ) 进行评估的 Racket 之上。 Honu如何定义没有括号的语句?

我可以在 Racket 中定义一些语法,但是在评估它们时,我需要添加环绕括号:

(require syntax/parse/define)
(define-syntax-rule (while Cond Form ...)
  (do [] [(not Cond)] Form ...)
)

(define I 0)
;HOW TO RUN while WITHOUT PARENTHESES?
(while (< I 10)
  (displayln I)
  (set! I (add1 I))
)

我可以测试 Racket 代码:https://www.jdoodle.com/execute-racket-online/

Honu 已经 while。你可以这样使用它:

#lang honu

var x = 0
while x < 10 {
  printf("~a\n", x);
  x = x + 1;
}

上面的程序打印 09

Honu 还提供了一种通过 define-honu-syntax 在 Racket 世界中定义自己的宏的方法。这是一个例子:

;; while.rkt

#lang racket

(provide mywhile)
(require honu-parse
         (for-syntax syntax/parse
                     honu-parse))

(define-honu-syntax mywhile
  (lambda (code)
    (syntax-parse code #:literal-sets (cruft)
      [(_ condition:honu-expression body:honu-body . rest)
       (values
        (racket-syntax (let loop ()
                         (when condition.result
                           body.result
                           (loop))))
        #'rest
        #t)])))
;; test.honu

#lang honu

require "while.rkt";

var x = 0
mywhile x <= 10 {
  printf("~a\n", x);
  x = x + 1;
}

运行 test.honu 也应该打印 09

Honu也支持在Honu世界定义宏。请参阅 https://github.com/racket/honu/blob/master/honu/tests/macros2.honu.

中的示例