How to run REPL in command-line Racket with another language?

In DrRacket I can simply set #lang basic-demo-3, then in REPL say

> let x = 5
> print x
5

In command line I tried

racket -I basic-demo-3

but it doesn't work.

$ racket -I basic-demo-3
Welcome to Racket v8.7 [cs].
> let x = 5
; let: undefined;
;  cannot reference an identifier before its definition
;   in module: top-level
; [,bt for context]
> ; x: undefined;
;  cannot reference an identifier before its definition
;   in module: top-level
; [,bt for context]
> ; =: undefined;
;  cannot reference an identifier before its definition
;   in module: top-level
; [,bt for context]
> 5
>
1 Like

My understanding is that using a non sexp reader in the command-line REPL is a relatively new feature (https://docs.racket-lang.org/reference/interaction-info.html, added in Racket 8.3). So far, Rhombus (https://github.com/racket/rhombus-prototype/blob/master/rhombus/private/runtime-config.rkt) seems to be the only language that uses this feature.

Chance is that this basic-demo-3 doesn't support the command-line REPL.

Note that DrRacket REPL has the capability to support a non sexp reader if the language cooperates. So you might want to try that.

2 Likes

But it supports: Beautiful Racket: Closing the loop: basic

Note that DrRacket REPL has the capability to support a non sexp reader if the language cooperates. So you might want to try that.

The same code works in DrRacket, but I cannot use DrRacket in text mode.

So far, Rhombus

Could you show, how to user Rhombus in text-mode Racket?

You can run racket -I rhombus after installing the rhombus-prototype package.

I have found. One need to write configure-expand submodule for console Racket.

If you want to use the "basic" language from Beautiful Racket in the command-line REPL, you need to require "setup.rkt" and "elements.rkt" and provide the bindings of "elements.rkt" as well as #%top-interaction #%app #%top #%datum to the expander macro "b-module-begin" in "expander.rkt". If you change the xrepl to use readline, you can enter basic expressions in the REPL, have them be evaluated and use REPL-history. Coloring doesn't work.

; "expander.rkt"

#lang br/quicklang
(require "struct.rkt" "run.rkt" "elements.rkt" "setup.rkt")
(provide (rename-out [b-module-begin #%module-begin])
         (all-from-out "elements.rkt"))

(define-macro (b-module-begin (b-program LINE ...))
  (with-pattern
      ([((b-line NUM STMT ...) ...) #'(LINE ...)]
       [(LINE-FUNC ...) (prefix-id "line-" #'(NUM ...))]
       [(VAR-ID ...) (find-property 'b-id #'(LINE ...))]
       [(IMPORT-NAME ...)
        (find-property 'b-import-name #'(LINE ...))]
       [(EXPORT-NAME ...)
        (find-property 'b-export-name #'(LINE ...))]
       [((SHELL-ID SHELL-IDX) ...)
        (make-shell-ids-and-idxs caller-stx)] 
       [(UNIQUE-ID ...)
        (unique-ids
         (syntax->list #'(VAR-ID ... SHELL-ID ...)))])
    #'(#%module-begin
       (module configure-runtime br
         (require basic/setup)
         (do-setup!))
;;;;;;;;;;;;;;;;; this is added! ;;;;;;;;;;;;;;;;;;;;;;;
       (require
         "setup.rkt"
         "elements.rkt")
       (provide
        (all-from-out "elements.rkt")
        #%top-interaction #%app #%top #%datum)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
       (require IMPORT-NAME) ...
       (provide EXPORT-NAME ...)
       (define UNIQUE-ID 0) ...
       (let ([clargs (current-command-line-arguments)])
         (set! SHELL-ID (get-clarg clargs SHELL-IDX)) ...)
       LINE ...
       (define line-table
         (apply hasheqv (append (list NUM LINE-FUNC) ...)))
       (parameterize
           ([current-output-port (basic-output-port)])
         (void (run line-table))))))

(define (get-clarg clargs idx)
  (if (<= (vector-length clargs) idx)
      0
      (let ([val (vector-ref clargs idx)])
        (or (string->number val) val))))

(begin-for-syntax
  (require racket/list)

  (define (unique-ids stxs)
    (remove-duplicates stxs #:key syntax->datum))

  (define (find-property which line-stxs)
    (unique-ids
     (for/list ([stx (in-list (stx-flatten line-stxs))]
                #:when (syntax-property stx which))
       stx)))

  (define (make-shell-ids-and-idxs ctxt)
    (define arg-count 10)
    (for/list ([idx (in-range arg-count)])
      (list (suffix-id #'arg idx #:context ctxt) idx))))
wurmli@cirrus7:~$ racket -I basic/sample
Welcome to Racket v8.10 [cs].
1
2
3
> div(7,6)
1.1666666666666667
> let z = 99
> z
99
> 

Nice catch! I was wrong about this. I see now that current-interaction-info is really about “syntax coloring and indentation support” in the REPL, but the REPL functionality doesn’t require it. Using configure-runtime suffices.