Run mathematical expression from string

2,052 views
Skip to first unread message

Archos

unread,
Nov 21, 2011, 9:20:19 AM11/21/11
to golang-nuts
How to run mathematical expressions got from a string? (expr := "1 <<
3 - 1")

Jan Mercl

unread,
Nov 21, 2011, 9:27:02 AM11/21/11
to golan...@googlegroups.com
On Monday, November 21, 2011 3:20:19 PM UTC+1, Archos wrote:
How to run mathematical expressions got from a string? (expr := "1 <<
3 - 1")

Parse it and then interpret the resulting AST. Depending on the accepted "language" it may be done sometimes while parsing. You might get inspired by https://github.com/cznic/golex/blob/master/calc/calc.y

Sebastien Binet

unread,
Nov 21, 2011, 9:46:33 AM11/21/11
to Jan Mercl, golan...@googlegroups.com

or leverage the facilities from go-eval (which already do the AST
interpretation)

https://bitbucket.org/binet/go-eval/src/tip/pkg/eval/main.go

note to self: I should probably make this a proper 'go-eval' cmd...

-s

--
#########################################
# Dr. Sebastien Binet
# Laboratoire de l'Accelerateur Lineaire
# Universite Paris-Sud XI
# Batiment 200
# 91898 Orsay
#########################################

Salman Aljammaz

unread,
Nov 21, 2011, 10:02:58 AM11/21/11
to Archos, golang-nuts
Judging from the way gotry works i'm guessing there's no ready made
"eval" function like you'd find in some languages.

I'd suggest having a look at how hoc was written in C. (I think it
was at the end of The Unix Programming Environment book.)

Alternatively, you might be able to reuse ParseExpr in go/parser if
you want something identical to how go parses expressions.

package main

import (
"go/ast"
"go/parser"
"go/token"
)

func main() {
fs := token.NewFileSet()
tr, _ := parser.ParseExpr(fs, "", "1 << 3 - 1")
ast.Print(fs, tr)
}

Then again, this might be an overkill.

Salman

Archos

unread,
Nov 21, 2011, 6:41:50 PM11/21/11
to golang-nuts
I just try this but I get panic message: "unimplemented". Does not
working?

package main

import (
"go/token"
"exp/types" // "go/types" in stable version
)

func main() {
x := types.MakeConst(token.INT, "1")
y := types.MakeConst(token.INT, "3")
x.BinaryOp(token.SHL, y)
}
// * * *
panic: unimplemented

goroutine 1 [running]:
exp/types.binaryIntOp(0xf84001ea80, 0x14, 0xf84001eac0, 0xf84001eac0,
0xf84001d501, ...)
/var/tmp/go/src/pkg/exp/types/const.go:219 +0x327
exp/types.Const.BinaryOp(0x4dd6f0, 0xf84001ea80, 0x3300000014,
0x4dd6f0, 0xf84001eac0, ...)
/var/tmp/go/src/pkg/exp/types/const.go:174 +0x2c5

Jan Mercl

unread,
Nov 22, 2011, 1:12:18 AM11/22/11
to golan...@googlegroups.com
On Tuesday, November 22, 2011 12:41:50 AM UTC+1, Archos wrote:
I just try this but I get panic message: "unimplemented". Does not
working?

$ godoc -src exp/types BinaryOp

unread,
Nov 22, 2011, 2:53:56 AM11/22/11
to golang-nuts
On Nov 22, 12:41 am, Archos <raul....@sent.com> wrote:
> I just try this but I get panic message: "unimplemented". Does not
> working?

It means that the functionality you are requesting isn't implemented
yet.

Prior to posting your question, you should have taken a look at the
source code of "exp/types" (or "go/types" in stable version):
http://golang.org/src/pkg/go/types/const.go?s=3787:3841#L158

The meaning of the message "panic: unimplemented" is clear from the
source code in file "go/types/const.go".

unread,
Nov 22, 2011, 2:56:25 AM11/22/11
to golang-nuts

After running the command you suggested, I do not see anything which
would explain the meaning of the panic message.

Jan Mercl

unread,
Nov 22, 2011, 3:26:28 AM11/22/11
to golan...@googlegroups.com
On Tuesday, November 22, 2011 8:56:25 AM UTC+1, ⚛ wrote:
On Nov 22, 7:12 am, Jan Mercl <jan....@nic.cz> wrote:
> $ godoc -src exp/types BinaryOp

After running the command you suggested, I do not see anything which
would explain the meaning of the panic message.

Well, it was not an explanation but a clue how oneself can find the explanation instead of asking N thousands subscribers of this list to do it.

godoc shows that BinaryOp will invoke in the type switch case statement the binaryIntOp (http://weekly.golang.org/src/pkg/exp/types/const.go?s=3787:3841#L197)

And then on line http://weekly.golang.org/src/pkg/exp/types/const.go?s=3787:3841#L197 is the case clause for token.SHL which has the statement panic("unimplemented").

(It's about the "How to fish instead of giving the fish".)

unread,
Nov 22, 2011, 4:40:51 AM11/22/11
to golang-nuts
On Nov 22, 9:26 am, Jan Mercl <jan.me...@nic.cz> wrote:
> On Tuesday, November 22, 2011 8:56:25 AM UTC+1, ⚛ wrote:
>
> > On Nov 22, 7:12 am, Jan Mercl <jan....@nic.cz> wrote:
> > > $ godoc -src exp/types BinaryOp
>
> > After running the command you suggested, I do not see anything which
> > would explain the meaning of the panic message.
>
> Well, it was not an explanation but a clue how oneself can find the
> explanation instead of asking N thousands subscribers of this list to do it.
>
> godoc shows that BinaryOp will invoke in the type switch case statement the binaryIntOp
> (http://weekly.golang.org/src/pkg/exp/types/const.go?s=3787:3841#L197)
>
> And then on linehttp://weekly.golang.org/src/pkg/exp/types/const.go?s=3787:3841#L197is the

> case clause for token.SHL which has the statement panic("unimplemented").
>
> (It's about the "How to *fish instead of giving* the *fish".)*

Well, but there is a difference between a clue and a method. Applying
a method leads (or at least should lead) to definite results. A clue
is an incomplete answer.

Then there is also the problem of description accuracy. I would argue
that "godoc -src exp/types BinaryOp" is less accurate than writing
"Examine the source code of exp/types/const.go in order to understand
why the code panics. ...". In my opinion, executing the latter will
lead to understanding more often than executing the former.

Prior to posting my answer I verified that it actually leads to
understanding of the cause of the issue Archos was asking about. The
question is whether you did perform verification on your side, prior
to posting your answer.

I mean no disrespect. This debate is purely about how to achieve
better accuracy in an answer to a question.

Pramod Devireddy

unread,
Apr 26, 2021, 12:59:12 PM4/26/21
to golang-nuts
https://github.com/Pramod-Devireddy/go-exprtk package does evaluate any kind of mathematical expressions dynamically during run-time.
Reply all
Reply to author
Forward
0 new messages