File: stmt.go

package info (click to toggle)
mumax3 3.10-9
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 7,596 kB
  • sloc: makefile: 181; ansic: 155; sh: 77
file content (55 lines) | stat: -rw-r--r-- 1,220 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package script

import (
	"go/ast"
	"reflect"
)

// compiles expression or statement
func (w *World) compile(n ast.Node) Expr {
	switch n := n.(type) {
	case ast.Stmt:
		return w.compileStmt(n)
	case ast.Expr:
		return w.compileExpr(n)
	default:
		panic(err(n.Pos(), "not allowed"))
	}
}

// compiles a statement
func (w *World) compileStmt(st ast.Stmt) Expr {
	switch st := st.(type) {
	default:
		panic(err(st.Pos(), "not allowed:", typ(st)))
	case *ast.EmptyStmt:
		return &emptyStmt{}
	case *ast.AssignStmt:
		return w.compileAssignStmt(st)
	case *ast.ExprStmt:
		return w.compileExpr(st.X)
	case *ast.IfStmt:
		return w.compileIfStmt(st)
	case *ast.ForStmt:
		return w.compileForStmt(st)
	case *ast.IncDecStmt:
		return w.compileIncDecStmt(st)
	case *ast.BlockStmt:
		w.EnterScope()
		defer w.ExitScope()
		return w.compileBlockStmt_noScope(st)
	}
}

// embed to get Type() that returns nil
type void struct{}

func (v *void) Type() reflect.Type { return nil }
func (v *void) Fix() Expr          { panic(invalid_closure) }

type emptyStmt struct{ void }

func (*emptyStmt) Child() []Expr     { return nil }
func (*emptyStmt) Eval() interface{} { return nil }

const invalid_closure = "illegal statement in closure"