1
0

Simplify grammar structure

This commit is contained in:
Jeff 2023-11-10 20:44:03 -05:00
parent 71c169a1cf
commit f0fb16607c
31 changed files with 29020 additions and 85617 deletions

View File

@ -1,42 +0,0 @@
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Block, Map, Result, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Async {
block: Block,
}
impl AbstractTree for Async {
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
debug_assert_eq!("async", node.kind());
let block_node = node.child(1).unwrap();
let block = Block::from_syntax_node(source, block_node)?;
Ok(Async { block })
}
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let statements = self.block.statements();
statements
.into_par_iter()
.enumerate()
.find_map_first(|(index, statement)| {
let mut context = context.clone();
let result = statement.run(source, &mut context);
if result.is_err() {
Some(result)
} else if index == statements.len() - 1 {
Some(result)
} else {
None
}
})
.unwrap_or(Ok(Value::Empty))
}
}

View File

@ -1,27 +1,26 @@
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Result, Statement};
use crate::{AbstractTree, Result, Statement, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Block {
pub statements: Vec<Statement>,
}
impl Block {
pub fn statements(&self) -> &Vec<Statement> {
&self.statements
}
is_async: bool,
statements: Vec<Statement>,
}
impl AbstractTree for Block {
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
debug_assert_eq!("block", node.kind());
let first_child = node.child(0).unwrap();
let is_async = first_child.kind() == "async";
let statement_count = node.child_count();
let mut statements = Vec::with_capacity(statement_count);
for index in 0..statement_count {
for index in 0..statement_count - 1 {
let child_node = node.child(index).unwrap();
if child_node.kind() == "statement" {
@ -30,17 +29,41 @@ impl AbstractTree for Block {
}
}
Ok(Block { statements })
Ok(Block {
is_async,
statements,
})
}
fn run(&self, source: &str, context: &mut crate::Map) -> crate::Result<crate::Value> {
for statement in &self.statements[0..self.statements.len() - 1] {
statement.run(source, context)?;
if self.is_async {
let statements = &self.statements;
statements
.into_par_iter()
.enumerate()
.find_map_first(|(index, statement)| {
let mut context = context.clone();
let result = statement.run(source, &mut context);
if result.is_err() {
Some(result)
} else if index == statements.len() - 1 {
Some(result)
} else {
None
}
})
.unwrap_or(Ok(Value::Empty))
} else {
for statement in &self.statements[0..self.statements.len() - 1] {
statement.run(source, context)?;
}
let final_statement = self.statements.last().unwrap();
let final_value = final_statement.run(source, context)?;
Ok(final_value)
}
let final_statement = self.statements.last().unwrap();
let final_value = final_statement.run(source, context)?;
Ok(final_value)
}
}

View File

@ -50,13 +50,13 @@ impl AbstractTree for Index {
Ok(item)
}
Value::Map(map) => {
Value::Map(mut map) => {
let value = if let Expression::Identifier(identifier) = &self.index {
let key = identifier.inner();
map.variables()?.get(key).cloned().unwrap_or(Value::Empty)
} else {
let value = self.index.run(source, context)?;
let value = self.index.run(source, &mut map)?;
let key = value.as_string()?;
map.variables()?.get(key).cloned().unwrap_or(Value::Empty)
@ -96,7 +96,7 @@ mod tests {
#[test]
fn evaluate_complex_index() {
let test = evaluate("{x = [1 2 3]; y = || => {0}; x:((y));}").unwrap();
let test = evaluate("x = [1 2 3]; y = || => {0}; x:((y));").unwrap();
assert_eq!(Value::Integer(1), test);
}

View File

@ -7,7 +7,6 @@
//! examples.
pub mod assignment;
pub mod r#async;
pub mod block;
pub mod built_in_function;
pub mod expression;
@ -31,9 +30,8 @@ pub mod r#while;
pub use {
assignment::*, block::*, built_in_function::*, expression::*, filter::*, find::*,
function_call::*, identifier::*, if_else::*, index::*, insert::*, logic::*, math::*,
r#async::*, r#for::*, r#match::*, r#while::*, remove::*, select::*, statement::*, transform::*,
value_node::*,
function_call::*, identifier::*, if_else::*, index::*, insert::*, logic::*, math::*, r#for::*,
r#match::*, r#while::*, remove::*, select::*, statement::*, transform::*, value_node::*,
};
use tree_sitter::Node;

View File

@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{
AbstractTree, Assignment, Async, Error, Expression, Filter, Find, For, IfElse, Insert, Map,
AbstractTree, Assignment, Block, Error, Expression, Filter, Find, For, IfElse, Insert, Map,
Match, Remove, Result, Select, Transform, Value, While,
};
@ -17,7 +17,7 @@ pub enum Statement {
IfElse(Box<IfElse>),
Match(Match),
While(Box<While>),
Async(Box<Async>),
Block(Box<Block>),
For(Box<For>),
Transform(Box<Transform>),
Filter(Box<Filter>),
@ -49,7 +49,7 @@ impl AbstractTree for Statement {
"while" => Ok(Statement::While(Box::new(While::from_syntax_node(
source, child,
)?))),
"async" => Ok(Statement::Async(Box::new(Async::from_syntax_node(
"block" => Ok(Statement::Block(Box::new(Block::from_syntax_node(
source, child,
)?))),
"for" => Ok(Statement::For(Box::new(For::from_syntax_node(
@ -89,7 +89,7 @@ impl AbstractTree for Statement {
Statement::IfElse(if_else) => if_else.run(source, context),
Statement::Match(r#match) => r#match.run(source, context),
Statement::While(r#while) => r#while.run(source, context),
Statement::Async(run) => run.run(source, context),
Statement::Block(block) => block.run(source, context),
Statement::For(r#for) => r#for.run(source, context),
Statement::Transform(transform) => transform.run(source, context),
Statement::Filter(filter) => filter.run(source, context),

View File

@ -6,7 +6,7 @@ use std::fmt::{self, Debug, Formatter};
use tree_sitter::{Parser, Tree as TSTree};
use crate::{language, AbstractTree, Block, Map, Result, Value};
use crate::{language, AbstractTree, Map, Result, Statement, Value};
/// Evaluate the given source code.
///
@ -89,9 +89,9 @@ impl<'context, 'code> Evaluator<'context, 'code> {
let root_node = cursor.node();
let mut prev_result = Ok(Value::Empty);
for block_node in root_node.children(&mut cursor) {
let block = Block::from_syntax_node(self.source, block_node)?;
prev_result = block.run(self.source, self.context);
for statement_node in root_node.children(&mut cursor) {
let statement = Statement::from_syntax_node(self.source, statement_node)?;
prev_result = statement.run(self.source, self.context);
}
prev_result
@ -227,7 +227,7 @@ mod tests {
}
#[test]
fn evaluate_if_else_else_if_else_if_else_if_else() {
fn evaluate_if_else_if_else_if_else_if_else() {
assert_eq!(
evaluate(
"
@ -253,7 +253,7 @@ mod tests {
assert_eq!(
evaluate(
"
foobar = |message| => message
foobar = |message| => { message }
(foobar 'Hiya')
",
),

View File

@ -1,27 +1,26 @@
==================
================================================================================
Simple Async Statements
==================
================================================================================
async { (output 'Whaddup') }
---
--------------------------------------------------------------------------------
(root
(block
(statement
(async
(block
(statement
(expression
(function_call
(built_in_function
(expression
(value
(string))))))))))))
(statement
(async
(block
(statement
(expression
(function_call
(built_in_function
(expression
(value
(string)))))))))))
==================
================================================================================
Complex Async Statements
==================
================================================================================
async {
if 1 % 2 == 0 {
@ -33,43 +32,42 @@ async {
'foobar'
}
---
--------------------------------------------------------------------------------
(root
(block
(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)))))))))
(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))))))))

View File

@ -1,49 +1,47 @@
==================
================================================================================
Simple Function Call
==================
================================================================================
(output 'hi')
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(function_call
(built_in_function
(expression
(value
(string)))))))))
(statement
(expression
(function_call
(built_in_function
(expression
(value
(string))))))))
==================
================================================================================
Nested Function Call
==================
================================================================================
(assert_equal (random_integer) 4)
assert_equal random_integer 4
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(function_call
(built_in_function
(expression
(function_call
(built_in_function)))
(expression
(value
(integer)))))))
(statement
(expression
(function_call
(built_in_function
(expression
(function_call
(built_in_function
(expression
(value
(integer))))))))))))
(statement
(expression
(function_call
(built_in_function
(expression
(function_call
(built_in_function)))
(expression
(value
(integer)))))))
(statement
(expression
(function_call
(built_in_function
(expression
(function_call
(built_in_function
(expression
(value
(integer)))))))))))

View File

@ -8,10 +8,9 @@ not_a_comment
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(identifier)))))
(statement
(expression
(identifier))))
================================================================================
Partial Line Comments
@ -22,10 +21,9 @@ not_a_comment # comment
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(identifier)))))
(statement
(expression
(identifier))))
================================================================================
Multiline Comments
@ -38,11 +36,10 @@ not_a_comment #
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(identifier)))
(statement
(expression
(value
(string))))))
(statement
(expression
(identifier)))
(statement
(expression
(value
(string)))))

