File: expr.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 (36 lines) | stat: -rw-r--r-- 851 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
34
35
36
package script

import (
	"go/ast"
	"reflect"
)

// an expression can be evaluated
type Expr interface {
	Eval() interface{}  // evaluate and return result (nil for void)
	Type() reflect.Type // return type, nil for void
	Child() []Expr
	Fix() Expr // replace all variables by their current value, except for the time "t".
}

// compiles an expression
func (w *World) compileExpr(e ast.Expr) Expr {
	switch e := e.(type) {
	default:
		panic(err(e.Pos(), "not allowed:", typ(e)))
	case *ast.Ident:
		return w.resolve(e.Pos(), e.Name)
	case *ast.BasicLit:
		return w.compileBasicLit(e)
	case *ast.BinaryExpr:
		return w.compileBinaryExpr(e)
	case *ast.UnaryExpr:
		return w.compileUnaryExpr(e)
	case *ast.CallExpr:
		return w.compileCallExpr(e)
	case *ast.ParenExpr:
		return w.compileExpr(e.X)
	case *ast.IndexExpr:
		return w.compileIndexExpr(e)
	}
}