Skip to content

jnpn/vast

Repository files navigation

vast – python to elisp transpiler [alpha]

usage

To setup Vast you first need to clone it with

git clone https://github.com/jnpn/vast.git
cd vast

Now that you have it cloned, Vast can be used either as a REPL,

python src/main.py repl
>>> def f(x): return x + 1 RET
;; elisp output

Or Vast can be passed a file to transpile,

python src/main.py load-file "</path/to/file.py>"

which will then print out the transpiled code into the terminal as well as write it to a file with the same filename, but ending in .el.

tests

pytest

todos

DONEternary if
DONEvarargs
DONEmultiline repl
TODOimplement list, set, map python APIrely on dash.el as a starting point ?
TODOdesugar python idioms into FP
TODOisomorphic Visitor from ~FP tree to Lisp
TODOAdd prelude generation

details

list, set, map

list (slice, append, update, pop, …)

(defun list-slice (l &optional beg end ...step?)
  (-drop beg (-take end l)))

slice   =lisp.  drop beg (take end)
append  =lisp.  append
update  =lisp.  nth . rplaca
pop	   =lisp. cdr
push	   =lisp. cons

set (intersect, union, diff, conjugate-diff)

map (put, get, get-default)

desugar python idioms into FP (NodeTransformer)

  • Guarantees presence of a strict subset of ast nodes.
  • No more ListComp, SetComp, Aug…, …

ListComp -> Lisp

[fun gen...]

gen* [fst (name iter pred) ...] ->
(map (lambda (<lisp name>)
        <gen* ...>
     )
 (filter <lisp pred> <lisp iter>))

 gen* [] -> <lisp fun>

isomorphic NodeVisitor for FP-ish tree to Lisp

add prelude generation

  • bootstrap generic python operation add, not, is, in, …
  • prepend to emacs lisp generation.

Cover all overloadable binary operators (see grammar):

add, iadd, sub… as generic-<op>

(defun generic-add(a b)
  (cond  ((and (numberp a) (numberp b)) (+ a b))
         ((and (stringp a) (stringp b)) (string-join a b))
         ((and (floatp a) (floatp b)) (+ a b))
         ((and (characterp a) (characterp b)) (string-join a b))
         ((and (consp a) (consp b)) (append a b))
         (t (error "non supported combination" a b))))

;;; or transpile a table of monomorphic relations
'((numberp . +)
  (floatp . +)
  (stringp . string-join)
  (characterp . (lambda (&rest chars)
                  (string-join
                   (mapcar #'char-to-string chars))))
  (consp . append))
def f(a, b):
    return a + b
(defun f(a b)
  (generic-add a b))

About

python to emacs lisp transpiler

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published