View File

@ -1,44 +1,43 @@
==================
================================================================================
Simple Filter Loop
==================
================================================================================
filter i in [1, 2, 3] {
i <= 1
}
---
--------------------------------------------------------------------------------
(root
(block
(statement
(filter
(identifier)
(expression
(value
(list
(expression
(value
(integer)))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(block
(statement
(statement
(filter
(identifier)
(expression
(value
(list
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(integer)))))))))))
(value
(integer)))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(block
(statement
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(integer))))))))))
==================
================================================================================
Nested Filter Loop
==================
================================================================================
filter i in big_list {
filter j in i {
@ -46,28 +45,27 @@ filter i in big_list {
}
}
---
--------------------------------------------------------------------------------
(root
(block
(statement
(filter
(identifier)
(expression
(identifier))
(block
(statement
(filter
(identifier)
(expression
(identifier))
(block
(statement
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(integer))))))))))))))
(statement
(filter
(identifier)
(expression
(identifier))
(block
(statement
(filter
(identifier)
(expression
(identifier))
(block
(statement
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(integer)))))))))))))

View File

@ -9,32 +9,31 @@ find i in [1, 2, 3] {
--------------------------------------------------------------------------------
(root
(block
(statement
(find
(identifier)
(expression
(value
(list
(expression
(value
(integer)))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(block
(statement
(statement
(find
(identifier)
(expression
(value
(list
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(integer)))))))))))
(value
(integer)))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(block
(statement
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(integer))))))))))
================================================================================
Nested Find Loop
@ -56,64 +55,63 @@ find i in ["one", "two", "three"] {
--------------------------------------------------------------------------------
(root
(block
(statement
(find
(identifier)
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(string)))
(expression
(value
(string))))))
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(find
(identifier)
(expression
(identifier))
(block
(statement
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(string)))))))))))
(statement
(if_else
(if
(statement
(find
(identifier)
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(string)))
(expression
(value
(string))))))
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(find
(identifier)
(expression
(logic
(expression
(function_call
(built_in_function
(expression
(identifier)))))
(logic_operator)
(expression
(value
(string)))))
(identifier))
(block
(statement
(expression
(value
(boolean))))))
(else
(block
(statement
(expression
(value
(boolean)))))))))))))
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(string)))))))))))
(statement
(if_else
(if
(expression
(logic
(expression
(function_call
(built_in_function
(expression
(identifier)))))
(logic_operator)
(expression
(value
(string)))))
(block
(statement
(expression
(value
(boolean))))))
(else
(block
(statement
(expression
(value
(boolean))))))))))))

