From: Jon Greenblatt
Subject: Algebraic syntax...
Date: 
Message-ID: <4919@umd5.umd.edu>
	Has anyone written a lisp macro to do the following:

Read in:

	(evalexpr q = a + b / q * (x - y))

and expand it to:

	(setf q (+ a (* (/ b q) (- x y))))

I am especialy interested in a macro that will work in xlisp2.0
If I do not get any responses I will have to write it my self and
I will post it to these news groups. I do not need it to handle this
exact syntax but anything close would be a good start.

	I realy think the world could benifit from such a macro.

				JonnyG
			(······@umd5.umd.edu)
			(······@rover.umd.edu)

From: Jon Greenblatt
Subject: Re: Algebraic syntax...
Date: 
Message-ID: <4924@umd5.umd.edu>
	 Well I got one reply to my request in the for Algebraic conversion.
 I took this information and made it a read macro for xlisp. This is
 realy basic code and I plan to eventialy include a optimizer to pull
 out constant expressions, reduce the number of function calls, and
 add support for unary operators. Given this is so simple I though I would
 post it in this form for right now. If anyone was wondering how to write
 read macros in Xlisp, here is how its done.

;;;
;;; An infix to prefix converter for algebraic expressions.
;;; From Winston and Horn, Second Edition, pp 185-189.
;;;
;
;	Adapted as a lisp macro by:
;		Jonathan Roger Greenblatt (······@rover.umd.edu)
;		University of Maryland at College Park
;
;
;	(usage:
;
;		[ <expr> <oper> <expr> ( <oper> <expr> ) ... ]
;
;	<expr>: a lisp expresion.
;	<oper>: =,+,-,*,/,mod.**,^
;
;	Note: [ and ] are part of the syntax, ( and ) mean this part is
;				optional.
;
;	Examples:
;
;		[a = 7 * 5 + 4]
;		[b = 7 + (sin (float a)) + (float [a / 7]) * [3 + a]]
;
;	These are expanded to:
;
;		(SETQ A (+ (* 7 5) 4))
;		(SETQ B (+ (+ 7 (SIN (FLOAT A))) (* (FLOAT (/ A 7)) (+ 3 A))))
;
;

(defun inf-to-pre (ae)
  (labels
	((weight (operator)
	  (case operator
	    (= 0)
	    (+ 1)
	    (- 1)
	    (* 2)
	    (/ 2)
	    (mod 2)
	    (** 3)
	    (^ 3)
	    (t 4)))

	(opcode (operator)
	  (case operator
	    (= 'setq)
	    (+ '+)
	    (- '-)
	    (* '*)
	    (/ '/)
	    (mod 'mod)
	    (** 'expt)
	    (^ 'expt)
	    (t (error "invalid operator" operator))))

	(inf-aux (ae operators operands)
	  (inf-iter (cdr ae)
	    operators
	    (cons (car ae) operands)))

	(inf-iter (ae operators operands)
	  (cond ((and (null ae) (null operators))
		 (car operands))
		((and (not (null ae))
		      (or (null operators)
			  (> (weight (car ae))
			     (weight (car operators)))))
		 (inf-aux (cdr ae)
			  (cons (car ae) operators)
			  operands))
		(t (inf-iter ae
			     (cdr operators)
			     (cons (list (opcode (car operators))
					 (cadr operands)
					 (car operands))
				   (cddr operands)))))))

  (if (atom ae)
      ae
      (inf-aux ae nil nil))))

(setf (aref *readtable* (char-int #\[))
  (cons :tmacro
	(lambda (f c &aux ex)
		(setf ex nil)
		(do () ((eq (peek-char t f) #\]))
			(setf ex (append ex (cons (read f) nil))))
		(read-char f)
		(cons (inf-to-pre ex) nil))))

(setf (aref *readtable* (char-int #\]))
  (cons :tmacro
	(lambda (f c)
		(error "misplaced right bracket"))))
From: julie zelenski
Subject: Re: Algebraic syntax...
Date: 
Message-ID: <2390@Portia.Stanford.EDU>
In article <····@umd5.umd.edu> ······@umd5.umd.edu (Jon Greenblatt) writes:
>
>	Has anyone written a lisp macro to do the following:
>Read in:
>	(evalexpr q = a + b / q * (x - y))
>
>and expand it to:
>	(setf q (+ a (* (/ b q) (- x y))))


I don't know about a macro, but there is a simple infix-to-prefix 
function in the chapter on Mathematical Examples in the Winston
and Horn Lisp book.  If people don't have access to this and would
like to see it, I could post it.

julie zelenski
stanford university
From: Eerke Boiten
Subject: Re: Algebraic syntax...
Date: 
Message-ID: <575@phoibos.cs.kun.nl>
From article <····@umd5.umd.edu>, by ······@umd5.umd.edu (Jon Greenblatt):
> 
> 	Has anyone written a lisp macro to do ...
> 	(evalexpr q = a + b / q * (x - y))
[translate to]
> 	(setf q (+ a (* (/ b q) (- x y))))
> 				JonnyG

Why not post this to comp.compilers? I suppose _someone_ has this
(what I call) translation to prefix notation ready in someLISP.
If not, maybe it can even be found in some introductory compiler
construction textbook.

Eerke Boiten, Department of Informatics, K.U.Nijmegen
Toernooiveld, 6525 AD Nijmegen, The Netherlands.
Tel. +31-80-612236.     ·····@cs.kun.nl

-- 
Eerke Boiten, Department of Informatics, K.U.Nijmegen
Toernooiveld, 6525 AD Nijmegen, The Netherlands.
Tel. +31-80-612236.     ·····@cs.kun.nl