-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcheckbasiclit.go
60 lines (56 loc) · 1.59 KB
/
checkbasiclit.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
package eval
import (
"strconv"
"go/ast"
"go/token"
)
func checkBasicLit(lit *ast.BasicLit, env Env) (*BasicLit, []error) {
aexpr := &BasicLit{BasicLit: lit}
switch lit.Kind {
case token.CHAR:
if r, _, tail, err := strconv.UnquoteChar(lit.Value[1:len(lit.Value)-1], '\''); err != nil {
return aexpr, []error{ErrBadBasicLit{aexpr}}
} else if tail != "" {
// parser.ParseExpr() should raise a syntax error before we get here.
panic("go-interactive: bad char lit " + lit.Value)
} else {
aexpr.constValue = constValueOf(NewConstRune(r))
aexpr.knownType = knownType{ConstRune}
return aexpr, nil
}
case token.STRING:
if str, err := strconv.Unquote(string(lit.Value)); err != nil {
return aexpr, []error{ErrBadBasicLit{aexpr}}
} else {
aexpr.constValue = constValueOf(str)
aexpr.knownType = knownType{ConstString}
return aexpr, nil
}
case token.INT:
if i, ok := NewConstInteger(lit.Value); !ok {
return aexpr, []error{ErrBadBasicLit{aexpr}}
} else {
aexpr.constValue = constValueOf(i)
aexpr.knownType = knownType{ConstInt}
return aexpr, nil
}
case token.FLOAT:
if f, ok := NewConstFloat(lit.Value); !ok {
return aexpr, []error{ErrBadBasicLit{aexpr}}
} else {
aexpr.constValue = constValueOf(f)
aexpr.knownType = knownType{ConstFloat}
return aexpr, nil
}
case token.IMAG:
if i, ok := NewConstImag(lit.Value); !ok {
return aexpr, []error{ErrBadBasicLit{aexpr}}
} else {
aexpr.constValue = constValueOf(i)
aexpr.knownType = knownType{ConstComplex}
return aexpr, nil
}
default:
return aexpr, []error{ErrBadBasicLit{aexpr}}
}
}