View File

@ -2,60 +2,64 @@
Simple For Loop
================================================================================
for i in [1, 2, 3] output i
for i in [1, 2, 3] {
output i
}
--------------------------------------------------------------------------------
(root
(block
(statement
(for
(identifier)
(expression
(value
(list
(expression
(value
(integer)))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(block
(statement
(statement
(for
(identifier)
(expression
(value
(list
(expression
(function_call
(built_in_function
(expression
(identifier)))))))))))
(value
(integer)))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(block
(statement
(expression
(function_call
(built_in_function
(expression
(identifier))))))))))
================================================================================
Nested For Loop
================================================================================
for list in list_of_lists for item in list output item
for list in list_of_lists {
for item in list {
(output item)
}
}
--------------------------------------------------------------------------------
(root
(block
(statement
(for
(identifier)
(expression
(identifier))
(block
(statement
(for
(identifier)
(expression
(identifier))
(block
(statement
(expression
(function_call
(built_in_function
(expression
(identifier))))))))))))))
(statement
(for
(identifier)
(expression
(identifier))
(block
(statement
(for
(identifier)
(expression
(identifier))
(block
(statement
(expression
(function_call
(built_in_function
(expression
(identifier)))))))))))))

View File

@ -2,45 +2,43 @@
Simple Function
================================================================================
=> "Hiya"
=> { "Hiya" }
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(value
(function
(block
(statement
(expression
(value
(string)))))))))))
(statement
(expression
(value
(function
(block
(statement
(expression
(value
(string))))))))))
================================================================================
Function Assignment
================================================================================
x = => "Hiya"
x = => { "Hiya" }
--------------------------------------------------------------------------------
(root
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(function
(block
(statement
(expression
(value
(string)))))))))))))
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(function
(block
(statement
(expression
(value
(string))))))))))))
================================================================================
Function Call
@ -51,14 +49,13 @@ Function Call
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(function_call
(identifier)
(expression
(value
(string))))))))
(statement
(expression
(function_call
(identifier)
(expression
(value
(string)))))))
================================================================================
Complex Function
@ -72,27 +69,26 @@ Complex Function
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(value
(function
(identifier_list
(identifier)
(identifier))
(block
(statement
(expression
(function_call
(built_in_function
(expression
(identifier))))))
(statement
(expression
(function_call
(built_in_function
(expression
(identifier)))))))))))))
(statement
(expression
(value
(function
(identifier_list
(identifier)
(identifier))
(block
(statement
(expression
(function_call
(built_in_function
(expression
(identifier))))))
(statement
(expression
(function_call
(built_in_function
(expression
(identifier))))))))))))
================================================================================
Complex Function Call
@ -110,27 +106,26 @@ Complex Function Call
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(function_call
(identifier)
(expression
(value
(string)))
(expression
(value
(integer)))
(expression
(value
(map
(identifier)
(statement
(expression
(value
(integer))))
(identifier)
(statement
(expression
(value
(integer))))))))))))
(statement
(expression
(function_call
(identifier)
(expression
(value
(string)))
(expression
(value
(integer)))
(expression
(value
(map
(identifier)
(statement
(expression
(value
(integer))))
(identifier)
(statement
(expression
(value
(integer)))))))))))

