File: lit.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 (66 lines) | stat: -rw-r--r-- 1,754 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
56
57
58
59
60
61
62
63
64
65
66
package script

import (
	"go/ast"
	"go/token"
	"reflect"
	"strconv"
)

// compiles a basic literal, like numbers and strings
func (w *World) compileBasicLit(n *ast.BasicLit) Expr {
	switch n.Kind {
	default:
		panic(err(n.Pos(), "not allowed:", n.Value, "(", typ(n), ")"))
	case token.FLOAT:
		return floatLit(parseFloat(n.Value))
	case token.INT:
		return intLit(parseInt(n.Value))
	case token.STRING:
		return stringLit(n.Value[1 : len(n.Value)-1]) // remove quotes
	}
}

type floatLit float64

func (l floatLit) Eval() interface{}  { return float64(l) }
func (l floatLit) Type() reflect.Type { return float64_t }
func (l floatLit) Child() []Expr      { return nil }
func (l floatLit) Fix() Expr          { return l }

type intLit int

func (l intLit) Eval() interface{}  { return int(l) }
func (l intLit) Type() reflect.Type { return int_t }
func (l intLit) Child() []Expr      { return nil }
func (l intLit) Fix() Expr          { return l }

type stringLit string

func (l stringLit) Eval() interface{}  { return string(l) }
func (l stringLit) Type() reflect.Type { return string_t }
func (l stringLit) Child() []Expr      { return nil }
func (l stringLit) Fix() Expr          { return l }

type boolLit bool

func (l boolLit) Eval() interface{}  { return bool(l) }
func (l boolLit) Type() reflect.Type { return bool_t }
func (l boolLit) Child() []Expr      { return nil }
func (l boolLit) Fix() Expr          { return l }

func parseFloat(str string) float64 {
	v, err := strconv.ParseFloat(str, 64)
	if err != nil {
		panic("internal error") // we were sure it was a number...
	}
	return v
}

func parseInt(str string) int {
	v, err := strconv.Atoi(str)
	if err != nil {
		panic("internal error") // we were sure it was a number...
	}
	return v
}