-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunaryexpr.go
33 lines (27 loc) · 891 Bytes
/
unaryexpr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package script
import (
"go/ast"
"go/token"
"reflect"
)
func (w *World) compileUnaryExpr(n *ast.UnaryExpr) Expr {
x := w.compileExpr(n.X)
switch n.Op {
default:
panic(err(n.Pos(), "not allowed:", n.Op))
case token.SUB:
return &minus{typeConv(n.X.Pos(), x, float64_t)}
case token.NOT:
return ¬{typeConv(n.X.Pos(), x, bool_t)}
}
}
type minus struct{ x Expr }
func (m *minus) Type() reflect.Type { return float64_t }
func (m *minus) Eval() interface{} { return -m.x.Eval().(float64) }
func (m *minus) Child() []Expr { return []Expr{m.x} }
func (m *minus) Fix() Expr { return &minus{m.x.Fix()} }
type not struct{ x Expr }
func (m *not) Type() reflect.Type { return bool_t }
func (m *not) Eval() interface{} { return !m.x.Eval().(bool) }
func (m *not) Child() []Expr { return []Expr{m.x} }
func (m *not) Fix() Expr { return ¬{m.x.Fix()} }