View File

@ -1,21 +1,20 @@
==================
================================================================================
Simple Identifiers
==================
================================================================================
x
_y
__xyz__
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(identifier)))
(statement
(expression
(identifier)))
(statement
(expression
(identifier)))))
(statement
(expression
(identifier)))
(statement
(expression
(identifier)))
(statement
(expression
(identifier))))

View File

@ -1,153 +1,151 @@
==================
================================================================================
Simple If
==================
================================================================================
if true { "True" }
---
--------------------------------------------------------------------------------
(root
(block
(statement
(if_else
(if
(expression
(value
(boolean)))
(block
(statement
(expression
(value
(string))))))))))
(statement
(if_else
(if
(expression
(value
(boolean)))
(block
(statement
(expression
(value
(string)))))))))
==================
================================================================================
Complex If
==================
================================================================================
if 1 == 1 && 2 == 2 && 3 == 3 "True"
if 1 == 1 && 2 == 2 && 3 == 3 { "True" }
---
--------------------------------------------------------------------------------
(root
(block
(statement
(if_else
(if
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(integer)))))))))))))
(block
(statement
(expression
(value
(string))))))))))
(statement
(if_else
(if
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(integer)))))))))))))
(block
(statement
(expression
(value
(string)))))))))
==================
================================================================================
Nested If
==================
================================================================================
if true
if 42 == 12
if true {
if 42 == 12 {
'hiya'
else
} else {
'bye'
}
}
---
--------------------------------------------------------------------------------
(root
(block
(statement
(if_else
(if
(expression
(value
(boolean)))
(block
(statement
(if_else
(if
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(integer)))))
(block
(statement
(expression
(value
(string))))))
(else
(block
(statement
(expression
(value
(string))))))))))))))
(statement
(if_else
(if
(expression
(value
(boolean)))
(block
(statement
(if_else
(if
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(integer)))))
(block
(statement
(expression
(value
(string))))))
(else
(block
(statement
(expression
(value
(string)))))))))))))
==================
================================================================================
If Else
==================
================================================================================
if false "True" else "False"
if false { "True" } else { "False" }
---
--------------------------------------------------------------------------------
(root
(block
(statement
(if_else
(if
(expression
(value
(boolean)))
(block
(statement
(expression
(value
(string))))))
(else
(block
(statement
(expression
(value
(string))))))))))
(statement
(if_else
(if
(expression
(value
(boolean)))
(block
(statement
(expression
(value
(string))))))
(else
(block
(statement
(expression
(value
(string)))))))))
==================
================================================================================
If Else If
==================
================================================================================
if 1 == 1 {
"math is fun"
@ -155,104 +153,103 @@ if 1 == 1 {
"math is broken"
}
---
--------------------------------------------------------------------------------
(root
(block
(statement
(if_else
(if
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(integer)))))
(block
(statement
(expression
(value
(string))))))
(else_if
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(integer)))))
(block
(statement
(expression
(value
(string))))))))))
(statement
(if_else
(if
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(integer)))))
(block
(statement
(expression
(value
(string))))))
(else_if
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(integer)))))
(block
(statement
(expression
(value
(string)))))))))
==================
================================================================================
If Else Else If Else
==================
================================================================================
if false
if false {
"no"
else if false
} else if false {
"no"
else if 1 + 1 == 9
} else if 1 + 1 == 9 {
"not the answer"
else
} else {
"42"
}
---
--------------------------------------------------------------------------------
(root
(block
(statement
(if_else
(if
(expression
(value
(boolean)))
(block
(statement
(expression
(value
(string))))))
(else_if
(expression
(value
(boolean)))
(block
(statement
(expression
(value
(string))))))
(else_if
(expression
(logic
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(value
(integer)))))
(block
(statement
(expression
(value
(string))))))
(else
(block
(statement
(expression
(value
(string))))))))))
(statement
(if_else
(if
(expression
(value
(boolean)))
(block
(statement
(expression
(value
(string))))))
(else_if
(expression
(value
(boolean)))
(block
(statement
(expression
(value
(string))))))
(else_if
(expression
(logic
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(value
(integer)))))
(block
(statement
(expression
(value
(string))))))
(else
(block
(statement
(expression
(value
(string)))))))))

