;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname A0) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) #| ;; A A0 implementation. @see http://www.radford.edu/itec380/2022spring-ibarland/Homeworks/Project/A0.html @author ibarland@radford.edu @version 2022-Mar-30 @original-at http://www.radford.edu/itec380/2022spring-ibarland/Homeworks/Project/A0.rkt @license CC-BY -- share/adapt this file freely, but include attribution, thx. https://creativecommons.org/licenses/by/4.0/ https://creativecommons.org/licenses/by/4.0/legalcode Including a link to the *original* file satisifies "appropriate attribution". |# (require racket/contract) (require "student-extras.rkt") (require "scanner.rkt") (provide (all-defined-out)) #| Expr ::= Num | Paren | BinOp | IfZero Paren ::= / Expr \ Interpretation: a parenthesized expression BinOp ::= ~ Op Expr Expr Interpretation: apply a binary operator Op ::= sum | dif | prd Interpretation: addition, subtraction, multiplication (resp.) IfZero ::= zro Expr ? Expr : Expr Interpretation: if 1st expr is zero, answer is the 2nd expr, else use the 3rd expr |# ; datatype defn: (define (expr? val) (or (value? val) (paren? val) (binop? val) (if-zero? val))) (define OP-FUNCS (list (list "sum" +) (list "dif" -) (list "prd" *) )) (define OPS (map first OP-FUNCS)) (define (op? val) (member val OPS)) (define-struct/c binop ([op op?] [left expr?] [right expr?])) (define-struct/c paren ([e expr?])) (define-struct/c if-zero ([tst expr?] [thn expr?] [els expr?])) (define (value? val) (number? val)) ; N.B. A4 will upgrade our notion of 'value' to include functions, as well as numbers. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Examples of Expr: 34 (make-paren 34) (make-binop "sum" 3 4) (make-binop "sum" (make-paren 34) (make-binop "prd" 3 4)) ; ~sum /34\ [prd 3 4] (make-if-zero 3 7 9) (make-if-zero (make-paren 1) (make-binop "sum" (make-paren 34) (make-binop "prd" 3 4)) (make-if-zero 0 7 9)) ;the above is the parse-tree for: "zro /1\ ? ~sum /34\ [prd 3 4] : zro 0 ? 7 : 9" ; An Op is: (one-of OPS) (define/contract (string->expr prog) (-> string? expr?) ; given a string, return the parse-tree for the A0 expression at its front. ; (parse! (create-scanner prog))) #| Our java version of `parse` corresponds more to: (define/contract (parse! s) (-> scanner? expr?) (cond [(number? (peek s)) (parse-number s)] [(string=? "/" (peek s)) (parse-paren s)] [(string=? "~" (peek s)) (parse-binop s)] [(string=? "zro" (peek s))) (parse-if-zero s)])) And then `parse-binop` etc are each in their own class. (They simply need to be sure to recur on `Expr.parse`; if they just write `parse` they'll get, say, `Binop.parse` which is not general enough.) |# (define/contract (parse! s) (-> scanner? expr?) ; given a scanner, consume one A0 expression off the front of it ; and return the corresponding parse-tree. ; ; Recursive-descent parsing: (cond [(number? (peek s)) (pop! s)] [(string=? "/" (peek s)) (let* {[_ (check-token= (pop! s) "/")] ; consume the '/' off the input-stream [the-inside-expr (parse! s)] ; recursively consume one whole Expr (no matter how long) [_ (check-token= (pop! s) "\\")] ; consume the trailing '\' } (make-paren the-inside-expr))] [(string=? "~" (peek s)) (let* {[_ (check-token= (pop! s) "~")] [op (pop! s)] [_ (if (not (member? op OPS)) (error 'parse! "Unknown op " op) 'keep-on-going)] [lefty (parse! s)] [righty (parse! s)] } (make-binop op lefty righty))] [(string=? "zro" (peek s)) (let* {[_ (check-token= (pop! s) "zro")] [the-test (parse! s)] [_ (check-token= (pop! s) "?")] [the-then-ans (parse! s)] [_ (check-token= (pop! s) ":")] [the-else-ans (parse! s)] } (make-if-zero the-test the-then-ans the-else-ans))] [else (error 'parse! (format "syntax error -- something has gone awry! Seeing ~v." (peek s)))])) (define/contract (eval e) (-> expr? value?) ; Return the value which `e` evaluates to. ; In A0, the only type of `value?`s are `number?`s. ; (cond [(number? e) e] [(paren? e) (eval (paren-e e))] [(binop? e) (let* {[the-op (binop-op e)] [left-val (eval (binop-left e))] [right-val (eval (binop-right e))] } (eval-binop the-op left-val right-val))] [(if-zero? e) (if (zero? (eval (if-zero-tst e))) (eval (if-zero-thn e)) (eval (if-zero-els e)))] [else (error 'eval "unknown type of expr: " (expr->string e))])) (define/contract (eval-binop op l r) (-> string? value? value? value?) ; Implement the binary operators. ; ; We just look up `op` in the list `OP-FUNCS`, and use the function that's in that list. (let* {[ops-entry (assoc op OP-FUNCS)]} ; OPS is a list of list-of-string-and-func; ; so `(second ops-entry)` is a function (if ops-entry is found at all). (cond [(cons? ops-entry) ((second ops-entry) l r)] [else (error 'eval-binop "Unimplemented op " op "; most be one of: " OPS)]))) ; An alternate implementation -- forces us to repeat ; the string-constants already in OPS: #;(cond [(string=? op "sum") (+ l r)] [(string=? op "dif") (- l r)] [(string=? op "prd") (* l r)] [else (error 'eval "Unimplemented op " op)]) ; ; *** In your A1/A2 submission, DELETE whichever eval-binop approach you don't use. (check-expect (eval-binop "sum" 3 2) 5) (check-expect (eval-binop "dif" 3 2) 1) (check-expect (eval-binop "prd" 3 2) 6) (define/contract (expr->string e) (-> expr? string?) ; Return a string-representation of `e`. ; (cond [(number? e) (number->string (if (integer? e) e (exact->inexact e)))] [(paren? e) (string-append "/" (expr->string (paren-e e)) "\\")] [(binop? e) (string-append "~" (binop-op e) " " (expr->string (binop-left e)) " " (expr->string (binop-right e)) )] [(if-zero? e) (string-append "zro " (expr->string (if-zero-tst e)) " ? " (expr->string (if-zero-thn e)) " : " (expr->string (if-zero-els e)) )] [else (error 'expr->string "unknown type of expr: " e)])) ; datatype definition: (define token? (or/c string? number?)) (define/contract (check-token= actual-token expected-token) (-> token? token? token?) ; Verify that `actual-token` equals `expected-token`; throw an error if not. ; IF they are equal, just return `actual-token` (as a convenience-value). ; (if (equal? actual-token expected-token) actual-token (error 'check-token= (format "Expected the token " expected-token ", but got " actual-token "."))))