Implement async statment
This commit is contained in:
parent
8ca97300d3
commit
cedf0a8c65
19
examples/async.ds
Normal file
19
examples/async.ds
Normal file
@ -0,0 +1,19 @@
|
||||
(output "This will print first.")
|
||||
|
||||
create_random_numbers = |count| => {
|
||||
numbers = [];
|
||||
|
||||
while (length numbers) < count {
|
||||
numbers += (random_integer)
|
||||
}
|
||||
|
||||
(output "Made " + count + " numbers.")
|
||||
}
|
||||
|
||||
async {
|
||||
(create_random_numbers 1000)
|
||||
(create_random_numbers 100)
|
||||
(create_random_numbers 10)
|
||||
}
|
||||
|
||||
(output "This will print last.")
|
@ -2,43 +2,25 @@ use rayon::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tree_sitter::Node;
|
||||
|
||||
use crate::{AbstractTree, Block, Error, Map, Result, Value};
|
||||
use crate::{AbstractTree, Block, Map, Result, Value};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub struct Async {
|
||||
statements: Vec<Block>,
|
||||
block: Block,
|
||||
}
|
||||
|
||||
impl AbstractTree for Async {
|
||||
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
|
||||
debug_assert_eq!("async", node.kind());
|
||||
|
||||
let child_count = node.child_count();
|
||||
let mut statements = Vec::with_capacity(child_count);
|
||||
let block_node = node.child(1).unwrap();
|
||||
let block = Block::from_syntax_node(source, block_node)?;
|
||||
|
||||
for index in 2..child_count - 1 {
|
||||
let child = node.child(index).unwrap();
|
||||
|
||||
let statement = match child.kind() {
|
||||
"statement" => Block::from_syntax_node(source, child)?,
|
||||
_ => {
|
||||
return Err(Error::UnexpectedSyntaxNode {
|
||||
expected: "comment or statement",
|
||||
actual: child.kind(),
|
||||
location: child.start_position(),
|
||||
relevant_source: source[child.byte_range()].to_string(),
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
statements.push(statement);
|
||||
}
|
||||
|
||||
Ok(Async { statements })
|
||||
Ok(Async { block })
|
||||
}
|
||||
|
||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||
let statements = &self.statements;
|
||||
let statements = self.block.statements();
|
||||
|
||||
statements
|
||||
.into_par_iter()
|
||||
@ -47,6 +29,7 @@ impl AbstractTree for Async {
|
||||
let mut context = context.clone();
|
||||
let result = statement.run(source, &mut context);
|
||||
|
||||
result.clone().unwrap();
|
||||
if result.is_err() {
|
||||
Some(result)
|
||||
} else if index == statements.len() - 1 {
|
||||
@ -55,6 +38,6 @@ impl AbstractTree for Async {
|
||||
None
|
||||
}
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap_or(Ok(Value::Empty))
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,12 @@ pub struct Block {
|
||||
statements: Vec<Statement>,
|
||||
}
|
||||
|
||||
impl Block {
|
||||
pub fn statements(&self) -> &Vec<Statement> {
|
||||
&self.statements
|
||||
}
|
||||
}
|
||||
|
||||
impl AbstractTree for Block {
|
||||
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
|
||||
debug_assert_eq!("block", node.kind());
|
||||
|
@ -219,19 +219,20 @@ impl Add for Value {
|
||||
type Output = Result<Value>;
|
||||
|
||||
fn add(self, other: Self) -> Self::Output {
|
||||
match (self.as_integer(), other.as_integer()) {
|
||||
(Ok(left), Ok(right)) => return Ok(Value::Integer(left + right)),
|
||||
_ => {}
|
||||
if let (Ok(left), Ok(right)) = (self.as_integer(), other.as_integer()) {
|
||||
return Ok(Value::Integer(left + right));
|
||||
}
|
||||
|
||||
match (self.as_number(), other.as_number()) {
|
||||
(Ok(left), Ok(right)) => return Ok(Value::Float(left + right)),
|
||||
_ => {}
|
||||
if let (Ok(left), Ok(right)) = (self.as_number(), other.as_number()) {
|
||||
return Ok(Value::Float(left + right));
|
||||
}
|
||||
|
||||
match (self.as_string(), other.as_string()) {
|
||||
(Ok(left), Ok(right)) => return Ok(Value::String(left.to_string() + right)),
|
||||
_ => {}
|
||||
if let (Ok(left), Ok(right)) = (self.as_string(), other.as_string()) {
|
||||
return Ok(Value::String(left.to_string() + right));
|
||||
}
|
||||
|
||||
if self.is_string() || other.is_string() {
|
||||
return Ok(Value::String(self.to_string() + &other.to_string()));
|
||||
}
|
||||
|
||||
let non_number_or_string = if !self.is_number() == !self.is_string() {
|
||||
|
Loading…
Reference in New Issue
Block a user