|
6 | 6 | bf-precision |
7 | 7 | bigfloat->string |
8 | 8 | bigfloat? |
9 | | - bf)) |
| 9 | + bf) |
| 10 | + profile) |
10 | 11 | (require "eval/main.rkt" |
11 | 12 | "eval/machine.rkt" |
12 | 13 | "utils.rkt") |
13 | | -(provide rival-repl) |
| 14 | +(provide repl-main repl-profile) |
14 | 15 |
|
15 | 16 | (define (create-discs args bodies repl) |
16 | 17 | (for/list ([body (in-list bodies)]) |
|
181 | 182 | (~r (* (execution-time exec) 1000) #:precision '(= 1))))) |
182 | 183 | value])))) |
183 | 184 |
|
184 | | -(define (rival-repl p) |
| 185 | +(define (repl-run repl cmd #:print? [print? #t]) |
| 186 | + (with-handlers ([exn:fail:user? (lambda (e) (eprintf "ERROR ~a\n" (exn-message e)))]) |
| 187 | + (match cmd |
| 188 | + [`(set precision fp64) |
| 189 | + (set-repl-precision! repl 'fp64)] |
| 190 | + [`(set precision ,(? integer? n)) |
| 191 | + (when (< n 4) |
| 192 | + (raise-user-error 'set "Precision must be an integer greater than 3")) |
| 193 | + (set-repl-precision! repl (list 'bf n))] |
| 194 | + [`(define (,(? symbol? name) ,(? symbol? args) ...) |
| 195 | + ,bodies ...) |
| 196 | + (repl-save-machine! repl name args bodies)] |
| 197 | + [`(eval ,name ,(? (disjoin real? boolean?) vals) ...) |
| 198 | + (define machine (repl-get-machine repl name)) |
| 199 | + (check-args! name machine vals) |
| 200 | + (define out (repl-apply repl machine vals)) |
| 201 | + (when print? |
| 202 | + (if (string? out) |
| 203 | + (displayln out) |
| 204 | + (for ([val (in-vector out)]) |
| 205 | + (displayln (bigfloat->string val)))))] |
| 206 | + [`(explain ,name ,(? (disjoin real? boolean?) vals) ...) |
| 207 | + (define machine (repl-get-machine repl name)) |
| 208 | + (check-args! name machine vals) |
| 209 | + |
| 210 | + ;; Make sure the cache is warm |
| 211 | + (repl-apply repl machine vals) |
| 212 | + ;; Make sure the profile is clear |
| 213 | + (rival-profile machine 'executions) |
| 214 | + |
| 215 | + ;; Time the actual execution |
| 216 | + (define start (current-inexact-milliseconds)) |
| 217 | + (repl-apply repl machine vals) |
| 218 | + (define end (current-inexact-milliseconds)) |
| 219 | + |
| 220 | + (when print? |
| 221 | + (write-explain machine) |
| 222 | + (printf "\nTotal: ~aµs\n" (~r (* (- end start) 1000) #:precision '(= 1))))] |
| 223 | + [(or '(help) 'help) |
| 224 | + (displayln "This is the Rival REPL, a demo of the Rival real evaluator.") |
| 225 | + (newline) |
| 226 | + (displayln "Commands:") |
| 227 | + (displayln " (set precision <n>) Set working precision to n") |
| 228 | + (displayln " (define (<name> <args> ...) <body> ...) Define a named function") |
| 229 | + (displayln " (eval <name> <vals> ...) Evaluate a named function") |
| 230 | + (displayln |
| 231 | + " (explain <name> <vals> ...) Show profile for evaluating a named function") |
| 232 | + (newline) |
| 233 | + (displayln "A closed expression can always be used in place of a named function.")] |
| 234 | + [_ (printf "Unknown command ~a; use help for command list\n" cmd)]))) |
| 235 | + |
| 236 | +(define (repl-main p) |
185 | 237 | (let/ec k |
186 | 238 | (parameterize ([read-decimal-as-inexact #f] |
187 | 239 | [*rival-name-constants* #t]) |
188 | 240 | (define repl (make-repl)) |
189 | 241 | (when (terminal-port? p) |
190 | 242 | (display "> ")) |
191 | 243 | (for ([cmd (in-port read p)]) |
192 | | - (with-handlers ([exn:fail:user? (lambda (e) (eprintf "ERROR ~a\n" (exn-message e)))]) |
193 | | - (match cmd |
194 | | - [`(set precision fp64) |
195 | | - (set-repl-precision! repl 'fp64)] |
196 | | - [`(set precision ,(? integer? n)) |
197 | | - (when (< n 4) |
198 | | - (raise-user-error 'set "Precision must be an integer greater than 3")) |
199 | | - (set-repl-precision! repl (list 'bf n))] |
200 | | - [`(define (,(? symbol? name) ,(? symbol? args) ...) |
201 | | - ,bodies ...) |
202 | | - (repl-save-machine! repl name args bodies)] |
203 | | - [`(eval ,name ,(? (disjoin real? boolean?) vals) ...) |
204 | | - (define machine (repl-get-machine repl name)) |
205 | | - (check-args! name machine vals) |
206 | | - (define out (repl-apply repl machine vals)) |
207 | | - (if (string? out) |
208 | | - (displayln out) |
209 | | - (for ([val (in-vector out)]) |
210 | | - (displayln (bigfloat->string val))))] |
211 | | - [`(explain ,name ,(? (disjoin real? boolean?) vals) ...) |
212 | | - (define machine (repl-get-machine repl name)) |
213 | | - (check-args! name machine vals) |
214 | | - |
215 | | - ;; Make sure the cache is warm |
216 | | - (repl-apply repl machine vals) |
217 | | - ;; Make sure the profile is clear |
218 | | - (rival-profile machine 'executions) |
219 | | - |
220 | | - ;; Time the actual execution |
221 | | - (define start (current-inexact-milliseconds)) |
222 | | - (repl-apply repl machine vals) |
223 | | - (define end (current-inexact-milliseconds)) |
224 | | - |
225 | | - (write-explain machine) |
226 | | - |
227 | | - (printf "\nTotal: ~aµs\n" (~r (* (- end start) 1000) #:precision '(= 1)))] |
228 | | - [(or '(help) 'help) |
229 | | - (displayln "This is the Rival REPL, a demo of the Rival real evaluator.") |
230 | | - (newline) |
231 | | - (displayln "Commands:") |
232 | | - (displayln " (set precision <n>) Set working precision to n") |
233 | | - (displayln " (define (<name> <args> ...) <body> ...) Define a named function") |
234 | | - (displayln " (eval <name> <vals> ...) Evaluate a named function") |
235 | | - (displayln |
236 | | - " (explain <name> <vals> ...) Show profile for evaluating a named function") |
237 | | - (newline) |
238 | | - (displayln "A closed expression can always be used in place of a named function.")] |
239 | | - [(or '(exit) 'exit) (k)] |
240 | | - [_ (printf "Unknown command ~a; use help for command list\n" cmd)])) |
| 244 | + (if (set-member? '((exit) exit) cmd) |
| 245 | + (k) |
| 246 | + (repl-run repl cmd)) |
241 | 247 | (when (terminal-port? p) |
242 | | - (display "> ")))) |
243 | | - (when (terminal-port? p) |
244 | | - (displayln "exit")))) |
| 248 | + (display "> "))))) |
| 249 | + (when (terminal-port? p) |
| 250 | + (displayln "exit"))) |
| 251 | + |
| 252 | +(define (repl-profile p) |
| 253 | + (define cmds |
| 254 | + (parameterize ([read-decimal-as-inexact #f]) |
| 255 | + (for/list ([cmd (in-port read p)]) |
| 256 | + cmd))) |
| 257 | + (define t0 (current-inexact-milliseconds)) |
| 258 | + (define m0 (current-memory-use 'cumulative)) |
| 259 | + (define repl (make-repl)) |
| 260 | + (match-define (cons t1 m1) |
| 261 | + (profile |
| 262 | + (begin |
| 263 | + (for ([cmd (in-list cmds)]) |
| 264 | + (repl-run repl cmd #:print? #f)) |
| 265 | + (cons (current-inexact-milliseconds) |
| 266 | + (current-memory-use 'cumulative))) |
| 267 | + #:delay 0.001)) |
| 268 | + (eprintf "Ran in ~ams, allocated ~aMB\n" |
| 269 | + (~r (- t1 t0) #:precision '(= 3)) |
| 270 | + (~r (/ (- m1 m0) 1000000) #:precision '(= 3)))) |
0 commit comments