DOCS: Add reduce expressions to the grammar and reference.

This commit is contained in:
Jeremy Wall 2019-01-06 21:04:01 -06:00
parent e46496c666
commit a062332b0a
2 changed files with 42 additions and 7 deletions

View File

@ -303,6 +303,38 @@ let tpl_filter = macro(name, val) => {
filter tpl_filter.result test_tpl == { quux = "baz" };
```
### Reduce expressions
Reduce expressions start with the reduce keyword followed by a symbol referencing a macro a dot and the output field, then an expression for the accumulator and finally the tuple or list to process.
**Tuples**
```
let test_tpl = {
foo = "bar",
quux = "baz",
};
let tpl_reducer = macro(acc, name, val) => {
result = acc{
keys = self.keys + [name],
vals = self.vals + [val],
},
};
reduce tpl_reducer.result {keys = [], vals = []}, test_tpl == {keys = ["foo", "quux"], vals = ["bar", "baz"]};
```
**Lists**
```
let list1 = [1, 2, 3, 4];
let list_reducer = macro(acc, item) => {
result = acc + item,
};
list_reducer.result 0, list1 == 0 + 1 + 2 + 3 + 4;
```
Include expressions
-------------------

View File

@ -50,6 +50,7 @@ include_keyword: "include" ;
as_keyword: "as" ;
macro_keyword: "macro" ;
map_keyword: "map" ;
reduce_keyword: "map" ;
filter_keyword: "filter" ;
module_keyword: "module" ;
mod_keyword: "mod" ;
@ -121,8 +122,8 @@ module_def: module_keyword, tuple, fatcomma, lbrace, [ { statement } ], rbrace ;
#### Copy and Call Expression
```
copy_expression: bareword, tuple ;
call_expression: bareword, lparen, [arglist], rparen ;
copy_expr: bareword, tuple ;
call_expr: bareword, lparen, [arglist], rparen ;
```
#### Format Expression
@ -135,7 +136,9 @@ format_expr: str, percent, lparen, [arglist], rparen ;
```
func_op_kind: map_keyword | filter_keyword ;
processing_expr: func_op_kind, bareword, dot, bareword, expr ;
map_or_filter_expr: func_op_kind, bareword, dot, bareword, expr ;
reduce_expr: reduce_keyword, bareword, dot, bareword, expr, expr ;
processing_expr: map_or_filter_expr | reduce_expr
```
#### Include Expression
@ -151,11 +154,11 @@ non_operator_expr: literal
| grouped
| macrodef
| module_def
| format_expression
| include_expression
| copy_expression
| format_expr
| include_expr
| copy_expr
| processing_expr
| call_expression ;
| call_expr ;
```
#### Operator Expressions