Remove yield
This commit is contained in:
parent
f0fb16607c
commit
e9e4e92f68
@ -2,7 +2,7 @@ use rayon::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
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)]
|
||||
pub struct Block {
|
||||
@ -17,13 +17,17 @@ impl AbstractTree for Block {
|
||||
let first_child = node.child(0).unwrap();
|
||||
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);
|
||||
|
||||
for index in 0..statement_count - 1 {
|
||||
for index in 1..statement_count + 1 {
|
||||
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)?;
|
||||
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 {
|
||||
let statements = &self.statements;
|
||||
|
||||
@ -56,14 +60,13 @@ impl AbstractTree for Block {
|
||||
})
|
||||
.unwrap_or(Ok(Value::Empty))
|
||||
} else {
|
||||
for statement in &self.statements[0..self.statements.len() - 1] {
|
||||
statement.run(source, context)?;
|
||||
let mut prev_result = None;
|
||||
|
||||
for statement in &self.statements {
|
||||
prev_result = Some(statement.run(source, context)?);
|
||||
}
|
||||
|
||||
let final_statement = self.statements.last().unwrap();
|
||||
let final_value = final_statement.run(source, context)?;
|
||||
|
||||
Ok(final_value)
|
||||
Ok(prev_result.unwrap_or(Value::Empty))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
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)]
|
||||
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)?;
|
||||
|
||||
println!("{self:?}");
|
||||
|
||||
match value {
|
||||
Value::List(list) => {
|
||||
let index = self.index.run(source, context)?.as_integer()? as usize;
|
||||
|
@ -8,15 +8,14 @@ async { (output 'Whaddup') }
|
||||
|
||||
(root
|
||||
(statement
|
||||
(async
|
||||
(block
|
||||
(statement
|
||||
(expression
|
||||
(function_call
|
||||
(built_in_function
|
||||
(expression
|
||||
(value
|
||||
(string)))))))))))
|
||||
(block
|
||||
(statement
|
||||
(expression
|
||||
(function_call
|
||||
(built_in_function
|
||||
(expression
|
||||
(value
|
||||
(string))))))))))
|
||||
|
||||
================================================================================
|
||||
Complex Async Statements
|
||||
@ -36,38 +35,37 @@ async {
|
||||
|
||||
(root
|
||||
(statement
|
||||
(async
|
||||
(block
|
||||
(statement
|
||||
(if_else
|
||||
(if
|
||||
(expression
|
||||
(logic
|
||||
(expression
|
||||
(math
|
||||
(expression
|
||||
(value
|
||||
(integer)))
|
||||
(math_operator)
|
||||
(expression
|
||||
(value
|
||||
(integer)))))
|
||||
(logic_operator)
|
||||
(expression
|
||||
(value
|
||||
(integer)))))
|
||||
(block
|
||||
(statement
|
||||
(expression
|
||||
(value
|
||||
(boolean))))))
|
||||
(else
|
||||
(block
|
||||
(statement
|
||||
(expression
|
||||
(value
|
||||
(boolean))))))))
|
||||
(statement
|
||||
(expression
|
||||
(value
|
||||
(string))))))))
|
||||
(block
|
||||
(statement
|
||||
(if_else
|
||||
(if
|
||||
(expression
|
||||
(logic
|
||||
(expression
|
||||
(math
|
||||
(expression
|
||||
(value
|
||||
(integer)))
|
||||
(math_operator)
|
||||
(expression
|
||||
(value
|
||||
(integer)))))
|
||||
(logic_operator)
|
||||
(expression
|
||||
(value
|
||||
(integer)))))
|
||||
(block
|
||||
(statement
|
||||
(expression
|
||||
(value
|
||||
(boolean))))))
|
||||
(else
|
||||
(block
|
||||
(statement
|
||||
(expression
|
||||
(value
|
||||
(boolean))))))))
|
||||
(statement
|
||||
(expression
|
||||
(value
|
||||
(string)))))))
|
||||
|
@ -3,7 +3,7 @@ Simple For Loop
|
||||
================================================================================
|
||||
|
||||
for i in [1, 2, 3] {
|
||||
output i
|
||||
output(i)
|
||||
}
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
@ -38,7 +38,7 @@ Nested For Loop
|
||||
|
||||
for list in list_of_lists {
|
||||
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, ')'),
|
||||
)),
|
||||
|
||||
_expression_kind: $ => prec.left(1, choice(
|
||||
_expression_kind: $ => prec(1, choice(
|
||||
$.function_call,
|
||||
$.identifier,
|
||||
$.index,
|
||||
$.logic,
|
||||
$.math,
|
||||
$.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]?/,
|
||||
|
||||
@ -284,12 +286,6 @@ module.exports = grammar({
|
||||
$.expression,
|
||||
)),
|
||||
|
||||
yield: $ => prec.left(seq(
|
||||
$.expression,
|
||||
'->',
|
||||
$.function_call,
|
||||
)),
|
||||
|
||||
function: $ => seq(
|
||||
field('parameters', optional($.identifier_list)),
|
||||
'=>',
|
||||
|
@ -20,6 +20,18 @@
|
||||
"block": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "async"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "{"
|
||||
@ -50,10 +62,6 @@
|
||||
"type": "SYMBOL",
|
||||
"name": "assignment"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "async"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "block"
|
||||
@ -154,7 +162,7 @@
|
||||
}
|
||||
},
|
||||
"_expression_kind": {
|
||||
"type": "PREC_LEFT",
|
||||
"type": "PREC",
|
||||
"value": 1,
|
||||
"content": {
|
||||
"type": "CHOICE",
|
||||
@ -182,10 +190,6 @@
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "value"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "yield"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1128,19 +1132,6 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"async": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "async"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "block"
|
||||
}
|
||||
]
|
||||
},
|
||||
"identifier_list": {
|
||||
"type": "PREC_RIGHT",
|
||||
"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": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
|
@ -27,21 +27,6 @@
|
||||
"named": true,
|
||||
"fields": {}
|
||||
},
|
||||
{
|
||||
"type": "async",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": false,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "block",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "block",
|
||||
"named": true,
|
||||
@ -142,10 +127,6 @@
|
||||
{
|
||||
"type": "value",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "yield",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -579,10 +560,6 @@
|
||||
"type": "assignment",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "async",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "block",
|
||||
"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": "!=",
|
||||
"named": false
|
||||
@ -805,10 +763,6 @@
|
||||
"type": "-=",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "->",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "..",
|
||||
"named": false
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user