SRFI 105 as a Racket language, need a few help

hello,

i'm porting SRFI 105 "Curly infix" to Racket.

I wrote a "reader" that works and the SRFI 105 is packaged with a REPL that works already with little change (only one line modified as far as ia can remember) but i'm facing a difficulty, being not easy with the Racket ecosytem of building lanaguages:

-first how can i make my implementation parse, not only the main program, but possibly include files? i.e if i have a (include "infix-file.scm") i want the reader/parser to load and parse it before the expansion step whch is too late and more difficult

here the beginning of my specialised code (the rest is as the official SRFI 105) file SRFI-105.rkt:

#lang racket
(require syntax/strip-context)
 
(provide (rename-out [literal-read read]
                     [literal-read-syntax read-syntax]))


(define (literal-read in)
  (syntax->datum
   (literal-read-syntax #f in)))
 
(define (literal-read-syntax src in)
  
  (define lst-code (process-input-code-rec in))

  `(module anything racket ,@lst-code))

;; read all the expression of program
;; a tail recursive version
(define (process-input-code-tail-rec in) ;; in: port
  (define (process-input acc)
    (define result (curly-infix-read in))  ;; read an expression
    (if (eof-object? result)
	(reverse acc)
	(process-input (cons result acc))))
  (process-input '()))


  ; ------------------------------
  ; Curly-infix support procedures
  ; ------------------------------

and here is an example of source file using it:

#lang reader "SRFI-105.rkt"
(- (+ 3 3)
   {2 + 2})

{5 + 2}

(define (fibonacci n)
  (if (< n 2)
      n
      (+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
(fibonacci 7)

(define (fib n)
  (if {n < 2}
      n
      {(fib {n - 1}) + (fib {n - 2})} ))

(fib 11)


and the obvious results:

Welcome to DrRacket, version 8.2 [cs].
Language: reader "SRFI-105.rkt", with debugging; memory limit: 128 MB.
2
7
13
89
> 

-second, i know how to make a #lang "my language.rkt" parse the following code but i do not know how to integrate the working SRFI 105 REPL in Racket ecosystem (for now it works in a separate file than parser).

here the official SRFI 105 that already work in Racket with minor changes:

  ; --------------
  ; Demo of reader
  ; --------------

(define-namespace-anchor a)
(define ns (namespace-anchor->namespace a))

;{1 + 1}
;(+ 1 1)
;2
;(define k {1 + 1})
;(define k (+ 1 1))
;#<void>
;k
;k
;2

  ; repeatedly read in curly-infix and write traditional s-expression.
  (define (process-input)
    (let ((result (curly-infix-read)))
      (cond ((not (eof-object? result))
	     (let ((rv (eval result ns)))
	       (write result) (display "\n")
	       (write rv)
	       (display "\n"))
	     ;; (force-output) ; flush, so can interactively control something else
	     (process-input)) ;; no else clause or other
	    )))

(process-input)

by the way Racket Discourse is really cool :slight_smile: instead of regular mail for editing and posting code.

Damien

2 Likes

You can override #%app to implement this feature without modifying the reader. And people have already ported SRFI 105 by doing so. See this as an example.

Also, since you only want to slightly modify the reader, it’s better to adjust the readtable rather than writing everything from scratch.

For the include issue, if you override #%app, include should work fine. Otherwise, if you want to adjust the reader, I think you need to use include/reader instead.

I'm not sure what you mean in the second question. Can you clarify?

2 Likes

thank you,
first link to happy-appis interesting but unfortunately it is inspired from SRFI 105 but not compatible for all expressions,example:
in Racket :
'{T[3]}
'(T (3))
in SRFI 105 with Guile:
scheme@(guile-user)> '{T[3]}
($bracket-apply$ T 3)
then i can rewite $bracket-apply$ and what i want but not in Racket.

unfortunately i do not understand how to build languages with readtable and #%app

include/reader sound well but how can i use it concretely in my code?

the second question perheaps would be resolved also with first one , basically it is how to have a valid REPL compatible with SRFI 105 when we already have a reader/parser for file written with SRFI 105 features.

and if i want to use (include/reader "infix-file.rkt" literal-read-syntax) i have an error unbound variable,how can i export my literal-read-syntax procedure to the included files?

it worked with require and inserting a #lang "srfi-105.rkt" at the top of the required file.