forked from mumax/3
/
lit.go
62 lines (50 loc) · 1.52 KB
/
lit.go
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
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 }
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 }
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 }
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 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
}