File: incdecstmt.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 (33 lines) | stat: -rw-r--r-- 953 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) compileIncDecStmt(n *ast.IncDecStmt) Expr {
	l := w.compileLvalue(n.X)
	switch n.Tok {
	case token.INC:
		rhs_plus1 := &addone{incdec{typeConv(n.Pos(), l, float64_t)}}
		return &assignStmt{lhs: l, rhs: typeConv(n.Pos(), rhs_plus1, l.Type())}
	case token.DEC:
		rhs_minus1 := &subone{incdec{typeConv(n.Pos(), l, float64_t)}}
		return &assignStmt{lhs: l, rhs: typeConv(n.Pos(), rhs_minus1, l.Type())}
	default:
		panic(err(n.Pos(), "not allowed:", n.Tok))
	}
}

type incdec struct{ x Expr }

func (e *incdec) Type() reflect.Type { return float64_t }
func (e *incdec) Child() []Expr      { return []Expr{e.x} }
func (e *incdec) Fix() Expr          { panic(invalid_closure) }

type addone struct{ incdec }
type subone struct{ incdec }

func (s *addone) Eval() interface{} { return s.x.Eval().(float64) + 1 }
func (s *subone) Eval() interface{} { return s.x.Eval().(float64) - 1 }