checktype.go 1.55 KB
Newer Older
1
2
3
4
5
6
7
8
package transform

import (
	"go/ast"
	"go/token"
)

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

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 {
35
					panic("Non-integer values are not supported.")
36
				}
37
			case *ast.CompositeLit:
38
				panic("Arrays are not supported.")
39
40
			}
		}
Hugo Moreau's avatar
Hugo Moreau committed
41
	case *ast.ChanType:
42
		panic("Channels are not supported.")
43
44
45
46
	case *ast.FuncDecl:
		if node.Type.Params.List == nil && node.Name.Name != "main" {
			panic("Parameterless function are not supported.")
		}
47
48
	case *ast.Ident:
		if node.Name == "panic" {
49
			panic("Panics are not supported.")
50
		}
Hugo Moreau's avatar
Hugo Moreau committed
51
	case *ast.InterfaceType:
52
		panic("Interfaces are not supported.")
53
	case *ast.MapType:
54
		panic("Maps are not supported.")
Hugo Moreau's avatar
Hugo Moreau committed
55
	case *ast.SelectorExpr:
56
		panic("Select are not supported.")
Hugo Moreau's avatar
Hugo Moreau committed
57
	case *ast.SliceExpr:
58
		panic("Slices are not supported.")
Hugo Moreau's avatar
Hugo Moreau committed
59
	case *ast.StarExpr:
60
		panic("Pointers are not supported.")
61
	case *ast.StructType:
62
		panic("Structs are not supported.")
63
	case *ast.SwitchStmt:
64
		panic("Switch are not supported.")
Hugo Moreau's avatar
Hugo Moreau committed
65
	case *ast.UnaryExpr:
66
		panic("Pointers are not supported.")
67
68
69
70
71
72
73
	}
	return true
}

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