vbuterin

Q8 starter for CS 145

Oct 4th, 2012
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. ;; The first three lines of this file were inserted by DrRacket. They record metadata
  2. ;; about the language level of this file in a form that our tools can easily process.
  3. #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname Q8-starter) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
  4. (require racket/match)
  5.  
  6. (define-struct bin (op fst snd))
  7.  
  8. ;; grammar for Faux-Racket (FR) so far:
  9. ;;
  10. ;; exp = number
  11. ;; | (op exp exp)
  12. ;;
  13. ;; op = + | * | - | /
  14. ;;
  15. ;; An sexp is a quoted exp.
  16. ;;
  17. ;; An AST is either a number or (make-bin opr x y),
  18. ;; where opr is a symbol and x and y are ASTs.
  19.  
  20. ;; parse: sexp -> AST
  21. ;; structurally recursive in the grammar for sexps
  22.  
  23. (define (parse sx)
  24. (match sx
  25. [`(+ ,x ,y) (make-bin '+ (parse x) (parse y))]
  26. [`(* ,x ,y) (make-bin '* (parse x) (parse y))]
  27. [`(- ,x ,y) (make-bin '- (parse x) (parse y))]
  28. [`(/ ,x ,y) (make-bin '/ (parse x) (parse y))]
  29. [x x]))
  30.  
  31. ;; interp: AST -> number
  32. ;; structurally recursive in definition of AST
  33.  
  34. (define (interp ast)
  35. (match ast
  36. [(struct bin ('+ x y)) (+ (interp x) (interp y))]
  37. [(struct bin ('* x y)) (* (interp x) (interp y))]
  38. [(struct bin ('- x y)) (- (interp x) (interp y))]
  39. [(struct bin ('/ x y)) (/ (interp x) (interp y))]
  40. [x (if (number? x)
  41. x
  42. (error 'interp "undefined variable " x))]))
  43.  
  44. ;; tests using check-expect and check-error for parse and interp removed
  45. ;; build your own set and keep adding to it as the interpreter grows
  46. ;; the set below is not adequate!
  47.  
  48. (check-expect (parse 1) 1)
  49. (check-expect (interp (parse 1)) 1)
  50.  
  51. ;; To be added for Q8:
  52. ;;
  53. ;; exp = (id exp)
  54. ;; | (if0 exp exp exp)
  55. ;; defn = (def (id id) exp)
  56. ;; def-list = (defn ...)
  57. ;;
  58. ;; An id is a symbol.
  59.  
  60. (define-struct app (fn arg))
  61. (define-struct ifzero (test tex fex))
  62.  
  63. ;; An AST can be (make-app rand rator),
  64. ;; where rand is a symbol, and rator is an AST.
  65. ;; It can also be (make-ifzero b t f),
  66. ;; where b, t, and f are ASTs.
  67.  
  68. (define-struct fundef (name param body))
  69.  
  70. ;; A definition is a (make-fundef nm par bdy)),
  71. ;; where nm and par are symbols and bdy is an AST.
  72. ;;
  73. ;; parse-fundefs: sexp -> (listof fundef)
  74. ;; parses an sexp satisfying the grammar rule for def-list
  75. ;;
  76. ;; (define (parse-fundefs sx) ...)
  77. ;;
  78. ;; interp now has a new contract:
  79. ;; interp: AST (listof fundef) -> number
  80. ;;
  81. ;; (define (interp ast defs) ...)
  82. ;;
  83. ;; As a helper for interp, for function application:
  84. ;;
  85. ;; subst: symbol number AST -> AST
  86. ;;
  87. ;; (define (subst var val ast) ...)
  88. ;;
  89. ;; Note that the recursive applications of interp needed
  90. ;; to handle function application make interp no longer
  91. ;; structurally recursive.
Advertisement
Add Comment
Please, Sign In to add comment