rpn: Reverse Polish Notation converter/interpreter.

Description Usage Arguments Details Value References Examples

View source: R/rpn.R

Description

Expects an expression in (reverse) polish notation as a character vector and returns the expression in infix notation and the evaluated expression.

Usage

1
2
rpn(rpn.expr, reverse = TRUE, eval = TRUE, clean = TRUE, vars = list(),
  ops = list())

Arguments

rpn.expr

[character]
Character vector representing the expression to convert in (reverse) polish notation.

reverse

[logical(1)]
Is rpn.expr in reverse polish notation or polish notation? Default is TRUE, i.e., reverse polish notation.

eval

[logical(1)]
Shall the expression be evaluated? Default is TRUE.

clean

[logical(1)]
Should braces be removed from rpn.expr before interpretation? Default is TRUE.

vars

[list]
Named list of variables which are used in the rpn.expr beside constants. Default is the empty list.

ops

[list]
Optional named list of operators. If non-empty, the list must be of key-value type, i.e., the key is the name of the function and the value is a list with the 1) the length of the parameter vector the corresponding function expects, 2) a logical value indicating whether the function is a binary infix operator and third the actual function to be called in case the expression is evaluated.

Details

This is a pure R implementation.

Value

List with the components

infix

Infix representation of rpn.expr.

value

Evaluated expression or NA if eval is set to FALSE.

References

Lukasiewicz, Jan (1957). Aristotle's Syllogistic from the Standpoint of Modern Formal Logic. Oxford University Press.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# simple example
r = c("4", "6", "*", "6", "+")
rpn(r)
rpn(r, eval = FALSE)

# the same example but with a variable
r = c("x", "6", "*", "6", "+")
rpn(r, eval = TRUE, vars = list(x = 4))
rpn(r, eval = FALSE)

# now a more complex expression with variables and custom operators/functions
rpe = c("x", "5", "6.4", "mysum", "5", "mystuff")
mysum = function(x, y, z) x + y + z # arity 3 and no infix operation
mystuff = function(x, y) 2 * (x + y) # arity 2 and no infix operation
ops = list(mysum = list(3, FALSE, mysum), mystuff = list(2, FALSE, mystuff))
vars = list(x = 3.6)
res = rpn(rpe, ops = ops, vars = vars)

jakobbossek/rpn documentation built on May 18, 2019, 10:11 a.m.