chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: compile-time deserialization


From: Kristian Lein-Mathisen
Subject: Re: compile-time deserialization
Date: Tue, 26 Dec 2023 22:47:57 +0100


Hi,

You could try to put everything you're doing with read-string / read / open-input-file inside syntax. For example:

~/tmp> cat ./compile-time-io.scm
(define file-contents
  (let-syntax
      ((load
        (er-macro-transformer
         (lambda (x r t)
           (import chicken.io chicken.port)
           `(quote
             ,(with-input-from-file
                 "/proc/cpuinfo"
               read-lines))))))
    (load)))

(write file-contents)
(newline)

~/tmp> csc compile-time-io.scm && ./compile-time-io
( "Processor\t: AArch64 Processor rev 14 (aarch64)" 
  "processor\t: 0"
  "BogoMIPS\t: 38.40" ... )

~/tmp> grep BogoMIPS ./compile-time-io
grep: ./compile-time-io: binary file matches


K.

On Tue, Dec 26, 2023, 21:07 Al <frm.ml@mailgw.com> wrote:
Hi, suppose I need to reference data from "file.txt" in my Scheme
program. I could use read-char / read / open-input-file etc (or other
(chicken io) procedures) to read the the contents of the file into a
variable.

However, such deserialization happens at run-time. If I compile my
program into an executable, it will try to open "file.txt" at run-time.
How can I arrange for "file.txt" to be read at compile-time, and inlined
into the executable, so that the executable doesn't require access to
the original file?

The only way I can think of is to convert "file.txt" to a scheme string
definition in a separate file:

; file.scm
(define file-contents " ... ")

and include it via (include "file.scm"). Then the definition would occur
at compile-time.

But of course this requires encoding (possibly binary) files as scheme
strings, and probably an extra Makefile step to convert file.txt into
file.scm. This is not attractive -- are there other options?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]