Git Product home page Git Product logo

sedlisp's Introduction

SedLisp

Lisp implementation in (GNU) sed

How to Use

$ sed -f sedlisp.sed  # '>' won't be shown.
> (car (quote (a b c)))
a
> (cdr (quote (a b c)))
(b c)
> (cons 1 (cons 2 (cons 3 ())))
(1 2 3)
> (defun fact (n) (if (eq n 0) 1 (* n (fact (- n 1)))))
(lambda (n) (if (eq n 0) 1 (* n (fact (- n 1)))))
> (fact 10)
3628800
> (defun fib (n) (if (eq n 1) 1 (if (eq n 0) 1 (+ (fib (- n 1)) (fib (- n 2))))))
(lambda (n) (if (eq n 1) 1 (if (eq n 0) 1 (+ (fib (- n 1)) (fib (- n 2))))))
> (fib 12)
233
> (defun gen (n) ((lambda (x y) y) (define G n) (lambda (m) (define G (+ G m)))))
(lambda (n) ((lambda (x y) y) (define G n) (lambda (m) (define G (+ G m)))))
> (define x (gen 100))
(lambda (m) (define G (+ G m)))
> (x 10)
110
> (x 90)
200
> (x 300)
500

Builtin Functions

  • car
  • cdr
  • cons
  • eq
  • atom
  • +, -, *, /, mod
  • neg?
  • print

Special Forms

  • quote
  • if
  • lambda
  • defun
  • define

More Complicated Examples

You can test a few more examples.

FizzBuzz:

$ cat fizzbuzz.l | sed -f sedlisp.sed
(lambda (n) (if (eq n 101) nil (if (print (if (eq (mod n 15) 0) FizzBuzz (if (eq (mod n 5) 0) Buzz (if (eq (mod n 3) 0) Fizz n)))) (fizzbuzz (+ n 1)) nil)))
PRINT: 1
PRINT: 2
PRINT: Fizz
...
PRINT: 98
PRINT: Fizz
PRINT: Buzz
nil

Sort:

$ cat sort.l /dev/stdin | sed -f sedlisp.sed
...
(sort (quote (4 2)))
(2 4)
(sort (quote (4 2 99 12 -4 -7)))
(-7 -4 2 4 12 99)

Though this Lisp implementation does not support eval function, we can implement eval on top of this interpreter - eval.l is the implementation:

$ cat eval.l /dev/stdin | sed -f sedlisp.sed
(eval (quote (+ 4 38)))
42
(eval (quote (defun fact (n) (if (eq n 0) 1 (* n (fact (- n 1)))))))
(fact (lambda (n) (if (eq n 0) 1 (* n (fact (- n 1))))))
(eval (quote (fact 4)))  ; Takes 10 seconds or so.
24

This essentially means we have a Lisp interpreter in Lisp. evalify.rb is a helper script to convert a normal Lisp program into the Lisp in Lisp. You can run the FizzBuzz program like:

$ ./evalify.rb fizzbuzz.l | sed -f sedlisp.sed
...
PRINT: 1
PRINT: 2
PRINT: Fizz

This takes very long time. For me, it took 45 minutes.

Though sedlisp.sed does not support defmacro, eval.l also defines defmacro:

$ ./evalify.rb | sed -f sedlisp.sed
(defmacro let (l e) (cons (cons lambda (cons (cons (car l) nil) (cons e nil))) (cons (car (cdr l)) nil)))
(let (x 42) (+ x 7))  ; Hit ^d after this.
...
49
$ ./evalify.rb | sed -f sedlisp.sed
(defun list0 (a) (cons a nil))
(defun cadr (a) (car (cdr a)))
(defmacro cond (l) (if l (cons if (cons (car (car l)) (cons (cadr (car l)) (cons (cons (quote cond) (list0 (cdr l))))))) nil))
(defun fb (n) (cond (((eq (mod n 5) 0) "Buzz") ((eq (mod n 3) 0) "Fizz") (t n))))
(fb 18)  ; Hit ^d after this. This will take about one minute.
...
Fizz

You can apply ./evalify.rb multiple times. However, sedlisp seems to be too slow to run the generated program. purelisp.rb, which is a reference implementation of sedlisp, can run it:

$ ./evalify.rb fizzbuzz.l | ./evalify.rb | ruby purelisp.rb
...
PRINT: 1
PRINT: 2
PRINT: Fizz
PRINT: 4
PRINT: Buzz
PRINT: Fizz
PRINT: 7
PRINT: 8

