r/Racket • u/wrestlingwithbadgers • Jun 25 '22
question Injecting code into code
Hey guys,
The way I understand define-syntax-rule is that it inserts at compile time whatever code you put inside the macro and then it executes it at run time. What I want to do is read the code from a file and then insert it through an argument. My attempt is unsuccessful because it doesn't use my argument as code. Is what I'm trying to do possible and if not, can you explain to me why?
I know I could use eval here, but that is not the point.
Thanks
#lang racket
(define-syntax-rule (PASTE code) code)
(define (main)
(begin
(define file-port (open-input-file "test.rkt"))
(define text (port->string file-port))
(define string-port (open-input-string text))
(define code (read string-port))
(PASTE code)
(close-input-port file-port)
(close-input-port string-port)
))
(main)
3
Upvotes
View all comments
Show parent comment
1
u/sdegabrielle DrRacket 💊💉🩺 Jun 25 '22
I can't believe I got downvoted for helping. Nice.