;; C311 Programming Laguages ;; Fall 2008 ;; Dana Vrajitoru ;; Evaluation functions for the grammar that recognizes an arithmetic ;; expression. This version returns the value of subtrees that cannot ;; be evaluated by themselves as a lambda expression. (load-file "parse_expr.el") ; To be able to test the others I made this return something for now. (defun eval2-E (Tree) (let ((Tres (eval2-T (elt Tree 1))) (TTres (eval2-TT (elt Tree 2)))) (funcall TTres Tres))) ; To be able to test the others I made this return something for now. (defun eval2-T (Tree) 2) ;; '(TT) when it expands to e. ;; '(TT (AO "+") (T ...) (TT ...)) (defun eval2-TT (Tree) (if (or (not Tree) (not (equal (car Tree) 'TT))) nil (if (equal (length Tree) 1) (lambda (x) x) (let ((Tres (eval2-T (elt Tree 2))) (TTres (eval2-TT (elt Tree 3))) (Res 0)) (setq Res (funcall TTres Tres)) (cond ((equal (cadr (cadr Tree)) "+") (append '(lambda (x)) (list (list '+ 'x Res)))) ((equal (cadr (cadr Tree)) "-") (append '(lambda (x)) (list (list '- 'x Res)))) (t nil)))))) (eval2-TT '(TT (AO "+") (T "...") (TT))) (lambda (x) (+ x 2)) ; '(F 2) ; '(F a) ; '(F "(" (E ...) ")") (defun eval2-F (Tree) (if (or (not Tree) (not (equal (car Tree) 'F))) nil (if (= (length Tree) 2) ; number or symbol (eval (cadr Tree)) (eval2-E (elt Tree 2))))) (eval2-F '(F 2)) 2 (setq a 5) 5 (eval2-F '(F a)) 5 (eval2-F '(F "(" (E (T) (TT)) ")" )) 2 ; Parses the list of tokens and build a parse tree, then calls the ; evaluation of the tree and returns the value. ((defun process (L) (let ((Tree (parse-input L))) (if (not Tree) nil (princ "The parse tree is:\n") (prin1 Tree) (princ "\nThis is evaluated to\n") (eval2-E Tree))))