Giter Club home page Giter Club logo

ulithp's Introduction

μLithp

A micro LISP implementation in 24 lines of Ruby, with a reader in 33 lines, REPL in 35 lines, a compiler in 15 lines and Ruby interop in 13 lines. Really frickin' small.

  • car
  • cdr
  • quote
  • atom
  • if
  • label
  • cons
  • eq
  • lambda

See the src/lithp.rb file for source and doc/index.org for implementation details and the original announcement in the μLithp blog post.

this is my entry into the December 2012 PLT Games

Embedded usage

The LISP interpreter is just a Ruby class that evals expressions in Ruby data structures, like so:

	 l = Lisp.new

	 l.eval [:label, :a, 42]

	 l.eval :a
	 #=> 42

	 l.eval [:eq, 42, :a]
	 #=> true

	 l.eval [:quote, [1, 2]]
	 #=> [1, 2]

	 l.eval [:car, [:quote, [1, 2]]]
	 #=> 1

	 l.eval [:cdr, [:quote, [1, 2]]]
	 #=> [2]

	 l.eval [:cons, 1, [:quote, [2,3]]]
	 #=> [1, 2, 3]

	 l.eval [:if, [:eq, 1, 2], 42, 43]
	 #=> 43

	 l.eval [:atom, [:quote, [1,2]]]
	 #=> false

	 l.eval [:label, :second, [:quote, [:lambda, [:x], 	 [:car, [:cdr, :x]]]]]

	 l.eval [:second, [:quote, [1, 2, 3]]]
	 #=> 2

Have fun!

REPL usage

In your shell use Ruby 1.9.2 to run the REPL:

    rvm use ruby-1.9.2
	ruby -I . repl.rb

You'll then see a prompt:

>

Start typing uLithp code:

    (car (quote (1 2 3)))

	(cdr (quote (1 2 3)))

	(label third (quote (lambda (x) (car (cdr (cdr x))))))
	(third (quote (1 2 3 4 5)))

Enjoy!

thanks

Thanks to Russ Olsen for the reader and REPL.

License

This software is provided as-is under the MIT license.

ulithp's People

Contributors

flada-auxv avatar fogus avatar sumbach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ulithp's Issues

execute lambas without labeling them?

awesome project, what would be an ideal implementation to run a lambda without having to label it into the context?, in your examples you have:

l.eval [:label, :second, [:quote, [:lambda, [:x],   [:car, [:cdr, :x]]]]]

l.eval [:second, [:quote, [1, 2, 3]]]
#=> 2

this pretty much defeats the purpose of creating lambdas if we are naming them, for example to make this possible:

l.eval [[:quote, [:lambda, [:x],   [:car, [:cdr, :x]]]], [:quote, [1, 2, 3]]]
#=> 2

how would you suggest implementing something like this?

one use case is to implement a map funcionality for example doing:

map: proc do |(fn, list), ctx|
  evaluate(list, ctx).map do |i|
      evaluate([fn, i], ctx)
  end
end,

this works fine for a form like this:

[:map, :inc, [:quote, [1, 2, 3]]]

(making inc: ->((n), _) { n + 1 })
which returns

[2, 3, 4]

as expected

BUT, for a lambda, will not work:

[
  :map,
  [:quote, [:lambda, [:x], [:inc, :x]]],
  [:quote, [1, 2, 3]]
]

since the key :lambda does not exist in the ctx, causing errors in:

evaluate ctx[fn][2], ctx.merge(Hash[*(ctx[fn][1].zip args).flatten(1)])

because fn (:lambda) does not exist in the context, but since we dont care, we can just use the "body", is a lambda after all right?

sorry if I am making the question more complex than needed, but this is why I wanted to check how would you implement lambda execution without labeling them, thanks

Add LICENSE.txt

Would like some license info. Pretty sure it's MIT though. 😉

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.