View File

@ -1,6 +1,6 @@
==================
================================================================================
Simple Indexes
==================
================================================================================
dust_data:1:name
@ -8,87 +8,85 @@ creature:total_clams
foobar:1:42
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(index
(expression
(index
(expression
(identifier))
(expression
(value
(integer)))))
(expression
(identifier)))))
(statement
(expression
(index
(expression
(identifier))
(expression
(identifier)))))
(statement
(statement
(expression
(index
(expression
(index
(expression
(index
(expression
(identifier))
(expression
(value
(integer)))))
(identifier))
(expression
(value
(integer))))))))
(integer)))))
(expression
(identifier)))))
(statement
(expression
(index
(expression
(identifier))
(expression
(identifier)))))
(statement
(expression
(index
(expression
(index
(expression
(identifier))
(expression
(value
(integer)))))
(expression
(value
(integer)))))))
==================
================================================================================
Nested Indexes
==================
================================================================================
[['answers' 'foobar'], 42, 666]:0:1:0..2
---
--------------------------------------------------------------------------------
(root
(block
(statement
(statement
(expression
(index
(expression
(index
(expression
(index
(expression
(index
(expression
(value
(list
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(string))))))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(expression
(value
(integer)))))
(value
(list
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(string))))))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(expression
(value
(integer)))))
(expression
(value
(integer)))
(expression
(value
(integer))))))))
(integer)))))
(expression
(value
(integer)))
(expression
(value
(integer)))))))

View File

@ -1,50 +1,48 @@
==================
================================================================================
List Declaration
==================
================================================================================
['answer', 42]
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(integer)))))))))
(statement
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(integer))))))))
==================
================================================================================
List Nesting
==================
================================================================================
['answers', [42, [666]]]
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(list
(expression
(value
(integer)))
(expression
(value
(list
(expression
(value
(integer)))))))))))))))
(statement
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(list
(expression
(value
(integer)))
(expression
(value
(list
(expression
(value
(integer))))))))))))))

