Giter Site home page Giter Site logo

jamesmoriarty / lisp Goto Github PK

View Code? Open in Web Editor NEW
51.0 5.0 5.0 143 KB

Minimal Lisp interpreter using 75LOC and only standard libraries.

Home Page: http://www.jamesmoriarty.xyz/lisp/

License: MIT License

Ruby 100.00%
ruby lisp interpreter exp lambda quote procedure conseq tinycode

lisp's Introduction

lisp

Gem Version Gem Build Status

Minimal Lisp interpreter using 75LOC and only standard libraries excluding the REPL. Inspired by Lis.py.

$ lisp-repl
ctrl-c to exit
> (begin                                                                        
(>   (define incf                                                               
((>     (lambda (x)                                                             
(((>       (set! x (+ x 1))))                                                   
(>   (define one 1)                                                             
(>   (incf one))                                                                
2
>                                      

Install

gem install lisp

Usage

require "lisp"

Lisp.eval(<<-eos)
  (begin
    (define fact
      (lambda (n)
        (if (<= n 1)
          1
          (* n (fact (- n 1))))))
    (fact 10))
eos # => 3628800

Commandline

lisp-repl

Documentation

Yardoc

Rubygem

Features

  • constant literal number - A number evaluates to itself. Example: 12 or -3.45e+6

  • procedure call - (proc exp...) If proc is anything other than one of the symbols if, set!, define, lambda, begin, or quote then it is treated as a procedure. It is evaluated using the same rules defined here. All the expressions are evaluated as well, and then the procedure is called with the list of expressions as arguments. Example: (square 12) ⇒ 144

  • variable reference - var A symbol is interpreted as a variable name; its value is the variable's value. Example: x

  • definition - (define var exp) Define a new variable and give it the value of evaluating the expression exp. Examples: (define r 3) or (define square (lambda (x) (* x x))).

  • procedure - (lambda (var...) exp) Create a procedure with parameter(s) named var... and the expression as the body. Example: (lambda (r) (* 3.141592653 (* r r)))

  • conditional - (if test conseq alt) Evaluate test; if true, evaluate and return conseq; otherwise evaluate and return alt. Example: (if (< 10 20) (+ 1 1) (+ 3 3)) ⇒ 2

  • quotation - (quote exp) Return the exp literally; do not evaluate it. Example: (quote (a b c)) ⇒ (a b c)

  • assignment - (set! var exp) Evaluate exp and assign that value to var, which must have been previously defined (with a define or as a parameter to an enclosing procedure). Example: (set! x2 (* x x))

  • sequencing - (begin exp...) Evaluate each of the expressions in left-to-right order, and return the final value. Example: (begin (set! x 1) (set! x (+ x 1)) (* x 2)) ⇒ 4

lisp's People

Contributors

caike avatar jamesmoriarty 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

Watchers

 avatar  avatar  avatar  avatar  avatar

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.