-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.scm
executable file
·80 lines (68 loc) · 1.88 KB
/
main.scm
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
; Copyright (c) 2011, Peter Brottveit Bock
; Released under the BSD 3-Clause License
; See: https://raw.github.com/peterbb/compiler/master/LICENSE
(load "ast.scm")
(load "parse.scm")
(load "gensym.scm")
(load "io.scm")
(load "cps.scm")
(load "beta-reduce.scm")
(load "llvm.scm")
(load "code-gen.scm")
(load "builtin.scm")
;;; "args" is a list of strings, which are the arguments.
;;; The first element in "args" is the name of the file to compile.
;;; The rest of the arguments should be the names of the stages to preform
;;; in the order as they appear.
(define (main args)
(let* ((file-name (car args))
(pipeline (make-pipeline (cdr args)))
(code (add-static-prelude (io:read-file file-name)))
(ast (parse code)))
(pipeline ast)))
;;; This code is implicitly appended to the begining of the
;;; compiled file.
(define *static-prelude*
(list
'(load "prelude/all.scm")
))
(define (add-static-prelude code)
(append *static-prelude* code))
(define (make-pipeline args)
(if (null? args)
(lambda (ast)
(display "; Done\n"))
(let ((stage (assoc (car args) *stages*))
(rest-stages (make-pipeline (cdr args))))
(if stage
(lambda (ast)
(rest-stages ((cdr stage) ast)))
(error "unknown argument:" (car args))))))
(define *stages*
(list (cons "ast"
(lambda (ast)
(display ";;; AST:\n")
(pp ast)
(newline)
ast))
(cons "cps"
cps-convert)
(cons "beta"
beta-reduce)
(cons "gen"
(lambda (ast)
(generate-code ast (map cdr *global-variables*))))
(cons "code"
(lambda (code)
(display ";;; LLVM:\n")
(for-each print-llvm-line code)
code))
))
(define (print-llvm-line line)
(if (and (not (char=? #\{ (string-ref line (- (string-length line) 1))))
(not (string=? line "}"))
(not (char=? #\@ (string-ref line 0))))
(display " "))
(display line)
(newline))
(main (command-line-arguments))