checktype.go 1.71 KB
Newer Older
1
2
3
4
5
package transform

import (
	"go/ast"
	"go/token"
Hugo Moreau's avatar
Hugo Moreau committed
6
	"strings"
7
8
9
)

/*
10
The TypeChecker triggers errors for uses of :
11
 - Array
Hugo Moreau's avatar
Hugo Moreau committed
12
 - Channel
13
 - Interface
14
 - Map
Hugo Moreau's avatar
Hugo Moreau committed
15
16
17
 - Non-integer variable
 - Panic
 - Pointer
18
 - Select
Hugo Moreau's avatar
Hugo Moreau committed
19
20
21
 - Slice
 - Struct
 - Switch
22
23
24
25
26
27
28
29
30
31
32
33
34
35
*/

type TypeChecker struct {
}

// Pre traversal check if there is use of variable other than integers.
func (t *TypeChecker) Pre(meta *Meta, v *Visitor) bool {
	c := v.Cursor()
	switch node := c.Node().(type) {
	case *ast.AssignStmt:
		for i := 0; i < len(node.Rhs); i++ {
			switch rhn := node.Rhs[i].(type) {
			case *ast.BasicLit:
				if rhn.Kind != token.INT {
36
					panic("Non-integer values are not supported.")
37
				}
38
			case *ast.CompositeLit:
39
				panic("Arrays are not supported.")
40
			}
Hugo Moreau's avatar
Hugo Moreau committed
41
42
43
44
45
46
			switch lhn := node.Lhs[i].(type) {
			case *ast.Ident:
				if strings.Contains(lhn.Name, "G2P_tmp_") {
					panic("Variable name must not contain \"G2P_tmp_\"")
				}
			}
47
		}
Hugo Moreau's avatar
Hugo Moreau committed
48
	case *ast.ChanType:
49
		panic("Channels are not supported.")
50
51
	case *ast.Ident:
		if node.Name == "panic" {
52
			panic("Panics are not supported.")
53
		}
Hugo Moreau's avatar
Hugo Moreau committed
54
	case *ast.InterfaceType:
55
		panic("Interfaces are not supported.")
56
	case *ast.MapType:
57
		panic("Maps are not supported.")
Hugo Moreau's avatar
Hugo Moreau committed
58
	case *ast.SliceExpr:
59
		panic("Slices are not supported.")
Hugo Moreau's avatar
Hugo Moreau committed
60
	case *ast.StarExpr:
61
		panic("Pointers are not supported.")
62
	case *ast.StructType:
63
		panic("Structs are not supported.")
64
	case *ast.SwitchStmt:
65
		panic("Switch are not supported.")
66
67
68
69
70
71
72
73
74
	case *ast.ValueSpec:
		for i := 0; i < len(node.Values); i++ {
			switch value := node.Values[i].(type) {
			case *ast.BasicLit:
				if value.Kind != token.INT {
					panic("Non integer global values are not supported.")
				}
			}
		}
75
76
77
78
79
80
81
	}
	return true
}

func (TypeChecker) Post(meta *Meta, v *Visitor) bool {
	return true
}