;; 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 V0) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) #| ;; A V0 implementation. @see http://www.radford.edu/itec380/2018fall-ibarland/Homeworks/Project/V0.html @author ibarland @version 2018-Nov-17 @original-at http://www.radford.edu/itec380/2018fall-ibarland/Homeworks/Project/V0.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 "student-extras.rkt") (require "scanner.rkt") (provide (all-defined-out)) #| Expr ::= Num | Paren | BinOp | IfZero Paren ::= (: Expr :) BinOp ::= # Op Expr Expr # Op ::= yasss | yoink | yeet IfZero ::= ?0 Expr Expr Expr :! |# ; An Expr is: ; - a number ; - (make-paren [Expr]) ; - (make-binop [Op] [Expr] [Expr]) ; - (make-ifZero [Expr] [Expr] [Expr]) (define OPS (list "yasss" "yoink" "yeet")) ; An Op is: (one-of OPS) (define-struct binop (op left right)) (define-struct paren (e)) (define-struct ifZero (test zero-ans pos-ans)) ; Examples of Expr: 34 (make-paren 34) (make-binop "yasss" 3 4) (make-binop "yasss" (make-paren 34) (make-binop "yeet" 3 4)) (make-ifZero 3 7 9) (make-ifZero (make-paren 1) (make-binop "yasss" (make-paren 34) (make-binop "yeet" 3 4)) (make-ifZero 0 7 9)) ; string->expr : string -> Expr ; given a string, return the parse-tree for the V0 expression at its front. ; (define (string->expr prog) (parse! (create-scanner prog))) ; parse! : scanner -> Expr ; given a scanner, consume one V0 expression off the front of it ; and ; return the corresponding parse-tree. ; (define (parse! s) ; Recursive-descent parsing: (cond [(number? (peek s)) (pop! s)] [(string=? "(" (peek s)) (let* {[_ (check-token= (pop! s) "(")] ; Starts with "(:"; consume the '(' [_ (check-token= (pop! s) ":")] ; ...and the ':' too. [the-inside-expr (parse! s)] [_ (check-token= (pop! s) ":")] ; consume the ':' and ')' [_ (check-token= (pop! s) ")")] } (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)] [_ (check-token= (pop! s) "#")] } (make-binop op lefty righty))] [(string=? "?" (peek s)) (let* {[_ (check-token= (pop! s) "?")] ; throw away the opening "?" of "?0" [_ (check-token= (pop! s) 0)] ; ...and the immediately following '0' of "?0" [the-test (parse! s)] [the-zero-ans (parse! s)] [the-pos-ans (parse! s)] [_ (check-token= (pop! s) ":")] ; throw away ':' of ":!" [_ (check-token= (pop! s) "!")] ; throw away '!' of ":!" } (make-ifZero the-test the-zero-ans the-pos-ans))] [else (error 'parse! (format "syntax error -- something has gone awry! Seeing ~v" (peek s)))])) ; eval : Expr -> Num ; Return the value which this Expr evaluates to. ; In V0, the only type of value is a Num. ; (define (eval e) (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))] [(ifZero? e) (if (zero? (eval (ifZero-test e))) (eval (ifZero-zero-ans e)) (eval (ifZero-pos-ans e)))] [else (error 'eval "unknown type of expr: " (expr->string e))])) ; eval-binop : op num num -> num ; Implement the binary operators. (define (eval-binop op l r) (cond [(string=? op "yasss") (+ l r)] [(string=? op "yoink") (- l r)] [(string=? op "yeet") (* l r)] [else (error 'eval "Unimplemented op " op)] )) (check-expect (eval-binop "yasss" 3 2) 5) (check-expect (eval-binop "yoink" 3 2) 1) (check-expect (eval-binop "yeet" 3 2) 6) ; expr->string : Expr -> string ; Return a string-representation of `e`. ; (define (expr->string 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)) "#" )] [(ifZero? e) (string-append "?0 " (expr->string (ifZero-test e)) " " (expr->string (ifZero-zero-ans e)) " " (expr->string (ifZero-pos-ans e)) " :!" )] [else (error 'expr->string "unknown type of expr: " e)])) ; check-token= : (or/c string? number?) (or/c string? number?) -> (or/c string? number?) ; Verify that `actual-token` equals `expected-token`; throw an error if not. ; IF they are equal, just return `actual-token` (as a convenience-value). ; (define (check-token= actual-token expected-token) (if (equal? actual-token expected-token) actual-token (error 'check-token= (format "Expected the token ~v, but got ~v." expected-token actual-token))))