test.l is the test program I was using during the development. test.rb runs it with sedlisp.sed and purelisp.rb and compare their results. You can run the test with evalify.rb by passing -e:

$ ./test.rb -e

Limitations

There should be a lot of limitations. sedlisp behaves very strangely when you pass a broken Lisp code.

I don't know how many GNU extensions I used, so it would not be easy to port this to other sed implementations.

sedlisp's People

Contributors

shinh avatar troydj avatar irori avatar

Stargazers

 avatar Ben Lovy avatar Shea Newton avatar  avatar Jason Wilkes avatar Stephen Wall avatar Artyom Bologov avatar Artsiom Karakin avatar  avatar Illia Ostapyshyn avatar Andrey Shcheglov avatar Kuroishi Mitsuo avatar  avatar Thomas Leary avatar  avatar Rhys avatar Max Smirnov avatar Masanori Ogino avatar Inulu avatar  avatar Zhao Xiaohong avatar  avatar PandaOS avatar Anthony Wang avatar Bowuigi avatar Ay355 avatar Firegem avatar  avatar Ammar Faizi avatar STYLIANOS IORDANIS avatar haruna avatar ebigram avatar Martin Vahi avatar Anthony Sorace avatar Tomohito Nakayama avatar Felix Queißner avatar  avatar Amadeusine avatar Bruce Seely avatar  avatar Kamil Adam avatar Jorge Gomez avatar ihsan avatar Bryan A. S. avatar Tim Kersey avatar Philip Zucker avatar hito avatar  avatar Zaki Mughal [sivoais] avatar Dan Kogai avatar Charles Horn avatar Nicolas Meinen avatar  avatar Rodrigo Fernandes avatar Nikita avatar  avatar Amir Keshavarz avatar Sora Morimoto avatar Shiimizu avatar henrywong avatar Pa Mu Selvakumar avatar peccu avatar  avatar muuk avatar Lucas Vieira avatar  avatar Aleksey Zubakov avatar Krzysztof Magosa avatar Dylan Weaver avatar Julia avatar Émilie avatar Aleksei Kioller avatar Matthias Nüßler avatar Bautista Sanchez avatar Martin Braun avatar Brad Svercl avatar Kisaragi Hiu avatar Dáel Muñiz avatar camille avatar Danny Lin avatar Iqbal Rifai avatar iomonad avatar lolisamurai avatar Olga Ustiuzhanina avatar  avatar Pocholo Pantoja avatar Arvin Verain avatar XXX avatar Shigeki Karita avatar Felix Gruber avatar Luke McCarthy avatar  avatar ✨ Schuyler St. Leger avatar ryan avatar Ju avatar  avatar Tyler Xing avatar Kurt L. Manion avatar The Archives avatar  avatar

Watchers

 avatar Spencer Tipping avatar Takeshi Watanabe avatar evandrix avatar MATSUOKA Hiroshi avatar  avatar Katunori Takesita avatar James Cloos avatar Wilfrido Nuqui Jr. avatar Francisco Jesús Rubio Reales avatar Eldar Zakirov avatar  avatar Ubu the Tech Guru avatar lloyd huang avatar Kamil Adam avatar  avatar

sedlisp's Issues

(print "Hello World")

First of all, I know nothing about Lisp and this was first time I ever ran anything related to Lisp. This might not be a bug report, since it might be the design of SedLisp, but I would want to know more about this and Lisp, although I don't plan to learn Lisp.

I got this Hello World from Wikipedia, which is said for Common Lisp:

(print "Hello world")

When I ran with d8621dc, I got this error:

$ sed -f sedlisp.sed <<< '(print "Hello world")'
invalid print: (print "Hello world")

Then I saw quote in examples, so I tried it even I had no idea what it actually did:

$ sed -f sedlisp.sed <<< '(print (quote "Hello World"))'
PRINT: "Hello,World"
"Hello World"
$ sed -f sedlisp.sed <<< '(print (quote Hello World))'
PRINT: Hello,World
Hello World

Frankly, I just randomly got what I wanted by messing around. Would anyone explain what they actually did and why in a few short lines? Especially the lack of quotation marks. I got a hunch from the look of PRINT message, that's when I removed the quotes, and it did work in the result, even I had no idea what really was going on.

As far as I can see from the Wikipedia, Lisp has many dialects, so which one is SedLisp close to and is this a bug?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.