View File

@ -1,26 +1,26 @@
==================
================================================================================
Simple Map
==================
================================================================================
{ answer = 42 }
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(value
(map
(identifier)
(statement
(expression
(value
(integer))))))))))
(statement
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(integer)))))))))
==================
================================================================================
Nested Maps
==================
================================================================================
x = {
y = {
@ -32,40 +32,43 @@ x = {
f = 12
}
---
--------------------------------------------------------------------------------
(root
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(map
(identifier)
(statement
(expression
(value
(map
(identifier)
(statement
(expression
(value
(string))))
(identifier)
(statement
(expression
(value
(map
(identifier)
(statement
(expression
(value
(string))))))))))))
(identifier)
(statement
(expression
(value
(integer))))))))))))
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(map
(identifier)
(statement
(expression
(value
(string))))
(identifier)
(statement
(expression
(value
(map
(identifier)
(statement
(expression
(value
(string))))))))))))))
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(integer)))))))))))

View File

@ -1,98 +1,95 @@
==================
================================================================================
\==
==================
================================================================================
3 == 1 + 1 + 1
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(math
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(math_operator)
(expression
(value
(integer))))))))))
(statement
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(math
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(math_operator)
(expression
(value
(integer)))))))))
==================
================================================================================
&&
==================
================================================================================
4 + 2 == 42 && true
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(logic
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(boolean))))))))))
(statement
(expression
(logic
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(boolean)))))))))
==================
================================================================================
\||
==================
================================================================================
4 + 2 == 42 || true
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(logic
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(boolean))))))))))
(statement
(expression
(logic
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(logic_operator)
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(value
(boolean)))))))))

View File

@ -1,73 +1,71 @@
==================
================================================================================
Simple Reduce Loop
==================
================================================================================
reduce i to acc in [1, 2, 3] {
acc += i
}
---
--------------------------------------------------------------------------------
(root
(block
(statement
(reduce
(identifier)
(identifier)
(expression
(value
(list
(statement
(reduce
(identifier)
(identifier)
(expression
(value
(list
(expression
(value
(integer)))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(integer)))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(identifier))))))))))
(identifier)))))))))
==================
================================================================================
Nested Reduce Loop
==================
================================================================================
reduce i to acc in ["one", "two", "three"] {
acc += i
}
---
--------------------------------------------------------------------------------
(root
(block
(statement
(reduce
(identifier)
(identifier)
(expression
(value
(list
(statement
(reduce
(identifier)
(identifier)
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(string)))
(expression
(value
(string))))))
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(string)))
(expression
(value
(string)))
(expression
(value
(string))))))
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(identifier))))))))))
(identifier)))))))))

View File

@ -1,44 +1,43 @@
==================
================================================================================
Simple Remove
==================
================================================================================
remove i from [1, 2, 3] {
i <= 2
}
---
--------------------------------------------------------------------------------
(root
(block
(statement
(remove
(identifier)
(expression
(value
(list
(expression
(value
(integer)))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(block
(statement
(statement
(remove
(identifier)
(expression
(value
(list
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(integer)))))))))))
(value
(integer)))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(block
(statement
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(integer))))))))))
==================
================================================================================
Nested Remove
==================
================================================================================
remove i from big_list {
remove j from i {
@ -46,28 +45,27 @@ remove i from big_list {
}
}
---
--------------------------------------------------------------------------------
(root
(block
(statement
(remove
(identifier)
(expression
(identifier))
(block
(statement
(remove
(identifier)
(expression
(identifier))
(block
(statement
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(integer))))))))))))))
(statement
(remove
(identifier)
(expression
(identifier))
(block
(statement
(remove
(identifier)
(expression
(identifier))
(block
(statement
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(integer)))))))))))))

View File

