1
0

Begin tree sitter implementation

This commit is contained in:
Jeff 2023-09-21 05:19:06 -04:00
parent f531ddd1de
commit 1f11904065
3 changed files with 38 additions and 24 deletions

23
Cargo.lock generated
View File

@ -1069,6 +1069,7 @@ name = "dust-lang"
version = "0.1.1"
dependencies = [
"ansi_term",
"cc",
"chrono",
"clap",
"comfy-table",
@ -1091,6 +1092,8 @@ dependencies = [
"toml",
"toml_edit",
"trash",
"tree-sitter",
"tree-sitter-rust",
]
[[package]]
@ -4022,6 +4025,26 @@ dependencies = [
"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]]
name = "try-lock"
version = "0.2.4"

View File

@ -20,7 +20,12 @@ name = "gui"
name = "dust_lib"
path = "src/lib.rs"
[build-dependencies]
cc = "*"
[dependencies]
tree-sitter = "0.20.10"
tree-sitter-rust = "0.20.3"
rand = "0.8.5"
chrono = "0.4.26"
trash = "3.0.3"

View File

@ -2,6 +2,8 @@
use crate::{token, tree, Result, Value, VariableMap};
use tree_sitter::{Language, Parser};
/// Evaluate the given expression string.
///
/// # 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)));
/// ```
pub fn eval_with_context(input: &str, context: &mut VariableMap) -> Result<Value> {
let without_comments = input
.lines()
.map(|line| {
let split = line.split_once('#');
let mut parser = Parser::new();
if let Some((code, _comment)) = split {
code
} else {
line
}
})
.collect::<String>();
let split = without_comments.split_once("->");
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)
}
parser
.set_language(tree_sitter_rust::language())
.expect("Error loading Rust grammar");
let tree = parser.parse(input, None).unwrap();
let root_node = tree.root_node().to_sexp();
println!("{root_node:?}");
Ok(Value::Empty)
}