File: unaryexpr.go

package info (click to toggle)
mumax3 3.11.1-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 10,668 kB
  • sloc: makefile: 194; ansic: 155; sh: 86; javascript: 16
file content (33 lines) | stat: -rw-r--r-- 891 bytes parent folder | download | duplicates (3)
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 &not{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 &not{m.x.Fix()} }