@ -1,58 +1,56 @@
==================
================================================================================
Simple Statements
==================
================================================================================
1
"one";
x
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(value
(integer))))
(statement
(expression
(value
(string))))
(statement
(expression
(identifier)))))
(statement
(expression
(value
(integer))))
(statement
(expression
(value
(string))))
(statement
(expression
(identifier))))
==================
================================================================================
Simple Assignment
==================
================================================================================
x = 1;
y = "one"
---
--------------------------------------------------------------------------------
(root
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(integer))))))
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(string))))))))
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(integer))))))
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(string)))))))
==================
================================================================================
Complex Assignment
==================
================================================================================
x = if 1 + 1 == 2 {
'yo'
@ -60,77 +58,75 @@ x = if 1 + 1 == 2 {
'no'
}
---
--------------------------------------------------------------------------------
(root
(block
(statement
(assignment
(identifier)
(assignment_operator)
(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
(string))))))
(else
(block
(statement
(expression
(value
(string))))))))))))
(statement
(assignment
(identifier)
(assignment_operator)
(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
(string))))))
(else
(block
(statement
(expression
(value
(string)))))))))))
==================
================================================================================
Expression Precedence
==================
================================================================================
x = 3 == 1 + 2 + 2
---
--------------------------------------------------------------------------------
(root
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(math
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(math_operator)
(expression
(value
(integer))))))))))))
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(logic
(expression
(value
(integer)))
(logic_operator)
(expression
(math
(expression
(math
(expression
(value
(integer)))
(math_operator)
(expression
(value
(integer)))))
(math_operator)
(expression
(value
(integer)))))))))))

View File

@ -1,6 +1,6 @@
==================
================================================================================
Table Declaration
==================
================================================================================
table |messages numbers| [
['hiya' 42]
@ -8,101 +8,98 @@ table |messages numbers| [
['bar' 99.99]
]
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(value
(table
(identifier_list
(identifier)
(identifier))
(expression
(value
(list
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(integer))))))
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(integer))))))
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(float)))))))))))))))
(statement
(expression
(value
(table
(identifier_list
(identifier)
(identifier))
(expression
(value
(list
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(integer))))))
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(integer))))))
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(float))))))))))))))
==================
================================================================================
Table Access
==================
================================================================================
select |number| from foobar {
text == 'answer'
}
---
--------------------------------------------------------------------------------
(root
(block
(statement
(select
(identifier_list
(identifier))
(expression
(identifier))
(block
(statement
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(string)))))))))))
(statement
(select
(identifier_list
(identifier))
(expression
(identifier))
(block
(statement
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(string))))))))))
==================
================================================================================
Table Insert
==================
================================================================================
insert into foobar [
['bob was here', 0]
]
---
--------------------------------------------------------------------------------
(root
(block
(statement
(insert
(identifier)
(expression
(value
(list
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(integer)))))))))))))
(statement
(insert
(identifier)
(expression
(value
(list
(expression
(value
(list
(expression
(value
(string)))
(expression
(value
(integer))))))))))))

View File

@ -9,29 +9,28 @@ transform i in [1, 2, 3] {
--------------------------------------------------------------------------------
(root
(block
(statement
(transform
(identifier)
(expression
(value
(list
(expression
(value
(integer)))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(block
(statement
(statement
(transform
(identifier)
(expression
(value
(list
(expression
(function_call
(built_in_function
(expression
(identifier)))))))))))
(value
(integer)))
(expression
(value
(integer)))
(expression
(value
(integer))))))
(block
(statement
(expression
(function_call
(built_in_function
(expression
(identifier))))))))))
================================================================================
Nested Transform Loop
@ -46,43 +45,42 @@ transform i in [['one'] ['two'] ['three']] {
--------------------------------------------------------------------------------
(root
(block
(statement
(transform
(identifier)
(expression
(value
(list
(expression
(value
(list
(statement
(transform
(identifier)
(expression
(value
(list
(expression
(value
(list
(expression
(value
(string))))))
(expression
(value
(list
(expression
(value
(string))))))
(expression
(value
(list
(expression
(value
(string)))))))))
(block
(statement
(transform
(identifier)
(expression
(identifier))
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(string))))))
(expression
(value
(list
(expression
(value
(string))))))
(expression
(value
(list
(expression
(value
(string)))))))))
(block
(statement
(transform
(identifier)
(expression
(identifier))
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(string))))))))))))))
(string)))))))))))))

View File

