BINFIX
Viktor Cerovski, Feb 2017.
BINFIX (blend from "Binary Infix") is a poweful infix syntax notation for Sexpressions of Common LISP ranging from simple arithmetic and logical forms to whole programs.
It is in experimental phase with a few important new features still to come. One of them, available from v0.16, is use of a single ;
symbol as a formseparating symbol in implicitprogn, expression terminator for SETFs, or as end of LET binds symbol or local functions definition. There is also def
, for defining things.
The most recent one is the same priority OPs (since v0.20).
Once the rest of them have been implemented, BINFIX will go to RC and then to reference 1.0 version.
Quicklisp makes the downloading/installation/loading trivial:
(ql:quickload :binfix)
After loading the package, the next step is to allow use of its symbols
(usepackage :binfix)
BINFIX is developed using SBCL, and checked to work fine with CLISP, and Clozure CL, while with ECL passes tests when handloaded but does not go through the package system yet (tried with v16.1.2).
BINFIX shadows !
and symbolmacrol
in CLISP , @
in Clozure CL and ECL, as well as var
(sbdebug:var
) in SBCL.
The latest version is available at gihub, and can be obtained by
git clone https://github.com/vcerovski/binfix
There is also a syntaxhighlighting file for vim
editor, binfix.vim
. Its installation consists of copying it into vimrc/syntax folder, which is on Linux located at ~/.vim/syntax
(should be created if it doesn't exist.)
Once installed, after loading a LISP file, LISP+BINFIX syntax highlighting can be activated by :set syntax=binfix
. Loading can be done automatically by adding
filetype on
au BufNewFile,BufRead *.lisp,*.cl set syntax=binfix
to .vimrc
.
Generally, quoting a BINFIX expression in REPL will produce the corresponding Sexpression.
For easier comparison of input and output forms in following examples, LISP printer is first setq
(operation =.
) to lowercase output with
{*printcase* =. :downcase}
=> :downcase
BINFIX is a freeform notation (just like Sexpr), i.e any number of empty spaces (including tabs and newlines) between tokens is treated the same as a single white space.
### Arithmetic and logical expressions
Classic math stuff:
{2 * 3 + 4}
=> 10
'{a * {b + c}}
=> (* a (+ b c))
'{ {x + y} / x * y}
=> ( (/ (+ x y) (* x y)))
'{0 < x < 1 && y >= 1  y >= 2}
=> (or (and (< 0 x 1) (>= y 1)) (>= y 2))
'{ f x  g x  h x}
=> ( ( (f x)) (g x) (h x))
Expressions like {(f x y) * (g a b)}
and {{f x y} * {g a b}}
generally produce the same result. The inner brackets, however, can be removed:
'{sqrt x * sin x}
=> (* (sqrt x) (sin x))
'{A ! i .= B ! j + C ! k}
=> (setf (aref a i) (+ (aref b j) (aref c k)))
'{a ! i j += b ! i k * c ! k j}
=> (incf (aref a i j) (* (aref b i k) (aref c k j)))
'{listp A && car A == 'x && cdr A  A}
=> (or (and (listp a) (eql (car a) 'x) (cdr x)) a)
Operation :.
stands for cons
. For instance,
{2 :. loop for i to 9 collect i}
=> (2 0 1 2 3 4 5 6 7 8 9)
with the familiar behaviour:
{1 :. 2 :. 3 equal '(1 2 . 3)}
=> t
{1 :. 2 :. 3 :. {} equal '(1 2 3)}
=> t
### Lambdas, definitions and type annotations
'{x > sqrt x * sin x}
=> (lambda (x) (* (sqrt x) (sin x)))
'{x :singlefloat > sqrt x * sin x}
=> (lambda (x) (declare (type singlefloat x)) (* (sqrt x) (sin x)))
'{x y > {x  y}/{x + y}}
=> (lambda (x y) (/ ( x y) (+ x y)))
Mixing of notations works as well, so each of the following
{x y > / ( x y) (+ x y)}
{x y > ( x y)/(+ x y)}
{x y > (/ ( x y) (+ x y))}
produces the same form.
Fancy way of writing {2 * 3 + 4}
{x > y > z > x * y + z @ 2 @ 3 @ 4}
=> 10
Quoting reveals the expanded Sexpr
'{x > y > z > x * y + z @ 2 @ 3 @ 4}
=>
(funcall (funcall (funcall
(lambda (x) (lambda (y) (lambda (z) (+ (* x y) z))))
2) 3) 4)
Indeed, @
is leftassociative, standing for funcall
.
More complicated types can be also explicitely given after an argument,
'{x :or symbol number > x :. x}
=>
(lambda (x) (declare (type (or symbol number) x)) (cons x x))
mapcar
is also supported:
'{x > sin x * sqrt x @. (f x)}
=>
(mapcar (lambda (x) (* (sin x) (sqrt x))) (f x))
Alternatively, it is possible to use the expressiontermination symbol ;
,
{x > sin x * sqrt x @. f x;}
to the same effect.
reduce
is represented by @/
,
'{#'max @/ x y > abs{x  y} @. a b}
=>
(reduce #'max (mapcar (lambda (x y) (abs ( x y))) a b))
and other maps have their @
's as well.
Factorial fun:
'{f n :integer := if {n <= 0} 1 {n * f {1 n}}}
=>
(defun f (n)
(declare (type integer n))
(if (<= n 0)
1
(* n (f (1 n)))))
Function documentation, local declarations, local bindings and comments have a straightforward syntax:
'{g x := "Auxilary fn."
declare (inline)
let x*x = x * x; ;; Note binds termination via ;
x*x / 1+ x*x}
=>
(defun g (x)
"Auxilary fn."
(declare (inline))
(let ((x*x (* x x)))
(/ x*x (1+ x*x))))
<a name="&optional"> #### &optional
is optional
Explicitely tailrecursive version of f
'{fac n m = 1 :=
declare (integer m n)
if {n <= 0} m
{fac {n  1} {n * m}}}
=>
(defun fac (n &optional (m 1))
(declare (integer m n))
(if (<= n 0)
m
(fac ( n 1) (* n m))))
As you may by now expect, the following is also permited
{fac n :integer m :integer = 1 :=
if {n <= 0} m
{fac {n  1} {n * m}}}
suppliedp variable var
for an optional/keyword argument is given by ?var
after the assignment.
Version of fac
with a local recursive function f
:
{fac n :integer :=
labels
f n m := {if {n = 0} m
{f (1 n) {n * m}}}
f n 1}
Another syntax to specify a local function is to use a single ;
as in
{fac n :integer :=
labels
f n m := if {n = 0} m
{f (1 n) {n * m}};
f n 1}
All three above definitions of fac
are transformed by binfix
to
(defun fac (n)
(declare (type integer n))
(labels ((f (n m)
(if (= n 0)
m
(f (1 n) (* n m)))))
(f n 1)))
which can be demonstrated by simply evaluating the quoted expressions.
The same syntax is used also in the case of flet
and macrolet
.
The following two generic versions of f
'{f n :integer : if {n <= 0} 1 {n * f {1 n}}}
'{f (n integer): if {n <= 0} 1 {n * f {1 n}}}
both produce
(defmethod f ((n integer))
(if (<= n 0)
1
(* n (f (1 n)))))
:
supports also eqlspecialization via ==
op, analogous to the way =
is used for optional arguments initialization, as well as an optional method qualifier, given as the first argument after the method name, that can be either a keyword or an atom surrounded by parens (i.e :around
, (reduce)
etc.)
Macros are defined via :==
operation, similar to the previous examples. See Sec. Support for macros.
#### Type annotations, declarations and definitions
The examples shown so far demonstrate the possibility to typeannotate symbols in binds and lambdalists by an (optional) keyword representing the type (for instance :fixnum
, :myclass
, :simplearray singlefloat
, :or symbol number
, :{symbol or number}
, etc.)
OPs that represent LISP forms which allow declaration(s), in BINFIX can have in addition to the standard (declare ...)
form also unparenthesized variant:
'{f x :fixnum y = 2 :=
declare (inline)
declare (fixnum y)
x + y ** 2}
=>
(defun f (x &optional (y 2))
(declare (type fixnum x))
(declare (inline))
(declare (fixnum y))
(+ x (expt y 2)))
Another way to declare x
and y
is
'{f x y = 2 :=
declare x y :fixnum
declare (inline)
x + y ** 2}
=>
(defun f (x &optional (y 2))
(declare (inline))
(declare (fixnum x y))
(+ x (expt y 2)))
Operation :>
can be used to specify function type. For example, in SBCL 1.1.17 function sin
has declared type that can be written as
'{number :> singlefloat 1.0 1.0 
doublefloat 1.0 1.0 
complex singlefloat 
complex doublefloat .x. &optional}
=>
(function (number)
(values
(or (singlefloat 1.0 1.0)
(doublefloat 1.0 1.0)
(complex singlefloat)
(complex doublefloat))
&optional))
Type definitions are given using :type=
OP, as in
`{mod n :type= `(integer 0 (,n))}
=>
(deftype mod (n) `(integer 0 (,n)))
Program typically consists of a number of definitions of functions, constants, parameters, types, etc. The operation def
is introduced to facilitate their easy writing:
'{def parameter *x* = 1 *y* = 2
def struct point x y z
def f x := sqrt x * sin x}
=>
(progn
nil
(defparameter *x* 1)
(defparameter *y* 2)
(defstruct point x y z)
(defun f (x) (* (sqrt x) (sin x))))
As it is clear from the example, the definitions are wrapped up in progn
.
More detailed definitions are also straightforward to specify:
'{def parameter
*x* :fixnum = 1
*y* :fixnum = 2;
struct point "Point"
:printfunction {p s d >
declare (ignore d)
withslots (x y z) p
(format s "#<~$ ~$ ~$>" x y z)}
:constructor createpoint (x y z = 0f0)
x :singlefloat = 0f0
y :singlefloat = 0f0
z :singlefloat = 0f0
def f x :singlefloat :=
declare (inline)
sqrt x * sin x}
=>
(progn
nil
(declaim (type fixnum *x*)
(type fixnum *y*))
(defparameter *x* 1)
(defparameter *y* 2)
(defstruct
(point
(:printfunction
(lambda (p s d)
(declare (ignore d))
(withslots (x y z)
p
(format s "#<~$ ~$ ~$>" x y z))))
(:constructor createpoint (x y &optional (z 0.0))))
"Point"
(x 0.0 :type singlefloat)
(y 0.0 :type singlefloat)
(z 0.0 :type singlefloat))
(defun f (x)
(declare (inline))
(declare (type singlefloat x))
(* (sqrt x) (sin x))))
def class
syntax is like defclass
without parens. For this to work, class options (:documentation
and :metaclass
) have to be given before description of slots, while :defaultinitargs
comes last as usual, just unparenthesized (see example.)
def
ining of symbols follows the same syntax as let
binding, which is covered next.
LET symbolbinding forms (let
, let*
, symbolmacrolet
, etc) in BINFIX use =
with an optional typeannotation:
'{let x :bit = 1
y = {2 ** 3}
z = 4
x + y * z}
=>
(let ((x 1) (y (expt 2 3)) (z 4))
(declare (type bit x))
(+ x (* y z)))
A single ;
can be used as a terminator of bindings:
'{let x :bit = 1
y = 2 ** 3
z = f a;
x + y * z}
=>
(let ((x 1) (y (expt 2 3)) (z (f a)))
(declare (type bit x))
(+ x (* y z)))
Finally, a single ;
can also be used to separate forms in implicitprogn, as in
'{let x :bit = 1
y = 2 ** 3
z = f a; ;; end of binds
print "Let binds"; ;; 1st form
x + y * z} ;; 2nd form of implicitprogn
=>
(let ((x 1) (y (expt 2 3)) (z (f a)))
(declare (type bit x))
(print "Let binds")
(+ x (* y z)))
Nesting of let
s without parens follows the rightassociativity
'{let a = f x;
if a
(g x)
let b = h x;
f b}
=>
(let ((a (f x)))
(if a
(g x)
(let ((b (h x)))
(f b))))
Note the three levels of parens gone.
In addition to =.
and .=
OPs representing, respectively, a single setq
and setf
assignment, multiple assignments via SETs can be done using =
,
'{psetq x = cos a * x + sin a * y
y =  sin a * x + cos a * y}
=>
(psetq x (+ (* (cos a) x) (* (sin a) y))
y (+ ( (* (sin a) x)) (* (cos a) y)))
If it is necessary to remove repeating sin a
and cos a
, it is easy to use let
,
{let sin = sin a
cos = cos a;
psetq x = cos * x + sin * y
y =  sin * x + cos * y}
and in the case of SETF assignments, RHS are represented with a single expression,
'{psetf a ! 0 = {a ! 1}
a ! 1 = {a ! 0}}
=>
(psetf (aref a 0) (aref a 1)
(aref a 1) (aref a 0))
Alternatively, it is possible to use a single ;
as an expressiontermination symbol,
'{psetf a ! 0 = a ! 1; ;; expr. termination via single ;
a ! 1 = a ! 0}
=>
(psetf (aref a 0) (aref a 1)
(aref a 1) (aref a 0))
It is also possible to mix infix SETFs with other expressions:
'{f x + setf a = b
c = d;
* h a c}
=>
(+ (f x)
(*
(setf a b
c d)
(h a c)))
An implicit progn
in BINFIX is achieved with a single ;
separating the forms forming the progn. In all cases (>
, :=
, :
and LETs) the syntax is following that of the LET example above.
As expected, other prog
s have to be explicitly given,
'{x > prog2 (format t "Calculating... ")
{f $ x * x}
(format t "done.~%")}
or
'{x > prog2
format t "Calculating... ";
f {x * x};
format t "done.~%"}
both producing the following form
(lambda (x)
(prog2 (format t "Calculating... ") (f (* x x)) (format t "done.~%")))
Since BINFIX is a freeform notation, the following oneliner also works:
'{x > prog2 format t "Calculating... "; f{x * x}; format t "done.~%"}
Binfix <&
stands for prog1
,
'{x > {f {x * x} <&
format t "Calculation done.~%"}}
=>
(lambda (x) (prog1 (f (* x x)) (format t "Calculation done.~%")))
while multiplevalueprog1
is given by <&..
.
Infix $
is a vanishing OP, leaving only its arguments, effectivelly splitting the list in two parts.
'{f $ g $ h x y z}
=> (f (g (h x y z)))
So its effect here is similar to $
in Haskell. Or perphaps:
'{declare $ optimize (speed 1) (safety 1)}
=> (declare (optimize (speed 1) (safety 1)))
$
plitter also allows writing a shorter cond
, as in
(cond {p x $ f x}
{q x $ g x}
{r x $ h x}
{t $ x})
compared to the equivalent
(cond ((p x) (f x))
((q x) (g x))
((r x) (h x))
(t x))
Another splitter is ?
, which can be used instead of $
in the previous example, as well as described in the next section.
### Multiplechoice forms (cond
, case
, ...)
An alternative syntax to describe multiplechoice forms is to use ?
and ;
{cond p x ? f x;
q x ? g x;
r x ? h x;
t ? x}
See also ordinal
example below.
### Destructuring, multiple values
Multiple values (values
) are represented by .x.
, multiplevaluebind
by =..
, and destructuringbind
by ..=
'{a (b) c ..= (f x) a + 1 .x. b + 2 .x. c + 3}
=>
(destructuringbind (a (b) c) (f x) (values (+ a 1) (+ b 2) (+ c 3)))
Another way to write the same expr:
'{a (b) c ..= (f x) values a + 1; b + 2; c + 3}
multiplevaluecall
is represented by .@.
'{#'list .@. 1 '(b 2) 3}
=>
(multiplevaluecall #'list 1 '(b 2) 3)
=>
(1 (b 2) 3)
Both ..=
and =..
can be nested,
'{a b c =.. (f x)
x y z =.. (g z)
a * x + b * y + c * z}
=>
(multiplevaluebind (a b c)
(f x)
(multiplevaluebind (x y z) (g z) (+ (* a x) (* b y) (* c z))))
Loops can be also nested without writing parens:
'{loop for i = 1 to 3
collect loop for j = 2 to 4
collect {i :. j}}
=>
(loop for i = 1 to 3
collect (loop for j = 2 to 4
collect (cons i j)))
Mappings and function applications are what @
ops are all about, as summarized in the following table,
@

funcall

@.

mapcar

@..

maplist

@n

mapcan

@.n

mapcon

.@

mapc

..@

mapl

@/

reduce

@@

apply

.@.

multiplevaluecall

They all have the same priority and are rightassociative, and, since they bind weaker than >
, are easy to string together with lambdas, as in a mapreduce expr.
{'max @/ x y > abs{x  y} @. a b}
The following table summarizes BINFIX OPs for indexing, from the weakest to the strongest binding OP:
<td><code>svref</code></td> </tr>
<td><code>elt</code></td> </tr>
<td><code>nth</code></td> </tr>
<td><code>rowmajoraref</code></td></tr>
<td><code>bit</code></td> </tr>
<td><code>aref</code></td> </tr>
<td><code>bit</code></td> </tr>
<td><code>aref</code></td> </tr>
thcdr

nthcdr

thbit

logbitp

!.. thvalue

nthvalue

!.


.!


th


!!.


.!!.


!!


.!.


!

!..
and thvalue
are mere synonyms and thus of the same priority, as are .!
!.
and !!.
, while !!
is a weaker binding !
, allowing easier writting of expr. with arithmetic operations with indices, like
{a !! i + j}
{a !! i + j; 1 k;}
etc. In the same relation stand .!.
and .!!.
Integer bitlogical BINFIX ops are given with a .
after the name of OP, while bitarray version of the same OP with .
before and after the name. For instance, {a or. b}
transforms to (logior a b)
, while {a .or. b}
transforms to (bitior a b)
.
If BINFIX terms only are inserted under backquote, everything should work fine,
'{let t1 = 'x
t2 = '{x + x}
`{x > ,t1 / ,t2}}
=>
(let ((t1 'x) (t2 '(+ x x)))
`(lambda (x) (/ ,t1 ,t2)))
Replacing, however, BINFIX operations inside a backquoted BINFIX will not work. This is currently not considered as a problem because direct call of binfix
will cover some important cases of macro transformations in a straightforward manner:
{m x y op = '/ type = :doublefloat :==
let a = (gensym)
b = (gensym)
binfix `(let ,a ,type = ,x
,b ,type = ,y
{,a  ,b} ,op {,a + ,b})}
Now macro m
works as expected:
(macroexpand1 '(m (f x y) {a + b}))
=>
(let ((#:g805 (f x y)) (#:g806 (+ a b)))
(declare (type doublefloat #:g806)
(type doublefloat #:g805))
(/ ( #:g805 #:g806) (+ #:g805 #:g806)))
t
or,
(macroexpand1 '(m (f x y) {a + b}) * :doublefloat)
=>
(let ((#:g817 (f x y)) (#:g818 (+ a b)))
(declare (type doublefloat #:g817)
(type doublefloat #:g818))
(* ( #:g817 #:g818) (+ #:g817 #:g818)))
t
See more in implementation details
Converting an integer into ordinal string in English can be defined as
{ordinal i :integer :=
let* a = i mod 10
b = i mod 100
suf = {cond
a = b = 1  a = 1 && 21 <= b <= 91 ? "st";
a = b = 2  a = 2 && 22 <= b <= 92 ? "nd";
a = b = 3  a = 3 && 23 <= b <= 93 ? "rd";
t ? "th"}
format () "~D~a" i suf}
It can be also written in a more "lispy" way without parens as
{ordinal1 i :integer :=
let* a = i mod 10
b = i mod 100
suf = {cond
= a b 1 or = a 1 and <= b 21 91 ? "st";
= a b 2 or = a 2 and <= b 22 92 ? "nd";
= a b 3 or = a 3 and <= b 23 93 ? "rd";
t ? "th"}
format () "~D~a" i suf}
which can be tried using @.
(mapcar
)
{#'ordinal @. '(0 1 12 22 43 57 1901)}
=> ("0th" "1st" "12th" "22nd" "43rd" "57th" "1901st")
(This example is picked up from Rust blog)
APLish joining of things into list:
{
defgeneric join (a b) &
join a :list b :list : append a b &
join a :t b :list : cons a b &
join a :list b :t : append a (list b) &
join a :t b :t : list a b &
defbinfix '++ 'join
}
; Must close here in order to use ++
{let e = '{2 in 'x ++ '(1 2 3) ++ '((a)) ++ 1 * 2}
format t "~S~%=> ~S" e (eval e)}
Evaluation of the above returns t
and prints the following
(member 2 (join 'x (join '(1 2 3) (join '((a)) (* 1 2)))))
=> (2 3 (a) 2)
Another way to write join
in BINFIX is as a single defgeneric
definition, using def generic
,
{def generic join a b;
"Generic join."
a :list b :list : append a b;
a :t b :list : a :. b;
a :list b :t : `(,@a ,b);
a :t b :t : list a b}
which expands into
(progn
(defgeneric join
(a b)
(:documentation "Generic join.")
(:method ((a list) (b list)) (append a b))
(:method ((a t) (b list)) (cons a b))
(:method ((a list) (b t)) `(,@a ,b))
(:method ((a t) (b t)) (list a b))))
Macro multiplevaluebind
with symbol _
in variable list standing for an ignored value can be defined as
{valuesbind v e &rest r :==
let* _ = ()
vars = a > if {a == '_} {car $ push (gensym) _} a @. v;
`(multiplevaluebind ,vars ,e
,@{_ && `({declare $ ignore ,@_})}
,@r)}
So, for instance,
(macroexpand1 '(valuesbind (a _) (truncate 10 3) a))
=>
(multiplevaluebind (a #:g823) (truncate 10 3) (declare (ignore #:g823)) a)
t
Nested BINFIX lambda lists can be used in definitions of macros, as in the following example of a procedural forloop macro
{for (v :symbol from below by = 1) &rest r :==
`(loop for,v fixnum from,from below,below ,@{by /= 1 && `(by,by)}
do ,@r)}
Now
(macroexpand1 '(for (i 0 n)
{a ! i .= 1+ i}))
=>
(loop for i fixnum from 0 below n
do (setf (aref a i) (1+ i)))
t
#### Cartesian to polar coordinates
An example from Common LISP the Language 2nd ed. where cartesian coordinates are converted into polar coordinates via change of class can be straightforwardly written in BINFIX as
{def class position () ();
class xyposition (position)
x :initform 0 :initarg :x
y :initform 0 :initarg :y;
class rhothetaposition (position)
rho :initform 0
theta :initform 0
def updateinstancefordifferentclass :before
old :xyposition
new :rhothetaposition &key :
;; Copy the position information from old to new to make new
;; be a rhothetaposition at the same position as old.
let x = slotvalue old 'x
y = slotvalue old 'y;
slotvalue new 'rho .= sqrt {x * x + y * y};
slotvalue new 'theta .= atan y x
;;; At this point an instance of the class xyposition can be
;;; changed to be an instance of the class rhothetaposition
;;; using changeclass:
& p1 =. makeinstance 'xyposition :x 2 :y 0
& changeclass p1 'rhothetaposition
;;; The result is that the instance bound to p1 is now
;;; an instance of the class rhothetaposition.
;;; The updateinstancefordifferentclass method
;;; performed the initialization of the rho and theta
;;; slots based on the values of the x and y slots,
;;; which were maintained by the old instance.
}
where Steele's comments are left verbatim.
BINFIX expression is written as a list enclosed in curly brackets {
... }
handled through LISP reader, so the usual syntax rules of LISP apply, e.g a+b
is a single symbol, while a + b
is three symbols. Lisp reader after tokenization calls the function binfix
which does shallow transformation of BINFIX into Sexpr representation of the expression.
BINFIX uses a simple rewrite algorithm that divides a list in two, LHS and RHS of the lowest priority infix operator found within the list, then recursively processes each one.
Bootstraping is done beginning with protoBINFIX,
(defparameter *binfix*
'((; infix (progn))
(:== def defmacro)
(:= def defun)
(: def defmethod)
( =. infix (setq))
(.= infix (setf))
(> deflambda)
($ infix ())
(symbolmacrolet let= symbolmacrolet)
(let let= let)
(let* let= let*)
(labels flet= labels)
(=.. varbind multiplevaluebind)
(.x. unreduc .x. values)
(:. infix (cons))
( infix (or))
(&& infix (and))
(== infix (eql))
(=c= infix (char=))
(in infix (member))
( ! infix (aref))))
(defun binfix (e &optional (ops *binfix*))
(cond ((atom e) e)
((null ops) (if (cdr e) e (car e)))
(t (let* ((op (car ops))
(op.rhs (member (pop op) e)))
(if (null op.rhs)
(binfix e (cdr ops))
(let ((lhs (ldiff e op.rhs)))
(macroexpand1
`(,@op ,lhs ,(cdr op.rhs)))))))))
(defmacro infix (op lhs rhs)
`(,@op ,(binfix lhs) ,(binfix rhs)))
(setmacrocharacter #\{
(lambda (s ch) (declare (ignore ch))
(binfix (readdelimitedlist #\} s t))))
(setmacrocharacter #\} (getmacrocharacter #\) ))
which captures the basics of BINFIX.
Since v0.15, BINFIX interns a symbol consisting of a single ;
char not followed by ;
char, while two or more consequtive ;
are interpreted as a usual LISP comment. This behavior is limited to BINFIX expressions only, while outside of them the standard LISP rules apply.
The next bootstrap phase defines macros def
, deflambda
, let=
, flet=
, unreduc
and varbind
, done in proto1.lisp
,
{defmacro def (what args body)
`(,what ,@(if (atom args)
`(,args ())
`(,(car args),(cdr args)))
,(binfix body));
deflambda args body :==
`(lambda ,(if (consp args) args `(,args))
,(binfix body));
let= let lhs body &aux vars :==
loop while {cadr body == '=}
do {push `(,(car body),(caddr body)) vars;
body =. cdddr body}
finally (return (let ((let `(,let ,(nreverse vars) ,(binfix body))))
(if lhs (binfix `(,@lhs ,let)) let)));
flet= flet lhs body &aux funs :==
loop for r = {'= in body} while r
for (name . lambda) = (ldiff body r)
do {push `(,name ,lambda ,(cadr r)) funs;
body =. cddr r}
finally {return let flet = `(,flet ,(reverse funs) ,(binfix body))
if lhs (binfix `(,@lhs ,flet)) flet};
unreduc op oplisp lhs rhs :==
labels
unreduce e &optional args arg =
(cond {null e $ nreverse {binfix (nreverse arg) :. args}}
{car e == op $ unreduce (cdr e) {binfix (nreverse arg) :. args}}
{t $ unreduce (cdr e) args {car e :. arg}})
`(,oplisp ,@(unreduce rhs `(,(binfix lhs))));
varbind op lhs rhs :== `(,op ,lhs ,(car rhs) ,(binfix (cdr rhs)))}
which wraps up protoBINFIX.
Since v0.15, BINFIX interns a symbol consisting of a single ;
char not followed by ;
char, while two or more consequtive ;
are interpreted as starting a comment. This behavior is limited to BINFIX expressions only, while outside of them the standard LISP rules apply.
The rest is written using protoBINFIX syntax, and consists of handling of lambda lists and let
s, a longer list of OPs with properties, redefined binfix
to its full capability, and, finally, several interface functions for dealing with OPs (lsbinfix
, defbinfix
and rmbinfix
).
Priorities of operations in protoBINIFIX are given only relatively, with no numerical values and thus with no two operations of the same priority.
Lhs and rhs of protoBINFIX expressions refer to other protoBINFIX expressions (since v0.22.3), which in particular means that there is no implicitprogn in protoBINFIX let's and def's.
Since v0.20, symbol of a BINFIX operation has a list of properties stored into the symbol property binfix::properties
, which includes a numerically given priority of the OP (which also considerably speeds up parsing.) The actual value of number representing priority is supposed to be immaterial since only relation to other OPs priority values is relevant. Defining new samepriority OPs should be done via defbinfix
with :as
option, which may change priority values of many other OPs.
Since shallow transformation into standard syntax is done by function binfix
invoked recursively by the reader, binfix
cannot be directly called for arbitrary macro transformation of BINFIX into BINFIX when standard macro helpers BACKTICK, COMA and COMAAT are used. The reason is that {
...}
is invoked before them while the correct order would be after them. Examples of succesful combinations of backquoting and BINFIX are given above.
Provided binfix.vim
file covers vim
editor with a syntaxhighlighting extension, which is based and dependends on lisp.vim
that comes bundled with vim
.
Here are gui and terminal looks:
(theme: solarized
, font: Inconsolata Medium
)
(theme: herald
, font: Terminus
)
:def
 Operation (OP) is a definition requiring LHS to has a name and lambda list.
:defm
 OP is a definition requiring LHS to have a name followed by unparenthesized method lambda list.
:lhslambda
 OP has lambda list as its LHS.
:rhslbinds
 OP has letbinds at the beginning of its RHS,
[symbol [keyword] =
expr]* declaration*
:rhsfbinds
 OP has fletbinds at the beginning of its LHS, including optional declarations.
:rhssbinds
 OP has symbolbinds as its RHS. They are letbinds without annotations or declarations.
:rhsebinds
 OP has exprbinds at the beginning of its RHS.
:unreduce
 All appearances of OP at the current level should be unreduced, i.e replaced with a single call with multiple arguments.
:leftassoc
 OP is leftassociative (OPs are rightassociative by default.)
:prefix
 OP is prefix with RHS being its arguments, given as one or more atoms/Sexpr or a single ;
separated Bexpr.
:alsoprefix
 OP can be used as prefix when LHS is missing.
:alsounary
 OP can be used as unary when LHS is missing.
:alsopostfix
 OP can be used as postfix when RHS is missing.
:lambda/expr
 OP takes lambdalist at LHS and an expression at RHS, followed by body.
:syms/expr
 OP takes a list of symbols as LHS (each with an optional keywordtype annotation,) an expression as RHS followed by optional declarations and a BINFIXexpression.
#'myfun
 function myfun
will be applied to the untransformed RHS.
:split
 OP splits the expr at this point.
:rhsargs
 OP takes LHS as 1st and RHS as remaining arguments.
:macro
 OP is a macro.
:single
 OP requires to be the only OP in the current expr with its priority. For example, parsing of: {values a b .x. c}
reports an ambiguity error. ### List of all operations
Command
(lsbinfix)
prints the table of all BINFIX OPs and their properties from the weakest to the strongestbinding OP, with parens enclosing OP(s) of the same priority:
BINFIX LISP Properties
==============================================================================
( <& prog1 )
( & progn :unreduce )
( def binfix::defs :macro )
( let let :rhslbinds
let* let* :rhslbinds
symbolmacrolet symbolmacrolet :rhslbinds
prog* prog* :rhslbinds
prog prog :rhslbinds )
( macrolet macrolet :rhsfbinds
flet flet :rhsfbinds
labels labels :rhsfbinds )
( :== defmacro :def
:= defun :def
: defmethod :defm
:type= deftype :def )
( block block :prefix
tagbody tagbody :prefix
catch catch :prefix
prog1 prog1 :prefix
prog2 prog2 :prefix
progn progn :prefix
cond cond :prefix
case case :prefix
ccase ccase :prefix
ecase ecase :prefix
typecase typecase :prefix
etypecase etypecase :prefix
ctypecase ctypecase :prefix )
( ? nil :split )
( $ nil :split )
( .= setf
+= incf
= decf
=. setq
.=. set )
( setq setq :rhssbinds
set set :rhssbinds
psetq psetq :rhssbinds )
( setf setf :rhsebinds
psetf psetf :rhsebinds )
( .@ mapc :rhsargs
..@ mapl :rhsargs
@/ reduce :rhsargs
@. mapcar :rhsargs
@.. maplist :rhsargs
@n mapcan :rhsargs
@.n mapcon :rhsargs
@@ apply :rhsargs
.@. multiplevaluecall :rhsargs
@ funcall :rhsargs :leftassoc :alsopostfix )
( :> function :lhslambda )
( > lambda :lhslambda )
( =.. multiplevaluebind :syms/expr )
( ..= destructuringbind :lambda/expr )
( values values :prefix
.x. values :unreduce :alsoprefix )
( loop #<FUNCTION identity> :prefix )
(  or :unreduce
or or :unreduce :alsoprefix )
( && and :unreduce
and and :unreduce :alsoprefix )
( === equalp )
( equal equal )
( == eql )
( eql eql :alsoprefix )
( eq eq )
( subtypeof subtypep )
( :. cons )
( in member )
( thcdr nthcdr )
( =s= string= )
( =c= char= :unreduce )
( = = :unreduce :alsoprefix )
( /= /= :unreduce :alsoprefix )
( < < :unreduce :alsoprefix )
( > > :unreduce :alsoprefix )
( <= <= :unreduce :alsoprefix )
( >= >= :unreduce :alsoprefix )
( thbit logbitp )
( coerce coerce )
( !.. nthvalue
thvalue nthvalue )
( th nth )
( .! elt
!. svref
!!. rowmajoraref )
( !! aref :rhsargs )
( .!!. bit :rhsargs )
( .eqv. biteqv :rhsargs )
( .or. bitior :rhsargs )
( .xor. bitxor :rhsargs )
( .and. bitand :rhsargs )
( .nand. bitand :rhsargs )
( .nor. bitnor :rhsargs )
( .not. bitnot :alsounary )
( .orc1. bitorc1 :rhsargs )
( .orc2. bitorc2 :rhsargs )
( .andc1. bitandc1 :rhsargs )
( .andc2. bitandc2 :rhsargs )
( dpb dpb :rhsargs )
( ldb ldb )
( ldbtest ldbtest )
( depositfield depositfield :rhsargs )
( maskfield maskfield )
( byte byte )
( eqv. logeqv :alsounary :unreduce )
( or. logior :alsounary :unreduce )
( xor. logxor :alsounary :unreduce )
( and. logand :alsounary :unreduce )
( nand. lognand )
( nor. lognor )
( test. logtest )
( orc1. logorc1 )
( orc2. logorc2 )
( andc1. logandc1 )
( andc2. logandc2 )
( << ash )
( lcm lcm :alsounary :unreduce )
( gcd gcd :alsounary :unreduce )
( mod mod )
( rem rem )
( min min :alsoprefix :unreduce
max max :alsoprefix :unreduce )
( + + :alsounary :unreduce )
(   :alsounary :unreduce )
( / / :alsounary )
( * * :alsoprefix :unreduce )
( ** expt )
( .!. bit :rhsargs )
( ! aref :rhsargs )
( ; ; )

=> nil
 Author
 Viktor Cerovski
 License
 GNU GPLv2