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
The TypeChecker triggers errors for uses of :
12
 - Array
Hugo Moreau's avatar
Hugo Moreau committed
13
 - Channel
14
 - Interface
15
 - Map
Hugo Moreau's avatar
Hugo Moreau committed
16
17
18
 - Non-integer variable
 - Panic
 - Pointer
19
 - Select
Hugo Moreau's avatar
Hugo Moreau committed
20
21
22
 - Slice
 - Struct
 - Switch
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
			}
		}
Hugo Moreau's avatar
Hugo Moreau committed
46
47
	case *ast.ChanType:
		fmt.Println("Channels are not supported.")
48
		os.Exit(1)
49
50
51
52
53
	case *ast.Ident:
		if node.Name == "panic" {
			fmt.Println("Panics are not supported.")
			os.Exit(1)
		}
Hugo Moreau's avatar
Hugo Moreau committed
54
55
56
	case *ast.InterfaceType:
		fmt.Println("Interfaces are not supported.")
		os.Exit(1)
57
58
59
	case *ast.MapType:
		fmt.Println("Maps are not supported.")
		os.Exit(1)
Hugo Moreau's avatar
Hugo Moreau committed
60
61
62
63
64
65
66
67
68
	case *ast.SelectorExpr:
		fmt.Println("Select are not supported.")
		os.Exit(1)
	case *ast.SliceExpr:
		fmt.Println("Slices are not supported.")
		os.Exit(1)
	case *ast.StarExpr:
		fmt.Println("Pointers are not supported.")
		os.Exit(1)
69
70
71
	case *ast.StructType:
		fmt.Println("Structs are not supported.")
		os.Exit(1)
72
73
74
	case *ast.SwitchStmt:
		fmt.Println("Switch are not supported.")
		os.Exit(1)
Hugo Moreau's avatar
Hugo Moreau committed
75
76
	case *ast.UnaryExpr:
		fmt.Println("Pointers are not supported.")
77
		os.Exit(1)
78
79
80
81
82
83
84
	}
	return true
}

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