1
0

Rearrange repo; Add rust example

This commit is contained in:
Jeff 2024-07-15 16:42:49 -04:00
parent a3917238d9
commit 501801b63e
12 changed files with 62 additions and 1 deletions

View File

@ -1,7 +1,7 @@
# Dust
High-level programming language with effortless concurrency, automatic memory management, type
safety and advanced error handling.
safety and familiar syntax.
```dust
io.write_line("Guess the number.")

View File

@ -0,0 +1,51 @@
use std::{
sync::{mpsc::channel, Arc},
thread,
time::Duration,
};
use context::Context;
use dust_lang::*;
fn run_fibnacci(interpreter: &Interpreter, i: u8) -> Value {
// These double brackets are not Dust syntax, it's just an escape sequence for Rust's format!
// macro.
let source = Arc::from(format!(
"
fib = fn (i: int) -> int {{
if i <= 1 {{
i
}} else {{
fib(i - 1) + fib(i - 2)
}}
}}
fib({i})"
));
interpreter
.run(Arc::from(i.to_string()), source)
.unwrap() // Panic if there are errors.
.unwrap() // Panic if the no value is returned.
}
fn main() {
let interpreter = Interpreter::new(Context::new());
let (tx, rx) = channel();
for i in 1..10 {
let interpreter = interpreter.clone();
let tx = tx.clone();
thread::spawn(move || {
let value = run_fibnacci(&interpreter, i);
tx.send(value).unwrap();
});
}
// Give the threads half a second to finish.
while let Ok(value) = rx.recv_timeout(Duration::from_millis(500)) {
println!("{}", value);
}
}

View File

@ -1,3 +1,12 @@
/**
The Dust programming language.
Dust is a statically typed, interpreted programming language.
The top-level module contains the `Interpreter` struct, which is used to lex, parse and/or
interpret Dust code. The `interpret` function is a convenience function that creates a new
`Interpreter` and runs the given source code.
*/
pub mod abstract_tree;
pub mod context;
pub mod error;
@ -33,6 +42,7 @@ pub fn interpret(source_id: &str, source: &str) -> Result<Option<Value>, Interpr
///
/// You must provide the interpreter with an ID for each piece of code you pass to it. These are
/// used to identify the source of errors and to provide more detailed error messages.
#[derive(Clone, Debug)]
pub struct Interpreter {
context: Context,
sources: Arc<RwLock<HashMap<Arc<str>, Arc<str>>>>,