if-statements: If Statement Nodes

Description Usage Arguments Details Value Functions Examples

Description

These function navigate logic statements.

Returns the id of the predicate of the if statement, i.e. the conditional statement.

Returns the id of the body of the branch executed if the predicate evaluates to true.

Gets the id of the alternate branch, i.e. the else branch.

Usage

1
2
3
4
5
6
7
pd_is_if(id, pd, .check = TRUE)

pd_get_if_predicate_id(id, pd, .check = TRUE)

pd_get_if_branch_id(id, pd, .check = TRUE)

pd_get_if_alternate_id(id, pd, .check = TRUE)

Arguments

id

id of the expression of interest

pd

The parse-data information

.check

Perform checks for input validation?

Details

If statements have the form of the following.

1
    if (predicate) branch else alternate

The predicate refers to the logical test being performed. The branch is the statement or block that is executed if predicate evaluates true. The alternate is the statement of block that is executed if predicate returns false.

Value

an id integer.

an id integer.

Functions

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# load example file and get_parse data
ex.file <- system.file("examples", "example.R", package="parsetools")
exprs <- parse(ex.file, keep.source = TRUE)
pd <- get_parse_data(exprs)

# There are 3 expressions so there should be three roots.
sum(pd_is_root(pd$id, pd))
roots <- pd_all_root_ids(pd)

# Find the if statement
is.if <- pd_is_if(pd$id, pd=pd)
sum(is.if)
if.id <- pd$id[is.if]

# The predicate
pd_reconstitute(pd_get_if_predicate_id(if.id, pd), pd)

# The branch for if predicate evaluates TRUE
pd_reconstitute(pd_get_if_branch_id(if.id, pd), pd)

# The alternate for if predicate evaluates FALSE
pd_reconstitute(pd_get_if_alternate_id(if.id, pd), pd)

parsetools documentation built on April 14, 2020, 5:32 p.m.