rusche 0.1.2

A minimalistic Scheme interpreter written in Rust
Documentation

Rusche

ci coverage crates.io

Overview

Rusche is a library for writing an interpreter for a Scheme-like language in Rust. It lets you embed a Scheme interpreter into your Rust applications, allowing you to use Scheme as a scripting language or to create standalone Scheme interpreters.

Features

  • Minimalistic library with no rumtime dependency
  • Garbage collection
  • Tail-call optimization
  • Interoperability with hosting Rust application via Foreign data type.

Usage

Implementing or embedding Rusche interpreter

use rusche::eval::Evaluator;
use rusche::lexer::tokenize;
use rusche::parser::Parser;

let source = "(+ 1 (% 9 2))";

// Create Evaluator with basic primitives
let evaluator = Evaluator::with_prelude();

let mut parser = Parser::new();

// Tokenize source and add tokens to parser
parser.add_tokens(tokenize(source).unwrap());

// Parse tokens into an expression
let expr = parser.parse().unwrap().unwrap();

// Evaluate the parsed expression
let result = evaluator.eval(&expr).unwrap();

println!("{}", result); // This will print 2

To learn about how to implement a standalone interpreter with REPL, have a look at examples/rusche-cli.

Rusche language

Here's a quick example to show what's possible with the Rusche language.

(defun fizzbuzz (n)
    (defun div? (n m) (= (% n m) 0))
    (cond ((div? n 15) "FizzBuzz")
          ((div? n 3) "Fizz")
          ((div? n 5) "Buzz")
          (#t n)))

(print "Enter a number to fizzbuzz: ")

(let ((n 1)
      (m (read-num))) ; read a number from stdio and store it to `m`
    (while (<= n m)
        (println (fizzbuzz n))
        (set! n (+ n 1))))

To see more example, please checkout *.rsc files in the examples directory.

Documentation