0
votes

This is the Honu lang created with Racket: https://docs.racket-lang.org/honu/Examples.html?q=hon

The language looks just like other structured langs but it's built on top of Racket which requires parentheses ( ) for evaluation. How does Honu define statements without parentheses?

I can define some syntaxes in Racket, but when evaluate them, I need to add the wrapping parentheses:

(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))
)

I can test the Racket code on: https://www.jdoodle.com/execute-racket-online/

1

1 Answers

2
votes

Honu has while already. You can use it like this:

#lang honu

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

The above program prints 0 to 9.

Honu also provides a way to define your own macros via define-honu-syntax in Racket world. Here's an example:

;; 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;
}

Running test.honu should print 0 to 9, too.

Honu also supports defining macros in Honu world. See an example at https://github.com/racket/honu/blob/master/honu/tests/macros2.honu.