checktype.go 1.51 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
15
 - Struct
 - Panic
16
17
 - Pointer
 - Interface
18
 - Map
19
20
21
22
23
24
25
26
27
28
29
30
31
32
*/

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

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