parsetools (version 0.1.1)

if-statements: If Statement Nodes

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

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?

Value

an id integer.

an id integer.

Functions

  • pd_is_if: Is node an if expression.

  • pd_get_if_predicate_id: Get the predicate node.

  • pd_get_if_branch_id: Get the branch statement or block node.

  • pd_get_if_alternate_id: Get the alternate statement or block node.

Details

If statements have the form of the following.

    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.

Examples

Run this code
# NOT RUN {
# 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)


# }

Run the code above in your browser using DataLab