-
Notifications
You must be signed in to change notification settings - Fork 0
/
native_int.go
63 lines (55 loc) · 1.71 KB
/
native_int.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
63
package joeson
import (
"strconv"
)
// NativeInt is an `int` that implements `Ast`.
type NativeInt int
func NewNativeInt(n int) NativeInt { return NativeInt(n) }
func NewNativeIntFromBool(b bool) NativeInt {
n := 0
if b {
n = 1
}
return NativeInt(n)
}
func NewNativeIntFromString(s string) NativeInt {
if n, e := strconv.Atoi(s); e == nil {
return NativeInt(n)
} else {
panic("can not convert string " + s + " to NativeInt")
}
}
func NewNativeIntFrom(x Ast) NativeInt {
return NativeIntFrom(x).(NativeInt)
}
// Create a NativeInt from an Ast.
// Return an Ast.
// It panics if necessary.
func NativeIntFrom(x Ast) Ast {
switch v := x.(type) {
case NativeInt:
return v
case NativeString:
if n, e := strconv.Atoi(string(v)); e == nil {
return NewNativeInt(n)
} else {
panic(e)
}
case *NativeArray:
// OPTIM
return NewNativeIntFromString(v.Concat().String())
default:
panic("Unable to make NativeInt from " + x.String())
}
}
func (n NativeInt) assertNode() {}
func (n NativeInt) Int() int { return int(n) }
func (n NativeInt) Bool() bool { return int(n) != 0 }
func (n NativeInt) String() string { return strconv.Itoa(int(n)) }
func (n NativeInt) SetLine(m int) {}
func (n NativeInt) GetLine() int { return 1 }
func (n NativeInt) SetOrigin(o Origin) {}
func (n NativeInt) GetOrigin() Origin { return Origin{} }
func (n NativeInt) HasAttribute(key interface{}) bool { return false }
func (n NativeInt) GetAttribute(key interface{}) interface{} { return nil }
func (n NativeInt) SetAttribute(key interface{}, value interface{}) { panic("N/A") }