;; 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 OG0) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) #| ;; An OG0 implementation. @see http://www.radford.edu/itec380/2024fall-ibarland/Homeworks/Project/OG0.html @author ibarland@radford.edu @version 2024-nov-11 @original-at http://www.radford.edu/itec380/2024fall-ibarland/Homeworks/Project/OG0.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)) #| | | | | → bruh slaps Interpretation: a parenthesized expression → finna Interpretation: apply a binary operator → rizz | drip | glow up Interpretation: addition, subtraction, multiplication (resp.) → no cap? ong bet Interpretation: if 1st-expr is 0 (within 1e-6?), eval to 2nd-expr, else 3rd-expr. // To be added in OG1: -> vibe check low-key ong bet // Interpretation: "if greater-or-equal-to": expr1 >= expr2 ? expr3 : expr4 // To be added in OG2: -> iykyk irl wya // Interpretation: bind Id to the val of "right-hand-side" (1st) expr, within scope of "body" (2nd) expr |# ; our parse tree for ; finna glow up bruh finna rizz 2 3 slaps 4 (aka `(2+3)*4` ) ; is: ; ; ; Our internal representation of that tree will be: ; (make-binop "glow up" (make-paren (make-binop "rizz" 2 3)) 4) (define OP-FUNCS (list (list "rizz" +) (list "drip" -) (list "glow up" *) )) (define OPS (map first OP-FUNCS)) ; datatype defn: (define (op? val) (member val OPS)) ; datatype defn (define (expr? v) #;(or/c number? binop? paren? if-zero?) (or (number? v) (binop? v) (paren? v) (if-zero? v))) (define-struct/c binop ([op op?] [left expr?] [right expr?])) ; NOTE: `op` is our *first* field, despite our infix syntax. (define-struct/c paren ([e expr?])) (define-struct/c if-zero ([tst expr?] [thn expr?] [els expr?])) (define value? number?) ; N.B. OG4 will upgrade our notion of 'value' to include *functions*, as well as numbers. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Examples of Expr: 34 (make-paren 34) (make-binop "rizz" 3 4) ; "finna rizz 3 4" (make-binop "rizz" (make-paren 34) (make-binop "drip" 3 4)) ; "finna rizz bruh 34 slaps finna drip 3 4" (make-if-zero 3 7 9) (make-if-zero (make-paren 1) (make-binop "rizz" (make-paren 34) (make-binop "glow up" 3 4)) (make-if-zero 0 7 9)) ; the above is the parse-tree for: no cap? bruh 1 slaps ong finna rizz bruh 34 slaps finna glow up 3 4 bet no cap? 0 ong 7 bet 9 ; Template, for *any* function handling an Expr: ; (define/contract (func-for-expr e) (-> expr? ...?) (cond [(number? e) ...] [(binop? e) (... (binop-op e) (func-for-expr (binop-left e)) (func-for-expr (binop-right e)))] [(paren? e) (func-for-expr (paren-e e))] [(if-zero? e) (... (func-for-expr (if-zero-tst e)) (func-for-expr (if-zero-thn e)) (func-for-expr (if-zero-els e)))])) ;;;;;;;;;;;;;;;;;;;;;;;;;; *evaluate* an expr ;;;;;;;;;;;;;;;;;;;;;; ;;; ;;; See OG0-tests.rkt, for test-cases ;;; ; Return the value which `e` evaluates to. ; In OG0, the only type of `value?`s are `number?`s. ; (define/contract (eval e) (-> expr? value?) (cond [(number? 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))] [(paren? e) (eval (paren-e e))] [(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))])) (check-expect (eval-binop "rizz" 3 2) 5) (check-expect (eval-binop "drip" 3 2) 1) (check-expect (eval-binop "glow up" 3 2) 6) ;;; ;;; See OG0-tests.rkt, for more test-cases ;;; ; Implement the binary operators. ; (define/contract (eval-binop op l r) (-> op? number? number? number?) ; 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 "rizz") (+ l r)] [(string=? op "drip") (- l r)] [(string=? op "glow up") (* l r)] [else (error 'eval "Unimplemented op " op)]) ; ; *** In your OG1/OG2 submission, DELETE whichever eval-binop approach you don't use. ; Return a string-representation of `e`. ; (define/contract (expr->string e) (-> expr? string?) (cond [(number? e) (number->string (if (integer? e) e (exact->inexact e)))] [(binop? e) (string-append "finna" " " (binop-op e) " " (expr->string (binop-left e)) " " (expr->string (binop-right e)) )] [(paren? e) (string-append "bruh" " " (expr->string (paren-e e)) " " "slaps")] [(if-zero? e) (string-append "no cap? " (expr->string (if-zero-tst e)) " ong " (expr->string (if-zero-thn e)) " bet " (expr->string (if-zero-els e)) )] [else (error 'expr->string "unknown type of expr: " e)])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;; parsing ;;;;;;;;;;;;;;;;;;;; ;;; ;;; See OG0-tests.rkt, for test-cases ;;; ; given a string, return the parse-tree for the OG0 expression at its front. ; (define/contract (string->expr prog) (-> string? expr?) (parse! (create-scanner prog))) ; Consume one OG0 off the front of `s` and return it. ; (define/contract (parse-op! s) (-> scanner? string?) (let* {[first-word (pop! s)] ; "glow up" being two words causes us a bit of headache. [next-word (if (string=? first-word "glow") (string-append " " (pop! s)) "")] [full-op (string-append first-word next-word)] [_ (if (not (member? full-op OPS)) (error 'parse! "Unknown op " full-op) 'keep-on-going)] } full-op)) (define some-words (create-scanner "rizz drip glow up glow up boujee")) (check-expect (parse-op! some-words) "rizz") (check-expect (parse-op! some-words) "drip") (check-expect (parse-op! some-words) "glow up") (check-expect (parse-op! some-words) "glow up") ;(check-error (parse-op! some-words) "Unknown op boujee") ; Given a scanner, consume one OG0 expression off the front of it ; and return the corresponding parse-tree. ; (define/contract (parse! s) (-> scanner? expr?) ; Recursive-descent parsing: (cond [(number? (peek s)) (pop! s)] [(string=? "finna" (peek s)) (let* {[_ (check-token= (pop! s) "finna")] ; consume "tilt" off front of input [op (parse-op! s)] [lefty (parse! s)] [righty (parse! s)] } (make-binop op lefty righty))] [(string=? "bruh" (peek s)) (let* {[_ (check-token= (pop! s) "bruh")] ; consume the '(' off the input-stream [the-inside-expr (parse! s)] ; recursively consume one whole Expr (no matter how long) [_ (check-token= (pop! s) "slaps")] ; consume the trailing ')' } (make-paren the-inside-expr))] [(string=? "no" (peek s)) (let* {[_ (check-token= (pop! s) "no")] [_ (check-token= (pop! s) "cap")] [_ (check-token= (pop! s) "?")] [test (parse! s)] [_ (check-token= (pop! s) "ong")] [the-then-ans (parse! s)] [_ (check-token= (pop! s) "bet")] [the-else-ans (parse! s)] } (make-if-zero test the-then-ans the-else-ans))] [else (error 'parse! (format "syntax error -- something has gone awry! Seeing ~v." (peek s)))])) #| 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=? "tilt" (peek s)) (parse-binop s)] [(string=? "if" (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.) |# ;;;;;;;;;;;;;; some small,miscellaneous helpers ;;;;;;;;;;;;;;;;; ; 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 ~v, but got ~v." expected-token actual-token)))) ;;;; testing expr->string, string->expr on some simple exprs. ;;;; These are NOT complete tests; see OG2-test.rkt for that. ;;;; But this IS a handy place to quickly test new types of exprs ;;;; and catching silly mistakes, before running the detailed tests. ;;;; (check-expect (string->expr "finna glow up 3 4") (make-binop "glow up" 3 4)) (check-expect (expr->string (make-binop "glow up" 3 4)) "finna glow up 3 4") #;(map (compose expr->string string->expr) '("finna rizz 2 3" "bruh 55 slaps" "finna drip 3 bruh 55 slaps" "no cap? 3 ong 4 bet 5" "no cap? finna rizz 3 -3 ong 1 bet 2" ))