Skip to content

Instantly share code, notes, and snippets.

@Goheeca
Last active November 17, 2019 18:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Goheeca/05e92c3a561a81737f2f177b7119766f to your computer and use it in GitHub Desktop.
Save Goheeca/05e92c3a561a81737f2f177b7119766f to your computer and use it in GitHub Desktop.
Common Lisp ≡ ⊤
#||| Python or JS that is the question. |#
#|| Let's define a JS function, provided we're in JS. |#
#+moody-js"
function fact(n) {
if (n == 0) return 1
else return n * fact(n - 1)
}
"
#|| Let's define a Python function, provided we're in Python. |#
#+moody-python"
def fact(n):
if n == 0:
return 1
else:
return n * fact(n - 1)
"
#|| Test our function, here we go! |#
print(fact(10))
;;; LOADING libraries
(load "~/.sbclrc")
(ql:quickload '("dexador" "cl-ppcre" "cl-js" "clpython") :silent t)
;;; OUTER WORLD api
(defun trivia (n)
(dex:get (format nil "http://numbersapi.com/~a" n)))
(defconstant +bound+ 100)
(defun random-trivia (&optional (bound +bound+))
(trivia (random bound)))
(defun oddly-long-p (trivia)
(oddp (length (cl-ppcre:split "\\s+" trivia))))
;;; SYNTAX facilities
(defun omnivore-read-func (read-function)
(lambda (stream char)
(unread-char char stream)
(funcall read-function stream)))
(defun setup-omnivore-readmacro (read-function)
(dotimes (i char-code-limit)
(set-macro-character (code-char i) (omnivore-read-func read-function) t *readtable*)))
(defun reader (interpreting-function stream)
(let ((input (apply #'concatenate 'string
(loop for line = (read-line stream nil nil t)
until (zerop (length line))
collect line collect (string #\Newline)))))
(funcall interpreting-function
(if (and (not (zerop (length input))) (eq #\# (char input 0)))
(with-standard-io-syntax
(concatenate 'string (read-from-string input nil nil) (string #\Newline)))
input))))
(defun js ()
(setup-omnivore-readmacro #'(lambda (stream) (reader #'cl-js:run-js stream))))
(defun python ()
(setup-omnivore-readmacro #'(lambda (stream) (reader #'clpython:run stream))))
(defun init (&optional (trivia '+trivia+) (interpreter '+interpreter+))
`(eval-when
(:compile-toplevel :load-toplevel :execute)
(setf *random-state* (make-random-state t))
(let ((trivia (random-trivia)))
(format *trace-output* "~&~s is ~:[fine~;odd~]; ~:*~:[Python~;JS~] it is!~%"
(cl-ppcre:split "\\s+" trivia) (oddly-long-p trivia))
(unintern ',trivia)
(defconstant ,trivia trivia)
(unintern ',interpreter)
(defconstant ,interpreter
(cond
((oddly-long-p trivia)
(js)
(push #1=:moody-js *features*)
#1#)
(t
(setf clpython:*habitat* (clpython:make-habitat))
(python)
(push #2=:moody-python *features*)
#2#))))))
(defmacro init-interpreter ()
(init))
(defun init-image ()
(init-interpreter)
(sb-impl::toplevel-repl nil))
(load #p"moody.lisp")
;;; ENABLING JS or Python randomly depending on the outer world
(progn (init-interpreter)
(load #p"ambiguous.pyjs"))
(load #p"moody.lisp")
;;; MAKING a core with a random interpreter depending on the outer world
(sb-ext:save-lisp-and-die #p"moody.core" :compression t :toplevel #'init-image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment