Begin tree sitter implementation
This commit is contained in:
parent
f531ddd1de
commit
1f11904065
23
Cargo.lock
generated
23
Cargo.lock
generated
@ -1069,6 +1069,7 @@ name = "dust-lang"
|
|||||||
version = "0.1.1"
|
version = "0.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ansi_term",
|
"ansi_term",
|
||||||
|
"cc",
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
"comfy-table",
|
"comfy-table",
|
||||||
@ -1091,6 +1092,8 @@ dependencies = [
|
|||||||
"toml",
|
"toml",
|
||||||
"toml_edit",
|
"toml_edit",
|
||||||
"trash",
|
"trash",
|
||||||
|
"tree-sitter",
|
||||||
|
"tree-sitter-rust",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -4022,6 +4025,26 @@ dependencies = [
|
|||||||
"windows 0.44.0",
|
"windows 0.44.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tree-sitter"
|
||||||
|
version = "0.20.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e747b1f9b7b931ed39a548c1fae149101497de3c1fc8d9e18c62c1a66c683d3d"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"regex",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tree-sitter-rust"
|
||||||
|
version = "0.20.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b0832309b0b2b6d33760ce5c0e818cb47e1d72b468516bfe4134408926fa7594"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"tree-sitter",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "try-lock"
|
name = "try-lock"
|
||||||
version = "0.2.4"
|
version = "0.2.4"
|
||||||
|
@ -20,7 +20,12 @@ name = "gui"
|
|||||||
name = "dust_lib"
|
name = "dust_lib"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
cc = "*"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
tree-sitter = "0.20.10"
|
||||||
|
tree-sitter-rust = "0.20.3"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
chrono = "0.4.26"
|
chrono = "0.4.26"
|
||||||
trash = "3.0.3"
|
trash = "3.0.3"
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
use crate::{token, tree, Result, Value, VariableMap};
|
use crate::{token, tree, Result, Value, VariableMap};
|
||||||
|
|
||||||
|
use tree_sitter::{Language, Parser};
|
||||||
|
|
||||||
/// Evaluate the given expression string.
|
/// Evaluate the given expression string.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
@ -30,32 +32,16 @@ pub fn eval(string: &str) -> Result<Value> {
|
|||||||
/// assert_eq!(eval_with_context("one + two + three", &mut context), Ok(Value::from(6)));
|
/// assert_eq!(eval_with_context("one + two + three", &mut context), Ok(Value::from(6)));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn eval_with_context(input: &str, context: &mut VariableMap) -> Result<Value> {
|
pub fn eval_with_context(input: &str, context: &mut VariableMap) -> Result<Value> {
|
||||||
let without_comments = input
|
let mut parser = Parser::new();
|
||||||
.lines()
|
|
||||||
.map(|line| {
|
|
||||||
let split = line.split_once('#');
|
|
||||||
|
|
||||||
if let Some((code, _comment)) = split {
|
parser
|
||||||
code
|
.set_language(tree_sitter_rust::language())
|
||||||
} else {
|
.expect("Error loading Rust grammar");
|
||||||
line
|
|
||||||
}
|
let tree = parser.parse(input, None).unwrap();
|
||||||
})
|
let root_node = tree.root_node().to_sexp();
|
||||||
.collect::<String>();
|
|
||||||
|
println!("{root_node:?}");
|
||||||
let split = without_comments.split_once("->");
|
|
||||||
|
Ok(Value::Empty)
|
||||||
if let Some((left, right)) = split {
|
|
||||||
let left_result = tree::tokens_to_operator_tree(token::tokenize(left)?)?
|
|
||||||
.eval_with_context_mut(context)?;
|
|
||||||
|
|
||||||
context.set_value("input", left_result)?;
|
|
||||||
|
|
||||||
let right_result = eval_with_context(right, context)?;
|
|
||||||
|
|
||||||
Ok(right_result)
|
|
||||||
} else {
|
|
||||||
tree::tokens_to_operator_tree(token::tokenize(&without_comments)?)?
|
|
||||||
.eval_with_context_mut(context)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user