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 serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Result, Statement}; use crate::{AbstractTree, 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 {
pub statements: Vec<Statement>, is_async: bool,
} 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());
let first_child = node.child(0).unwrap();
let is_async = first_child.kind() == "async";
let statement_count = node.child_count(); let statement_count = node.child_count();
let mut statements = Vec::with_capacity(statement_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(); let child_node = node.child(index).unwrap();
if child_node.kind() == "statement" { 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> { fn run(&self, source: &str, context: &mut crate::Map) -> crate::Result<crate::Value> {
for statement in &self.statements[0..self.statements.len() - 1] { if self.is_async {
statement.run(source, context)?; 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) Ok(item)
} }
Value::Map(map) => { Value::Map(mut map) => {
let value = if let Expression::Identifier(identifier) = &self.index { let value = if let Expression::Identifier(identifier) = &self.index {
let key = identifier.inner(); let key = identifier.inner();
map.variables()?.get(key).cloned().unwrap_or(Value::Empty) map.variables()?.get(key).cloned().unwrap_or(Value::Empty)
} else { } else {
let value = self.index.run(source, context)?; let value = self.index.run(source, &mut map)?;
let key = value.as_string()?; let key = value.as_string()?;
map.variables()?.get(key).cloned().unwrap_or(Value::Empty) map.variables()?.get(key).cloned().unwrap_or(Value::Empty)
@ -96,7 +96,7 @@ mod tests {
#[test] #[test]
fn evaluate_complex_index() { 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); assert_eq!(Value::Integer(1), test);
} }

View File

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

View File

@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{ 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, Match, Remove, Result, Select, Transform, Value, While,
}; };
@ -17,7 +17,7 @@ pub enum Statement {
IfElse(Box<IfElse>), IfElse(Box<IfElse>),
Match(Match), Match(Match),
While(Box<While>), While(Box<While>),
Async(Box<Async>), Block(Box<Block>),
For(Box<For>), For(Box<For>),
Transform(Box<Transform>), Transform(Box<Transform>),
Filter(Box<Filter>), Filter(Box<Filter>),
@ -49,7 +49,7 @@ impl AbstractTree for Statement {
"while" => Ok(Statement::While(Box::new(While::from_syntax_node( "while" => Ok(Statement::While(Box::new(While::from_syntax_node(
source, child, source, child,
)?))), )?))),
"async" => Ok(Statement::Async(Box::new(Async::from_syntax_node( "block" => Ok(Statement::Block(Box::new(Block::from_syntax_node(
source, child, source, child,
)?))), )?))),
"for" => Ok(Statement::For(Box::new(For::from_syntax_node( "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::IfElse(if_else) => if_else.run(source, context),
Statement::Match(r#match) => r#match.run(source, context), Statement::Match(r#match) => r#match.run(source, context),
Statement::While(r#while) => r#while.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::For(r#for) => r#for.run(source, context),
Statement::Transform(transform) => transform.run(source, context), Statement::Transform(transform) => transform.run(source, context),
Statement::Filter(filter) => filter.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 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. /// Evaluate the given source code.
/// ///
@ -89,9 +89,9 @@ impl<'context, 'code> Evaluator<'context, 'code> {
let root_node = cursor.node(); let root_node = cursor.node();
let mut prev_result = Ok(Value::Empty); let mut prev_result = Ok(Value::Empty);
for block_node in root_node.children(&mut cursor) { for statement_node in root_node.children(&mut cursor) {
let block = Block::from_syntax_node(self.source, block_node)?; let statement = Statement::from_syntax_node(self.source, statement_node)?;
prev_result = block.run(self.source, self.context); prev_result = statement.run(self.source, self.context);
} }
prev_result prev_result
@ -227,7 +227,7 @@ mod tests {
} }
#[test] #[test]
fn evaluate_if_else_else_if_else_if_else_if_else() { fn evaluate_if_else_if_else_if_else_if_else() {
assert_eq!( assert_eq!(
evaluate( evaluate(
" "
@ -253,7 +253,7 @@ mod tests {
assert_eq!( assert_eq!(
evaluate( evaluate(
" "
foobar = |message| => message foobar = |message| => { message }
(foobar 'Hiya') (foobar 'Hiya')
", ",
), ),

View File

@ -1,27 +1,26 @@
================== ================================================================================
Simple Async Statements Simple Async Statements
================== ================================================================================
async { (output 'Whaddup') } async { (output 'Whaddup') }
--- --------------------------------------------------------------------------------
(root (root
(block (statement
(statement (async
(async (block
(block (statement
(statement (expression
(expression (function_call
(function_call (built_in_function
(built_in_function (expression
(expression (value
(value (string)))))))))))
(string))))))))))))
================== ================================================================================
Complex Async Statements Complex Async Statements
================== ================================================================================
async { async {
if 1 % 2 == 0 { if 1 % 2 == 0 {
@ -33,43 +32,42 @@ async {
'foobar' 'foobar'
} }
--- --------------------------------------------------------------------------------
(root (root
(block (statement
(statement (async
(async (block
(block (statement
(statement (if_else
(if_else (if
(if (expression
(expression (logic
(logic (expression
(expression (math
(math (expression
(expression (value
(value (integer)))
(integer))) (math_operator)
(math_operator) (expression
(expression (value
(value (integer)))))
(integer))))) (logic_operator)
(logic_operator) (expression
(expression (value
(value (integer)))))
(integer))))) (block
(block (statement
(statement (expression
(expression (value
(value (boolean))))))
(boolean)))))) (else
(else (block
(block (statement
(statement (expression
(expression (value
(value (boolean))))))))
(boolean)))))))) (statement
(statement (expression
(expression (value
(value (string))))))))
(string)))))))))

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,15 +7,14 @@ Simple Yield
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block (statement
(statement (expression
(expression (yield
(yield (expression
(expression (value
(value (integer)))
(integer))) (function_call
(function_call (built_in_function))))))
(built_in_function)))))))
================================================================================ ================================================================================
Long Yield Long Yield
@ -30,33 +29,32 @@ Long Yield
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(root (root
(block (statement
(statement (expression
(expression (yield
(yield (expression
(expression (yield
(yield (expression
(expression (yield
(yield (expression
(expression (yield
(yield (expression
(expression (value
(value (list
(list (expression
(expression (value
(value (integer)))
(integer))) (expression
(expression (value
(value (integer)))
(integer))) (expression
(expression (value
(value (integer))))))
(integer)))))) (function_call
(function_call (identifier))))
(identifier)))) (function_call
(function_call (identifier))))
(identifier)))) (function_call
(function_call (identifier))))
(identifier)))) (function_call
(function_call (built_in_function))))))
(built_in_function)))))))

View File

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

View File

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

View File

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

File diff suppressed because it is too large Load Diff