-
Notifications
You must be signed in to change notification settings - Fork 3
/
sinscm.rkt
84 lines (71 loc) · 2.77 KB
/
sinscm.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#lang racket
(require (only-in "compiler.rkt"
scm->exe llvm->exe scm->llvm))
; TODO: delete all the generated headers at some point? Maybe after
; everything works and no error generated?
(define outfileparam (make-parameter null))
(define infileparam (make-parameter null))
(define sourcecodeparam (make-parameter null))
(define outputtypeparam (make-parameter null))
(define inputtypeparam (make-parameter null))
(command-line
#:program "Sinscheme compiler"
#:once-each
[("-o" "--outfile") outfile
"LLVM output to specific file"
(outfileparam outfile)]
[("-t" "--out-type") outputtype
("What kind of thing to output. -t LLVM"
" for llvm ir text file, -t EXE for executable.")
(outputtypeparam outputtype)]
[("-j" "--in-type") inputtype
"What kind of thing is input, LLVM or SCM (-j LLVM or -j SCM)"
(inputtypeparam inputtype)]
#:once-any
["-e" source-code
"Compile given code directly"
(sourcecodeparam source-code)]
[("-i" "--infile") infile
"Sin Scheme file that will be compiled to LLVM"
(infileparam infile)])
(define input-port
(cond
[(not (null? (infileparam))) (open-input-file (infileparam) #:mode 'text)]
[(not (null? (sourcecodeparam))) (open-input-string (sourcecodeparam))]
[else (error "Please provide an input file name (-i)")]))
(define output-port
(if (null? (outfileparam))
(error "Please provide an output file name (-o)")
(open-output-file (outfileparam) #:mode 'text #:exists 'replace)))
(define input-type
(if (null? (inputtypeparam))
'()
(case (inputtypeparam)
[("LLVM" "llvm") 'llvm]
[("SCM" "scm" "SCHEME" "scheme" "SINSCM" "sinscm") 'scm]
[else (error "Please provide an input type (-j)")])))
(define output-type
(if (null? (outputtypeparam))
'()
(case (outputtypeparam)
[("LLVM" "llvm") 'llvm]
[("EXE" "exe") 'exe]
[else (error "Please provide an output type (-t)")])))
(match (cons input-type output-type)
[(cons 'llvm 'exe)
(llvm->exe)
(define input-llvm-string (file->string (infileparam)))
(llvm->exe input-llvm-string (outfileparam))]
[(cons 'scm 'llvm)
(define out-file (open-output-file (outfileparam)))
(display (scm->llvm input-port) out-file)
(close-output-port out-file)]
[(cons 'scm 'exe)
(scm->exe input-port (outfileparam))]
[(cons i o) (error (format "Unsupported input/output combo I:'~a' and O:'~a'" i o))])
(if (null? input-port)
(displayln "Why is input port null?")
(close-input-port input-port))
(if (null? output-port)
(displayln "Why is output port null?")
(close-output-port output-port))