From 501801b63eb1603cedf798a7f2c92ab5493f0c58 Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 15 Jul 2024 16:42:49 -0400 Subject: [PATCH] Rearrange repo; Add rust example --- README.md | 2 +- dust-lang/examples/interpreter_async.rs | 51 +++++++++++++++++++ dust-lang/src/lib.rs | 10 ++++ {examples => dust_examples}/assets/data.json | 0 .../assets/jq_data.json | 0 .../assets/seaCreatures.json | 0 {examples => dust_examples}/async_count.ds | 0 {examples => dust_examples}/fizzbuzz.ds | 0 {examples => dust_examples}/guessing_game.ds | 0 {examples => dust_examples}/hello_world.ds | 0 {examples => dust_examples}/json_length.ds | 0 {examples => dust_examples}/type_inference.ds | 0 12 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 dust-lang/examples/interpreter_async.rs rename {examples => dust_examples}/assets/data.json (100%) rename {examples => dust_examples}/assets/jq_data.json (100%) rename {examples => dust_examples}/assets/seaCreatures.json (100%) rename {examples => dust_examples}/async_count.ds (100%) rename {examples => dust_examples}/fizzbuzz.ds (100%) rename {examples => dust_examples}/guessing_game.ds (100%) rename {examples => dust_examples}/hello_world.ds (100%) rename {examples => dust_examples}/json_length.ds (100%) rename {examples => dust_examples}/type_inference.ds (100%) diff --git a/README.md b/README.md index 8e6d8e7..e495ac9 100644 --- a/README.md +++ b/README.md @@ -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.") diff --git a/dust-lang/examples/interpreter_async.rs b/dust-lang/examples/interpreter_async.rs new file mode 100644 index 0000000..7e2432a --- /dev/null +++ b/dust-lang/examples/interpreter_async.rs @@ -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); + } +} diff --git a/dust-lang/src/lib.rs b/dust-lang/src/lib.rs index 4b964fb..cb0f8c7 100644 --- a/dust-lang/src/lib.rs +++ b/dust-lang/src/lib.rs @@ -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, 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, Arc>>>, diff --git a/examples/assets/data.json b/dust_examples/assets/data.json similarity index 100% rename from examples/assets/data.json rename to dust_examples/assets/data.json diff --git a/examples/assets/jq_data.json b/dust_examples/assets/jq_data.json similarity index 100% rename from examples/assets/jq_data.json rename to dust_examples/assets/jq_data.json diff --git a/examples/assets/seaCreatures.json b/dust_examples/assets/seaCreatures.json similarity index 100% rename from examples/assets/seaCreatures.json rename to dust_examples/assets/seaCreatures.json diff --git a/examples/async_count.ds b/dust_examples/async_count.ds similarity index 100% rename from examples/async_count.ds rename to dust_examples/async_count.ds diff --git a/examples/fizzbuzz.ds b/dust_examples/fizzbuzz.ds similarity index 100% rename from examples/fizzbuzz.ds rename to dust_examples/fizzbuzz.ds diff --git a/examples/guessing_game.ds b/dust_examples/guessing_game.ds similarity index 100% rename from examples/guessing_game.ds rename to dust_examples/guessing_game.ds diff --git a/examples/hello_world.ds b/dust_examples/hello_world.ds similarity index 100% rename from examples/hello_world.ds rename to dust_examples/hello_world.ds diff --git a/examples/json_length.ds b/dust_examples/json_length.ds similarity index 100% rename from examples/json_length.ds rename to dust_examples/json_length.ds diff --git a/examples/type_inference.ds b/dust_examples/type_inference.ds similarity index 100% rename from examples/type_inference.ds rename to dust_examples/type_inference.ds