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 serde::{Deserialize, Serialize};
|
||||||
use tree_sitter::Node;
|
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)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
||||||
pub struct Async {
|
pub struct Async {
|
||||||
statements: Vec<Block>,
|
block: Block,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AbstractTree for Async {
|
impl AbstractTree for Async {
|
||||||
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
|
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
|
||||||
debug_assert_eq!("async", node.kind());
|
debug_assert_eq!("async", node.kind());
|
||||||
|
|
||||||
let child_count = node.child_count();
|
let block_node = node.child(1).unwrap();
|
||||||
let mut statements = Vec::with_capacity(child_count);
|
let block = Block::from_syntax_node(source, block_node)?;
|
||||||
|
|
||||||
for index in 2..child_count - 1 {
|
Ok(Async { block })
|
||||||
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 })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||||
let statements = &self.statements;
|
let statements = self.block.statements();
|
||||||
|
|
||||||
statements
|
statements
|
||||||
.into_par_iter()
|
.into_par_iter()
|
||||||
@ -47,6 +29,7 @@ impl AbstractTree for Async {
|
|||||||
let mut context = context.clone();
|
let mut context = context.clone();
|
||||||
let result = statement.run(source, &mut context);
|
let result = statement.run(source, &mut context);
|
||||||
|
|
||||||
|
result.clone().unwrap();
|
||||||
if result.is_err() {
|
if result.is_err() {
|
||||||
Some(result)
|
Some(result)
|
||||||
} else if index == statements.len() - 1 {
|
} else if index == statements.len() - 1 {
|
||||||
@ -55,6 +38,6 @@ impl AbstractTree for Async {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.unwrap()
|
.unwrap_or(Ok(Value::Empty))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,12 @@ pub struct Block {
|
|||||||
statements: Vec<Statement>,
|
statements: Vec<Statement>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Block {
|
||||||
|
pub fn statements(&self) -> &Vec<Statement> {
|
||||||
|
&self.statements
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl AbstractTree for Block {
|
impl AbstractTree for Block {
|
||||||
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
|
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
|
||||||
debug_assert_eq!("block", node.kind());
|
debug_assert_eq!("block", node.kind());
|
||||||
|
@ -219,19 +219,20 @@ impl Add for Value {
|
|||||||
type Output = Result<Value>;
|
type Output = Result<Value>;
|
||||||
|
|
||||||
fn add(self, other: Self) -> Self::Output {
|
fn add(self, other: Self) -> Self::Output {
|
||||||
match (self.as_integer(), other.as_integer()) {
|
if let (Ok(left), Ok(right)) = (self.as_integer(), other.as_integer()) {
|
||||||
(Ok(left), Ok(right)) => return Ok(Value::Integer(left + right)),
|
return Ok(Value::Integer(left + right));
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
match (self.as_number(), other.as_number()) {
|
if let (Ok(left), Ok(right)) = (self.as_number(), other.as_number()) {
|
||||||
(Ok(left), Ok(right)) => return Ok(Value::Float(left + right)),
|
return Ok(Value::Float(left + right));
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
match (self.as_string(), other.as_string()) {
|
if let (Ok(left), Ok(right)) = (self.as_string(), other.as_string()) {
|
||||||
(Ok(left), Ok(right)) => return Ok(Value::String(left.to_string() + right)),
|
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() {
|
let non_number_or_string = if !self.is_number() == !self.is_string() {
|
||||||
|
Loading…
Reference in New Issue
Block a user