2

I have a boolean expression in string format, example:

name := "Fred"

type := "Person"

I want to evaluate this expression as true or false.

exp := "(name == Fred) && (type == Person)"

Eventually, I would like to be able to execute conditional statements such as:

if (exp) {
    ...
}

However, from research this is not something Go supports out of the box. I have seen suggestions on using AST to parse and evaluate. But, I am fairly new to go and especially AST, thus not sure how to go about that. Can someone please provide any guidance on how I may go about evaluating a string boolean expression? I have not come across any packages that support this entirely.

Saad Karim
  • 229
  • 2
  • 10
  • o.õ this is pretty smelly, why do you need the string – Plato Jul 28 '16 at 20:19
  • 1
    I have a boolean expression stored as a JSON in a file. I parse through the JSON to create my string boolean expression. Once I have the expression I then need to evaluate it. – Saad Karim Jul 28 '16 at 20:58
  • 1
    Is your "boolean expression" guaranteed to be correct Go syntax? (that example isn't valid Go, so you can't directly parse it as Go) – JimB Jul 28 '16 at 21:05
  • Yes, lets assume that it is. I am trying to understand the mechanism that would be involved doing something like this. – Saad Karim Jul 28 '16 at 21:15
  • I bet this is from some previous system that was an interpreter. Perl, Python, Ruby, etc. And they just took that JSON field and `eval`'d it. Crappy for security, eh. – Zan Lynx Jul 28 '16 at 23:20
  • 1
    Possible duplicate of [Evaluate formula in Go](http://stackoverflow.com/questions/23923383/evaluate-formula-in-go) – Zan Lynx Jul 28 '16 at 23:51
  • Definitely a duplicate. Look at the comment on the accepted answer on that other question. It shows how to use the Go parser to evaluate an expression. – Zan Lynx Jul 28 '16 at 23:58
  • It only evaluates constant folding it appears, so multiple variable assignments and operations like in your example probably wouldn't work. – Zan Lynx Jul 29 '16 at 00:05
  • You can use [go-exprtk](https://github.com/Pramod-Devireddy/go-exprtk) package to evaluate these kinds of mathematical expressions. – Pramod Apr 26 '21 at 06:22

1 Answers1

1

The following is true in theory. But since you're using Go if you can use Go syntax then you can use Go's parser and AST. I don't see any code that can evaluate a Go AST at runtime. But you could probably write one that supported the parts you wanted. Then you'd have a Go interpreter.

The following is what you need to do to support any random expression syntax:

You are going to want to lex and parse. Build an AST (Abstract Syntax Tree) in memory. Then evaluate it.

Your tree nodes might be (my Go syntax is way wrong for this):

 Scope {Tree {
  Assignment { Symbol: "name", Symbol: "_literal_1" }
  Assignment { Symbol: "exp", Value: Tree: {
    AndOperation { Tree{...}, Tree{...} }
  }
}

Etc.

Then your program can traverse your AST directly or you can write it into bytecode form, but that's really only useful if you want it to be smaller and easy to cache for later.

Community
  • 1
  • 1
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131