Example #1
0
File: type.go Project: enex/RUN
func typeOf(n ast.Node, s *ast.Scope) (t *ast.Ident) {
	switch e := n.(type) {
	case *ast.AssignExpr:
		t = typeOfObject(s.Lookup(e.Name.Name))
	case *ast.BasicLit:
		t = typeOfBasic(e)
	case *ast.BinaryExpr:
		t = typeOf(e.List[0], s)
	case *ast.CallExpr:
		t = typeOfObject(s.Lookup(e.Name.Name))
	case *ast.DeclExpr:
		t = typeOfObject(s.Lookup(e.Name.Name))
	case *ast.ExprList:
		// BUG: should follow chain of execution to make sure all return
		// values match return type
		t = typeOf(e.List[len(e.List)-1], s)
	case *ast.Ident:
		t = typeOfObject(s.Lookup(e.Name))
	case *ast.IfExpr:
		if e.Type != nil {
			t = e.Type
		}
	case *ast.UnaryExpr:
		t = typeOf(e.Value, s)
	case *ast.VarExpr:
		t = typeOf(e.Name, s)
	}

	if t == nil {
		t = &ast.Ident{Name: "unknown", NamePos: n.Pos()}
	}
	return t
}
Example #2
0
File: type.go Project: unreal/calc
func typeOf(n ast.Node, s *ast.Scope) string {
	t := "unknown"
	switch e := n.(type) {
	case *ast.BasicLit:
		t = typeOfBasic(e)
	case *ast.BinaryExpr:
		t = "int"
	case *ast.CallExpr:
		ob := s.Lookup(e.Name.Name)
		t = typeOfObject(ob)
	case *ast.DeclExpr:
		ob := s.Lookup(e.Name.Name)
		t = typeOfObject(ob)
	case *ast.Ident:
		ob := s.Lookup(e.Name)
		t = typeOfObject(ob)
	}
	return t
}