Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify ast tree #43

Closed
alperyilmaz opened this issue Jan 2, 2020 · 1 comment
Closed

modify ast tree #43

alperyilmaz opened this issue Jan 2, 2020 · 1 comment

Comments

@alperyilmaz
Copy link

Hi,
My aim is to replace & with function min() and replace | with max(). So, a & (b | c) should be rewritten as min(a, max(b,c)). I thought modifying the syntax tree is the way to go. Is it possible to modify ast() output and then convert back into an expression?

I'm sharing a small example:

a <- 5
b <- 10
c <- 4
express <- a & (b | c)
express_adv <- (c & b) & (a & (b | c))

The rewritten express should evaluate to 5 and express_adv should evaluate to 4. Expected expressions are :

min(a, max(b,c))  # modified express 
min( min(c,b), min(a, max(b,c)))  # modified express_adv

So, this is before:

ast( a & (b | c) )

█─`&` 
├─a 
└─█─`(` 
  └─█─`|` 
    ├─b 
    └─c 

and this is after:

ast(min(a,(max(b,c))))

█─min 
├─a 
└─█─`(` 
  └─█─max 
    ├─b 
    └─c 
@hadley
Copy link
Member

hadley commented Dec 4, 2020

ast() just prints the AST; it already exists in R in a way you can work with. See https://adv-r.hadley.nz/expressions.html#ast-funs for some details.

@hadley hadley closed this as completed Dec 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants