How to run mathematical expressions got from a string? (expr := "1 <<
3 - 1")
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
#########################################
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
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
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".
After running the command you suggested, I do not see anything which
would explain the meaning of the panic message.
On Nov 22, 7:12 am, Jan Mercl <jan....@nic.cz> wrote:
> $ godoc -src exp/types BinaryOpAfter running the command you suggested, I do not see anything which
would explain the meaning of the panic message.
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.