chsasank / llama.lisp

Lisp dialect designed for HPC and AI
GNU Lesser General Public License v2.1
15 stars 6 forks source link

Prelisp: Preprocessor for S-expression/Lisp-like Languages #36

Closed GlowingScrewdriver closed 3 months ago

GlowingScrewdriver commented 5 months ago

Prelisp currently implements macro support. Rather than define the macro language, macro evaluation is offloaded to the Guile Scheme implementation. This means that macro bodies are Scheme procedures!

Not only does this make it trivial to write macros that return s-expressions, but also gives the programmer access to a powerful set of features.

Consider, for example, the first test added as part of this PR: src/tests/prelisp/macro.sexp. A macro called time is defined and used in a C-Lisp program.

(time (lambda (t)
    (car (mktime (car (strptime "%d%m%Y" t))))))
; ... ;
(c-lisp
    (define ((print int) (n int)))

    (define ((main void))
        (eval-macro declare-int val)
        (set val (eval-macro time "19062024"))
        (call print val)
        (ret)))

What the macro body does is to accept a string of the form "MMDDYYYY", convert it to seconds since epoch and return it; the resulting integer overwrites the macro invocation to produce the following C-Lisp program (indented by hand, otherwise identical to the original output):

(c-lisp
   (define ((print int) (n int)))
   (define ((main void))
      (declare (val int))
      (set val 1718735400)
      (call print val)
      (ret)))
chsasank commented 3 months ago

Superseded by #83