@ -1,82 +1,79 @@
==================
================================================================================
Booleans
==================
================================================================================
true
false
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(value
(boolean))))
(statement
(expression
(value
(boolean))))))
(statement
(expression
(value
(boolean))))
(statement
(expression
(value
(boolean)))))
==================
================================================================================
Integers
==================
================================================================================
1 2 3
456 7
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(value
(integer))))
(statement
(expression
(value
(integer))))
(statement
(expression
(value
(integer))))
(statement
(expression
(value
(integer))))
(statement
(expression
(value
(integer))))))
(statement
(expression
(value
(integer))))
(statement
(expression
(value
(integer))))
(statement
(expression
(value
(integer))))
(statement
(expression
(value
(integer))))
(statement
(expression
(value
(integer)))))
==================
================================================================================
Strings
==================
================================================================================
"one" 'two' "three" `four` 'five'
---
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(value
(string))))
(statement
(expression
(value
(string))))
(statement
(expression
(value
(string))))
(statement
(expression
(value
(string))))
(statement
(expression
(value
(string))))))
(statement
(expression
(value
(string))))
(statement
(expression
(value
(string))))
(statement
(expression
(value
(string))))
(statement
(expression
(value
(string))))
(statement
(expression
(value
(string)))))

View File

@ -9,20 +9,19 @@ while true {
--------------------------------------------------------------------------------
(root
(block
(statement
(while
(expression
(value
(boolean)))
(block
(statement
(expression
(function_call
(built_in_function
(expression
(value
(string))))))))))))
(statement
(while
(expression
(value
(boolean)))
(block
(statement
(expression
(function_call
(built_in_function
(expression
(value
(string)))))))))))
================================================================================
Nested While Loop
@ -37,29 +36,28 @@ while (true) {
--------------------------------------------------------------------------------
(root
(block
(statement
(while
(expression
(value
(boolean)))
(block
(statement
(while
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(integer)))))
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(integer))))))))))))))
(statement
(while
(expression
(value
(boolean)))
(block
(statement
(while
(expression
(logic
(expression
(identifier))
(logic_operator)
(expression
(value
(integer)))))
(block
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(integer)))))))))))))

View File

@ -7,15 +7,14 @@ Simple Yield
--------------------------------------------------------------------------------
(root
(block
(statement
(expression
(yield
(expression
(value
(integer)))
(function_call
(built_in_function)))))))
(statement
(expression
(yield
(expression
(value
(integer)))
(function_call
(built_in_function))))))
================================================================================
Long Yield
@ -30,33 +29,32 @@ Long Yield
--------------------------------------------------------------------------------
(root
(block
(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)))))))
(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))))))

View File

@ -10,19 +10,21 @@ module.exports = grammar({
],
rules: {
root: $ => repeat1($.block),
root: $ => prec(1, repeat1($.statement)),
_comment: $ => /[#][^#\n]*[#|\n]/,
block: $ => prec.right(choice(
block: $ => seq(
optional('async'),
'{',
repeat1($.statement),
seq('{', repeat1($.statement), '}'),
)),
'}',
),
statement: $ => prec.right(seq(
choice(
$.assignment,
$.async,
$.block,
$.expression,
$.filter,
$.find,
@ -268,11 +270,6 @@ module.exports = grammar({
$.expression,
)),
async: $ => seq(
'async',
$.block,
),
identifier_list: $ => prec.right(choice(
seq(
'|',

View File

@ -3,10 +3,14 @@
"word": "identifier",
"rules": {
"root": {
"type": "REPEAT1",
"type": "PREC",
"value": 1,
"content": {
"type": "SYMBOL",
"name": "block"
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "statement"
}
}
},
"_comment": {
@ -14,40 +18,24 @@
"value": "[#][^#\\n]*[#|\\n]"
},
"block": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "CHOICE",
"members": [
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "statement"
}
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "statement"
}
},
{
"type": "STRING",
"value": "}"
}
]
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "statement"
}
]
}
},
{
"type": "STRING",
"value": "}"
}
]
},
"statement": {
"type": "PREC_RIGHT",
@ -66,6 +54,10 @@
"type": "SYMBOL",
"name": "async"
},
{
"type": "SYMBOL",
"name": "block"
},
{
"type": "SYMBOL",
"name": "expression"

View File

@ -538,7 +538,7 @@
"required": true,
"types": [
{
"type": "block",
"type": "statement",
"named": true
}
]
@ -583,6 +583,10 @@
"type": "async",
"named": true
},
{
"type": "block",
"named": true
},
{
"type": "expression",
"named": true

File diff suppressed because it is too large Load Diff