The create_tree
function takes a string and generates a tree. For example, we can parse the simple expression 2+3
:
We can see the structure is a list of lists.
Under the hoodYou donât need to understand the structure of the tree to use it. Just like you can drive a car without knowing how an engine works. This section will help reveal how the trees are formed.
First lets confirm that we can replicate the tree structure:
tree <- list(
pval = list(),
eval = list("+", list("atomic", "2"), list("atomic", "3"))
)
testthat::expect_equal(x,tree)
This test passes with zero error.
A full tree is made up of two main branches:
pval
- stands for parenthesis valueseval
- stands for verification valuesThe first thing the function does is find all parenthesis blocks and treats them as sub statements. Each of these sub statements becomes an element of the pval
element. The eval
tree will have references to these pval
entries.
For example, lets tweak the 2+3
to (2)+(3)
:
x <- evalR::create_tree("(2)+(3)")
str(x)
#> List of 2
#> $ pval:List of 2
#> ..$ \0:List of 2
#> .. ..$ pval: list()
#> .. ..$ eval:List of 2
#> .. .. ..$ : chr "atomic"
#> .. .. ..$ : chr "2"
#> ..$ \1:List of 2
#> .. ..$ pval: list()
#> .. ..$ eval:List of 2
#> .. .. ..$ : chr "atomic"
#> .. .. ..$ : chr "3"
#> $ eval:List of 3
#> ..$ : chr "+"
#> ..$ :List of 2
#> .. ..$ : chr "atomic"
#> .. ..$ : chr "\\0"
#> ..$ :List of 2
#> .. ..$ : chr "atomic"
#> .. ..$ : chr "\\1"
To replicate the structure:
pval_list <- list(
"\\0"=list(
pval = list(),
eval = list("atomic", "2")
),
"\\1"=list(
pval = list(),
eval = list("atomic", "3")
)
)
tree <- list(
pval = pval_list,
eval = list("+", list("atomic", "\\0"), list("atomic", "\\1"))
)
testthat::expect_equal(x,tree)
This test passes with zero error.
Now the pval
list is not empty. We have an entry for (2)
and (3)
. Each entry in pval
is a new tree unto itself and contains pval
and eval
branches.
The eval
branch splits the statement by operators into âatomicâ elements.
For example, if we just parse 2
:
x <- evalR::create_tree("2")
str(x)
#> List of 2
#> $ pval: list()
#> $ eval:List of 2
#> ..$ : chr "atomic"
#> ..$ : chr "2"
The eval
is one level deep and the first element is the string atomic
. This signifies that this is an end node of the tree.
Lets expand this just a little bit:
x <- evalR::create_tree("-2")
str(x)
#> List of 2
#> $ pval: list()
#> $ eval:List of 2
#> ..$ : chr "-"
#> ..$ :List of 2
#> .. ..$ : chr "atomic"
#> .. ..$ : chr "2"
Now eval
is two levels deep. The first element states the operator -
and the second element is another branch that looks exactly like the eval
branch in the previous example.
If a parenthesis block is found, then the atomic element will be a reference to the which pval
element:
x <- evalR::create_tree("-(2)")
str(x)
#> List of 2
#> $ pval:List of 1
#> ..$ \0:List of 2
#> .. ..$ pval: list()
#> .. ..$ eval:List of 2
#> .. .. ..$ : chr "atomic"
#> .. .. ..$ : chr "2"
#> $ eval:List of 2
#> ..$ : chr "-"
#> ..$ :List of 2
#> .. ..$ : chr "atomic"
#> .. ..$ : chr "\\0"
In this example, the eval
second level atomic element is \0
. This is a reference to the \0
named element of the pval
branch.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4