This repository has been archived by the owner on Dec 4, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.rkt
60 lines (56 loc) · 1.79 KB
/
compiler.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#lang racket/base
(require racket/runtime-path
racket/contract
racket/match
racket/file
racket/system)
(provide
(contract-out
[compile (-> language? path-string?
language? path-string?
void)]
[link (-> path-string? path-string?
void)]))
(define language? symbol?)
(define nasm-pth (find-executable-path "nasm"))
(define ld-pth (find-executable-path "ld"))
(define (link a-pth b-pth)
(define o-pth (make-temporary-file "~a.o"))
(system* nasm-pth "-f" "macho" a-pth "-o" o-pth)
(unless (file-exists? o-pth)
(error 'link "Failed to produce object file for ~e" a-pth))
(case (system-type)
[(macosx)
(system* ld-pth "-macosx_version_min" "10.7" "-lSystem" "-o" b-pth o-pth)]
[(unix)
(system* ld-pth "-o" b-pth o-pth)])
(unless (file-exists? b-pth)
(error 'link "Failed to product binary file for ~e" a-pth))
(delete-file o-pth))
(define-runtime-path langs "langs")
(define (compile in-lang in-pth
out-lang out-pth)
(define in-read
(dynamic-require (build-path langs (format "~a.rkt" in-lang))
'read
(lambda () read)))
(define in
(with-input-from-file in-pth
in-read))
(define in-parse
(dynamic-require (build-path langs (format "~a.rkt" in-lang))
'parse))
(define in-parsed
(in-parse in))
(define to-out
(dynamic-require (build-path langs (format "~a.rkt" in-lang))
(string->symbol (format "to-~a" out-lang))))
(define out-form
(to-out in-parsed))
(define out-write
(dynamic-require (build-path langs (format "~a.rkt" out-lang))
'write))
(with-output-to-file
out-pth #:exists 'replace
(lambda ()
(out-write out-form))))