-
Notifications
You must be signed in to change notification settings - Fork 7
/
checkunaryexpr_gen.go
115 lines (97 loc) · 2.21 KB
/
checkunaryexpr_gen.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"io"
"text/template"
"go/token"
"github.com/0xfaded/go-testgen"
)
type Test struct{}
var comment = template.Must(template.New("Comment").Parse(
`// Test {{ .Op.Value }} {{ .Rhs.Name }}
`))
var body = template.Must(template.New("Body").Parse(
` env := MakeSimpleEnv()
{{ if .Errors }}
expectCheckError(t, `+"`{{ .Expr }}`"+`, env,{{ range .Errors }}
`+"`{{ . }}`"+`,{{ end }}
)
{{ else }}
expectConst(t, `+"`{{ .Expr }}`"+`, env, {{ .NewConstType }}({{ .Expr }}), {{ .ResultType }}){{ end }}
`))
func (*Test) Package() string {
return "eval"
}
func (*Test) Prefix() string {
return "CheckUnaryExpr"
}
func (*Test) Imports() map[string]string {
return nil
}
func (*Test) Dimensions() []testgen.Dimension {
// All numeric values have been chosen to be a power of two, for good reason :)
types := []testgen.Element{
{"Int", "4"},
{"Rune", "'@'"},
{"Float", "2.0"},
{"Complex", "8.0i"},
{"Bool", "true"},
{"String", `"abc"`},
{"Nil", "nil"},
}
ops := []testgen.Element{
{"Add", token.ADD},
{"Sub", token.SUB},
{"Xor", token.XOR},
{"Not", token.NOT},
}
return []testgen.Dimension{
ops,
types,
}
}
func (*Test) Globals(w io.Writer) error {
return nil
}
func (*Test) Comment(w io.Writer, elts ...testgen.Element) error {
vars := map[string] interface{} {
"Op": elts[0],
"Rhs": elts[1],
}
return comment.Execute(w, vars)
}
func (*Test) Body(w io.Writer, elts ...testgen.Element) error {
op := elts[0].Value.(token.Token)
rhs := elts[1].Name
expr := fmt.Sprintf("%v %v", op, elts[1].Value)
compileErrs, err := compileExpr(expr)
if err != nil {
return err
}
var newConstType string
var resultType string
switch rhs {
case "Int":
newConstType = "NewConstInt64"
resultType = "ConstInt"
case "Rune":
newConstType = "NewConstRune"
resultType = "ConstRune"
case "Float":
newConstType = "NewConstFloat64"
resultType = "ConstFloat"
case "Complex":
newConstType = "NewConstComplex128"
resultType = "ConstComplex"
case "Bool":
resultType = "ConstBool"
}
vars := map[string] interface{} {
"Expr": expr,
"Errors": compileErrs,
"Op": elts[1],
"NewConstType": newConstType,
"ResultType": resultType,
}
return body.Execute(w, &vars)
}