Skip to content

Commit 2afbfc4

Browse files
authored
Merge pull request #315 from uwplse/convert-demo
Script to convert demo files to FPCore
2 parents 0c7aa1e + 2d1d36b commit 2afbfc4

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

infra/convert-json.rkt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#lang racket
2+
3+
(require json)
4+
(require "../src/programs.rkt")
5+
6+
(define (fix-expr expr pre-fpcore?)
7+
(let loop ([expr expr])
8+
(match* (pre-fpcore? expr)
9+
[(_ (? string?))
10+
(or (string->number expr) (error 'fix-expr "string that is not a num: ~a" expr))]
11+
[(#t (list 'expt (app loop a) (app loop b))) (list 'pow a b)]
12+
[(#t (list 'cube (app loop a))) (list 'pow a 3)]
13+
[(#t (list 'cot (app loop a))) (list '/ 1 (list 'tan a))]
14+
[(#t (list 'sqr (app loop a))) (list '* a a)]
15+
[(#t (list 'abs (app loop a))) (list 'fabs a)]
16+
[(#t (list 'mod (app loop a))) (list 'fmod a)]
17+
[(#t 'e) 'E]
18+
[(#t 'pi) 'PI]
19+
[(_ (list op (app loop args) ...)) (list* op args)]
20+
[(_ _) expr])))
21+
22+
(define (make-fpcore test pre-fpcore?)
23+
(define expr (fix-expr (call-with-input-string (hash-ref test 'input) read) pre-fpcore?))
24+
(define vars (map string->symbol (hash-ref test 'vars (λ () (map ~a (free-variables expr))))))
25+
(define spec (fix-expr (call-with-input-string (hash-ref test 'spec (~s expr)) read) pre-fpcore?))
26+
(define pre (fix-expr (call-with-input-string (hash-ref test 'pre "TRUE") read) pre-fpcore?))
27+
`(FPCore ,vars
28+
,@(if (hash-has-key? test 'name) (list ':name (hash-ref test 'name)) '())
29+
,@(if (not (equal? pre "TRUE")) (list ':pre pre) '())
30+
,@(if (not (equal? spec expr)) (list ':spec spec) '())
31+
,@(if (hash-has-key? test 'prec) (list ':precision (string->symbol (hash-ref test 'prec))) '())
32+
,expr))
33+
34+
(define (convert-files json-files pre-fpcore?)
35+
(define seen (mutable-set))
36+
(for ([json-file (in-list json-files)])
37+
(define data (call-with-input-file json-file read-json))
38+
(for ([test (hash-ref data 'tests)])
39+
(define expr (make-fpcore test pre-fpcore?))
40+
(unless (set-member? seen expr)
41+
(set-add! seen expr)
42+
(pretty-print expr (current-output-port) 1)
43+
(newline)))))
44+
45+
(module+ main
46+
(define pre-fpcore? #f)
47+
(command-line
48+
#:program "convert-json"
49+
#:once-each
50+
[("--pre-fpcore") "The demo file dates from before Herbie 1.0"
51+
(set! pre-fpcore? #t)]
52+
#:args json-files
53+
(convert-files json-files pre-fpcore?)))
54+

0 commit comments

Comments
 (0)