-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdragonscheme.rkt
executable file
·70 lines (63 loc) · 2.75 KB
/
dragonscheme.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
#!/usr/bin/env racket
(module dragonscheme racket
(require
"lexer.rkt"
"parser.rkt"
"ast.rkt"
"llvm.rkt"
)
; The main user interface of the compiler.
; Open the filename passed as the (required) command-line argument, and parse it.
(define verbose-mode (make-parameter #f))
(define lex-only (make-parameter #f))
(define parse-only (make-parameter #f))
(define outfilename (make-parameter ""))
(define exec-mode (make-parameter #f))
(define raw-code (make-parameter #f))
(define cmdline-args
(command-line ; http://docs.racket-lang.org/reference/Command-Line_Parsing.html
#:program "dragonscheme"
#:once-each
[("-v" "--verbose") "Compile with verbose messages."
(verbose-mode #t)]
[("-o" "--output") output ; flag takes one argument
"Specify the name of the output *.bc file. Default is [filename].bc An *.ll file will also be produced."
(outfilename output)]
[("-i") "Instead of an input filename, take raw code to compile. Must also specify either -p or -o"
(raw-code #t)]
#:once-any
[("-p" "--parse") "Lex and parse only; Don't emit IR code."
(parse-only #t)]
;[("-l" "--lex") "Lex only; Don't parse or emit IR code."
; (lex-only #t)]
[("-x" "--exec") "Execute compiled IR code immediately."
(exec-mode #t)]
#:multi
;
#:args (filename) ; expect one command-line argument: <filename>
; return the argument as a filename to compile
filename))
(define (web-pretty-print xexp)
(let ((pretty-xexp (open-output-string)))
(pretty-print xexp pretty-xexp)
(get-output-string pretty-xexp)))
(let ((path cmdline-args))
(let ((filestream (cond
((raw-code) (open-input-string path)) ; "path" is the actual code
(else (open-input-file path #:mode 'binary))))) ; otherwise it's the path to the code file
(cond
((parse-only) (parse filestream))
(else ; compile (and maybe exec)
(let
((bcfilename
(cond
((and (not raw-code) (equal? (outfilename) "")) ; no output file specified? Use input filename w/ extension changed to ".bc"
(let*
((basename path)
(extension (filename-extension path)) )
(string-append (substring basename 0 (- (string-length basename) (bytes-length extension))) "bc"))) ; replace extension with .bc
(else (outfilename)))))
; The actual compile:
(code-gen (parse filestream) bcfilename (verbose-mode) (exec-mode)))
))))
)