Arc Forumnew | comments | leaders | submitlogin
M-Expression Syntax
7 points by rincewind 5681 days ago | 3 comments
I wrote this reader for M-expression syntax as an abbreviation for S-Expressions. It may not be very useful for experienced Arc-/Lisp-ers, but it may serve as either syntactical training wheels for imperative programmers who want to learn Arc, or as an ugly example of why S-Expressions are a much better syntax for Lisp.

Description of the Syntax:

McCarthy's M-Expressions in the original paper looked like this: "cons[a;b];", which is in S-expression form (cons a b). So in general the grammar is (not real ebnf, but you get the idea):

   m-expr = <car of s-expr as symbol> <cdr of sexpr as list> 
   m-expr = <atom>
   m-expr = <infix-math>
   m-expr = <list>
   list = "[]"
   list =  "[" <mexpr> <list-tail>
   list-tail = "]"
   list-tail = ";" <mexpr> <list-tail> "]" 
Examples for this syntax

  M-expr            S-expr

  cons[a;b];        (cons a b)
  list[1;2;3];      (list 1 2 3)
  n + 1;            (+ n 1)
  n + 3 * 4;        (+ n (* 3 4))
  (n + 1) * 3;      (* (+ n 1) 3)
  [1;2;3;4];        (1 2 3 4)
One generalisation of this syntax is that juxtaposition means consing(left-associative), so the resulting S-exprs can have lists as cars:

  only[fun][arg];   ((only fun) arg)
But is impossible to write dotted lists this way, so we need additional syntax for them.

  : a b c;          (a b . c)
  : a b [c;d;e];    (a b c d e)
This is also useful for binding forms and all macros with (arg arg2 foo . body) arglists

  : let var val [   (let var val
      m-expr;           s-expr
      m-expr;           s-expr
      m-expr            s-expr)
  ];
All infix-operators are n-ary,

  a < b < c;        (< a b c)
this may not be what you want, but I don't want to second-guess programmers who override their fixed-arity functions, so use caution:

  a is b is c;      (is a b c d)
  - a - b;          (- a b)
  (-[a])- b;        (- (- a) b)
You can work around this by writing a macro that wraps a fixed-arity function and changing the m-opswap function to replace it with your macro.

Implementation details:

1.mzscheme-read is used to read the symbols and everything that is not delimited by () or []. This means that #\newline, `(a ',b), and {a b c} are read with mzscheme-read. However, this does not disable restructuring of expressions which have subexpressions of this kind, so:

  m-expr            read as
  cons{a b};        (cons a b)
  {x}y;             ((x) . y)
2.Developed with anarki, not tested on arc2

URL: http://placebo.hpi.uni-potsdam.de/~robert.pfeiffer/arc/



1 point by rincewind 5681 days ago | link

clickable links:

The reader http://placebo.hpi.uni-potsdam.de/~robert.pfeiffer/arc/mexpr...

test case and examples http://placebo.hpi.uni-potsdam.de/~robert.pfeiffer/arc/test....

-----

1 point by eds 5679 days ago | link

Is this implemented as its own reader, or does it mutate the arc reader when loaded?

Have you seen http://arclanguage.org/item?id=2610 and http://github.com/nex3/arc/tree/master/lib/infix.arc ?

-----

1 point by rincewind 5679 days ago | link

1. It is implemented as its own reader, but it uses arc/mzscheme-read for symbols and quotation syntax (the version at http://placebo.hpi.uni-potsdam.de/~robert.pfeiffer/arc/mexpr... also handles quasiquotation-syntax on its own, but is less well tested)

You can load a file in m-expression format with "(load-m filename)" (or "load-m[filename];").

2.Yes I have. The M-Expr reader converts into prefix form at read time, so the read forms look like arc code. M-Expressions are just a skin for S-Expressions, therefor I tried not to break anything by changing the run-time semantics of Arc (unlike infix.arc, which makes numbers callable, which my break drcodes currying (or my idea for automatically converting numbers in functional position into Church Numerals)).

-----