1
0

Fix simple value parsing

This commit is contained in:
Jeff 2023-10-05 08:18:33 -04:00
parent 7fa5dd0f54
commit 9aaa9f1e6d
2 changed files with 29 additions and 10 deletions

View File

@ -99,17 +99,14 @@ impl<'context, 'code> Evaluator<'context, 'code> {
} }
fn run(self) -> Vec<Result<Value>> { fn run(self) -> Vec<Result<Value>> {
let mut cursor_0 = self.tree.walk(); let mut cursor = self.tree.walk();
let mut cursor_1 = self.tree.walk(); let root_node = cursor.node();
let node = cursor_0.node(); let item_count = root_node.child_count();
let item_count = node.child_count();
let mut results = Vec::with_capacity(item_count); let mut results = Vec::with_capacity(item_count);
println!("{}", node.to_sexp()); println!("{}", root_node.to_sexp());
assert_eq!(cursor_0.node().kind(), "root"); for item_node in root_node.children(&mut cursor) {
for item_node in node.children(&mut cursor_0) {
let item_result = Item::from_syntax_node(item_node, self.source); let item_result = Item::from_syntax_node(item_node, self.source);
match item_result { match item_result {

View File

@ -54,9 +54,31 @@ impl Value {
let child = node.child(0).unwrap(); let child = node.child(0).unwrap();
match child.kind() { match child.kind() {
"integer" | "float" | "boolean" | "string" | "empty" => { "integer" => {
Ok(Value::from_syntax_node(child, source)?) let bytes = &source[child.byte_range()];
let raw_value = bytes.parse::<i64>().unwrap();
Ok(Value::Integer(raw_value))
} }
"float" => {
let bytes = &source[child.byte_range()];
let raw_value = bytes.parse::<f64>().unwrap();
Ok(Value::Float(raw_value))
}
"string" => {
let byte_range_without_quotes = child.start_byte() - 1..child.end_byte();
let text = &source[byte_range_without_quotes];
Ok(Value::String(text.to_string()))
}
"boolean" => {
let bytes = &source[child.byte_range()];
let raw_value = bytes.parse::<bool>().unwrap();
Ok(Value::Boolean(raw_value))
}
"empty" => Ok(Value::Empty),
"list" => { "list" => {
let item_count = child.named_child_count(); let item_count = child.named_child_count();
let mut values = Vec::with_capacity(item_count); let mut values = Vec::with_capacity(item_count);