runtime.rkt 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #lang racket/base
  2. (provide
  3. value?
  4. basic-value?
  5. primitive-procedure? gen:primitive-procedure
  6. simple-primitive-proc define-primitive-procs
  7. prim:+ prim:- prim:* prim:/
  8. stack-frame?
  9. evaluate1
  10. evaluate*
  11. apply-procedure
  12. binding? make-bindings binding-name binding-def
  13. current-store store-ref store-init! store-set!
  14. structure? make-structure structure-add-bindings!
  15. instantiate-binding!
  16. instantiate-structure!)
  17. (require
  18. racket/generic
  19. racket/match
  20. (only-in racket/string string-join)
  21. (only-in racket/format ~a ~s)
  22. (only-in racket/promise delay force)
  23. syntax/parse/define
  24. (for-syntax racket/base racket/syntax))
  25. ;; VALUES
  26. ;; v = bv | prim
  27. ;; bv = integer | string
  28. ;; prim = {primitive-procedure}
  29. (define (basic-value? x)
  30. (or (exact-integer? x)
  31. (string? x)))
  32. (define (value? x)
  33. (or (basic-value? x)
  34. (primitive-procedure? x)))
  35. (define-generics primitive-procedure
  36. ;; prim [listof v] -> v
  37. (primitive-apply primitive-procedure args))
  38. ;; symbol [v ... -> v] -> prim
  39. (struct simple-primitive-proc [name func]
  40. #:methods gen:primitive-procedure
  41. [(define (primitive-apply sp args)
  42. (apply (simple-primitive-proc-func sp) args))]
  43. #:methods gen:custom-write
  44. [(define (write-proc sp port _mode)
  45. (fprintf port "{prim:~a}" (simple-primitive-proc-name sp)))])
  46. ;; (define-primitive-procs id ...)
  47. ;; For each "id", defines "prim:id" to be a primitive-proc that calls function "id".
  48. (define-simple-macro (define-primitive-procs f ...)
  49. #:with [f* ...] (map (λ (x) (format-id x "prim:~a" x)) (attribute f))
  50. (begin (define f* (simple-primitive-proc 'f f))
  51. ...))
  52. (define-primitive-procs
  53. + - * /)
  54. ;; ================================================
  55. ;; EXPRESSIONS
  56. ;; e = bv literal value
  57. ;; | prim primitive func
  58. ;; | `(top B sym) toplevel binding
  59. ;; | `($ e e ...) application
  60. ;; STACK FRAMES
  61. ;; s = (sf ...)
  62. ;; sf = ($ v ... □ e ...)
  63. (define (stack-frame? x)
  64. (sf:apply? x))
  65. (struct sf:apply [left right]
  66. #:methods gen:custom-write
  67. [(define (write-proc sf port _unused)
  68. (match-define (sf:apply vs es) sf)
  69. (fprintf port "~s" `($ ,@(reverse vs) □ ,@es)))])
  70. ; stop/continue = (continue s e)
  71. ; | (stop v)
  72. (struct continue [stack redex])
  73. (struct stop [value])
  74. ;; Evaluate expression until it reaches a value.
  75. ;; e -> v
  76. (define (evaluate* e)
  77. (let loop ([s '[]] [e e])
  78. (match (evaluate1 s e)
  79. [(stop v) v]
  80. [(continue s* e*) (loop s* e*)])))
  81. ;; Take a single step in evaluating expression 'e' with stack 's'.
  82. ;; s e -> stop/continue
  83. (define (evaluate1 s e)
  84. (match e
  85. [(? value?)
  86. (pop s e)]
  87. ;; {Σ;s | (top B)} ⇒ {Σ;s | Σ[B]}
  88. [`(top ,B ,name)
  89. (pop s
  90. (store-ref B
  91. #:default
  92. (λ () (error (~a "undefined: " name)))))]
  93. ; {s | ($ e1 e2 ...)} ⇒ {s ($ □ e2 ...) | e1}
  94. [`($ ,e1 ,e2s ...)
  95. (continue (cons (sf:apply '() e2s) s)
  96. e1)]))
  97. ;; Advance the top of stack 's' by applying the value 'v0'.
  98. ;; s v -> stop/continue
  99. (define (pop s v0)
  100. (match s
  101. ['()
  102. (stop v0)]
  103. [(cons (sf:apply vs es) s*)
  104. (match es
  105. ; {($ f v ... □) | v0} ⇒ apply f(v ... v0)
  106. ['()
  107. (match-define (cons f args)
  108. (reverse (cons v0 vs)))
  109. (apply-procedure s* f args)]
  110. ; {($ v1 ... □ e1 e2 ...) | v0} ⇒ {($ v1 ... v0 □ e2 ...) | e1}
  111. [(cons e1 e2s)
  112. (continue (cons (sf:apply (cons v0 vs) e2s) s*)
  113. e1)])]))
  114. ;; Apply arguments 'vs' to procedure value 'fun'.
  115. ;; RAISES: exn:fail - if 'fun' cannot be applied
  116. ;; s v [listof v] -> stop/continue
  117. (define (apply-procedure s fun vs)
  118. (match fun
  119. [(? primitive-procedure? prim)
  120. (continue s
  121. (primitive-apply prim vs))]
  122. [_ (error "not an applicable value: ~s" fun)]))
  123. (module+ test
  124. (require rackunit)
  125. (check-equal? (evaluate* `($ ,prim:+ 1 ($ ,prim:* ($ ,prim:- 4 1) 5)))
  126. (+ 1 (* (- 4 1) 5))))
  127. ;; ================================================
  128. ;; BINDINGS, STORES
  129. ;; Σ = ([B v] ...)
  130. ;; B = (x def)
  131. ;; def = `(val e)
  132. ;; [hasheq B => v]
  133. (define current-store
  134. (make-parameter (hasheq)))
  135. ;; (binding symbol [lazy def])
  136. (struct binding [name def/lz]
  137. #:methods gen:custom-write
  138. [(define (write-proc B port _)
  139. (cond [(print-hide-binding-rhs)
  140. (fprintf port
  141. "{binding:~a}"
  142. (binding-name B))]
  143. [else
  144. (parameterize ([print-hide-binding-rhs #t])
  145. (fprintf port
  146. "{binding:~a=~s}"
  147. (binding-name B)
  148. (binding-def B)))]))])
  149. (define print-hide-binding-rhs
  150. (make-parameter #f))
  151. ;; binding -> def
  152. (define (binding-def b)
  153. (force (binding-def/lz b)))
  154. ;; Create bindings for each given symbol. Uses the given function to create the 'def' parts
  155. ;; of each binding. The function should return a def corresponding to each binding. The bindings
  156. ;; given to to the function are also returned.
  157. ;; [listof symbol] [[listof B] -> [listof def]] -> [listof B]
  158. (define (make-bindings names mk-defs)
  159. (define bindings
  160. (for/list ([x (in-list names)]
  161. [i (in-naturals)])
  162. (binding x
  163. (delay (vector-ref defs i)))))
  164. (define defs
  165. (list->vector
  166. (mk-defs bindings)))
  167. bindings)
  168. ;; Generate possibly-recursive bindings (as in the struct) and bind them to given names (as in
  169. ;; racket variablebindings).
  170. ;;
  171. ;; (with-bindings ([id maybe-name def-expr] ...)
  172. ;; body ...)
  173. ;;
  174. ;; maybe-name =
  175. ;; | name-expr
  176. ;;
  177. (define-simple-macro (with-bindings ([B:id {~optional {~seq name:expr} #:defaults ([name #''B])}
  178. def:expr]
  179. ...)
  180. body ...+)
  181. (match-let ([(list B ...)
  182. (make-bindings (list name ...)
  183. (match-lambda
  184. [(list B ...)
  185. (list def ...)]))])
  186. body ...))
  187. ;; B -> value
  188. ;; B [-> X] -> (or value X)
  189. (define (store-ref B #:default [dft (λ ()
  190. (error (~a "undefined: "
  191. (binding-name B))))])
  192. (hash-ref (current-store)
  193. B
  194. (λ () (dft))))
  195. ;; B v -> void
  196. (define (store-init! B v)
  197. (hash-set! (current-store) B v))
  198. ;; B v -> void
  199. (define (store-set! B v
  200. #:ok [ok void]
  201. #:error [err (λ ()
  202. (error (~a "undefined: "
  203. (binding-name B))))])
  204. (cond
  205. [(hash-has-key? (current-store) B)
  206. (hash-ref (current-store) B)
  207. (ok)]
  208. [else
  209. (err)]))
  210. (module+ test
  211. (match-define (list B-x B-y)
  212. (make-bindings '[x y]
  213. (match-lambda
  214. [(list b1 b2)
  215. (list `(val 3)
  216. `(val (,prim:+ (top ,b1 x) 1)))])))
  217. (check-equal? (binding-name B-x) 'x)
  218. (check-equal? (binding-name B-y) 'y)
  219. (check-equal? (binding-def B-x) '(val 3))
  220. (check-match (binding-def B-y)
  221. `(val (,pls (top ,b x) 1))
  222. (and (eq? pls prim:+)
  223. (eq? b B-x))))
  224. ;; ================================================
  225. ;; STRUCTURES
  226. ;; S = (B ...)
  227. ;; (structure [listof binding])
  228. (struct structure
  229. [bindings] #:mutable)
  230. ;; Create a new empty structure.
  231. ;; -> S
  232. (define (make-structure)
  233. (structure '[]))
  234. ;; Add the list of bindings to the structure.
  235. ;; S [listof B] -> void
  236. (define (structure-add-bindings! S Bs)
  237. (set-structure-bindings! S (append (reverse Bs)
  238. (structure-bindings S))))
  239. ;; S B ... -> void
  240. (define (structure-add-bindings*! S . Bs)
  241. (structure-add-bindings! S Bs))
  242. ;; Evaluate a binding, adding its value to the store.
  243. ;; B -> void
  244. (define (instantiate-binding! B)
  245. (define v
  246. (match (binding-def B)
  247. [`(val ,e)
  248. (evaluate* e)]))
  249. (store-init! B v))
  250. ;; S -> void
  251. (define (instantiate-structure! S)
  252. (for-each instantiate-binding!
  253. (reverse (structure-bindings S))))
  254. (module+ test
  255. (define-simple-macro (with-structure S:id ([x:id . stuff] ...) body ...+)
  256. (let ([S (make-structure)])
  257. (with-bindings ([x . stuff] ...)
  258. (structure-add-bindings*! S x ...)
  259. body ...)))
  260. (define-simple-macro (with-empty-store Σ:id body ...+)
  261. (let ([Σ (make-hasheq)])
  262. (parameterize ([current-store Σ])
  263. body ...)))
  264. (with-structure S ([x `(val 3)]
  265. [y `(val ($ ,prim:+ 2 3))]
  266. [z `(val ($ ,prim:* (top ,x x) (top ,y y)))])
  267. (with-empty-store Σ
  268. (instantiate-structure! S)
  269. (check-equal? (store-ref x) 3)
  270. (check-equal? (store-ref y) 5)
  271. (check-equal? (store-ref z) 15)
  272. (check-true (hash-has-key? Σ x))
  273. (check-true (hash-has-key? Σ y))
  274. (check-true (hash-has-key? Σ z)))))