UNB/ CS/ David Bremner/ teaching/ cs4613/ lectures/ lecture3/ snippet-018.rkt
#lang plait
#| BNF for the AE language:
   <AE> ::= <num>
          | { + <AE> <AE> }
          | { - <AE> <AE> }
          | { * <AE> <AE> }
          | { / <AE> <AE> }
|#

;; AE abstract syntax trees
(define-type AE
  [Num (val : Number)]
  [Add (l : AE) (r : AE)]
  [Sub (l : AE) (r : AE)]
  [Mul (l : AE) (r : AE)]
  [Div (l : AE) (r : AE)])
;; to convert s-expressions into AEs
  (define (parse-sx sx)
    (let ([rec (lambda (fn)
		(parse-sx (fn (s-exp->list sx))))])
      (cond
	[(s-exp-match? `NUMBER sx)
	 (Num (s-exp->number sx))]
	[(s-exp-match? `(+ ANY ANY) sx)
	 (Add (rec second) (rec third))]
	[(s-exp-match? `(- ANY ANY) sx)
	 (Sub (rec second) (rec third))]
	[(s-exp-match? `(* ANY ANY) sx)
	 (Mul (rec second) (rec third))]
	[(s-exp-match? `(/ ANY ANY) sx)
	 (Div (rec second) (rec third))]
	[else (error 'parse-sx (to-string sx))])))

(test (parse-sx `{+ {- 3 4} 7})
       (Add (Sub (Num 3) (Num 4)) (Num 7)))