Remove yield
This commit is contained in:
parent
f0fb16607c
commit
e9e4e92f68
@ -2,7 +2,7 @@ use rayon::prelude::*;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tree_sitter::Node;
|
use tree_sitter::Node;
|
||||||
|
|
||||||
use crate::{AbstractTree, Result, Statement, Value};
|
use crate::{AbstractTree, Map, Result, Statement, Value};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
||||||
pub struct Block {
|
pub struct Block {
|
||||||
@ -17,13 +17,17 @@ impl AbstractTree for Block {
|
|||||||
let first_child = node.child(0).unwrap();
|
let first_child = node.child(0).unwrap();
|
||||||
let is_async = first_child.kind() == "async";
|
let is_async = first_child.kind() == "async";
|
||||||
|
|
||||||
let statement_count = node.child_count();
|
let statement_count = if is_async {
|
||||||
|
node.child_count() - 3
|
||||||
|
} else {
|
||||||
|
node.child_count() - 2
|
||||||
|
};
|
||||||
let mut statements = Vec::with_capacity(statement_count);
|
let mut statements = Vec::with_capacity(statement_count);
|
||||||
|
|
||||||
for index in 0..statement_count - 1 {
|
for index in 1..statement_count + 1 {
|
||||||
let child_node = node.child(index).unwrap();
|
let child_node = node.child(index).unwrap();
|
||||||
|
|
||||||
if child_node.kind() == "statement" {
|
if child_node.is_named() {
|
||||||
let statement = Statement::from_syntax_node(source, child_node)?;
|
let statement = Statement::from_syntax_node(source, child_node)?;
|
||||||
statements.push(statement);
|
statements.push(statement);
|
||||||
}
|
}
|
||||||
@ -35,7 +39,7 @@ impl AbstractTree for Block {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, source: &str, context: &mut crate::Map) -> crate::Result<crate::Value> {
|
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||||
if self.is_async {
|
if self.is_async {
|
||||||
let statements = &self.statements;
|
let statements = &self.statements;
|
||||||
|
|
||||||
@ -56,14 +60,13 @@ impl AbstractTree for Block {
|
|||||||
})
|
})
|
||||||
.unwrap_or(Ok(Value::Empty))
|
.unwrap_or(Ok(Value::Empty))
|
||||||
} else {
|
} else {
|
||||||
for statement in &self.statements[0..self.statements.len() - 1] {
|
let mut prev_result = None;
|
||||||
statement.run(source, context)?;
|
|
||||||
|
for statement in &self.statements {
|
||||||
|
prev_result = Some(statement.run(source, context)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
let final_statement = self.statements.last().unwrap();
|
Ok(prev_result.unwrap_or(Value::Empty))
|
||||||
let final_value = final_statement.run(source, context)?;
|
|
||||||
|
|
||||||
Ok(final_value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tree_sitter::Node;
|
use tree_sitter::Node;
|
||||||
|
|
||||||
use crate::{AbstractTree, Error, Expression, List, Result, Value};
|
use crate::{AbstractTree, Error, Expression, List, Map, Result, Value};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
||||||
pub struct Index {
|
pub struct Index {
|
||||||
@ -32,9 +32,11 @@ impl AbstractTree for Index {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, source: &str, context: &mut crate::Map) -> crate::Result<crate::Value> {
|
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
||||||
let value = self.collection.run(source, context)?;
|
let value = self.collection.run(source, context)?;
|
||||||
|
|
||||||
|
println!("{self:?}");
|
||||||
|
|
||||||
match value {
|
match value {
|
||||||
Value::List(list) => {
|
Value::List(list) => {
|
||||||
let index = self.index.run(source, context)?.as_integer()? as usize;
|
let index = self.index.run(source, context)?.as_integer()? as usize;
|
||||||
|
@ -8,7 +8,6 @@ async { (output 'Whaddup') }
|
|||||||
|
|
||||||
(root
|
(root
|
||||||
(statement
|
(statement
|
||||||
(async
|
|
||||||
(block
|
(block
|
||||||
(statement
|
(statement
|
||||||
(expression
|
(expression
|
||||||
@ -16,7 +15,7 @@ async { (output 'Whaddup') }
|
|||||||
(built_in_function
|
(built_in_function
|
||||||
(expression
|
(expression
|
||||||
(value
|
(value
|
||||||
(string)))))))))))
|
(string))))))))))
|
||||||
|
|
||||||
================================================================================
|
================================================================================
|
||||||
Complex Async Statements
|
Complex Async Statements
|
||||||
@ -36,7 +35,6 @@ async {
|
|||||||
|
|
||||||
(root
|
(root
|
||||||
(statement
|
(statement
|
||||||
(async
|
|
||||||
(block
|
(block
|
||||||
(statement
|
(statement
|
||||||
(if_else
|
(if_else
|
||||||
@ -70,4 +68,4 @@ async {
|
|||||||
(statement
|
(statement
|
||||||
(expression
|
(expression
|
||||||
(value
|
(value
|
||||||
(string))))))))
|
(string)))))))
|
||||||
|
@ -3,7 +3,7 @@ Simple For Loop
|
|||||||
================================================================================
|
================================================================================
|
||||||
|
|
||||||
for i in [1, 2, 3] {
|
for i in [1, 2, 3] {
|
||||||
output i
|
output(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
@ -38,7 +38,7 @@ Nested For Loop
|
|||||||
|
|
||||||
for list in list_of_lists {
|
for list in list_of_lists {
|
||||||
for item in list {
|
for item in list {
|
||||||
(output item)
|
output(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,60 +0,0 @@
|
|||||||
================================================================================
|
|
||||||
Simple Yield
|
|
||||||
================================================================================
|
|
||||||
|
|
||||||
1 -> output
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
(root
|
|
||||||
(statement
|
|
||||||
(expression
|
|
||||||
(yield
|
|
||||||
(expression
|
|
||||||
(value
|
|
||||||
(integer)))
|
|
||||||
(function_call
|
|
||||||
(built_in_function))))))
|
|
||||||
|
|
||||||
================================================================================
|
|
||||||
Long Yield
|
|
||||||
================================================================================
|
|
||||||
|
|
||||||
[1, 2, 3]
|
|
||||||
-> add_one
|
|
||||||
-> subtract_one
|
|
||||||
-> foobar
|
|
||||||
-> output
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
(root
|
|
||||||
(statement
|
|
||||||
(expression
|
|
||||||
(yield
|
|
||||||
(expression
|
|
||||||
(yield
|
|
||||||
(expression
|
|
||||||
(yield
|
|
||||||
(expression
|
|
||||||
(yield
|
|
||||||
(expression
|
|
||||||
(value
|
|
||||||
(list
|
|
||||||
(expression
|
|
||||||
(value
|
|
||||||
(integer)))
|
|
||||||
(expression
|
|
||||||
(value
|
|
||||||
(integer)))
|
|
||||||
(expression
|
|
||||||
(value
|
|
||||||
(integer))))))
|
|
||||||
(function_call
|
|
||||||
(identifier))))
|
|
||||||
(function_call
|
|
||||||
(identifier))))
|
|
||||||
(function_call
|
|
||||||
(identifier))))
|
|
||||||
(function_call
|
|
||||||
(built_in_function))))))
|
|
@ -46,17 +46,19 @@ module.exports = grammar({
|
|||||||
seq('(', $._expression_kind, ')'),
|
seq('(', $._expression_kind, ')'),
|
||||||
)),
|
)),
|
||||||
|
|
||||||
_expression_kind: $ => prec.left(1, choice(
|
_expression_kind: $ => prec(1, choice(
|
||||||
$.function_call,
|
$.function_call,
|
||||||
$.identifier,
|
$.identifier,
|
||||||
$.index,
|
$.index,
|
||||||
$.logic,
|
$.logic,
|
||||||
$.math,
|
$.math,
|
||||||
$.value,
|
$.value,
|
||||||
$.yield,
|
|
||||||
)),
|
)),
|
||||||
|
|
||||||
_expression_list: $ => repeat1(prec.right(seq($.expression, optional(',')))),
|
_expression_list: $ => repeat1(prec.right(seq(
|
||||||
|
$.expression,
|
||||||
|
optional(','),
|
||||||
|
))),
|
||||||
|
|
||||||
identifier: $ => /[_a-zA-Z]+[_a-zA-Z0-9]?/,
|
identifier: $ => /[_a-zA-Z]+[_a-zA-Z0-9]?/,
|
||||||
|
|
||||||
@ -284,12 +286,6 @@ module.exports = grammar({
|
|||||||
$.expression,
|
$.expression,
|
||||||
)),
|
)),
|
||||||
|
|
||||||
yield: $ => prec.left(seq(
|
|
||||||
$.expression,
|
|
||||||
'->',
|
|
||||||
$.function_call,
|
|
||||||
)),
|
|
||||||
|
|
||||||
function: $ => seq(
|
function: $ => seq(
|
||||||
field('parameters', optional($.identifier_list)),
|
field('parameters', optional($.identifier_list)),
|
||||||
'=>',
|
'=>',
|
||||||
|
@ -20,6 +20,18 @@
|
|||||||
"block": {
|
"block": {
|
||||||
"type": "SEQ",
|
"type": "SEQ",
|
||||||
"members": [
|
"members": [
|
||||||
|
{
|
||||||
|
"type": "CHOICE",
|
||||||
|
"members": [
|
||||||
|
{
|
||||||
|
"type": "STRING",
|
||||||
|
"value": "async"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "BLANK"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "STRING",
|
"type": "STRING",
|
||||||
"value": "{"
|
"value": "{"
|
||||||
@ -50,10 +62,6 @@
|
|||||||
"type": "SYMBOL",
|
"type": "SYMBOL",
|
||||||
"name": "assignment"
|
"name": "assignment"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"type": "SYMBOL",
|
|
||||||
"name": "async"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "SYMBOL",
|
"type": "SYMBOL",
|
||||||
"name": "block"
|
"name": "block"
|
||||||
@ -154,7 +162,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"_expression_kind": {
|
"_expression_kind": {
|
||||||
"type": "PREC_LEFT",
|
"type": "PREC",
|
||||||
"value": 1,
|
"value": 1,
|
||||||
"content": {
|
"content": {
|
||||||
"type": "CHOICE",
|
"type": "CHOICE",
|
||||||
@ -182,10 +190,6 @@
|
|||||||
{
|
{
|
||||||
"type": "SYMBOL",
|
"type": "SYMBOL",
|
||||||
"name": "value"
|
"name": "value"
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "SYMBOL",
|
|
||||||
"name": "yield"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -1128,19 +1132,6 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"async": {
|
|
||||||
"type": "SEQ",
|
|
||||||
"members": [
|
|
||||||
{
|
|
||||||
"type": "STRING",
|
|
||||||
"value": "async"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "SYMBOL",
|
|
||||||
"name": "block"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"identifier_list": {
|
"identifier_list": {
|
||||||
"type": "PREC_RIGHT",
|
"type": "PREC_RIGHT",
|
||||||
"value": 0,
|
"value": 0,
|
||||||
@ -1208,27 +1199,6 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"yield": {
|
|
||||||
"type": "PREC_LEFT",
|
|
||||||
"value": 0,
|
|
||||||
"content": {
|
|
||||||
"type": "SEQ",
|
|
||||||
"members": [
|
|
||||||
{
|
|
||||||
"type": "SYMBOL",
|
|
||||||
"name": "expression"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "STRING",
|
|
||||||
"value": "->"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "SYMBOL",
|
|
||||||
"name": "function_call"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"function": {
|
"function": {
|
||||||
"type": "SEQ",
|
"type": "SEQ",
|
||||||
"members": [
|
"members": [
|
||||||
|
@ -27,21 +27,6 @@
|
|||||||
"named": true,
|
"named": true,
|
||||||
"fields": {}
|
"fields": {}
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"type": "async",
|
|
||||||
"named": true,
|
|
||||||
"fields": {},
|
|
||||||
"children": {
|
|
||||||
"multiple": false,
|
|
||||||
"required": true,
|
|
||||||
"types": [
|
|
||||||
{
|
|
||||||
"type": "block",
|
|
||||||
"named": true
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "block",
|
"type": "block",
|
||||||
"named": true,
|
"named": true,
|
||||||
@ -142,10 +127,6 @@
|
|||||||
{
|
{
|
||||||
"type": "value",
|
"type": "value",
|
||||||
"named": true
|
"named": true
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "yield",
|
|
||||||
"named": true
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -579,10 +560,6 @@
|
|||||||
"type": "assignment",
|
"type": "assignment",
|
||||||
"named": true
|
"named": true
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"type": "async",
|
|
||||||
"named": true
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "block",
|
"type": "block",
|
||||||
"named": true
|
"named": true
|
||||||
@ -742,25 +719,6 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"type": "yield",
|
|
||||||
"named": true,
|
|
||||||
"fields": {},
|
|
||||||
"children": {
|
|
||||||
"multiple": true,
|
|
||||||
"required": true,
|
|
||||||
"types": [
|
|
||||||
{
|
|
||||||
"type": "expression",
|
|
||||||
"named": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "function_call",
|
|
||||||
"named": true
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "!=",
|
"type": "!=",
|
||||||
"named": false
|
"named": false
|
||||||
@ -805,10 +763,6 @@
|
|||||||
"type": "-=",
|
"type": "-=",
|
||||||
"named": false
|
"named": false
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"type": "->",
|
|
||||||
"named": false
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "..",
|
"type": "..",
|
||||||
"named": false
|
"named": false
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user