forked from sctb/lumen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.l
131 lines (117 loc) · 3.83 KB
/
main.l
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
(define reader (_G .reader))
(define compiler (_G .compiler))
(define system (_G .system))
(define eval-print (form)
(let ((ok v) (guard (compiler .eval form)))
(if (not ok)
(print (v .stack))
(is? v) (print (str v)))))
(define rep (s)
(eval-print (reader .read-string s)))
(define repl ()
(let buf ""
(define rep1 (s)
(cat! buf s)
(let (more (list)
form (reader .read-string buf more))
(unless (= form more)
(eval-print form)
(set buf "")
(system .write "> ")))))
(system .write "> ")
(let in (process .stdin)
(in (.removeAllListeners))
(in .setEncoding "utf8")
(in .on "data" rep1)))
(define-global pp-to-string (body)
(if (atom? body) (str body)
(empty? body) (str body)
(let s "("
(step x body
(cat! s (str x) "\n\n"))
(cat s ")"))))
(define-global pp (body)
(print (pp-to-string body))
body)
(define-global read-file (path)
(let (s (reader .stream (system .read-file path))
body (reader .read-all s))
(if (one? body) (hd body) `(do ,@body))))
(define-global expand-file (path)
(let body (read-file path)
(compiler .expand body)))
(define-global compile-file (path)
(let (body (expand-file path)
form (compiler .expand body))
(compiler .compile form stmt: true)))
(define-global load (path)
(let code (compile-file path)
(let prev (or (_G .exports) (obj))
(with x (set (_G .exports) (obj))
(compiler .run code)
(set (_G .exports) prev)))))
(define script-file? (path)
(not (or (= "-" (char path 0))
(= ".js" (clip path (- (# path) 3))))))
(define run-file (path)
(if (script-file? path)
(load path)
(compiler .run (system .read-file path))))
(define usage ()
(print "usage: dax [<file> <arguments> | options <object files>]")
(print " <file>\t\tProgram read from script file")
(print " <arguments>\tPassed to program in system.argv")
(print " <object files>\tLoaded before compiling <input>")
(print "options:")
(print " -c <input>\tCompile input file")
(print " -x <input>\tExpand input file")
(print " -a <input>\tRead input file")
(print " -o <output>\tOutput file")
(print " -e <expr>\tExpression to evaluate"))
(define main ()
(let arg (hd (system .argv))
(if (and arg (script-file? arg))
(load arg)
(or (= arg "-h")
(= arg "--help"))
(usage)
(let (pre (list)
op nil
input nil
output nil
expr nil
argv (system .argv))
(for i (# argv)
(let a (at argv i)
(if (or (= a "-c") (= a "-x") (= a "-a") (= a "-o") (= a "-t") (= a "-e"))
(if (= i (edge argv))
(print (cat "missing argument for " a))
(do (inc i)
(let val (at argv i)
(if (= a "-c") (set input val op 'compile)
(= a "-x") (set input val op 'expand)
(= a "-a") (set input val op 'read)
(= a "-o") (set output val)
(= a "-e") (set expr val)))))
(not (= "-" (char a 0))) (add pre a))))
(step file pre
(run-file file))
(if (nil? input) (if expr (rep expr) (repl))
(do (let code (if (= op 'expand) (pp-to-string (expand-file input))
(= op 'read) (pp-to-string (read-file input))
(compile-file input))
(if (or (nil? output) (= output "-"))
(print code)
(system .write-file output code)))))))))
(export reader
compiler
system
eval-print
rep
repl
compile-file
load
script-file?
run-file
usage
main)