GithubHelp home page GithubHelp logo

qaphan3007 / scalisp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mononofu/scalisp

0.0 1.0 0.0 391 KB

A lisp interpreter and compiler, written in Scala. Compiles Lisp to Scala !

License: MIT License

Scala 92.34% Common Lisp 7.66%

scalisp's Introduction

		 .oooooo..o                     oooo   o8o                      
		d8P'    `Y8                     `888   `"'                      
		Y88bo.       .ooooo.   .oooo.    888  oooo   .oooo.o oo.ooooo.  
		 `"Y8888o.  d88' `"Y8 `P  )88b   888  `888  d88(  "8  888' `88b 
		     `"Y88b 888        .oP"888   888   888  `"Y88b.   888   888 
		oo     .d8P 888   .o8 d8(  888   888   888  o.  )88b  888   888 
		8""88888P'  `Y8bod8P' `Y888""8o o888o o888o 8""888P'  888bod8P' 
		                                                      888       
		                                                     o888o      

A lisp interpreter and compiler written in Scala, inspired by Peter Norvig

Download

You can either clone this repo and use sbt or you can download the precompiled jar and execute that.

To be able to compile the resulting scala code, you should also download compiled_builtins.scala and place it in the same directory as the compiled .scala file.

Interpeter

It's a bit incomplete and probably buggy, but it works good enough to for most applications:

(define fact (lambda (n) (if (< n 2) 1 (* n (fact (- n 1))))))
(fact 10) 

produces 3628800

Also, Merge sort can be implemented without a problem:

; note that you can also use comments
; and split functions over multiple lines for readability
(define msort 
  (lambda (list) 
    (if (<= (length list) 1) 
      list 
      (begin 
        (define split (/ (length list) 2)) 
        (merge 
          (msort (subseq list 0 split)) 
          (msort (subseq list split)) 
        ) 
      ) 
    ) 
  )
)

; ordering is not important, as functions are evaluated lazily
(define merge
  (lambda (a b)
    (if (< (length a) 1)
      b
      (if (< (length b) 1)
        a
        (if (< (car a) (car b))
          (cons (car a) (merge (cdr a) b))
          (cons (car b) (merge a (cdr b)))
        )
      )
    )
  )
)

Usage is just like you'd expect

(msort '(5 7 2 1 3 4 6))

And results in '(1 2 3 4 5 6 7)

It's also possible to execute files, simply do

scalisp filename.l

or, from the sbt-console

run filename.l

Compiler

Instead of taking the traditional route and compiling to byte code (which Clojure already does), I decided to compile to Scala instead. Just use scalisp like you would for interpetation, but add the -c switch.

For example, the compiled merge sort looks like this:

def merge(a: Any, b: Any): Any = {
  if(length(a) < 1l) {
    b
  } else {
    if(length(b) < 1l) {
      a
    } else {
      if(car(a) < car(b)) {
        car(a) :: merge(cdr(a), b)
      } else {
        car(b) :: merge(a, cdr(b))
      }
    }
  }
}

def msort(list: Any): Any = {
  if(length(list) <= 1l) {
    list
  } else {
    {
      var split = (length(list) / 2l)
      merge(msort(subseq(list, 0l, split)), msort(subseq(list, split)))
    }
  }
}

If you specify a whole file, you'll get a complete .scala file back, ready to be compiled. If you just execute scalisp -c, you get a REPL, which simply compiles snippets of code.

scalisp's People

Contributors

mononofu avatar pauljbernard avatar reactormonk avatar

Watchers

James Cloos 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.