From 983d9f3b20e5fb30cb8e530f33ded90971bbac5f Mon Sep 17 00:00:00 2001 From: Iqbal Ansari Date: Thu, 25 Aug 2016 22:26:26 +0530 Subject: [PATCH] Common Lisp: Implement step 0 --- .gitignore | 1 + Makefile | 3 ++- common-lisp/Makefile | 4 ++++ common-lisp/run | 2 ++ common-lisp/step0_repl.asd | 22 ++++++++++++++++++ common-lisp/step0_repl.lisp | 45 +++++++++++++++++++++++++++++++++++++ 6 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 common-lisp/Makefile create mode 100755 common-lisp/run create mode 100644 common-lisp/step0_repl.asd create mode 100644 common-lisp/step0_repl.lisp diff --git a/.gitignore b/.gitignore index 54c01585..5d30679a 100644 --- a/.gitignore +++ b/.gitignore @@ -119,3 +119,4 @@ basic/step8_macros.bas basic/step9_try.bas basic/stepA_mal.bas basic/*.prg +common-lisp/*.image diff --git a/Makefile b/Makefile index eb810be4..250b25cd 100644 --- 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 index 00000000..a98c1f7c --- /dev/null +++ b/common-lisp/Makefile @@ -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 index 00000000..8ba68a54 --- /dev/null +++ b/common-lisp/run @@ -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 index 00000000..3acb2659 --- /dev/null +++ b/common-lisp/step0_repl.asd @@ -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 index 00000000..e891ee60 --- /dev/null +++ b/common-lisp/step0_repl.lisp @@ -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))))) -- 2.20.1