-
Notifications
You must be signed in to change notification settings - Fork 0
/
old.lisp
286 lines (271 loc) · 10.9 KB
/
old.lisp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
;(ql:quickload "optima")
;(ql:quickload "alexandria")
(in-package :cl-golang-generator)
(setf (readtable-case *readtable*) :invert)
(defparameter *file-hashes* (make-hash-table))
(defun write-source (name code &optional (dir (user-homedir-pathname))
ignore-hash)
(let* ((fn (merge-pathnames (format nil "~a.py" name)
dir))
(code-str (emit-py
:clear-env t
:code code))
(fn-hash (sxhash fn))
(code-hash (sxhash code-str)))
(multiple-value-bind (old-code-hash exists) (gethash fn-hash *file-hashes*)
(when (or (not exists) ignore-hash (/= code-hash old-code-hash))
;; store the sxhash of the c source in the hash table
;; *file-hashes* with the key formed by the sxhash of the full
;; pathname
(setf (gethash fn-hash *file-hashes*) code-hash)
(with-open-file (s fn
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(write-sequence code-str s))
(sb-ext:run-program "/usr/local/go/bin/go" (list "fmt" (namestring fn)))))))
(defun print-sufficient-digits-f64 (f)
"print a double floating point number as a string with a given nr. of
digits. parse it again and increase nr. of digits until the same bit
pattern."
(let* ((ff (coerce f 'double-float))
(s (format nil "~E" ff)))
#+nil (assert (= 0d0 (- ff
(read-from-string s))))
(assert (< (abs (- ff
(read-from-string s)))
1d-12))
(substitute #\e #\d s)))
;(print-sufficient-digits-f64 1d0)
(defparameter *env-functions* nil)
(defparameter *env-macros* nil)
(defun emit-go (&key code (str nil) (clear-env nil) (level 0))
;(format t "emit ~a ~a~%" level code)
(when clear-env
(setf *env-functions* nil
*env-macros* nil))
(flet ((emit (code &optional (dl 0))
(emit-go :code code :clear-env nil :level (+ dl level))))
(if code
(if (listp code)
(case (car code)
(tuple (let ((args (cdr code)))
(format nil "(~{~a,~})" (mapcar #'emit args))))
(paren (let ((args (cdr code)))
(format nil "(~{~a~^, ~})" (mapcar #'emit args))))
(ntuple (let ((args (cdr code)))
(format nil "~{~a~^, ~}" (mapcar #'emit args))))
(list (let ((args (cdr code)))
(format nil "[~{~a~^, ~}]" (mapcar #'emit args))))
(dict (let* ((args (cdr code)))
(let ((str (with-output-to-string (s)
(loop for (e f) in args
do
(format s "(~a):(~a)," (emit e) (emit f))))))
(format nil "{~a}" ;; remove trailing comma
(subseq str 0 (- (length str) 1))))))
(indent (format nil "~{~a~}~a"
(loop for i below level collect " ")
(emit (cadr code))))
(do (with-output-to-string (s)
(format s "~{~&~a~}" (mapcar #'(lambda (x) (emit `(indent ,x) 1)) (cdr code)))))
(class (destructuring-bind (name parents &rest body) (cdr code)
(format nil "class ~a~a:~%~a"
name
(emit `(paren ,@parents))
(emit `(do ,@body)))))
(do0 (with-output-to-string (s)
(format s "~&~a~{~&~a~}"
(emit (cadr code))
(mapcar #'(lambda (x) (emit `(indent ,x) 0)) (cddr code)))))
(lambda (destructuring-bind (lambda-list &rest body) (cdr code)
(multiple-value-bind (req-param opt-param res-param
key-param other-key-p aux-param key-exist-p)
(parse-ordinary-lambda-list lambda-list)
(declare (ignorable req-param opt-param res-param
key-param other-key-p aux-param key-exist-p))
(with-output-to-string (s)
(format s "lambda ~a: ~a"
(emit `(ntuple ,@(append req-param
(loop for e in key-param collect
(destructuring-bind ((keyword-name name) init suppliedp)
e
(declare (ignorable keyword-name suppliedp))
(if init
`(= ,name ,init)
`(= ,name "None")))))))
(if (cdr body)
(break "body ~a should have only one entry" body)
(emit (car body))))))))
(def (destructuring-bind (name lambda-list &rest body) (cdr code)
(multiple-value-bind (req-param opt-param res-param
key-param other-key-p aux-param key-exist-p)
(parse-ordinary-lambda-list lambda-list)
(declare (ignorable req-param opt-param res-param
key-param other-key-p aux-param key-exist-p))
(with-output-to-string (s)
(format s "def ~a~a:~%"
name
(emit `(paren ,@(append req-param
(loop for e in key-param collect
(destructuring-bind ((keyword-name name) init suppliedp)
e
(declare (ignorable keyword-name suppliedp))
(if init
`(= ,name ,init)
`(= ,name "None"))))))))
(format s "~a" (emit `(do ,@body)))))))
(= (destructuring-bind (a b) (cdr code)
(format nil "~a=~a" (emit a) (emit b))))
(in (destructuring-bind (a b) (cdr code)
(format nil "(~a in ~a)" (emit a) (emit b))))
(as (destructuring-bind (a b) (cdr code)
(format nil "~a as ~a" (emit a) (emit b))))
(setf (let ((args (cdr code)))
(format nil "~a"
(emit `(do0
,@(loop for i below (length args) by 2 collect
(let ((a (elt args i))
(b (elt args (+ 1 i))))
`(= ,a ,b))))))))
(aref (destructuring-bind (name &rest indices) (cdr code)
(format nil "~a[~{~a~^,~}]" (emit name) (mapcar #'emit indices))))
(slice (let ((args (cdr code)))
(if (null args)
(format nil ":")
(format nil "~{~a~^:~}" (mapcar #'emit args)))))
(dot (let ((args (cdr code)))
(format nil "~{~a~^.~}" (mapcar #'emit args))))
(+ (let ((args (cdr code)))
(format nil "(~{(~a)~^+~})" (mapcar #'emit args))))
(- (let ((args (cdr code)))
(format nil "(~{(~a)~^-~})" (mapcar #'emit args))))
(* (let ((args (cdr code)))
(format nil "(~{(~a)~^*~})" (mapcar #'emit args))))
(== (let ((args (cdr code)))
(format nil "(~{(~a)~^==~})" (mapcar #'emit args))))
(<< (let ((args (cdr code)))
(format nil "(~{(~a)~^<<~})" (mapcar #'emit args))))
(!= (let ((args (cdr code)))
(format nil "(~{(~a)~^!=~})" (mapcar #'emit args))))
(< (let ((args (cdr code)))
(format nil "(~{(~a)~^<~})" (mapcar #'emit args))))
(<= (let ((args (cdr code)))
(format nil "(~{(~a)~^<=~})" (mapcar #'emit args))))
(>> (let ((args (cdr code)))
(format nil "(~{(~a)~^>>~})" (mapcar #'emit args))))
(/ (let ((args (cdr code)))
(format nil "((~a)/(~a))"
(emit (first args))
(emit (second args)))))
(** (let ((args (cdr code)))
(format nil "((~a)**(~a))"
(emit (first args))
(emit (second args)))))
(// (let ((args (cdr code)))
(format nil "((~a)//(~a))"
(emit (first args))
(emit (second args)))))
(% (let ((args (cdr code)))
(format nil "((~a)%(~a))"
(emit (first args))
(emit (second args)))))
(and (let ((args (cdr code)))
(format nil "(~{(~a)~^ and ~})" (mapcar #'emit args))))
(& (let ((args (cdr code)))
(format nil "(~{(~a)~^ & ~})" (mapcar #'emit args))))
(|\|| (let ((args (cdr code)))
(format nil "(~{(~a)~^ | ~})" (mapcar #'emit args))))
(or (let ((args (cdr code)))
(format nil "(~{(~a)~^ or ~})" (mapcar #'emit args))))
(string (format nil "\"~a\"" (cadr code)))
(string3 (format nil "\"\"\"~a\"\"\"" (cadr code)))
(rstring3 (format nil "r\"\"\"~a\"\"\"" (cadr code)))
(return_ (format nil "return ~a" (emit (caadr code))))
(return (let ((args (cdr code)))
(format nil "~a" (emit `(return_ ,args)))))
(for (destructuring-bind ((vs ls) &rest body) (cdr code)
(with-output-to-string (s)
;(format s "~a" (emit '(indent)))
(format s "for ~a in ~a:~%"
(emit vs)
(emit ls))
(format s "~a" (emit `(do ,@body))))))
(while (destructuring-bind (vs &rest body) (cdr code)
(with-output-to-string (s)
(format s "while ~a:~%"
(emit `(paren ,vs)))
(format s "~a" (emit `(do ,@body))))))
(if (destructuring-bind (condition true-statement &optional false-statement) (cdr code)
(with-output-to-string (s)
(format s "if ( ~a ):~%~a"
(emit condition)
(emit `(do ,true-statement)))
(when false-statement
(format s "~&~a:~%~a"
(emit `(indent "else"))
(emit `(do ,false-statement)))))))
(import (destructuring-bind (args) (cdr code)
(if (listp args)
(format nil "import ~a as ~a~%" (second args) (first args))
(format nil "import ~a~%" args))))
(imports (destructuring-bind (args) (cdr code)
(format nil "~{~a~}" (mapcar #'(lambda (x) (emit `(import ,x))) args))))
(with (destructuring-bind (form &rest body) (cdr code)
(with-output-to-string (s)
(format s "~a~a:~%~a"
(emit "with ")
(emit form)
(emit `(do ,@body))))))
(try (destructuring-bind (prog &rest exceptions) (cdr code)
(with-output-to-string (s)
(format s "~&~a:~%~a"
(emit "try")
(emit `(do ,prog)))
(loop for e in exceptions do
(destructuring-bind (form &rest body) e
(if (member form '(else finally))
(format s "~&~a~%"
(emit `(indent ,(format nil "~a:" form))))
(format s "~&~a~%"
(emit `(indent ,(format nil "except ~a:" (emit form))))))
(format s "~a" (emit `(do ,@body)))))))
#+nil (let ((body (cdr code)))
(with-output-to-string (s)
(format s "~a:~%" (emit "try"))
(format s "~a" (emit `(do ,@body)))
(format s "~a~%~a"
(emit "except Exception as e:")
(emit `(do "print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)"))))))
(t (destructuring-bind (name &rest args) code
(if (listp name)
;; lambda call and similar complex constructs
(format nil "(~a)(~a)" (emit name) (if args
(emit `(paren ,@args))
""))
#+nil(if (eq 'lambda (car name))
(format nil "(~a)(~a)" (emit name) (emit `(paren ,@args)))
(break "error: unknown call"))
;; function call
(let* ((positional (loop for i below (length args) until (keywordp (elt args i)) collect
(elt args i)))
(plist (subseq args (length positional)))
(props (loop for e in plist by #'cddr collect e)))
(format nil "~a~a" name
(emit `(paren ,@(append
positional
(loop for e in props collect
`(= ,(format nil "~a" e) ,(getf plist e))))))))))))
(cond
((or (symbolp code)
(stringp code)) ;; print variable
(format nil "~a" code))
((numberp code) ;; print constants
(cond ((integerp code) (format str "~a" code))
((floatp code)
(format str "(~a)" (print-sufficient-digits-f64 code)))
((complexp code)
(format str "((~a) + 1j * (~a))"
(print-sufficient-digits-f64 (realpart code))
(print-sufficient-digits-f64 (imagpart code))))))))
"")))