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

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

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

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 {
37
					fmt.Println("Non-integer values are not supported.")
38
39
40
					fmt.Println("Identified type :", rhn.Kind)
					os.Exit(1)
				}
41
42
43
			case *ast.CompositeLit:
				fmt.Println("Arrays are not supported.")
				os.Exit(1)
44
45
			}
		}
46
47
48
49
50
51
	case *ast.StarExpr:
		fmt.Println("Pointers are not supported.")
		os.Exit(1)
	case *ast.UnaryExpr:
		fmt.Println("Pointers are not supported.")
		os.Exit(1)
52
53
54
	case *ast.SliceExpr:
		fmt.Println("Slices are not supported.")
		os.Exit(1)
55
56
57
	case *ast.InterfaceType:
		fmt.Println("Interfaces are not supported.")
		os.Exit(1)
58
59
60
61
62
	case *ast.Ident:
		if node.Name == "panic" {
			fmt.Println("Panics are not supported.")
			os.Exit(1)
		}
63
64
65
	case *ast.MapType:
		fmt.Println("Maps are not supported.")
		os.Exit(1)
66
67
68
	case *ast.StructType:
		fmt.Println("Structs are not supported.")
		os.Exit(1)
69
70
71
	case *ast.ChanType:
		fmt.Println("Channels are not supported.")
		os.Exit(1)
72
73
74
	case *ast.SwitchStmt:
		fmt.Println("Switch are not supported.")
		os.Exit(1)
75
76
77
	case *ast.SelectorExpr:
		fmt.Println("Select are not supported.")
		os.Exit(1)
78
79
80
81
82
83
84
	}
	return true
}

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