File: for.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 (50 lines) | stat: -rw-r--r-- 1,011 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
package script

import (
	"go/ast"
)

// for statement
type forStmt struct {
	init, cond, post, body Expr
	void
}

func (b *forStmt) Eval() interface{} {
	for b.init.Eval(); b.cond.Eval().(bool); b.post.Eval() {
		b.body.Eval()
	}
	return nil // void
}

func (w *World) compileForStmt(n *ast.ForStmt) *forStmt {
	w.EnterScope()
	defer w.ExitScope()

	stmt := &forStmt{init: &nop{}, cond: &nop{}, post: &nop{}, body: &nop{}}
	if n.Init != nil {
		stmt.init = w.compileStmt(n.Init)
	}
	if n.Cond != nil {
		stmt.cond = typeConv(n.Cond.Pos(), w.compileExpr(n.Cond), bool_t)
	} else {
		stmt.cond = boolLit(true)
	}
	if n.Post != nil {
		stmt.post = w.compileStmt(n.Post)
	}
	if n.Body != nil {
		stmt.body = w.compileBlockStmt_noScope(n.Body)
	}
	return stmt
}

type nop struct{ void }

func (e *nop) Child() []Expr     { return nil }
func (e *nop) Eval() interface{} { return nil }
func (e *nop) Fix() Expr         { return e }

func (e *forStmt) Child() []Expr {
	return []Expr{e.init, e.cond, e.post, e.body}
}