Use async function to read file input; Clean up

This commit is contained in:
Jeff 2023-10-30 18:30:41 -04:00
parent 3bb4d3c7ec
commit 9c565e810e
6 changed files with 315 additions and 47 deletions

293
Cargo.lock generated
View File

@ -83,6 +83,124 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "async-attributes"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3203e79f4dd9bdda415ed03cf14dae5a2bf775c683a00f94e9cd1faf0f596e5"
dependencies = [
"quote",
"syn 1.0.109",
]
[[package]]
name = "async-channel"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81953c529336010edd6d8e358f886d9581267795c61b19475b71314bffa46d35"
dependencies = [
"concurrent-queue",
"event-listener",
"futures-core",
]
[[package]]
name = "async-executor"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b0c4a4f319e45986f347ee47fef8bf5e81c9abc3f6f58dc2391439f30df65f0"
dependencies = [
"async-lock",
"async-task",
"concurrent-queue",
"fastrand 2.0.1",
"futures-lite",
"slab",
]
[[package]]
name = "async-global-executor"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1b6f5d7df27bd294849f8eec66ecfc63d11814df7a4f5d74168a2394467b776"
dependencies = [
"async-channel",
"async-executor",
"async-io",
"async-lock",
"blocking",
"futures-lite",
"once_cell",
]
[[package]]
name = "async-io"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fc5b45d93ef0529756f812ca52e44c221b35341892d3dcc34132ac02f3dd2af"
dependencies = [
"async-lock",
"autocfg",
"cfg-if",
"concurrent-queue",
"futures-lite",
"log",
"parking",
"polling",
"rustix 0.37.27",
"slab",
"socket2 0.4.9",
"waker-fn",
]
[[package]]
name = "async-lock"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b"
dependencies = [
"event-listener",
]
[[package]]
name = "async-std"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d"
dependencies = [
"async-attributes",
"async-channel",
"async-global-executor",
"async-io",
"async-lock",
"crossbeam-utils",
"futures-channel",
"futures-core",
"futures-io",
"futures-lite",
"gloo-timers",
"kv-log-macro",
"log",
"memchr",
"once_cell",
"pin-project-lite",
"pin-utils",
"slab",
"wasm-bindgen-futures",
]
[[package]]
name = "async-task"
version = "4.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4eb2cdb97421e01129ccb49169d8279ed21e829929144f4a22a6e54ac549ca1"
[[package]]
name = "atomic-waker"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
[[package]]
name = "autocfg"
version = "1.1.0"
@ -122,6 +240,22 @@ version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635"
[[package]]
name = "blocking"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c36a4d0d48574b3dd360b4b7d95cc651d2b6557b6402848a27d4b228a473e2a"
dependencies = [
"async-channel",
"async-lock",
"async-task",
"fastrand 2.0.1",
"futures-io",
"futures-lite",
"piper",
"tracing",
]
[[package]]
name = "bumpalo"
version = "3.14.0"
@ -219,6 +353,15 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "concurrent-queue"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f057a694a54f12365049b0958a1685bb52d567f5593b355fbf685838e873d400"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "core-foundation"
version = "0.9.3"
@ -316,9 +459,10 @@ dependencies = [
[[package]]
name = "dust-lang"
version = "0.3.3"
version = "0.3.4"
dependencies = [
"ansi_term",
"async-std",
"cc",
"clap",
"comfy-table",
@ -393,6 +537,21 @@ dependencies = [
"str-buf",
]
[[package]]
name = "event-listener"
version = "2.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
[[package]]
name = "fastrand"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be"
dependencies = [
"instant",
]
[[package]]
name = "fastrand"
version = "2.0.1"
@ -406,7 +565,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef033ed5e9bad94e55838ca0ca906db0e043f517adda0c8b79c7a8c66c93c1b5"
dependencies = [
"cfg-if",
"rustix",
"rustix 0.38.17",
"windows-sys",
]
@ -461,6 +620,21 @@ version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964"
[[package]]
name = "futures-lite"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49a9d51ce47660b1e808d3c990b4709f2f415d928835a17dfd16991515c46bce"
dependencies = [
"fastrand 1.9.0",
"futures-core",
"futures-io",
"memchr",
"parking",
"pin-project-lite",
"waker-fn",
]
[[package]]
name = "futures-sink"
version = "0.3.28"
@ -520,6 +694,18 @@ dependencies = [
"url",
]
[[package]]
name = "gloo-timers"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b995a66bb87bebce9a0f4a95aed01daca4872c050bfcb21653361c03bc35e5c"
dependencies = [
"futures-channel",
"futures-core",
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "h2"
version = "0.3.21"
@ -673,6 +859,26 @@ dependencies = [
"hashbrown 0.14.1",
]
[[package]]
name = "instant"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
dependencies = [
"cfg-if",
]
[[package]]
name = "io-lifetimes"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2"
dependencies = [
"hermit-abi",
"libc",
"windows-sys",
]
[[package]]
name = "ipnet"
version = "2.8.0"
@ -709,6 +915,15 @@ version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd"
[[package]]
name = "kv-log-macro"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f"
dependencies = [
"log",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -761,6 +976,12 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "linux-raw-sys"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519"
[[package]]
name = "linux-raw-sys"
version = "0.4.8"
@ -782,6 +1003,9 @@ name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
dependencies = [
"value-bag",
]
[[package]]
name = "memchr"
@ -932,6 +1156,12 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "parking"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae"
[[package]]
name = "parking_lot"
version = "0.12.1"
@ -973,12 +1203,39 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "piper"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "668d31b1c4eba19242f2088b2bf3316b82ca31082a8335764db4e083db7485d4"
dependencies = [
"atomic-waker",
"fastrand 2.0.1",
"futures-io",
]
[[package]]
name = "pkg-config"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]]
name = "polling"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce"
dependencies = [
"autocfg",
"bitflags 1.3.2",
"cfg-if",
"concurrent-queue",
"libc",
"log",
"pin-project-lite",
"windows-sys",
]
[[package]]
name = "ppv-lite86"
version = "0.2.17"
@ -1145,6 +1402,20 @@ version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "rustix"
version = "0.37.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea8ca367a3a01fe35e6943c400addf443c0f57670e6ec51196f71a4b8762dd2"
dependencies = [
"bitflags 1.3.2",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys 0.3.8",
"windows-sys",
]
[[package]]
name = "rustix"
version = "0.38.17"
@ -1154,7 +1425,7 @@ dependencies = [
"bitflags 2.4.0",
"errno",
"libc",
"linux-raw-sys",
"linux-raw-sys 0.4.8",
"windows-sys",
]
@ -1441,9 +1712,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef"
dependencies = [
"cfg-if",
"fastrand",
"fastrand 2.0.1",
"redox_syscall",
"rustix",
"rustix 0.38.17",
"windows-sys",
]
@ -1628,12 +1899,24 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "value-bag"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a72e1902dde2bd6441347de2b70b7f5d59bf157c6c62f0c44572607a1d55bbe"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "waker-fn"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3c4517f54858c779bbcbf228f4fca63d121bf85fbecb2dc578cdf4a39395690"
[[package]]
name = "want"
version = "0.3.1"

View File

@ -1,7 +1,7 @@
[package]
name = "dust-lang"
description = "Data-Oriented Programming Language"
version = "0.3.4"
version = "0.3.5"
repository = "https://github.com/tree-sitter/tree-sitter-dust"
edition = "2018"
license = "MIT"
@ -12,6 +12,7 @@ path = "src/main.rs"
[dependencies]
ansi_term = "0.12.1"
async-std = { version = "1.12.0", features = ["attributes"] }
clap = { version = "4.4.4", features = ["derive"] }
comfy-table = "7.0.1"
csv = "1.2.2"

View File

@ -1,4 +1,5 @@
//! Command line interface for the dust programming language.
use async_std::{fs::read_to_string, prelude::*};
use clap::Parser;
use rustyline::{
completion::FilenameCompleter,
@ -9,7 +10,7 @@ use rustyline::{
Completer, Context, Editor, Helper, Validator,
};
use std::{borrow::Cow, fs::read_to_string};
use std::borrow::Cow;
use dust_lang::{evaluate_with_context, Map, Value};
@ -33,13 +34,22 @@ struct Args {
path: Option<String>,
}
fn main() {
#[async_std::main]
async fn main() {
let args = Args::parse();
if args.path.is_none() && args.command.is_none() {
return run_cli_shell();
}
let source = if let Some(path) = &args.path {
read_to_string(path).await.unwrap()
} else if let Some(command) = &args.command {
command.clone()
} else {
"".to_string()
};
let mut context = Map::new();
if let Some(input) = args.input {
@ -49,22 +59,14 @@ fn main() {
}
if let Some(path) = args.input_path {
let file_contents = read_to_string(path).unwrap();
let file_contents = read_to_string(path).await.unwrap();
context
.variables_mut()
.insert("input".to_string(), Value::String(file_contents));
}
let eval_result = if let Some(path) = args.path {
let file_contents = read_to_string(path).unwrap();
evaluate_with_context(&file_contents, &mut context)
} else if let Some(command) = args.command {
evaluate_with_context(&command, &mut context)
} else {
Ok(Value::Empty)
};
let eval_result = evaluate_with_context(&source, &mut context);
match eval_result {
Ok(value) => {

View File

@ -28,7 +28,7 @@ impl Map {
pub fn clone_from(other: &Self) -> Self {
let mut new_map = BTreeMap::new();
for (key, value) in other.inner().read().unwrap().iter() {
for (key, value) in other.variables().iter() {
new_map.insert(key.clone(), value.clone());
}
@ -45,18 +45,6 @@ impl Map {
self.variables.write().unwrap()
}
/// Removes an assigned variable.
///
/// TODO: Support dot notation.
pub fn remove(&mut self, key: &str) -> Option<Value> {
self.variables.write().unwrap().remove(key)
}
/// Returns a reference to the inner BTreeMap.
pub fn inner(&self) -> Arc<RwLock<BTreeMap<String, Value>>> {
Arc::clone(&self.variables)
}
/// Returns the number of stored variables.
pub fn len(&self) -> usize {
self.variables.read().unwrap().len()

View File

@ -282,10 +282,9 @@ impl From<&mut List> for Table {
impl From<Map> for Table {
fn from(map: Map) -> Self {
let inner_map = map.inner();
let read_map = inner_map.read().unwrap();
let keys = read_map.keys().cloned().collect();
let values = read_map.values().cloned().collect();
let variables = map.variables();
let keys = variables.keys().cloned().collect();
let values = variables.values().cloned().collect();
let mut table = Table::new(keys);
table
@ -298,10 +297,9 @@ impl From<Map> for Table {
impl From<&Map> for Table {
fn from(map: &Map) -> Self {
let inner_map = map.inner();
let read_map = inner_map.read().unwrap();
let keys = read_map.keys().cloned().collect();
let values = read_map.values().cloned().collect();
let variables = map.variables();
let keys = variables.keys().cloned().collect();
let values = variables.values().cloned().collect();
let mut table = Table::new(keys);
table
@ -314,10 +312,9 @@ impl From<&Map> for Table {
impl From<&mut Map> for Table {
fn from(map: &mut Map) -> Self {
let inner_map = map.inner();
let read_map = inner_map.read().unwrap();
let keys = read_map.keys().cloned().collect();
let values = read_map.values().cloned().collect();
let variables = map.variables();
let keys = variables.keys().cloned().collect();
let values = variables.values().cloned().collect();
let mut table = Table::new(keys);
table

View File

@ -114,10 +114,7 @@ impl From<&Value> for ValueType {
Value::Map(map) => {
let mut value_nodes = BTreeMap::new();
let inner_map = map.inner().clone();
let read_map = inner_map.read().unwrap();
for (key, value) in read_map.clone() {
for (key, value) in map.variables().iter() {
let value_type = value.value_type();
let value_node = ValueNode::new(value_type, 0, 0);
let expression = Expression::Value(value_node);