Crates.io | rusche |
lib.rs | rusche |
version | 0.2.4 |
source | src |
created_at | 2024-10-14 11:07:55.009656 |
updated_at | 2024-10-24 11:48:27.131132 |
description | A lightweight Scheme interpreter embeddable in Rust applications |
homepage | |
repository | https://github.com/chanryu/rusche |
max_upload_size | |
id | 1408186 |
size | 162,492 |
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.
`
), unquote (,
), unquote-splicing (,@
)Foreign
data type.Span
support for informative error message, for example:
repl:01❯ (define plus
....:02❯ (lambda (x 7) ;; 7 should be y
....:03❯ (+ x y)))
error: 7 is not a symbol.
1| (define plus
2| (lambda (x 7)
| ^
use rusche::{tokenize, Evaluator, Expr, Parser};
let source = "(+ 1 (% 9 2))"; // 1 + (9 % 2) = 1 + 1 = 2
// Create Evaluator with basic primitives
let evaluator = Evaluator::with_prelude();
let mut parser = Parser::new();
// Tokenize source and add tokens to parser
let tokens = tokenize(source, None).unwrap();
parser.add_tokens(tokens);
// Parse tokens into an expression
let expr = parser.parse().unwrap().unwrap();
// Evaluate the parsed expression
let result = evaluator.eval(&expr);
assert_eq!(result, Ok(Expr::from(2)));
println!("{}", result.unwrap()); // this prints out 2
To learn about how to implement a standalone interpreter with REPL, have a look at examples/rusche-cli.
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 (num-parse (read)))) ; read a number from stdio and store it to `m`
(while (<= n m)
(println (fizzbuzz n))
(set! n (+ n 1))))
To see more examples, please checkout *.rsc files in the examples directory.
Also, you can run rusche-cli
yourself with the following command:
cargo run --example rusche-cli