Git Product home page Git Product logo

peroxide's Introduction

Peroxide

A scheme interpreter in Rust. Aims for R5RS/R7RS compliance. Heavily based on the interpreter described in Lisp in Small Pieces.

Usage

You can just run cargo run to run the interpreter. Some internal options can be tweaked; try cargo run -- --help for more information.

Set RUST_LOG=peroxide=debug or RUST_LOG=peroxide=trace to see debugging information, especially GC-related messages. (This may make the system very slow.)

General implementation notes

This is a bytecode compiling implementation: scheme code is first converted to bytecode, then interpreted by a virtual machine.

The standard library is essentially ripped off Chibi Scheme. See init.scm for license details. Credit to Alex Shinn for writing it.

Peroxide is strictly single-threaded.

This comes with a very simple garbage collector. See the comment in heap.rs for implementation details. Unfortunately it meshes poorly with Rust's memory management. The key thing to remember when making changes, especially to the AST parser, is that any call to arena.insert() (the method used to ask the GC for memory) may trigger a garbage-collection pass and destroy anything that isn't rooted. Make sure to hold RootPtrs to any Scheme data you care about when doing stuff!

The macro system was another important implementation question. I ended up going with a system similar to Chibi Scheme's so that I could reuse more of the standard library ๐Ÿ™ƒ. This does mean that, in addition to syntax-case, Peroxide supports the more general syntactic closure macro paradigm. See doc/macros.md for details.

Todo

See todo.md for a list of things to do.

Useful references

peroxide's People

Contributors

jpellegrini avatar mattx avatar

Stargazers

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

Watchers

 avatar  avatar

peroxide's Issues

src/scheme-lib/init.scm - No such file

should be verbose error

current output

$ ./result/bin/main
No previous history.
Error: No such file or directory (os error 2)

$ strace --trace=file ./result/bin/main 2>&1 | grep 'No such file' | grep -v -e /nix/store -e /etc/ld-nix.so.preload
openat(AT_FDCWD, "history.txt", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "src/scheme-lib/init.scm", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
Error: No such file or directory (os error 2)

interpreter.initialize("src/scheme-lib/init.scm")?;

the file path src/scheme-lib/init.scm is relative to the current workdir (cwd)
suggestion: allow to set library path via argv or env

peroxide --scheme-lib /usr/share/peroxide/scheme-lib
SCHEME_LIB=/usr/share/peroxide/scheme-lib peroxide

benchmark

as expected for such a young project, peroxide is really slow, about 150K times slower than petite-chez

benchmark script from reddit

(define (erato n z primes)
   (define (sieve s)
      (if (or (null? s)                  ; prime
              (< n (* (car s) (car s)))) ; prime!
          (erato (+ n 1) z (append primes (list n)))
          (if (zero? (modulo n (car s))) ; composite
              (erato (+ n 1) z primes)
              (sieve (cdr s)))))

   (if (< n z)
       (sieve primes)
       primes))

(define (primes<2n n)
  (erato 2 (* n 2) (list)))

; load and time are not implemented in peroxide
(primes<2n 100)
; 25 seconds on peroxide scheme interpreter
;  0.000167 seconds on petite-chez scheme interpreter
; -> peroxide is about 150K times slower than petite-chez

; (load "erato.scm")
; (define a (time (primes<2n 100000)))
; 2.7 seconds on petite-chez scheme interpreter
; 3.2 seconds on racket scheme interpreter
; 6.9 seconds on gambit scheme interpreter
; 48.3 seconds on chicken scheme interpreter

call/cc crashes with no arguments

call/cc works when called properly:

>>> (call/cc (lambda (k) (+ 1 (k -1))))
 => -1
>>> 

But it seems that some argument check is missing:

RUST_BACKTRACE=full RUST_BACKTRACE=1 target/debug/main
Handler set!
>>> (call/cc)
thread 'main' panicked at 'index out of bounds: the len is 0 but the index is 0', src/environment.rs:238:13
stack backtrace:
   0: rust_begin_unwind
             at /usr/src/rustc-1.49.0/library/std/src/panicking.rs:495:5
   1: core::panicking::panic_fmt
             at /usr/src/rustc-1.49.0/library/core/src/panicking.rs:92:14
   2: core::panicking::panic_bounds_check
             at /usr/src/rustc-1.49.0/library/core/src/panicking.rs:69:5
   3: <usize as core::slice::index::SliceIndex<[T]>>::index
             at /usr/src/rustc-1.49.0/library/core/src/slice/index.rs:182:10
   4: core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
             at /usr/src/rustc-1.49.0/library/core/src/slice/index.rs:15:9
   5: <alloc::vec::Vec<T> as core::ops::index::Index<I>>::index
             at /usr/src/rustc-1.49.0/library/alloc/src/vec.rs:1939:9
   6: peroxide::environment::ActivationFrame::get
             at ./src/environment.rs:238:13
   7: peroxide::environment::ActivationFrame::get
             at ./src/environment.rs:240:13
   8: peroxide::vm::run_one_instruction
             at ./src/vm.rs:276:26
   9: peroxide::vm::run
             at ./src/vm.rs:200:15
  10: peroxide::Interpreter::compile_run
             at ./src/lib.rs:176:9
  11: peroxide::Interpreter::parse_compile_run
             at ./src/lib.rs:169:9
  12: main::rep
             at ./src/bin/main.rs:141:15
  13: main::handle_one_expr
             at ./src/bin/main.rs:132:13
  14: main::handle_one_expr_wrap
             at ./src/bin/main.rs:79:5
  15: main::do_main
             at ./src/bin/main.rs:68:13
  16: main::main
             at ./src/bin/main.rs:29:11
  17: core::ops::function::FnOnce::call_once
             at /usr/src/rustc-1.49.0/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Discussion about syntax-case

I would like to address some inaccuracies in the discussion on macro systems in Scheme: https://github.com/MattX/peroxide/blob/master/doc/macros.md, so let me give a number of facts:

  • syntax-case is hygienic by default; however, using datum->syntax can be used to break hygiene explicitly. In particular, one cannot write an unhygienic macro by accident. (Compare with ER macros, where one has to be very careful to maintain hygiene.
  • That no one cared about R6RS is utterly wrong. People still care about R6RS because it was not succeeded by R7RS, which is to be seen as an independent successor of R5RS.
  • R7RS did not supersede R6RS.
  • syntax-case is no more complex to implement than syntactic closures, for example.
  • syntax-case will be part of the large language of R7RS.
  • It is not possible to implement syntax-case on top of syntactic closures. I wrote Chibi's implementation but that implementation is not fully compliant (it cannot be because of the limitations of syntactic closures).
  • It is not possible to write unhygienic macros that work in all contexts with ER macros.
  • Unhygienic macros written with syntactic closures are incompatible with hygienic syntax-rules macros.
  • Syntactic closures are strictly less powerful than syntax-case; for example, the unhygienic define-record-type of SRFI 99 cannot be expressed with syntactic closures.

The upshot is that from the set of discussed macro systems, only syntax-case turns out to be a viable choice.

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.