Common Lisp: Implement step 0
authorIqbal Ansari <iqbalansari02@yahoo.com>
Thu, 25 Aug 2016 16:56:26 +0000 (22:26 +0530)
committerIqbal Ansari <iqbalansari02@yahoo.com>
Fri, 18 Nov 2016 12:29:27 +0000 (17:59 +0530)
.gitignore
Makefile
common-lisp/Makefile [new file with mode: 0644]
common-lisp/run [new file with mode: 0755]
common-lisp/step0_repl.asd [new file with mode: 0644]
common-lisp/step0_repl.lisp [new file with mode: 0644]

index 54c0158..5d30679 100644 (file)
@@ -119,3 +119,4 @@ basic/step8_macros.bas
 basic/step9_try.bas
 basic/stepA_mal.bas
 basic/*.prg
+common-lisp/*.image
index eb810be..250b25c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -77,7 +77,7 @@ DOCKERIZE =
 # Settings
 #
 
-IMPLS = ada awk bash basic c d chuck clojure coffee clisp cpp crystal cs dart \
+IMPLS = ada awk bash basic c d chuck clojure coffee clisp common-lisp cpp crystal cs dart \
        erlang elisp elixir es6 factor forth fsharp go groovy guile haskell \
        haxe io java julia js kotlin logo lua make mal ocaml matlab miniMAL \
        nim objc objpascal perl perl6 php pil plpgsql plsql powershell ps \
@@ -154,6 +154,7 @@ chuck_STEP_TO_PROG =   chuck/$($(1)).ck
 clojure_STEP_TO_PROG = clojure/target/$($(1)).jar
 coffee_STEP_TO_PROG =  coffee/$($(1)).coffee
 clisp_STEP_TO_PROG = clisp/$($(1)).fas
+common-lisp_STEP_TO_PROG =  common-lisp/$($(1))
 cpp_STEP_TO_PROG =     cpp/$($(1))
 crystal_STEP_TO_PROG = crystal/$($(1))
 cs_STEP_TO_PROG =      cs/$($(1)).exe
diff --git a/common-lisp/Makefile b/common-lisp/Makefile
new file mode 100644 (file)
index 0000000..a98c1f7
--- /dev/null
@@ -0,0 +1,4 @@
+ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
+
+step% : step%.lisp
+       cl-launch -v -l sbcl +Q -S $(ROOT_DIR) -s $@ -d $@.image -o $@ -E 'mal:main'
diff --git a/common-lisp/run b/common-lisp/run
new file mode 100755 (executable)
index 0000000..8ba68a5
--- /dev/null
@@ -0,0 +1,2 @@
+#!/bin/bash
+exec $(dirname $0)/${STEP:-stepA_mal} "${@}"
diff --git a/common-lisp/step0_repl.asd b/common-lisp/step0_repl.asd
new file mode 100644 (file)
index 0000000..3acb265
--- /dev/null
@@ -0,0 +1,22 @@
+#-quicklisp
+(let ((quicklisp-init (merge-pathnames "quicklisp/setup.lisp"
+                                       (user-homedir-pathname))))
+  (when (probe-file quicklisp-init)
+    (load quicklisp-init)))
+
+(ql:quickload :cl-readline)
+(ql:quickload :uiop)
+
+(defpackage #:mal-asd
+  (:use :cl :asdf))
+
+(in-package :mal-asd)
+
+(defsystem "step0_repl"
+  :name "MAL"
+  :version "1.0"
+  :author "Iqbal Ansari"
+  :description "Implementation of step 0 of MAL in Common Lisp"
+  :serial t
+  :components ((:file "step0_repl"))
+  :depends-on (:uiop :cl-readline))
diff --git a/common-lisp/step0_repl.lisp b/common-lisp/step0_repl.lisp
new file mode 100644 (file)
index 0000000..e891ee6
--- /dev/null
@@ -0,0 +1,45 @@
+(defpackage :mal
+  (:use :common-lisp
+        :uiop)
+  (:export :main))
+
+(in-package :mal)
+
+(defun mal-read (string)
+  string)
+
+(defun mal-eval (ast)
+  ast)
+
+(defun mal-print (expression)
+  expression)
+
+(defun rep (string)
+  (mal-print (mal-eval (mal-read string))))
+
+(defvar *use-readline-p* nil)
+
+(defun raw-input (prompt)
+  (format *standard-output* prompt)
+  (force-output *standard-output*)
+  (read-line *standard-input* nil))
+
+(defun mal-readline (prompt)
+  (if *use-readline-p*
+      (cl-readline:readline :prompt prompt
+                            :add-history t
+                            :novelty-check (lambda (old new)
+                                             (not (string= old new))))
+      (raw-input prompt)))
+
+(defun mal-writeline (string)
+  (when string
+    (write-line string)
+    (force-output *standard-output*)))
+
+(defun main (&optional (argv nil argv-provided-p))
+  (declare (ignorable argv argv-provided-p))
+  (setf *use-readline-p* (not (or (string= (uiop:getenv "PERL_RL") "false")
+                                  (string= (uiop:getenv "TERM") "dumb"))))
+  (loop do (let ((line (mal-readline "user> ")))
+             (if line (mal-writeline (rep line)) (return)))))